1

I got ParcelJS project with multiple package.json file scripts commands.

"scripts": {
    "parcel": "./node_modules/@mms/parcel-bundler/bin/cli.js",

    "start": "parcel templates/layouts/index.pug",
    "start:news": "parcel templates/layouts/news.pug",
    "start:news-inner": "parcel templates/layouts/news-inner.pug",

    more and more commads here...
}

The package.json file above works fine but it's become bigger and unreadable as the project is growing up.

So I want to compress package.json scripts block by remove all :postfix pages names and pass the necessary page name as NPM arguments with alternative fallback page name at, if there is no page specified

Sample of expected code:

"scripts": {
    "parcel": "./node_modules/@mms/parcel-bundler/bin/cli.js",
    "start": "parcel templates/layouts/\"$1 || index\".pug"
}

Of course the example above isn't working because it has syntax problems, so as the first steps I tried the simpler code below, and it's not working too, and I don't know why.

"scripts": {
    "parcel": "./node_modules/@mms/parcel-bundler/bin/cli.js",
    "start": "parcel templates/layouts/\"$1\".pug"
}

Terminal result of the above script runing:

$ yarn start index
yarn run v1.19.1
$ parcel templates/layouts/"${1}".pug index
Server running at http://localhost:1234 
  No entries found.
error Command failed with exit code 1.

Could you please help me to fix this NPM script?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
north.inhale
  • 483
  • 2
  • 9
  • 20
  • In the link provided I explain that on a _*nix_ platform you can utilize a shell function in npm scripts because npm utilizes `sh` to run scripts. Given your scenario you can set your `start` script to `"start": "func () { parcel templates/layouts/\"${1:-index}\".pug; }; func"`. When running `npm start` (i.e. without passing an arg) it will set path to `templates/layouts/index.pug`, i.e. it uses the default `index`. When you run `npm start -- news` (i.e. pass a `news` arg) it sets the path to `templates/layouts/news.pug`. For a x-platform solution see section _"Solution 2 - Cross-platform"_ – RobC Jan 23 '20 at 12:14
  • Hi, while I delve into the proposed answer =) – north.inhale Jan 23 '20 at 12:20
  • Hi, If you're wondering what the `${1:-index}` part is in my previous comment, refer to the `${parameter:-word}` example in [Shell Parameter Expansion](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion). It provides us with a mechanism to specify a default value (`index`) which can be substituted by an arg provided via the CLI - such as; `news`, `news-inner`, etc. As mentioned previously, for a cross-platform solution you'll need to utilize a _node.js_ helper script instead - On windows npm utilizes `cmd` as the default shell, and _not_ `sh`. – RobC Jan 23 '20 at 12:48
  • I choose the second mentioned method, and it's work – north.inhale Jan 23 '20 at 14:31
  • I close this ASK, but wonna some roll my comments in you mentioned answer by the link below (see you there!): https://stackoverflow.com/questions/51388921/pass-command-line-args-to-npm-scripts-in-package-json – north.inhale Jan 23 '20 at 14:34

0 Answers0