replace-in-file
Advanced tools
Comparing version 8.0.2 to 8.0.3
@@ -28,3 +28,3 @@ #!/usr/bin/env node | ||
//Extract settings | ||
const {from, to, files, isRegex, verbose, quiet} = options | ||
const {from, to, files, verbose, quiet} = options | ||
@@ -36,9 +36,2 @@ //Single star globs already get expanded in the command line | ||
//If the isRegex flag is passed, convert the from parameter to a RegExp object | ||
if (isRegex && typeof from === 'string') { | ||
const flags = from.replace(/.*\/([gimyus]*)$/, '$1') | ||
const pattern = from.replace(new RegExp(`^/(.*?)/${flags}$`), '$1') | ||
options.from = new RegExp(pattern, flags) | ||
} | ||
//Log | ||
@@ -45,0 +38,0 @@ if (!quiet) { |
@@ -13,5 +13,8 @@ ## 8.0.0 | ||
### New features | ||
You can now specify a `getTargetFile` config param to modify the target file for saving the new file contents to. For example: | ||
- The `isRegex` flag is no longer required. | ||
- You can now specify a `getTargetFile` config param to modify the target file for saving the new file contents to. For example: | ||
```js | ||
@@ -18,0 +21,0 @@ const options = { |
{ | ||
"name": "replace-in-file", | ||
"type": "module", | ||
"version": "8.0.2", | ||
"version": "8.0.3", | ||
"description": "A simple utility to quickly replace text in one or more files.", | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/adamreisnz/replace-in-file#readme", |
@@ -487,3 +487,2 @@ # Replace in file | ||
[--disableGlobs] | ||
[--isRegex] | ||
[--verbose] | ||
@@ -505,3 +504,3 @@ [--quiet] | ||
A regular expression may be used for the `from` parameter by specifying the `--isRegex` flag. | ||
A regular expression may be used for the `from` parameter by passing in a string correctly formatted as a regular expression. The library will automatically detect that it is a regular expression. | ||
@@ -524,4 +523,2 @@ The `from` and `to` parameters, as well as the files list, can be omitted if you provide this | ||
It is not necessary to use the `--isRegex` flag in this case. | ||
## A note on using globs with the CLI | ||
@@ -528,0 +525,0 @@ When using the CLI, the glob pattern is handled by the operating system. But if you specify the glob pattern in the configuration file, the package will use the glob module from the Node modules, and this can lead to different behaviour despite using the same pattern. |
@@ -17,11 +17,3 @@ import path from 'node:path' | ||
const json = await fs.readFile(path.resolve(file), 'utf8') | ||
const config = JSON.parse(json) | ||
//Since we can't store Regexp in JSON, convert from string if needed | ||
if (config.from && config.from.match(/.*\/([gimyus]*)$/)) { | ||
config.from = new RegExp(config.from) | ||
} | ||
//Return config | ||
return config | ||
return JSON.parse(json) | ||
} | ||
@@ -77,2 +69,9 @@ | ||
//Since we can't store Regexp in JSON, convert from string if needed | ||
if (typeof from === 'string' && from.match(/.*\/([gimyus]*)$/)) { | ||
const flags = from.replace(/.*\/([gimyus]*)$/, '$1') | ||
const pattern = from.replace(new RegExp(`^/(.*?)/${flags}$`), '$1') | ||
config.from = new RegExp(pattern, flags) | ||
} | ||
//Use default encoding if invalid | ||
@@ -90,3 +89,2 @@ if (typeof encoding !== 'string' || encoding === '') { | ||
countMatches: false, | ||
isRegex: false, | ||
verbose: false, | ||
@@ -111,3 +109,3 @@ quiet: false, | ||
from, to, files, ignore, encoding, verbose, | ||
allowEmptyPaths, disableGlobs, isRegex, dry, quiet, | ||
allowEmptyPaths, disableGlobs, dry, quiet, | ||
} = config | ||
@@ -138,5 +136,2 @@ | ||
} | ||
if (typeof isRegex === 'undefined') { | ||
isRegex = !!argv.isRegex | ||
} | ||
if (typeof verbose === 'undefined') { | ||
@@ -155,4 +150,4 @@ verbose = !!argv.verbose | ||
from, to, files, ignore, encoding, verbose, | ||
allowEmptyPaths, disableGlobs, isRegex, dry, quiet, | ||
allowEmptyPaths, disableGlobs, dry, quiet, | ||
}) | ||
} |
@@ -49,58 +49,2 @@ import {expect, use, should} from 'chai' | ||
}) | ||
it('should convert from regex if provided in JSON', async () => { | ||
//Test config | ||
const config = { | ||
files: ['file.txt'], | ||
from: '/foo/g', | ||
to: 'bar', | ||
} | ||
fs.writeFileSync('config.json', JSON.stringify(config), 'utf8') | ||
//Load config | ||
const cfg = await loadConfig('config.json') | ||
expect(cfg.from).to.be.an.instanceof(RegExp) | ||
//Clean up | ||
fs.unlinkSync('config.json') | ||
}) | ||
it('should not convert from regex if it is a regular string', async () => { | ||
//Test config | ||
const config = { | ||
files: ['file.txt'], | ||
from: '/foo', | ||
to: 'bar', | ||
} | ||
fs.writeFileSync('config.json', JSON.stringify(config), 'utf8') | ||
//Load config | ||
const cfg = await loadConfig('config.json') | ||
expect(cfg.from).not.to.be.an.instanceof(RegExp) | ||
expect(cfg.from).to.equal(config.from) | ||
//Clean up | ||
fs.unlinkSync('config.json') | ||
}) | ||
it('should ignore the isRegex flag if a regex has already been provided', async () => { | ||
//Test config | ||
const config = { | ||
files: ['file.txt'], | ||
from: '/foo/g', | ||
to: 'bar', | ||
isRegex: true, | ||
} | ||
fs.writeFileSync('config.json', JSON.stringify(config), 'utf8') | ||
//Load config | ||
const cfg = await loadConfig('config.json') | ||
expect(cfg.from).to.be.an.instanceof(RegExp) | ||
//Clean up | ||
fs.unlinkSync('config.json') | ||
}) | ||
}) | ||
@@ -118,3 +62,2 @@ | ||
disableGlobs: true, | ||
isRegex: true, | ||
dry: true, | ||
@@ -130,3 +73,2 @@ quiet: true, | ||
expect(combined.disableGlobs).to.be.true | ||
expect(combined.isRegex).to.be.true | ||
expect(combined.dry).to.be.true | ||
@@ -240,2 +182,21 @@ expect(combined.quiet).to.be.true | ||
it('should convert from regex if provided in JSON', async () => { | ||
const parsed = parseConfig({ | ||
files: ['file.txt'], | ||
from: '/foo/g', | ||
to: 'bar', | ||
}) | ||
expect(parsed.from).to.be.an.instanceof(RegExp) | ||
}) | ||
it('should not convert from regex if it is a regular string', async () => { | ||
const parsed = parseConfig({ | ||
files: ['file.txt'], | ||
from: '/foo', | ||
to: 'bar', | ||
}) | ||
expect(parsed.from).not.to.be.an.instanceof(RegExp) | ||
expect(parsed.from).to.equal('/foo') | ||
}) | ||
it('should overwrite the config defaults', () => { | ||
@@ -251,3 +212,2 @@ const parsed = parseConfig({ | ||
countMatches: true, | ||
isRegex: true, | ||
verbose: true, | ||
@@ -266,3 +226,2 @@ quiet: true, | ||
expect(parsed.countMatches).to.be.true | ||
expect(parsed.isRegex).to.be.true | ||
expect(parsed.verbose).to.be.true | ||
@@ -269,0 +228,0 @@ expect(parsed.quiet).to.be.true |
@@ -829,2 +829,17 @@ import {expect, use, should} from 'chai' | ||
}) | ||
//#197 | ||
describe('#197', () => { | ||
before(() => fs.writeFileSync('test197', 'ABC 123\n6666666\nDEF 456', 'utf8')) | ||
after(() => fs.unlinkSync('test197')) | ||
it('should replace contents when a regex string is passed', () => { | ||
replaceInFileSync({ | ||
files: 'test197', | ||
from: '/\\w{3} \\d{3}/g', | ||
to: 'replaced', | ||
}) | ||
const test197 = fs.readFileSync('test197', 'utf8') | ||
expect(test197).to.equal('replaced\n6666666\nreplaced') | ||
}) | ||
}) | ||
}) | ||
@@ -831,0 +846,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
272560
2156
568