diff --git a/src/Options.ts b/src/Options.ts index 18d3c757..0e9d0c1c 100644 --- a/src/Options.ts +++ b/src/Options.ts @@ -46,12 +46,21 @@ export default class Options { readonly stdio: boolean, readonly help: boolean, readonly dry: boolean - ) {} + ) { + const defaultFileSystemExtensions = new Set(['.js']); + if (transpilePluginsWithTypescript) { + { + defaultFileSystemExtensions.add('.ts'); + } + } - private pluginLoader = new PluginLoader([ - new FileSystemResolver(), - new PackageResolver() - ]); + this.pluginLoader = new PluginLoader([ + new FileSystemResolver(defaultFileSystemExtensions), + new PackageResolver() + ]); + } + + private pluginLoader: PluginLoader; private remotePluginLoader = new PluginLoader([ new AstExplorerResolver(), @@ -110,16 +119,15 @@ export default class Options { } } - loadTypescriptTranspile() { - try{ + try { require.resolve('typescript'); } catch (e) { - throw new Error('Typescript is not installed locally. You must installed Typescript locally in order to transpile plugins written in Typescript'); + throw new Error( + 'Typescript is not installed locally. You must installed Typescript locally in order to transpile plugins written in Typescript' + ); } - //TODO: what about options???? - require('ts-node').register(); } diff --git a/test/cli/CLITest.ts b/test/cli/CLITest.ts index e6e43d09..8397be95 100644 --- a/test/cli/CLITest.ts +++ b/test/cli/CLITest.ts @@ -254,6 +254,30 @@ describe('CLI', function() { strictEqual(await readFile(afile, 'utf8'), '4 + 5;'); }); + it('can load plugins written in Typescript without ts extension', async function() { + let afile = await createTemporaryFile('a-file.js', '3 + 4;'); + let pluginFile = join( + __dirname, + '../fixtures/plugin/typescript/increment-typescript' + ); + let { status, stdout, stderr } = await runCodemodCLI([ + afile, + '-p', + pluginFile, + '--transpile-ts-plugins' + ]); + + deepEqual( + { status, stdout, stderr }, + { + status: 0, + stdout: `${afile}\n1 file(s), 1 modified, 0 errors\n`, + stderr: '' + } + ); + strictEqual(await readFile(afile, 'utf8'), '4 + 5;'); + }); + it('can load plugins with multiple files written in Typescript', async function() { let afile = await createTemporaryFile('a-file.js', '3 + 4;'); let pluginFile = join( @@ -277,4 +301,28 @@ describe('CLI', function() { ); strictEqual(await readFile(afile, 'utf8'), '4 + 5;'); }); + + it('can load plugins with multiple files written in Typescript and Javascript', async function() { + let afile = await createTemporaryFile('a-file.js', '3 + 4;'); + let pluginFile = join( + __dirname, + '../fixtures/plugin/typescript/increment-export-default-multiple/increment-export-index.ts' + ); + let { status, stdout, stderr } = await runCodemodCLI([ + afile, + '-p', + pluginFile, + '--transpile-ts-plugins' + ]); + + deepEqual( + { status, stdout, stderr }, + { + status: 0, + stdout: `${afile}\n1 file(s), 1 modified, 0 errors\n`, + stderr: '' + } + ); + strictEqual(await readFile(afile, 'utf8'), '4 + 5;'); + }); }); diff --git a/test/fixtures/plugin/typescript/increment-export-default-multiple/increment-export-default-js.js b/test/fixtures/plugin/typescript/increment-export-default-multiple/increment-export-default-js.js new file mode 100644 index 00000000..3a04ca07 --- /dev/null +++ b/test/fixtures/plugin/typescript/increment-export-default-multiple/increment-export-default-js.js @@ -0,0 +1,3 @@ +export function incrementValue(x) { + return x + 1; +} diff --git a/test/fixtures/plugin/typescript/increment-export-default-multiple/increment-export-index.ts b/test/fixtures/plugin/typescript/increment-export-default-multiple/increment-export-index.ts new file mode 100644 index 00000000..1a85e16d --- /dev/null +++ b/test/fixtures/plugin/typescript/increment-export-default-multiple/increment-export-index.ts @@ -0,0 +1,13 @@ +import { incrementValue } from './increment-export-default-js'; + +/* tslint:disable */ +export default function() { + return { + visitor: { + NumericLiteral(path) { + let value: number = path.node.value; + path.node.value = incrementValue(value); + } + } + }; +}