Skip to content

Commit

Permalink
Merge pull request #62 from NullVoxPopuli/add-hbs-to-supported-extens…
Browse files Browse the repository at this point in the history
…ions

Add support for custom file extensions
  • Loading branch information
rwjblue authored Jul 9, 2019
2 parents c9c1c48 + b65885f commit fb9f468
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ can update your project's README and your transforms README via:
codemod-cli update-docs
```

### File Types

By default the bin script that is generated for your `codemod-cli` project will run against `.js` and `.ts` files.
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
to add `--extensions=hbs,jsx`:

```js
#!/usr/bin/env node
'use strict';

require('codemod-cli').runTransform(
__dirname,
process.argv[2], /* transform name */,
process.argv.slice(3), /* paths or globs */
'hbs,jsx'
)
```

Contributing
------------------------------------------------------------------------------

Expand Down
8 changes: 6 additions & 2 deletions src/bin-support.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

async function runTransform(binRoot, transformName, args) {
const DEFAULT_EXTENSIONS = 'js,ts';

async function runTransform(binRoot, transformName, args, extensions = DEFAULT_EXTENSIONS) {
const globby = require('globby');
const execa = require('execa');
const chalk = require('chalk');
Expand All @@ -17,7 +19,9 @@ async function runTransform(binRoot, transformName, args) {
let jscodeshiftPath = path.dirname(require.resolve('jscodeshift/package'));
let binPath = path.join(jscodeshiftPath, jscodeshiftPkg.bin.jscodeshift);

return execa(binPath, ['-t', transformPath, '--extensions', 'js,ts', ...foundPaths], {
let binOptions = ['-t', transformPath, '--extensions', extensions, ...foundPaths];

return execa(binPath, binOptions, {
stdio: 'inherit',
env: {
CODEMOD_CLI_ARGS: JSON.stringify(options),
Expand Down
25 changes: 25 additions & 0 deletions tests/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ QUnit.module('codemod-cli', function(hooks) {
},
},
});

userProject.write({
foo: { 'something.js': `let blah = "bar";` },
});
Expand All @@ -392,6 +393,30 @@ QUnit.module('codemod-cli', function(hooks) {
});
});

QUnit.test('can specify additional extensions to run against', async function(assert) {
codemodProject.write({
transforms: {
main: {
'index.js': `
module.exports = function transformer(file, api) {
return file.source.toUpperCase();
}
`,
},
},
});

userProject.write({
foo: { 'something.hbs': `<Foo />` },
});

await CodemodCLI.runTransform(codemodProject.path('bin'), 'main', ['foo/**'], 'hbs');

assert.deepEqual(userProject.read(), {
foo: { 'something.hbs': `<FOO />` },
});
});

QUnit.test('runs transform against class syntax', async function(assert) {
userProject.write({
foo: {
Expand Down

0 comments on commit fb9f468

Please sign in to comment.