diff --git a/.travis.yml b/.travis.yml index bdf6f64..6b4e30d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,11 @@ node_js: - '6' - '8' - '10' + - '12' +before_install: + - npm i npminstall -g install: - - npm i npminstall && npminstall + - npminstall script: - npm run ci after_script: diff --git a/appveyor.yml b/appveyor.yml index 3dc637e..39749ee 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,6 +3,7 @@ environment: - nodejs_version: '6' - nodejs_version: '8' - nodejs_version: '10' + - nodejs_version: '12' install: - ps: Install-Product node $env:nodejs_version diff --git a/index.d.ts b/index.d.ts index 2872307..9250533 100644 --- a/index.d.ts +++ b/index.d.ts @@ -49,7 +49,7 @@ declare class CommonBin { */ protected context: CommonBin.Context; - constructor(rawArgv: string[]); + constructor(rawArgv?: string[]); /** * command handler, could be generator / async function / normal function which return promise diff --git a/lib/command.js b/lib/command.js index 57f607b..59a442c 100644 --- a/lib/command.js +++ b/lib/command.js @@ -103,6 +103,10 @@ class CommonBin { assert(fs.existsSync(target) && fs.statSync(target).isFile(), `${target} is not a file.`); debug('[%s] add command `%s` from `%s`', this.constructor.name, name, target); target = require(target); + // try to require es module + if (target && target.__esModule && target.default) { + target = target.default; + } assert(target.prototype instanceof CommonBin, 'command class should be sub class of common-bin'); } diff --git a/package.json b/package.json index eb5b8cf..3589513 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ ], "types": "index.d.ts", "ci": { - "version": "6, 8, 10", + "version": "6, 8, 10, 12", "license": { "year": "2017", "fullname": "node-modules and other contributors" diff --git a/test/es-module-bin.test.js b/test/es-module-bin.test.js new file mode 100644 index 0000000..d7399f8 --- /dev/null +++ b/test/es-module-bin.test.js @@ -0,0 +1,30 @@ +'use strict'; + +const path = require('path'); +const coffee = require('coffee'); +const mm = require('mm'); + +describe('test/es-module-bin.test.js', () => { + afterEach(mm.restore); + + const myBin = require.resolve('./fixtures/es-module-bin/bin/es-module-bin.js'); + const cwd = path.join(__dirname, 'fixtures/test-files'); + + it('es-module-bin --help', () => { + return coffee.fork(myBin, [ '--help' ], { cwd }) + .debug() + .expect('stdout', /Options:/) + .expect('stdout', /-h, --help.*/) + .expect('code', 0) + .end(); + }); + + it('es-module-bin start', done => { + coffee.fork(myBin, [ 'start' ], { cwd }) + .debug() + // .coverage(false) + .expect('stdout', /override start command/) + .expect('code', 0) + .end(done); + }); +}); diff --git a/test/fixtures/es-module-bin/bin/es-module-bin.js b/test/fixtures/es-module-bin/bin/es-module-bin.js new file mode 100755 index 0000000..30f4461 --- /dev/null +++ b/test/fixtures/es-module-bin/bin/es-module-bin.js @@ -0,0 +1,7 @@ +#!/usr/bin/env node + +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); +const __1 = require('..'); +new __1.default().start(); diff --git a/test/fixtures/es-module-bin/command/start.js b/test/fixtures/es-module-bin/command/start.js new file mode 100644 index 0000000..592ed96 --- /dev/null +++ b/test/fixtures/es-module-bin/command/start.js @@ -0,0 +1,17 @@ +'use strict'; +Object.defineProperty(exports, '__esModule', { value: true }); + +const Command = require('../../../..'); + +class StartCommand extends Command { + + * run() { + console.log('override start command'); + } + + get description() { + return 'start app override'; + } +} + +exports.default = StartCommand; diff --git a/test/fixtures/es-module-bin/index.js b/test/fixtures/es-module-bin/index.js new file mode 100644 index 0000000..11e1ddb --- /dev/null +++ b/test/fixtures/es-module-bin/index.js @@ -0,0 +1,17 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +const Command = require('../../..'); +const path = require('path'); + +class MainCommand extends Command { + constructor(rawArgv) { + super(rawArgv); + this.yargs.usage('Usage: es-module [options]'); + // load directory + this.load(path.join(__dirname, 'command')); + } +} + +exports.default = MainCommand; diff --git a/test/fixtures/es-module-bin/package.json b/test/fixtures/es-module-bin/package.json new file mode 100644 index 0000000..cad7940 --- /dev/null +++ b/test/fixtures/es-module-bin/package.json @@ -0,0 +1,7 @@ +{ + "name": "es-module-bin", + "version": "3.0.0", + "bin": { + "es-module-bin": "bin/es-module-bin.js" + } +}