54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
|
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}/`, '');
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
pathGlob: path.resolve(__dirname, 'traits', '*.js'),
|
||
|
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');
|
||
|
}
|