Skip to content

Commit

Permalink
Merge pull request #849 from KillWolfVlad/feature/executeSubCommand-t…
Browse files Browse the repository at this point in the history
…snode

add support for execute typescript subcommand via ts-node
  • Loading branch information
abetomo committed Aug 28, 2018
2 parents e5b27cc + bf47dcc commit 8bcc493
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -523,7 +523,7 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
// executable
var f = argv[1];
// name of the subcommand, link `pm-install`
var bin = basename(f, '.js') + '-' + args[0];
var bin = basename(f, path.extname(f)) + '-' + args[0];

// In case of globally installed, get the base dir where executable
// subcommand file should be located at
Expand All @@ -539,11 +539,14 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
// prefer local `./<bin>` to bin in the $PATH
var localBin = path.join(baseDir, bin);

// whether bin file is a js script with explicit `.js` extension
// whether bin file is a js script with explicit `.js` or `.ts` extension
var isExplicitJS = false;
if (exists(localBin + '.js')) {
bin = localBin + '.js';
isExplicitJS = true;
} else if (exists(localBin + '.ts')) {
bin = localBin + '.ts';
isExplicitJS = true;
} else if (exists(localBin)) {
bin = localBin;
}
Expand Down
52 changes: 52 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -31,6 +31,7 @@
"should": "^13.2.3",
"sinon": "^6.1.4",
"standard": "^11.0.1",
"ts-node": "^7.0.1",
"typescript": "^2.9.2"
},
"typings": "typings/index.d.ts"
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures-ts/pm-install.ts
@@ -0,0 +1,3 @@
#!/usr/bin/env node

console.log('install');
8 changes: 8 additions & 0 deletions test/fixtures-ts/pm.ts
@@ -0,0 +1,8 @@
#!/usr/bin/env node

var program = require('../../');

program
.version('0.0.1')
.command('install [name]', 'install one or more packages')
.parse(process.argv);
10 changes: 10 additions & 0 deletions test/test.command.executableSubcommand.tsnode.js
@@ -0,0 +1,10 @@
var exec = require('child_process').exec
, path = require('path')
, should = require('should');

var bin = path.join(__dirname, './fixtures-ts/pm.ts')

// success case
exec(process.argv[0] + ' -r ts-node/register ' + bin + ' install', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

0 comments on commit 8bcc493

Please sign in to comment.