refactor: defgen!
This commit is contained in:
parent
0f737601fb
commit
d635918cfd
|
@ -1,21 +1,57 @@
|
|||
const glob = require('glob');
|
||||
const {getOptions} = require('loader-utils');
|
||||
const path = require('path');
|
||||
const validateOptions = require('schema-utils');
|
||||
|
||||
const schema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
paths: {
|
||||
items: {
|
||||
type: 'string',
|
||||
},
|
||||
type: 'array',
|
||||
},
|
||||
registrar: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
function: {
|
||||
type: 'string',
|
||||
},
|
||||
module: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'function',
|
||||
'module',
|
||||
],
|
||||
},
|
||||
type: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'registrar',
|
||||
'type',
|
||||
],
|
||||
};
|
||||
|
||||
// Dynamically require all traits.
|
||||
module.exports = function(source) {
|
||||
const options = getOptions(this);
|
||||
if (!options.paths) {
|
||||
options.paths = [];
|
||||
}
|
||||
// Validate schema.
|
||||
validateOptions(schema, options, 'Avocado defgen');
|
||||
// Extract options.
|
||||
const {paths, registrar, type} = options;
|
||||
// Search avocado.
|
||||
options.paths.push(path.resolve(
|
||||
__dirname, 'node_modules', '@avocado',
|
||||
));
|
||||
// Get all trait source paths.
|
||||
const sourcePaths = [];
|
||||
options.paths.forEach((path) => {
|
||||
const files = glob.sync(`${path}/**/*.trait.js`, {
|
||||
paths.forEach((path) => {
|
||||
const files = glob.sync(`${path}/**/*.${type}.js`, {
|
||||
follow: true,
|
||||
});
|
||||
sourcePaths.push(...files);
|
||||
|
@ -23,7 +59,7 @@ module.exports = function(source) {
|
|||
// Build import definitions.
|
||||
const importDefinitions = sourcePaths.map((sourcePath) => {
|
||||
const relativePath = path.relative(__dirname, sourcePath);
|
||||
const basename = path.basename(relativePath, '.trait.js');
|
||||
const basename = path.basename(relativePath, `.${type}.js`);
|
||||
const parts = relativePath.split('/');
|
||||
// Chop off basename.
|
||||
parts.pop();
|
||||
|
@ -35,7 +71,7 @@ module.exports = function(source) {
|
|||
else {
|
||||
importDirectory = `./${parts.join('/')}`
|
||||
}
|
||||
const importPath = `${importDirectory}/${basename}.trait`;
|
||||
const importPath = `${importDirectory}/${basename}.${type}`;
|
||||
// Extract class name.
|
||||
const baseparts = basename.split('-');
|
||||
const className = baseparts.reduce((className, part) => {
|
||||
|
@ -46,12 +82,12 @@ module.exports = function(source) {
|
|||
// Import and register.
|
||||
return [
|
||||
`import {${className}} from '${importPath}';`,
|
||||
`registerTrait(${className});`,
|
||||
`${registrar.function}(${className});`,
|
||||
].join('\n');
|
||||
});
|
||||
// Import trait registry first.
|
||||
return [
|
||||
`import {registerTrait} from '@avocado/entity/trait-registry'`,
|
||||
`import {${registrar.function}} from '${registrar.module}'`,
|
||||
'',
|
||||
...importDefinitions
|
||||
].join('\n');
|
|
@ -17,14 +17,19 @@ const config = {
|
|||
},
|
||||
},
|
||||
{
|
||||
test: /register-traits.js/,
|
||||
test: /register-traits\.js/,
|
||||
use: {
|
||||
loader: './generate-trait-defs',
|
||||
loader: './defgen',
|
||||
options: {
|
||||
paths: [
|
||||
path.resolve(__dirname, 'common'),
|
||||
],
|
||||
}
|
||||
registrar: {
|
||||
function: 'registerTrait',
|
||||
module: '@avocado/entity/trait-registry',
|
||||
},
|
||||
type: 'trait',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue
Block a user