Skip to content

Commit fb9f468

Browse files
author
Robert Jackson
authored
Merge pull request #62 from NullVoxPopuli/add-hbs-to-supported-extensions
Add support for custom file extensions
2 parents c9c1c48 + b65885f commit fb9f468

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ can update your project's README and your transforms README via:
5757
codemod-cli update-docs
5858
```
5959

60+
### File Types
61+
62+
By default the bin script that is generated for your `codemod-cli` project will run against `.js` and `.ts` files.
63+
If you'd like to change that (e.g. to run against `.hbs` or `.jsx` files) you can tweak your projects `bin/cli.js` script
64+
to add `--extensions=hbs,jsx`:
65+
66+
```js
67+
#!/usr/bin/env node
68+
'use strict';
69+
70+
require('codemod-cli').runTransform(
71+
__dirname,
72+
process.argv[2], /* transform name */,
73+
process.argv.slice(3), /* paths or globs */
74+
'hbs,jsx'
75+
)
76+
```
77+
6078
Contributing
6179
------------------------------------------------------------------------------
6280

src/bin-support.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

3-
async function runTransform(binRoot, transformName, args) {
3+
const DEFAULT_EXTENSIONS = 'js,ts';
4+
5+
async function runTransform(binRoot, transformName, args, extensions = DEFAULT_EXTENSIONS) {
46
const globby = require('globby');
57
const execa = require('execa');
68
const chalk = require('chalk');
@@ -17,7 +19,9 @@ async function runTransform(binRoot, transformName, args) {
1719
let jscodeshiftPath = path.dirname(require.resolve('jscodeshift/package'));
1820
let binPath = path.join(jscodeshiftPath, jscodeshiftPkg.bin.jscodeshift);
1921

20-
return execa(binPath, ['-t', transformPath, '--extensions', 'js,ts', ...foundPaths], {
22+
let binOptions = ['-t', transformPath, '--extensions', extensions, ...foundPaths];
23+
24+
return execa(binPath, binOptions, {
2125
stdio: 'inherit',
2226
env: {
2327
CODEMOD_CLI_ARGS: JSON.stringify(options),

tests/cli-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ QUnit.module('codemod-cli', function(hooks) {
375375
},
376376
},
377377
});
378+
378379
userProject.write({
379380
foo: { 'something.js': `let blah = "bar";` },
380381
});
@@ -392,6 +393,30 @@ QUnit.module('codemod-cli', function(hooks) {
392393
});
393394
});
394395

396+
QUnit.test('can specify additional extensions to run against', async function(assert) {
397+
codemodProject.write({
398+
transforms: {
399+
main: {
400+
'index.js': `
401+
module.exports = function transformer(file, api) {
402+
return file.source.toUpperCase();
403+
}
404+
`,
405+
},
406+
},
407+
});
408+
409+
userProject.write({
410+
foo: { 'something.hbs': `<Foo />` },
411+
});
412+
413+
await CodemodCLI.runTransform(codemodProject.path('bin'), 'main', ['foo/**'], 'hbs');
414+
415+
assert.deepEqual(userProject.read(), {
416+
foo: { 'something.hbs': `<FOO />` },
417+
});
418+
});
419+
395420
QUnit.test('runs transform against class syntax', async function(assert) {
396421
userProject.write({
397422
foo: {

0 commit comments

Comments
 (0)