简体   繁体   English

将命令行参数从 NPM 脚本传递给 NodeJS 脚本

[英]Pass command line arguments to NodeJS script from NPM script

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 .我有一个主脚本( publish-all.js ),我想从中调用一个 Angular 项目的npm 发布脚本,它还有一个子脚本( publish.js做一般事情(创建文件夹、复制文件、移动文件夹...) 在ng build

I need to pass some environment variables to that second script.我需要将一些环境变量传递给第二个脚本。 I am using shelljs to run unix-like commands.我正在使用shelljs来运行类 Unix 的命令。

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-all.js 中,其中productVersionproductDestinationPath是在该行上方声明的常量,并从 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:最后,在我的publish.js脚本中,我尝试通过以下方式获取这些变量:

let version = process.env.VERSION;
let destinationPath = process.env.DESTDIR;

But I get undefined values.但我得到undefined值。

What am I doing wrong?我究竟做错了什么? Is the a better way of doing all this?做这一切的更好方法是什么?
Should I maybe use process.argv instead??我应该改用process.argv吗??

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编辑 2021-07-13

I tried using export (with shelljs, since I am in Windows and using powershell) but I am getting an exception.我尝试使用导出(使用 shelljs,因为我在 Windows 中并使用 powershell)但我得到了一个例外。 I have the following code in publish-all.js now:我现在在publish-all.js 中有以下代码:

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:在 ANGULAR 项目的publish.js脚本中:

version = process.env.VERSION;
destinationPath = process.env.DESTDIR;

Though it does not get to publish.js .虽然它没有得到publish.js It gets stuck in the shelljs.exec('npm run publish') , with the following exception:它卡在shelljs.exec('npm run publish') ,但有以下例外:

异常截图,上面写着:错误:ENOENT:没有这样的文件或目录,stat 'project folder/node.exe'

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 .由于隐私政策,我不得不隐藏项目文件夹,但它是我执行publish-all.js的文件夹内的子文件夹。

Environmental variables go BEFORE the command.环境变量在命令之前。 So, instead of passing them after you can add them BEFORE:因此,您可以在之前添加它们,而不是传递它们:

VERSION=${productVersion} DESTDIR=${productDestinationPath} npm run publish

Or, You can export the variables first then run the script:或者,您可以先导出变量,然后运行脚本:

export VERSION=${productVersion}
export DESTDIR=${productDestinationPath}
npm run publish

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM