Skip to content

Commit

Permalink
Starting to introduce a plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal committed Nov 2, 2018
1 parent c4d5b2b commit c1857a8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 34 deletions.
28 changes: 26 additions & 2 deletions lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,50 @@ const defaultsDeep = require('lodash/defaultsDeep');

class API {
constructor(context, mode = 'development', verbose = false) {
this.plugins = [];
this.commands = [];
this.context = context;
this.mode = mode;
this.verbose = verbose;
this.logger = initLogger();
this.projectOptions = defaultsDeep(loadUserOptions(), defaults());
this.resolvePlugins();
}

registerCommand(commandName, opts, fn) {
this.commands[commandName] = { opts, fn };
}

executeCommand(commandName, params) {
if (!commandName) {
throw new Error('You must specify a command to run.');
}

const command = require(resolve(__dirname, 'commands', commandName));
const command = this.commands[commandName];
if (!command) {
throw new Error(`Command "${commandName}" does not exist.`);
}

return new Promise((resolve, reject) => {
return command(...params)
return command.fn(params)
.then(() => resolve())
.catch(err => reject(err));
});
}

/**
* @private
*/
resolvePlugins() {
this.plugins = [
'./commands/build',
'./commands/lint',
];

this.plugins.forEach(plugin => {
require(plugin)(this);
});
}
}

module.exports = API;
Expand Down
2 changes: 1 addition & 1 deletion lib/Cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Cli {
this.init(args.mode, args.v);

return new Promise((resolve, reject) => {
return this.api.executeCommand(commandName, [this.api, args])
return this.api.executeCommand(commandName, args)
.then(() => resolve())
.catch(err => reject(err));
});
Expand Down
28 changes: 19 additions & 9 deletions lib/commands/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ const { readAssets } = require('../../utils/assets');
const handle = require('./handle');
const watch = require('./watch');

module.exports = build = (api, args) => {
return new Promise(() => {
const assets = readAssets(api, args);
module.exports = api => {
api.registerCommand('build', {
description: 'build files',
usage: 'yprox-cli build [options]',
options: {
...require('../commonOptions'),
'--watch': 'enable watch mode',
'--lint': 'lint before build, if lint fails, files will not be build',
},
}, args => {
return new Promise(() => {
const assets = readAssets(api, args);

assets.forEach(entry => {
if (args.watch && entry.handler !== 'rollup') { // we gonna use rollup own watcher
watch(api, entry, args)(handle);
} else {
handle(api, entry, args);
}
assets.forEach(entry => {
if (args.watch && entry.handler !== 'rollup') { // we gonna use rollup own watcher
watch(api, entry, args)(handle);
} else {
handle(api, entry, args);
}
});
});
});
};
5 changes: 5 additions & 0 deletions lib/commands/commonOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
'--help': 'display help',
'--mode': 'specify env mode (default: development)',
'--filter:<filterName> <value>': 'apply a filter on file entries',
};
53 changes: 31 additions & 22 deletions lib/commands/lint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,40 @@ const { chain } = require('lodash');
const { readAssets } = require('../../utils/assets');
const linters = require('./linters');

module.exports = lint = (api, args) => {
return new Promise((resolve, reject) => {
const files = chain(readAssets(api, args))
.filter(({ handler }) => Object.keys(linters).includes(handler))
.map(normalizeEntrySrc)
.groupBy('handler')
.value();

Object.entries(files).forEach(([linter, entries]) => {
if (!(linter in linters)) {
return;
}

const filesToLint = chain(entries)
.map(entry => entry.src)
.flatten()
module.exports = api => {
api.registerCommand('lint', {
description: 'lint files',
usage: 'yprox-cli lint [options]',
options: {
...require('../commonOptions'),
'--fix': 'automatically fix lint errors',
},
}, args => {
return new Promise((resolve, reject) => {
const files = chain(readAssets(api, args))
.filter(({ handler }) => Object.keys(linters).includes(handler))
.map(normalizeEntrySrc)
.groupBy('handler')
.value();

if (filesToLint.length === 0) {
return;
}
Object.entries(files).forEach(([linter, entries]) => {
if (!(linter in linters)) {
return;
}

return linters[linter]()(api, args, filesToLint)
.then(() => resolve())
.catch(err => reject(err));
const filesToLint = chain(entries)
.map(entry => entry.src)
.flatten()
.value();

if (filesToLint.length === 0) {
return;
}

return linters[linter]()(api, args, filesToLint)
.then(() => resolve())
.catch(err => reject(err));
});
});
});
};
Expand Down

0 comments on commit c1857a8

Please sign in to comment.