Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for execute typescript subcommand via ts-node #849

Merged
merged 1 commit into from Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
});