I have a main script (publish-all.js) from which I want to invoke the npm publish script of an Angular project, which also has a sub-script (publish.js that does general stuff (creating folders, copying files, moving folders...) after ng build
.
I need to pass some environment variables to that second script. I am using shelljs to run unix-like commands.
I tried using:
npm run publish -- VERSION=${productVersion} DESTDIR=${productDestinationPath}
From publish-all.js where productVersion and productDestinationPath are constants declared above that line, and which invokes the following script from the package.json:
"publish": "ng build --prod && node ./scripts/publish.js"
But the actual command line I get is
ng build --prod && node ./scripts/publish.js "VERSION=value" "DESTDIR=value"
Finally, in my publish.js script I tried getting those variables the following way:
let version = process.env.VERSION; let destinationPath = process.env.DESTDIR;
But I get undefined
values.
What am I doing wrong? Is the a better way of doing all this?
Should I maybe use process.argv instead??
I am using this strategy because it is what I were told to do, but I would like to know if there is a less confusing way.ç
EDIT 2021-07-13
I tried using export (with shelljs, since I am in Windows and using powershell) but I am getting an exception. I have the following code in publish-all.js now:
shelljs.exec(`export VERSION=${productVersion}`); shelljs.exec(`export DESTDIR=${productDestinationPath}`); shelljs.exec('npm run publish');
And in the publish.js script from the ANGULAR project:
version = process.env.VERSION; destinationPath = process.env.DESTDIR;
Though it does not get to publish.js. It gets stuck in the shelljs.exec('npm run publish')
, with the following exception:
I had to hide the project folder because of privacy policies, but it is a subfolder inside the folder where I am executing publish-all.js.
process.argv
.VERSION=${productVersion} DESTDIR=${productDestinationPath} npm run publish
? Thank you for the clarification!