Skip to content

Commit

Permalink
feat: add the ability to specify a custom output file extension (#119)
Browse files Browse the repository at this point in the history
Fixes #91
  • Loading branch information
alangpierce authored May 7, 2017
1 parent bb37652 commit edd4b2d
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ more information.
extensionless files), but this mapping can be used to override the behavior
to provide a specific target directory, name, and/or file extension for any
specific files being converted.
* `outputFileExtension`: an optional file extension, like `"ts"` or `"jsx"`. If
specified, all converted files will have this extension.
* `jscodeshiftScripts`: an optional array of paths to
[jscodeshift](https://github.com/facebook/jscodeshift) scripts to run after
decaffeinate. This is useful to automate any cleanups to convert the output of
Expand Down
1 change: 1 addition & 0 deletions src/config/resolveConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default async function resolveConfig(commander) {
searchDirectory: config.searchDirectory,
fileFilterFn: config.fileFilterFn,
customNames: resolveCustomNames(config.customNames),
outputFileExtension: config.outputFileExtension || 'js',
fixImportsConfig: config.fixImportsConfig,
jscodeshiftScripts: config.jscodeshiftScripts,
landConfig: config.landConfig,
Expand Down
4 changes: 2 additions & 2 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ Re-run with the "check" command for more details.`);
});

await runWithProgressBar(
'Renaming files from .coffee to .js...',
`Renaming files from .coffee to .${config.outputFileExtension}...`,
movingCoffeeFiles,
async function(coffeePath) {
await move(coffeePath, jsPathFor(coffeePath, config));
});

let shortDescription = getShortDescription(coffeeFiles);
let renameCommitMsg =
`decaffeinate: Rename ${shortDescription} from .coffee to .js`;
`decaffeinate: Rename ${shortDescription} from .coffee to .${config.outputFileExtension}`;

if (movingCoffeeFiles.length > 0) {
console.log(`Generating the first commit: "${renameCommitMsg}"...`);
Expand Down
2 changes: 1 addition & 1 deletion src/util/FilePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function jsPathFor(path, config) {
if (isExtensionless(path)) {
return path;
} else {
return basePathFor(path) + '.js';
return basePathFor(path) + '.' + config.outputFileExtension;
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/convert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ console.log(x);
});
});

it('allows a custom file extension', async function() {
await runWithTemplateDir('convert-to-typescript', async function() {
await initGitRepo();
await runCli('convert');
await assertExists('./A.ts');
});
});

it('handles a missing eslint config', async function() {
await runWithTemplateDir('simple-success', async function() {
await initGitRepo();
Expand Down
1 change: 1 addition & 0 deletions test/examples/convert-to-typescript/A.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log 'This is a file'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
outputFileExtension: 'ts',
};
8 changes: 6 additions & 2 deletions test/util/FilePaths-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ describe('FilePaths', () => {
});

it('generates correct js paths', () => {
let emptyConfig = {customNames: {}};
let emptyConfig = {customNames: {}, outputFileExtension: 'js'};
assert.equal(FilePaths.jsPathFor('./a/b/foo.coffee', emptyConfig), 'a/b/foo.js');
assert.equal(FilePaths.jsPathFor('foo.coffee', emptyConfig), 'foo.js');
assert.equal(FilePaths.jsPathFor('foo.coffee.md', emptyConfig), 'foo.js');
assert.equal(FilePaths.jsPathFor('foo.cjsx', emptyConfig), 'foo.js');
assert.equal(FilePaths.jsPathFor('foo', emptyConfig), 'foo');
assert.equal(FilePaths.jsPathFor('foo', {customNames: {foo: 'bar'}}), 'bar');
assert.equal(
FilePaths.jsPathFor('foo', {customNames: {foo: 'bar'}}), 'bar');
assert.equal(
FilePaths.jsPathFor('foo.coffee', {customNames: {}, outputFileExtension: 'ts'}),
'foo.ts');
});

it('generates correct decaffeinate out paths', () => {
Expand Down

0 comments on commit edd4b2d

Please sign in to comment.