-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opensource version of egg-bin #1
Comments
使用 Program 类来让 egg-bin 变成可扩展的。 'use strict';
/**
* Module dependencies.
*/
const co = require('co');
const path = require('path');
const chalk = require('chalk');
class Program {
constructor() {
this.availableCommands = {
test: path.join(__dirname, 'test.js'),
debug: path.join(__dirname, 'debug.js'),
cov: path.join(__dirname, 'cov.js'),
stc: path.join(__dirname, 'stc.js'),
dev: path.join(__dirname, 'dev.js'),
};
this.version = require('../package.json').version;
}
addCommand(cmd, filepath) {
// each cmd module should contain two methods: run(args) and help()
this.availableCommands[cmd] = filepath;
}
onAction(cmd) {
const filepath = this.availableCommands[cmd];
if (!filepath) {
this.help();
}
const args = process.argv.slice(3);
co(function*() {
const action = require(filepath);
yield action.run(args);
}).catch(err => {
console.error('[egg-bin] run %s %j error:', cmd, args);
console.error(chalk.red(err.stack));
process.exit(1);
});
}
help() {
console.log('');
for (const cmd in this.availableCommands) {
const action = require(this.availableCommands[cmd]);
console.log(' %s - %s', cmd, action.help());
}
console.log('');
}
}
module.exports = Program; |
addCommand 用函数吧 addCommand('test', () => new Promise()); |
@popomore 我重新设计了,等看代码。 |
Command 基类 'use strict';
const helper = require('./helper');
class Command {
constructor() {
this.helper = helper;
}
run(/* cwd, args */) {
throw new Error('Must impl this method');
}
help() {
throw new Error('Must impl this method');
}
}
module.exports = Command; |
@guangao 可以用了 |
Closed
Closed
Closed
Closed
elrrrrrrr
pushed a commit
to elrrrrrrr/egg
that referenced
this issue
Feb 21, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Features
Custom command
deps
The text was updated successfully, but these errors were encountered: