feat: generate app traits

This commit is contained in:
cha0s 2019-03-20 22:45:57 -05:00
parent 9fd29a64a9
commit fc32a614a1
5 changed files with 54 additions and 38 deletions

53
generate-trait-defs.js Normal file
View File

@ -0,0 +1,53 @@
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');
}

View File

@ -1,34 +0,0 @@
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');
}

View File

@ -9,9 +9,6 @@ import {
registerTrait,
} from '@avocado/entity';
import {StateSynchronizer} from '@avocado/state';
// Traits.
import {Informed} from './traits/informed';
registerTrait(Informed);
// Create game.
export default function(avocadoServer) {
avocadoServer.on('connect', createConnectionListener(avocadoServer));

View File

@ -19,7 +19,7 @@ const config = {
{
test: /register-traits.js/,
use: {
loader: './generate-traits',
loader: './generate-trait-defs',
},
},
],