react-scripts init doesn't replace all "npm"/"npm run" instances in template.json scripts
Created by: tomvalorsa
Describe the bug
When creating a new project using a custom template and using yarn, if I have a script in my template.json
file which combines multiple npm scripts, e.g.
{
"scripts": {
"pre-commit": "npm run prettier && npm run lint && npm test"
}
}
the output in my newly initialised app's package.json
is as follows
{
"scripts": {
"pre-commit": "yarn prettier && npm run lint && npm test"
}
}
Only the first instance of npm
or npm run
is replaced in each script string.
It doesn't break the process of initialising the new app, and perhaps "bug" isn't the right word given that this was presumably written in the first instance to cover default scripts added to a project when templates weren't a thing yet.
However it would be nice to fix this.
Did you try recovering your dependencies?
N/A
Which terms did you search for in User Guide?
N/A
Environment
N/A
Steps to reproduce
- create a new template, extending
cra-template
- add a script which chains multiple other scripts together, using
npm
and/ornpm run
- build a new app using this template and look at the resulting
package.json
generated
Expected behavior
All instances of npm
and npm run
replaced with yarn
.
Actual behavior
Only first instance replaced.
I think this is an easy 1 character fix on L189 of react-scripts/init.js - simply giving the regex a global flag so that it replaces all instances of the match. I've tested this on the strings in the example above in my browser console and it works.
I'm more than happy to make a PR for this but it feels a bit cheeky given it's a 1 char change
Thanks :)