Skip to content

Commit

Permalink
fix: support es module (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacke2 authored and whxaxes committed Aug 8, 2019
1 parent 25025a1 commit f0f4b64
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/command.js
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
Expand Down
30 changes: 30 additions & 0 deletions 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);
});
});
7 changes: 7 additions & 0 deletions 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();
17 changes: 17 additions & 0 deletions 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;
17 changes: 17 additions & 0 deletions 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 <command> [options]');
// load directory
this.load(path.join(__dirname, 'command'));
}
}

exports.default = MainCommand;
7 changes: 7 additions & 0 deletions 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"
}
}

0 comments on commit f0f4b64

Please sign in to comment.