-1

Is it possible to include a for loop inside a package.json file to recurse into subdirectories and execute a command in each:

# .env File
DIRECTORIES="Dir1, Dir2"
# package.json file in parent directory
"scripts": {
  "install-all-inefficient": "(cd ./dir/Dir1/ && npm install) && (cd ./dir/Dir2/ && npm install)",
  "install-all-ideal": ". .env && functionToLoopThroughDirectoriesDefinedInEnvironmentVariables",
},

I was looking at this: Defining an array as an environment variable in node.js as an example for defining the array, and then trying to somehow use process.env.DIRECTORIES to create a loop without writing too much ugly bash script.

timhc22
  • 7,213
  • 8
  • 48
  • 66

1 Answers1

1

You can try :

"install-all-ideal": ". .env; for dir in \"${DIRECTORIES[@]}\"; do (cd \"$dir\" && npm install); done"

with .env :

DIRECTORIES=("Dir1" "Dir2")
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • This is almost there, (I didn't know about the `@` which is helpful). Unfortunately, `DIRECTORIES` is still a string of comma separated variables, so I need to find a way of splitting them.. was thinking maybe: `array=(\`echo $string | sed 's/,/\n/g'\`)`, but I'm getting bad substitution errors – timhc22 Dec 15 '22 at 16:03