diff --git a/python/install.py b/python/install.py index 857b2b1..15dcfc8 100644 --- a/python/install.py +++ b/python/install.py @@ -76,4 +76,4 @@ def main(argv=None): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/src/converter.ts b/src/converter.ts index 45d235e..373e889 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -67,7 +67,7 @@ export interface CompletionInfo { candidates: string[]; /** * The original completion from TS compiler. - * It's exposed for debugging purpuse. + * It's exposed for debugging purpose. */ original?: ts.CompletionInfo; } @@ -78,12 +78,14 @@ export interface IsCompleteResult { } export interface ConverterOptions { - /** If true, JavaScript mode. TypeSceript mode otherwise */ + /** If true, JavaScript mode. TypeScript mode otherwise */ isJS?: boolean; /** If true, creates a converter for browser mode. Otherwise, Node.js */ isBrowser?: boolean; /** Only for testing. File changes are forwarded to this handler. */ _fileWatcher?: ts.FileWatcherCallback; + /** If specified target will be used */ + target?: string; } export interface Converter { @@ -117,17 +119,17 @@ export function createConverter(options?: ConverterOptions): Converter { const dstFilename = normalizeJoin(outDir, "__tslab__.js"); const dstDeclFilename = normalizeJoin(outDir, "__tslab__.d.ts"); + const transpileTargetOption = ts.ScriptTarget[options?.target]; // c.f. // https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping // https://github.com/microsoft/TypeScript/issues/22306#issuecomment-412266626 - const transpileTarget = - semver.major(process.version) >= 12 + const transpileTarget = transpileTargetOption ?? + (semver.major(process.version) >= 12 ? ts.ScriptTarget.ES2019 - : ts.ScriptTarget.ES2018; + : ts.ScriptTarget.ES2018); // References: // https://github.com/microsoft/TypeScript/blob/master/src/lib/es2019.full.d.ts - const transpileLib = - transpileTarget === ts.ScriptTarget.ES2019 ? ["es2019"] : ["es2018"]; + const transpileLib = [ ts.ScriptTarget[transpileTarget] ]; if (options?.isBrowser) { transpileLib.push("dom"); transpileLib.push("dom.iterable"); diff --git a/src/main.ts b/src/main.ts index 61143b6..9951208 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,19 +11,21 @@ class ConverterSetImpl implements ConverterSet { private jsKernel: boolean; private _node: Converter; private _browser: Converter; + private target?: string; - constructor(jsKernel: boolean) { + constructor(jsKernel: boolean, target?: string) { this.jsKernel = jsKernel; + this.target = target; } get node(): Converter { if (!this._node) { - this._node = createConverter({ isJS: this.jsKernel, isBrowser: false }); + this._node = createConverter({ isJS: this.jsKernel, isBrowser: false, target: this.target }); } return this._node; } get browser(): Converter { if (!this._browser) { - this._browser = createConverter({ isJS: this.jsKernel, isBrowser: true }); + this._browser = createConverter({ isJS: this.jsKernel, isBrowser: true, target: this.target }); } return this._browser; } @@ -83,15 +85,16 @@ export function startKernel({ enableFindLocal = true, jsKernel = false, globalVersion = "", + target, }): void { if (enableFindLocal) { const local = findLocalStartKernel(); if (local) { - local({ configPath, enableFindLocal: false, jsKernel, globalVersion }); + local({ configPath, enableFindLocal: false, jsKernel, globalVersion, target }); return; } } - const convs = new ConverterSetImpl(jsKernel); + const convs = new ConverterSetImpl(jsKernel, target); convs.node; // Warm the converter for Node.js const executor = createExecutor(process.cwd(), convs, { log: console.log, @@ -145,17 +148,8 @@ export function main() { "--prefix [prefix]", "Kernelspec will be installed in {PREFIX}/share/jupyter/kernels/" ) - .action(function () { - if (arguments.length != 1) { - console.error( - "Unused args:", - Array.from(arguments).filter((arg) => { - return typeof arg === "string"; - }) - ); - process.exit(1); - } - let { binary, python, user, sysPrefix, prefix } = arguments[0]; + .action(function (options) { + const { binary, python, user, sysPrefix, prefix } = options; const args = [path.join(path.dirname(__dirname), "python", "install.py")]; args.push(`--tslab=${binary}`); if (user) { @@ -184,18 +178,10 @@ export function main() { .description("Start Jupyter kernel. Used from Jupyter internally") .option("--config-path ", "Path of config file") .option("--js", "If set, start JavaScript kernel. Otherwise, TypeScript.") - .action(function () { - if (arguments.length != 1) { - console.error( - "Unused args:", - Array.from(arguments).filter((arg) => { - return typeof arg === "string"; - }) - ); - process.exit(1); - } - let { configPath, js: jsKernel } = arguments[0]; - startKernel({ configPath, jsKernel, globalVersion: getVersion() }); + .option("-t, --target [target]", "If set, target will be used by transpiler.") + .action(function (options) { + const { configPath, js: jsKernel, target } = options; + startKernel({ configPath, jsKernel, globalVersion: getVersion(), target }); }); program.parse(process.argv);