从npm脚本获取版本级别

时间:2019-06-10 19:32:10

标签: node.js npm

我想编写npm scripts,当通过npm version修改软件包版本时运行。我不打算直接通过ReportingBox_Loads来调用我的脚本;相反,当调用CurrentDb.Execute "INSERT INTO MATLog(Job #, Job Name, Date, Superintendent, Hauling Vendor, Material, LTC, Loads) " & _ " VALUES(" & Me.ReportingCOM_Job & "," & Me.ReportingCom_JobName & ",#" & Me.ReportingBox_Date & "#," & _ Me.ReportingCom_SI & "," & Me.ReportingCom_Mat & "," & Me.ReportingCom_Vendor & "," & Me.ReportingCom_LoadType & ",#" & Me.ReportingBox_Loads & "#)" (或npm run _____npm version ____等)时,应该由npm调用它们。

如何在脚本中引用版本级别参数?

例如如果我的脚本以preversion的身份运行并从postversion调用,那么我的脚本如何引用preversion

1 个答案:

答案 0 :(得分:0)

在我的脚本中,我可以参考process.env.npm_config_argv。它的值是一个JSON字符串,其中包含npm的原始参数。

如果我的package.json包含:

"scripts": {
  "preversion": "node log_argv"
}

log_argv.js包含:

console.log('Type:', typeof process.env.npm_config_argv);
console.log('Value:', process.env.npm_config_argv);
console.log('Original npm args:', JSON.parse(process.env.npm_config_argv).original);

throw new Error("aborting");

然后我运行npm version patch,然后看到输出:

Type: string
Value: {"remain":["patch"],"cooked":["version","patch"],"original":["version","patch"]}
Original npm args: [ 'version', 'patch' ]