Skip to content

Commit 754fd80

Browse files
committed
[enhance] Allow passing of glob config
1 parent 6ed5456 commit 754fd80

File tree

8 files changed

+86
-16
lines changed

8 files changed

+86
-16
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ A simple utility to quickly replace text in one or more files or globs. Works sy
2929
- [Ignore multiple files or globs](#ignore-multiple-files-or-globs)
3030
- [Allow empty/invalid paths](#allow-emptyinvalid-paths)
3131
- [Disable globs](#disable-globs)
32+
- [Specify glob configuration](#glob-configuration)
3233
- [Specify character encoding](#specify-character-encoding)
3334
- [Dry run](#dry-run)
3435
- [CLI usage](#cli-usage)
@@ -263,6 +264,19 @@ const options = {
263264
};
264265
```
265266

267+
### Specify glob configuration
268+
Specify configuration passed to the `glob` call:
269+
270+
```js
271+
const options = {
272+
glob: {
273+
//Glob settings here
274+
},
275+
};
276+
```
277+
278+
Please note that the setting `nodir` will always be passed as `false`.
279+
266280
### Specify character encoding
267281
Use a different character encoding for reading/writing files. Defaults to `utf-8`.
268282

lib/helpers/get-paths-async.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const globAsync = require('./glob-async');
99
* Get paths asynchrously
1010
*/
1111
module.exports = function getPathsAsync(
12-
patterns, ignore, disableGlobs, allowEmptyPaths
12+
patterns, ignore, disableGlobs, allowEmptyPaths, cfg
1313
) {
1414

1515
//Not using globs?
@@ -19,6 +19,7 @@ module.exports = function getPathsAsync(
1919

2020
//Expand globs and flatten paths
2121
return Promise
22-
.all(patterns.map(pattern => globAsync(pattern, ignore, allowEmptyPaths)))
22+
.all(patterns
23+
.map(pattern => globAsync(pattern, ignore, allowEmptyPaths, cfg)))
2324
.then(paths => [].concat.apply([], paths));
2425
};

lib/helpers/get-paths-sync.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ const glob = require('glob');
88
/**
99
* Get paths (sync)
1010
*/
11-
module.exports = function getPathsSync(patterns, ignore, disableGlobs) {
11+
module.exports = function getPathsSync(patterns, ignore, disableGlobs, cfg) {
1212

1313
//Not using globs?
1414
if (disableGlobs) {
1515
return patterns;
1616
}
1717

18+
//Prepare glob config
19+
cfg = Object.assign({ignore}, cfg, {nodir: true});
20+
1821
//Get paths
1922
const paths = patterns
20-
.map(pattern => glob.sync(pattern, {ignore, nodir: true}));
23+
.map(pattern => glob.sync(pattern, cfg));
2124

2225
//Return flattened
2326
return [].concat.apply([], paths);

lib/helpers/glob-async.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ const glob = require('glob');
88
/**
99
* Async wrapper for glob
1010
*/
11-
module.exports = function globAsync(pattern, ignore, allowEmptyPaths) {
11+
module.exports = function globAsync(pattern, ignore, allowEmptyPaths, cfg) {
12+
13+
//Prepare glob config
14+
cfg = Object.assign({ignore}, cfg, {nodir: true});
15+
16+
//Return promise
1217
return new Promise((resolve, reject) => {
13-
glob(pattern, {ignore, nodir: true}, (error, files) => {
18+
glob(pattern, cfg, (error, files) => {
1419

1520
//istanbul ignore if: hard to make glob error
1621
if (error) {

lib/helpers/parse-config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const defaults = {
1111
isRegex: false,
1212
verbose: false,
1313
dry: false,
14+
glob: {},
1415
};
1516

1617
/**
@@ -23,8 +24,11 @@ module.exports = function parseConfig(config) {
2324
throw new Error('Must specify configuration object');
2425
}
2526

27+
//Fix glob
28+
config.glob = config.glob || {};
29+
2630
//Extract data
27-
const {files, from, to, ignore, encoding} = config;
31+
const {files, from, to, ignore, encoding, glob} = config;
2832

2933
//Validate values
3034
if (typeof files === 'undefined') {
@@ -36,6 +40,9 @@ module.exports = function parseConfig(config) {
3640
if (typeof to === 'undefined') {
3741
throw new Error('Must specify a replacement (can be blank string)');
3842
}
43+
if (typeof glob !== 'object') {
44+
throw new Error('Invalid glob config');
45+
}
3946

4047
//Ensure arrays
4148
if (!Array.isArray(files)) {

lib/replace-in-file.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ function replaceInFile(config, cb) {
2929
//Get config
3030
const {
3131
files, from, to, encoding, ignore, allowEmptyPaths, disableGlobs,
32-
dry, verbose,
32+
dry, verbose, glob,
3333
} = config;
3434

3535
//Dry run?
36+
//istanbul ignore if: No need to test console logs
3637
if (dry && verbose) {
3738
console.log(chalk.yellow('Dry run, not making actual changes'));
3839
}
3940

4041
//Find paths
41-
return getPathsAsync(files, ignore, disableGlobs, allowEmptyPaths)
42+
return getPathsAsync(files, ignore, disableGlobs, allowEmptyPaths, glob)
4243

4344
//Make replacements
4445
.then(paths => Promise.all(paths.map(file => {
@@ -81,12 +82,13 @@ replaceInFile.sync = function(config) {
8182

8283
//Get config, paths, and initialize changed files
8384
const {
84-
files, from, to, encoding, ignore, disableGlobs, dry, verbose,
85+
files, from, to, encoding, ignore, disableGlobs, dry, verbose, glob,
8586
} = config;
86-
const paths = getPathsSync(files, ignore, disableGlobs);
87+
const paths = getPathsSync(files, ignore, disableGlobs, glob);
8788
const changedFiles = [];
8889

8990
//Dry run?
91+
//istanbul ignore if: No need to test console logs
9092
if (dry && verbose) {
9193
console.log(chalk.yellow('Dry run, not making any changes'));
9294
}

lib/replace-in-file.spec.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ describe('Replace in file', () => {
6868
})).to.eventually.be.rejectedWith(Error);
6969
});
7070

71+
it('should throw an error when invalid `glob` config defined', () => {
72+
return expect(replace({
73+
files: 'test1',
74+
from: /re\splace/g,
75+
to: 'test',
76+
glob: 'invalid',
77+
})).to.eventually.be.rejectedWith(Error);
78+
});
79+
7180
it('should replace contents in a single file with regex', done => {
7281
replace({
7382
files: 'test1',
@@ -89,7 +98,7 @@ describe('Replace in file', () => {
8998
expect(file).to.equal('test1');
9099
return /re\splace/g;
91100
},
92-
to: 'b'
101+
to: 'b',
93102
}).then(() => {
94103
const test1 = fs.readFileSync('test1', 'utf8');
95104
const test2 = fs.readFileSync('test2', 'utf8');
@@ -344,6 +353,35 @@ describe('Replace in file', () => {
344353
done();
345354
});
346355
});
356+
357+
it('should accept glob configuration', done => {
358+
replace({
359+
files: 'test1',
360+
from: /re\splace/g,
361+
to: 'b',
362+
allowEmptyPaths: true,
363+
glob: {
364+
ignore: ['test1'],
365+
},
366+
}).then(() => {
367+
const test1 = fs.readFileSync('test1', 'utf8');
368+
expect(test1).to.equal('a re place c');
369+
done();
370+
});
371+
});
372+
373+
it('should ignore empty glob configuration', done => {
374+
replace({
375+
files: 'test1',
376+
from: /re\splace/g,
377+
to: 'b',
378+
glob: null,
379+
}).then(() => {
380+
const test1 = fs.readFileSync('test1', 'utf8');
381+
expect(test1).to.equal('a b c');
382+
done();
383+
});
384+
});
347385
});
348386

349387
/**
@@ -416,7 +454,7 @@ describe('Replace in file', () => {
416454
expect(file).to.equal('test1');
417455
return /re\splace/g;
418456
},
419-
to: 'b'
457+
to: 'b',
420458
}, () => {
421459
const test1 = fs.readFileSync('test1', 'utf8');
422460
const test2 = fs.readFileSync('test2', 'utf8');
@@ -749,7 +787,7 @@ describe('Replace in file', () => {
749787
expect(file).to.equal('test1');
750788
return /re\splace/g;
751789
},
752-
to: 'b'
790+
to: 'b',
753791
});
754792
const test1 = fs.readFileSync('test1', 'utf8');
755793
const test2 = fs.readFileSync('test2', 'utf8');

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"coverage": "open -a \"Google Chrome\" ./coverage/lcov-report/index.html"
3232
},
3333
"dependencies": {
34-
"chalk": "^2.3.0",
34+
"chalk": "^2.3.2",
3535
"glob": "^7.1.2",
3636
"yargs": "^11.0.0"
3737
},
@@ -43,7 +43,7 @@
4343
"chai-as-promised": "^7.1.1",
4444
"dirty-chai": "^2.0.1",
4545
"istanbul": "^1.0.0-alpha.2",
46-
"mocha": "^5.0.0"
46+
"mocha": "^5.0.4"
4747
},
4848
"browserify": {
4949
"transform": [

0 commit comments

Comments
 (0)