2017-08-29 64 views
1

讀的package.json版本我有一個package.json文件看起來像這樣:內的package.json

{ 
    "name": "myapp", 
    "version": "0.0.1", 
    "license": "MIT", 
    "scripts": { 
    "ng": "ng", 
    "start": "webpack-dev-server --port=4200", 
    "build": "webpack", 
    "docker-build": "docker build -t myapp .", 
    "docker-run": "docker run -d --name myapp -p 9090:9090 myapp", 
    "docker-push": "docker build -t myapp . & docker tag myapp myrepo/myapp:{$VERSION} & docker push myrepo/myapp:{$VERSION}" 
    }, 
... 
} 

我試着去獲得的版本號,當我運行npm run docker-push所以它會自動標記與版本的發佈編號爲package.json。我怎樣才能做到這一點?

回答

3

您可以使用環境變量npm_package_version

提示:要查看NPM設定所有可用的環境變量,您可以添加 「printenv | grep的故宮」 作爲腳本:

{ 
    "name": "myapp", 
    "version": "1.0.0", 
    "scripts": { 
    "start": "node app.js", 
    "version": "echo ${npm_package_version}", // return current version 
    "vars": "printenv | grep npm" // return all ENV vars with 'npm' 
    } 
    // ... 
}