Skip to content

Commit

Permalink
Merge pull request #508 from ElonVolo/GitIgnoreFlagIgnores
Browse files Browse the repository at this point in the history
Adding --gitignore flag
  • Loading branch information
ElonVolo authored Jun 14, 2024
2 parents d3b3c77 + 67f9953 commit 0fac51d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
48 changes: 42 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ All options are also passed to the transformer, which means you can supply custo
-h, --help print this help and exit
--ignore-config=FILE ... ignore files if they match patterns sourced from a configuration file (e.g. a .gitignore)
--ignore-pattern=GLOB ... ignore files that match a provided glob expression
--(no-)gitignore adds entries the current directory's .gitignore file
(default: false)
--parser=babel|babylon|flow|ts|tsx the parser to use for parsing the source files
(default: babel)
--parser-config=FILE path to a JSON file containing a custom parser configuration for flow or babylon
Expand Down Expand Up @@ -216,14 +218,14 @@ __Example: specifying parser type string in the transform file__
module.exports = function transformer(file, api, options) {
const j = api.jscodeshift;
const rootSource = j(file.source);

// whatever other code...

return rootSource.toSource();
}

// use the flow parser
module.exports.parser = 'flow';
module.exports.parser = 'flow';
```

__Example: specifying a custom parser object in the transform file__
Expand All @@ -233,9 +235,9 @@ __Example: specifying a custom parser object in the transform file__
module.exports = function transformer(file, api, options) {
const j = api.jscodeshift;
const rootSource = j(file.source);

// whatever other code...

return rootSource.toSource();
}

Expand Down Expand Up @@ -396,6 +398,40 @@ jscodeshift(ast).findIdentifiers().logNames();
jscodeshift(ast).logNames(); // error, unless `ast` only consists of Identifier nodes
```

### Ignoring files and directories


Sometimes there are files and directories that you want to avoid running transforms on. For example, the node_modules/ directory, where the project's installed local npm packages reside, can introduce bugs if any files in it are accidentally transformed by jscodeshift.

The simplest way to avoid many of these unwanted transforms is to pass jscodeshift the __--gitignore__ flag, which uses the glob patterns specified in your project’s .gitignore file to avoid transforming anything in directories such as node_modules/, dist/, etc. In most cases anything you want git to ignore you proabably are also going to want jscodeshift to ignore as well. _Please note that the .gitignore file use will be taken from the current working directory from which jscodeshift is being run._

```
jscodeshift --gitignore mytransform.js
```

For more custom ignore functionality, the __--ignore-pattern__ and the __--ignore-config__ arguments can be used.


__--ignore-pattern__ takes a [.gitignore format](https://git-scm.com/docs/gitignore#_pattern_format) glob pattern that specifies file and directory patterns to ignore

```
jscodeshift --ignore-pattern="js_configuration_files/**/*” mytransform.js
// More than one ignore
jscodeshift --ignore-pattern="first_ignored_dir/**/*” -—ignore-pattern="second_ignored_dir/**/*” mytransform.js
```

__--ignore-config__ takes one or more paths to files containing lines with [.gitignore format](https://git-scm.com/docs/gitignore#_pattern_format) glob patterns.
```
// note: .gitignore is a random made-up filename extension for this example
jscodeshift --ignore-config="MyIgnoreFile.gitignore" mytransform.js
// More than one ignore file
jscodeshift --ignore-pattern="first_ignore_file.gitignore” --ignore-pattern="second_ignore_file.gitignore” mytransform.js
```

### Passing options to [recast]

You may want to change some of the output settings (like setting `'` instead of `"`).
Expand Down
43 changes: 43 additions & 0 deletions bin/__tests__/jscodeshift-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const path = require('path');
const temp = require('temp');
const mkdirp = require('mkdirp');
const testUtils = require('../../utils/testUtils');
const {chdir} = require('process');

const createTransformWith = testUtils.createTransformWith;
const createTempFileWith = testUtils.createTempFileWith;
Expand Down Expand Up @@ -310,6 +311,48 @@ describe('jscodeshift CLI', () => {
);
});

it('sources ignore patterns from configuration file', () => {
const patterns = ['sub/dir/', '*-test.js'];
const gitignore = createTempFileWith(patterns.join('\n'), '.gitignore');
sources.push(createTempFileWith('subfile', 'sub/dir/file.js'));

return run(['-t', transform, '--ignore-config', gitignore].concat(sources)).then(
() => {
expect(readFile(sources[0]).toString()).toBe('transforma');
expect(readFile(sources[1]).toString()).toBe('a');
expect(readFile(sources[2]).toString()).toBe('subfile');
}
);
});

it('sources ignore patterns from a root directory\'s .gitignore file', () => {
// This test is a little different from the one above only in that we have
// to simulate automatically hitting up the .gitignore file from the current
// directory that the codeshift process is running from
const patterns = ['sub/dir/', '*-test.js'];
const gitignore = createTempFileWith(patterns.join('\n'), '.gitignore');
sources.push(createTempFileWith('subfile', 'sub/dir/file.js'));

// Make the temp directory with our test files the current working directory
let currPath = process.cwd();
let tempDirPath = path.dirname(sources[0]);
process.chdir(tempDirPath);;

return run(['-t', transform, '--gitignore'].concat(sources)).then(
() => {
expect(readFile(sources[0]).toString()).toBe('transforma');
expect(readFile(sources[1]).toString()).toBe('a');
expect(readFile(sources[2]).toString()).toBe('subfile');
}
)
.catch((err) => {
console.log(err);
})
.finally(() => {
process.chdir(currPath);
})
});

it('accepts a list of configuration files', () => {
const gitignore = createTempFileWith(['sub/dir/'].join('\n'), '.gitignore');
const eslintignore = createTempFileWith(['**/*test.js', 'sourceA.js'].join('\n'), '.eslintignore');
Expand Down
7 changes: 7 additions & 0 deletions src/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,16 @@ function run(transformFile, paths, options) {
const statsCounter = {};
const startTime = process.hrtime();

ignores.add(options.ignoreSet);
ignores.add(options.ignorePattern);
ignores.addFromFile(options.ignoreConfig);

if (options.gitignore) {
let currDirectory = process.cwd();
let gitIgnorePath = path.join(currDirectory, '.gitignore');
ignores.addFromFile(gitIgnorePath);
}

if (/^http/.test(transformFile)) {
usedRemoteScript = true;
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 0fac51d

Please sign in to comment.