Skip to content
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

Closed
9 tasks done
fengmk2 opened this issue Jun 18, 2016 · 5 comments
Closed
9 tasks done

Opensource version of egg-bin #1

fengmk2 opened this issue Jun 18, 2016 · 5 comments
Assignees
Milestone

Comments

@fengmk2
Copy link
Member

fengmk2 commented Jun 18, 2016

Features

  • dev
  • test
  • cov
  • debug
  • stc

Custom command

  • jars
  • proxy

deps

@fengmk2 fengmk2 self-assigned this Jun 18, 2016
@fengmk2 fengmk2 added this to the v1.0 milestone Jun 18, 2016
@fengmk2
Copy link
Member Author

fengmk2 commented Jun 18, 2016

使用 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;

@popomore
Copy link
Member

popomore commented Jun 18, 2016

addCommand 用函数吧

addCommand('test', () => new Promise());

@fengmk2
Copy link
Member Author

fengmk2 commented Jun 18, 2016

@popomore 我重新设计了,等看代码。

@fengmk2
Copy link
Member Author

fengmk2 commented Jun 18, 2016

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;

@fengmk2
Copy link
Member Author

fengmk2 commented Jun 19, 2016

@guangao 可以用了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants