Giter Site home page Giter Site logo

Opensource version of egg-bin about egg HOT 5 CLOSED

eggjs avatar eggjs commented on April 17, 2024
Opensource version of egg-bin

from egg.

Comments (5)

fengmk2 avatar fengmk2 commented on April 17, 2024

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

from egg.

popomore avatar popomore commented on April 17, 2024

addCommand 用函数吧

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

from egg.

fengmk2 avatar fengmk2 commented on April 17, 2024

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

from egg.

fengmk2 avatar fengmk2 commented on April 17, 2024

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;

from egg.

fengmk2 avatar fengmk2 commented on April 17, 2024

@guangao 可以用了

from egg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.