Skip to content

Commit

Permalink
Don't try to create a regex from a regex, use better regex detection
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreisnz committed Jul 2, 2024
1 parent a5e19f5 commit 760102a
Show file tree
Hide file tree
Showing 8 changed files with 2,339 additions and 1,368 deletions.
Binary file added .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ If you are using a configuration file, and you want to use a regular expression
}
```

It is not necessary to use the `--isRegex` flag in this case.

## A note on using globs with the CLI
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.

Expand Down
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function main() {
}, [])

//If the isRegex flag is passed, convert the from parameter to a RegExp object
if (isRegex) {
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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "replace-in-file",
"type": "module",
"version": "8.0.1",
"version": "8.0.2",
"description": "A simple utility to quickly replace text in one or more files.",
"homepage": "https://github.com/adamreisnz/replace-in-file#readme",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function loadConfig(file) {
const config = JSON.parse(json)

//Since we can't store Regexp in JSON, convert from string if needed
if (config.from && config.from.startsWith('/')) {
if (config.from && config.from.match(/.*\/([gimyus]*)$/)) {
config.from = new RegExp(config.from)
}

Expand Down
21 changes: 20 additions & 1 deletion src/helpers/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('helpers/config.js', () => {
//Test config
const config = {
files: ['file.txt'],
from: 'foo',
from: '/foo',
to: 'bar',
}
fs.writeFileSync('config.json', JSON.stringify(config), 'utf8')
Expand All @@ -83,6 +83,25 @@ describe('helpers/config.js', () => {
//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')
})
})

/**
Expand Down
Loading

0 comments on commit 760102a

Please sign in to comment.