Skip to content

Commit 1d3a48a

Browse files
committed
Added support for jscodeshift ignore options
1 parent 7f61739 commit 1d3a48a

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

src/bin-support.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ async function runTransform(binRoot, transformName, args, extensions = DEFAULT_E
77
const execa = require('execa');
88
const chalk = require('chalk');
99
const path = require('path');
10-
const { parseTransformArgs } = require('./options-support');
10+
const { parseTransformArgs, extractJSCodeShiftOptions } = require('./options-support');
1111

1212
let { paths, options } = parseTransformArgs(args);
13+
let { cliOptions, jsCodeShiftOptions } = extractJSCodeShiftOptions(options);
1314

1415
try {
1516
let foundPaths = await globby(paths, {
@@ -21,12 +22,12 @@ async function runTransform(binRoot, transformName, args, extensions = DEFAULT_E
2122
let jscodeshiftPath = path.dirname(require.resolve('jscodeshift/package'));
2223
let binPath = path.join(jscodeshiftPath, jscodeshiftPkg.bin.jscodeshift);
2324

24-
let binOptions = ['-t', transformPath, '--extensions', extensions, ...foundPaths];
25+
let binOptions = ['-t', transformPath, '--extensions', extensions, ...jsCodeShiftOptions, ...foundPaths];
2526

2627
return execa(binPath, binOptions, {
2728
stdio: 'inherit',
2829
env: {
29-
CODEMOD_CLI_ARGS: JSON.stringify(options),
30+
CODEMOD_CLI_ARGS: JSON.stringify(cliOptions),
3031
},
3132
});
3233
} catch (error) {

src/options-support.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const yargs = require('yargs');
22

3+
const jsCodeShiftOptions = ['ignore-config', 'ignore-pattern'];
4+
35
function parseTransformArgs(args) {
46
let parsedArgs = yargs.parse(args);
57
let paths = parsedArgs._;
@@ -20,4 +22,18 @@ function getOptions() {
2022
}
2123
}
2224

23-
module.exports = { parseTransformArgs, getOptions };
25+
function extractJSCodeShiftOptions(options, codeShiftOptions = jsCodeShiftOptions) {
26+
let cliOptions = Object.assign({}, options);
27+
let jsCodeShiftOptions = [];
28+
codeShiftOptions.forEach(option => {
29+
if(cliOptions.hasOwnProperty(option)) {
30+
let camelCaseOption = option.replace(/-([a-z])/g, (_, up) => up.toUpperCase());
31+
jsCodeShiftOptions.push(`--${option}`, options[option]);
32+
delete cliOptions[option];
33+
delete cliOptions[camelCaseOption];
34+
}
35+
});
36+
return { cliOptions, jsCodeShiftOptions };
37+
}
38+
39+
module.exports = { parseTransformArgs, getOptions, extractJSCodeShiftOptions };

tests/cli-test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,70 @@ QUnit.module('codemod-cli', function(hooks) {
445445
});
446446
});
447447

448+
QUnit.test('should ignore patterns from configuration option', async function(assert) {
449+
codemodProject.write({
450+
transforms: {
451+
main: {
452+
'index.js': `
453+
module.exports = function transformer(file, api) {
454+
return file.source.toUpperCase();
455+
}
456+
`,
457+
},
458+
},
459+
});
460+
461+
userProject.write({
462+
foo: { 'something.hbs': `<Foo />` },
463+
bar: { 'something.hbs': `<Foo />` },
464+
});
465+
466+
await CodemodCLI.runTransform(codemodProject.path('bin'), 'main', [
467+
'foo/**',
468+
'bar/**',
469+
'--ignore-pattern',
470+
'foo/'
471+
], 'hbs');
472+
473+
assert.deepEqual(userProject.read(), {
474+
foo: { 'something.hbs': `<Foo />` },
475+
bar: { 'something.hbs': `<FOO />` },
476+
});
477+
});
478+
479+
QUnit.test('should ignore patterns from configuration file', async function(assert) {
480+
codemodProject.write({
481+
transforms: {
482+
main: {
483+
'index.js': `
484+
module.exports = function transformer(file, api) {
485+
return file.source.toUpperCase();
486+
}
487+
`,
488+
},
489+
},
490+
});
491+
492+
userProject.write({
493+
config: { '.gitignore': `foo/` },
494+
foo: { 'something.hbs': `<Foo />` },
495+
bar: { 'something.hbs': `<Foo />` },
496+
});
497+
498+
await CodemodCLI.runTransform(codemodProject.path('bin'), 'main', [
499+
'foo/**',
500+
'bar/**',
501+
'--ignore-config',
502+
'config/.gitignore'
503+
], 'hbs');
504+
505+
assert.deepEqual(userProject.read(), {
506+
config: { '.gitignore': `foo/` },
507+
foo: { 'something.hbs': `<Foo />` },
508+
bar: { 'something.hbs': `<FOO />` },
509+
});
510+
});
511+
448512
QUnit.test('runs transform against class syntax', async function(assert) {
449513
userProject.write({
450514
foo: {

0 commit comments

Comments
 (0)