0

I'm trying to reduce the script duplication in my package.json. If I could parameterize the scripts that would help a lot.

How can I access an argument from an arbitrary position within a script (and keep it within package.json, rather than creating an external js file for this), and not just have node append the argument to the end of the line? Note that in several cases I want to make use of the same argument multiple times.

e.g.

This is what I currently have, and this works:

{
  "name": "angular-apps",
  "scripts": {
    "start-drt": "ng serve data-review --open",
    "build-drt": "ng lint data-review --type-check && ng build data-review",
    "build-drt-prod": "ng lint data-review --type-check && node replace-nonce data-review && ng build data-review --prod --progress=false && ng test data-review --watch=false --reporters teamcity",
... // etc.
/* There are several more application scripts that aren't listed.
   The only difference is the project name (e.g. `data-review` in this case).
   They all follow the same pattern as the set of scripts above. */

What I would like to have is something like this (this doesn't work, because it tries to use $1 literally rather than as a variable):

{
  "name": "angular-apps",
  "scripts": {
    "start-app": "ng serve $1 --open",
    "build-app": "ng lint $a --type-check && ng build $a",
    "build-app-prod": "ng lint $1 --type-check && node replace-nonce $1 && ng build $1 --prod --progress=false && ng test $1 --watch=false --reporters teamcity",

    "start-drt": "npm run start-app data-review",
    "build-drt": "npm run build-app data-review",
    "build-drt-prod": "npm run build-app-prod data-review",

    "start-sat": "npm run start-app section-analysis",
    "build-sat": "npm run build-app section-analysis",
    "build-sat-prod": "npm run build-app-prod section-analysis",
... // etc. Now I can add more apps and re-use the same scripts for all of them
HankScorpio
  • 3,612
  • 15
  • 27
  • Yeah... I think it is a dupe. Basically... what I want to be done can't be done without Bash or an external script... :-( – HankScorpio Oct 18 '19 at 05:06

1 Answers1

0

You can doing it like this.

// package.json
script: {
     start: "ng serve $*"
 }

Then call it like this npm run start analytics_app it named process.argv

mandaputtra
  • 922
  • 3
  • 18
  • 36
  • That wouldn't work in this case because the arguments just get appended to the end. I'd need them in the middle of the script, and in several places. – HankScorpio Oct 18 '19 at 05:04