Skip to content

Commit

Permalink
feat: load ts as default file extension with FileSystemResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
mcMickJuice committed Jan 19, 2018
1 parent c8775cb commit c8f00c0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
}

Expand Down
48 changes: 48 additions & 0 deletions test/cli/CLITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function incrementValue(x) {
return x + 1;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
}

0 comments on commit c8f00c0

Please sign in to comment.