Skip to content

Opensource version of egg-bin #1

Closed

Description

@fengmk2

Features

  • dev
  • test
  • cov
  • debug
  • stc

Custom command

  • jars
  • proxy

deps

Activity

  1. self-assigned this
    on Jun 18, 2016
  2. added this to the milestone on Jun 18, 2016
  3. fengmk2 commented on Jun 18, 2016

    @fengmk2
    MemberAuthor

    使用 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;
  4. popomore commented on Jun 18, 2016

    @popomore
    Member

    addCommand 用函数吧

    addCommand('test', () => new Promise());
  5. fengmk2 commented on Jun 18, 2016

    @fengmk2
    MemberAuthor

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

  6. fengmk2 commented on Jun 18, 2016

    @fengmk2
    MemberAuthor

    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;
  7. fengmk2 commented on Jun 19, 2016

    @fengmk2
    MemberAuthor

    @guangao 可以用了

  8. added a commit that references this issue on Jun 20, 2026
    7b062db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions