From bf47dcca69951a3dbef299d1052c8dc1a3e10c76 Mon Sep 17 00:00:00 2001 From: KillWolfVlad Date: Fri, 17 Aug 2018 16:21:26 +0500 Subject: [PATCH] add support for execute typescript subcommand via ts-node --- index.js | 7 ++- package-lock.json | 52 +++++++++++++++++++ package.json | 1 + test/fixtures-ts/pm-install.ts | 3 ++ test/fixtures-ts/pm.ts | 8 +++ ...est.command.executableSubcommand.tsnode.js | 10 ++++ 6 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 test/fixtures-ts/pm-install.ts create mode 100644 test/fixtures-ts/pm.ts create mode 100644 test/test.command.executableSubcommand.tsnode.js diff --git a/index.js b/index.js index 3ad0cacfd..22c15adac 100644 --- a/index.js +++ b/index.js @@ -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 @@ -539,11 +539,14 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { // prefer local `./` 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; } diff --git a/package-lock.json b/package-lock.json index 37366edfa..070123580 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1150,6 +1150,12 @@ "yallist": "^2.1.2" } }, + "make-error": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.4.tgz", + "integrity": "sha512-0Dab5btKVPhibSalc9QGXb559ED7G7iLjFXBaj9Wq8O3vorueR5K5jaE3hkG6ZQINyhA/JgG6Qk4qdFQjsYV6g==", + "dev": true + }, "mimic-fn": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-1.2.0.tgz", @@ -1787,6 +1793,22 @@ "is-fullwidth-code-point": "^2.0.0" } }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + }, + "source-map-support": { + "version": "0.5.8", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.8.tgz", + "integrity": "sha512-WqAEWPdb78u25RfKzOF0swBpY0dKrNdjc4GvLwm7ScX/o9bj8Eh/YL8mcMhBHYDGl87UkkSXDOFnW4G7GhWhGg==", + "dev": true, + "requires": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, "spdx-correct": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.0.0.tgz", @@ -2137,6 +2159,30 @@ "os-tmpdir": "~1.0.2" } }, + "ts-node": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-7.0.1.tgz", + "integrity": "sha512-BVwVbPJRspzNh2yfslyT1PSbl5uIk03EZlb493RKHN4qej/D06n1cEhjlOJG69oFsE7OT8XjpTUcYf6pKTLMhw==", + "dev": true, + "requires": { + "arrify": "^1.0.0", + "buffer-from": "^1.1.0", + "diff": "^3.1.0", + "make-error": "^1.1.1", + "minimist": "^1.2.0", + "mkdirp": "^0.5.1", + "source-map-support": "^0.5.6", + "yn": "^2.0.0" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", + "dev": true + } + } + }, "type-check": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.3.2.tgz", @@ -2236,6 +2282,12 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=", "dev": true + }, + "yn": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/yn/-/yn-2.0.0.tgz", + "integrity": "sha1-5a2ryKz0CPY4X8dklWhMiOavaJo=", + "dev": true } } } diff --git a/package.json b/package.json index d3a1b24b5..ce30a0b66 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/fixtures-ts/pm-install.ts b/test/fixtures-ts/pm-install.ts new file mode 100644 index 000000000..a99408b45 --- /dev/null +++ b/test/fixtures-ts/pm-install.ts @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +console.log('install'); diff --git a/test/fixtures-ts/pm.ts b/test/fixtures-ts/pm.ts new file mode 100644 index 000000000..723ccb772 --- /dev/null +++ b/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); diff --git a/test/test.command.executableSubcommand.tsnode.js b/test/test.command.executableSubcommand.tsnode.js new file mode 100644 index 000000000..c7e5524cc --- /dev/null +++ b/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'); +});