Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Issues
  • #8795
Closed
Open
Issue created Apr 06, 2020 by Administrator@rootContributor

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

  1. create a new template, extending cra-template
  2. add a script which chains multiple other scripts together, using npm and/or npm run
  3. 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 😅 perhaps I could split this bit of logic out into a function and write some tests for it in order to make it worthwhile.

Thanks :)

Assignee
Assign to
Time tracking