35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
|
const path = require('path');
|
||
|
|
||
|
const glob = require('glob');
|
||
|
|
||
|
// Dynamically require all traits.
|
||
|
module.exports = (source) => {
|
||
|
const modeModulesPath = path.resolve(
|
||
|
__dirname, 'node_modules',
|
||
|
);
|
||
|
const traitPaths = [
|
||
|
path.resolve(
|
||
|
modeModulesPath, '@avocado', 'entity', 'traits', '*.js',
|
||
|
),
|
||
|
];
|
||
|
const files = glob.sync(traitPaths.join());
|
||
|
const modules = files.map((file) => {
|
||
|
let dirname = path.dirname(file);
|
||
|
dirname = dirname.replace(`${modeModulesPath}/`, '');
|
||
|
const basename = path.basename(file, '.js');
|
||
|
return `${dirname}/${basename}`;
|
||
|
});
|
||
|
const defs = 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`;
|
||
|
});
|
||
|
defs.unshift(`import {registerTrait} from '@avocado/entity/trait-registry';\n`);
|
||
|
return defs.join('\n');
|
||
|
}
|