将参数传递给包含多个命令的npm运行脚本

时间:2016-02-16 09:30:29

标签: javascript node.js npm

我想在我的npm脚本中将参数传递给两个命令中的第一个:

"scripts": {
  "myscript": "a && b"
}

当我运行npm run myscript -- --somearg=somevalue时,参数将传递给命令b,但不传递给命令a。有没有办法确保第一个命令收到参数?

4 个答案:

答案 0 :(得分:2)

UnboundLocalError: local variable 'flight' referenced before assignment

答案 1 :(得分:0)

经过大量讨论,我找到了以下解决方案:

https://github.com/npm/npm/issues/9627#issuecomment-152607809

https://github.com/npm/npm/issues/9627#issuecomment-178576802

解决方案1。 因此,在您的json包中添加一个这样的配置对象

{
  "name": "packageName",
  "config" : { "variable1" : "value1" }
}

然后您可以像在在Windows上

那样访问变量
    {
      "name": "packageName",
      "config" : { "variable1" : "value1" },
      "scripts": {
              "myscript": "a -c %npm_package_config_variable1%  && b -c %npm_package_config_variable1%"
      }
    }
我假设在Mac / Linux中

(不是100%)

{
  "name": "packageName",
  "config" : { "variable1" : "value1" },
  "scripts": {
          "myscript": "a -c $npm_package_config_variable1  && b -c $npm_package_config_variable1"
  }
}

然后您可以通过调用脚本来覆盖变量,如下所示:

npm run myscript --packageName:variable1=value2

解决方案2。

不需要package.json中的配置条目

{
  "name": "packageName",
  "scripts": {
          "myscript": "a -c %npm_config_variable1%  && b -c %npm_config_variable1%"
  }
}

并这样称呼

npm run myscript --variable1=value2

选择一个理由的理由:

https://github.com/npm/npm/issues/9627#issuecomment-178813965

答案 2 :(得分:0)

使用Yarn v2 "berry"时,这是一个简单的解决方案:

"scripts": {
  "myscript": "a $@ && b $@"
}

由于yarn berry托管了它自己的小跨平台,类似于bash的环境,用于在包中执行scripts,因此它也可以跨平台工作。

答案 3 :(得分:-1)

import requests
import json
import time
from datetime import datetime as dt
from urllib.parse import urlencode 

data = {
        "Step":"PT5M",
        "TimeFrame":"D1",
        "StartDate":int(dt(2019, 5, 1).timestamp()*1000),
        "EndDate":int(dt(2019, 5, 5).timestamp()*1000),
        "EntitlementToken":"57494d5ed7ad44af85bc59a51dd87c90",
        "IncludeMockTick":True,
        "FilterNullSlots":False,
        "FilterClosedPoints":True,
        "IncludeClosedSlots":False,
        "IncludeOfficialClose":True,
        "InjectOpen":False,
        "ShowPreMarket":False,
        "ShowAfterHours":False,
        "UseExtendedTimeFrame":True,
        "WantPriorClose":False,
        "IncludeCurrentQuotes":False,
        "ResetTodaysAfterHoursPercentChange":False,
        "Series":[{"Key":"STOCK/US/XNYS/ABB","Dialect":"Charting","Kind":"Ticker","SeriesId":"s1","DataTypes":["Last"],"Indicators":[{"Parameters":[{"Name":"Period","Value":"50"}],"Kind":"SimpleMovingAverage","SeriesId":"i2"},{"Parameters":[],"Kind":"Volume","SeriesId":"i3"}]}]
    }

data = {
    'json' : json.dumps(data)
}

data = urlencode(data)

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Dylan2010.EntitlementToken': '57494d5ed7ad44af85bc59a51dd87c90',
    'Origin': 'https://quotes.wsj.com',
    'Referer': 'https://quotes.wsj.com/ABB/advanced-chart',
    'Sec-Fetch-Mode': 'cors',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'
}

url = 'https://api.wsj.net/api/michelangelo/timeseries/history?' + data + '&ckey=57494d5ed7'
print(url)
r = requests.get(url, headers = headers)
r.text

请参见https://docs.npmjs.com/cli/run-script