humus-old/generate-trait-defs.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-03-20 22:45:57 -05:00
const path = require('path');
const glob = require('glob');
// Dynamically require all traits.
module.exports = (source) => {
const nodeModulesPath = path.resolve(
__dirname, 'node_modules',
);
const traitPaths = [
{
pathGlob: path.resolve(
nodeModulesPath,
'@avocado',
'entity',
'traits',
'*.js'
),
replacer: (dirname) => {
return dirname.replace(`${nodeModulesPath}/`, '');
},
},
{
2019-04-14 18:39:59 -05:00
pathGlob: path.resolve(__dirname, 'common', 'traits', '*.js'),
2019-03-20 22:45:57 -05:00
replacer: (dirname) => {
return dirname.replace(__dirname, '.');
},
},
];
let lines = [];
traitPaths.forEach(({pathGlob, replacer}) => {
const files = glob.sync(pathGlob);
const modules = files.map((file) => {
let dirname = path.dirname(file);
dirname = replacer(dirname);
const basename = path.basename(file, '.js');
return `${dirname}/${basename}`;
});
const moduleLines = modules.map((module_) => {
const basename = path.basename(module_);
const parts = basename.split('-');
const className = parts.reduce((className, part) => {
const firstLetter = part.charAt(0).toUpperCase();
const rest = part.substr(1).toLowerCase();
return className + firstLetter + rest;
}, '');
return `import {${className}} from '${module_}';\nregisterTrait(${className});\n`;
});
lines = lines.concat(moduleLines);
});
lines.unshift(`import {registerTrait} from '@avocado/entity/trait-registry';\n`);
return lines.join('\n');
}