2016-03-28 8 views
19

私のpackage.jsonは以下のようになります。npmスクリプトで複数行のスクリプトを書くにはどうすればいいですか?

{ 
    "name": "project", 
    "version": "1.0.0", 
    "description": "", 
    "main": "server.js", 
    "scripts": { 
    "lint": "./node_modules/eslint/bin/eslint.js --format \"./node_modules/eslint-friendly-formatter/index.js\" .", 
    "build:server": "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*" 
    } 
} 

あなたが見ることができるように、lintbuild:serverコマンドは、私は複数行にそれらを破るしたい、読みにくいです。

私は\を使用しようとしましたが、それは私がこれを行うことができますどのように

npm ERR! Failed to parse json 
npm ERR! Unexpected token ' ' at 11:80 
npm ERR! :server": "./node_modules/babel-cli/bin/babel.js . -d dist/server \ 
npm ERR!                 ^

のようなエラーがスローされますか?

build.shのような別のbashファイルを作成し、./build.sh serverのようなnpmスクリプトで使用するだけですか?

答えて

10

それ。

次のコードは、実行するrun-script.jsで使用されるパッケージnode_modules/npm/node_modules/read-package-jsonであるread-json.jsである$ npm run-script ~~またはそのエイリアスである$ npm run ~~scriptpath_

function scriptpath (file, data, cb) { 
    if (!data.scripts) return cb(null, data) 
    var k = Object.keys(data.scripts) 
    k.forEach(scriptpath_, data.scripts) 
    cb(null, data) 
} 

function scriptpath_ (key) { 
    var s = this[key] 
    // This is never allowed, and only causes problems 
    if (typeof s !== 'string') return delete this[key] 

    var spre = /^(\.[\/\\])?node_modules[\/\\].bin[\\\/]/ 
    if (s.match(spre)) { 
    this[key] = this[key].replace(spre, '') 
    } 
} 

keyあなたのコードで"build:server"のようなものです。

this[key]は、コードに"./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*"と同じです。あなたがパッケージnpm/read-package-jsonに貢献していない限り

だから、あなたはあなたがpackage.jsonstringテキストを書いていない場合は、他の言葉で、stringタイプではないコードを書く場合、それはエラーになります。

+0

のようなコードは、文字列の配列をサポートするように作られているようです...私は誰かがいつかそれをすることを願っています。 –

39

あなたがチェーンの独立したタスクをすることができます:ここに

は一例です。ここ

"scripts": { 
    "lint-jshint": "jshint --verbose --show-non-errors ./src/main/js", 
    "lint-eslint": "eslint ./src/main/js ./src/test/js", 
    "lint-csslint": "csslint ./src/main/js", 

    "lint": "npm run -s lint-jshint & npm run -s lint-eslint & npm run -s lint-csslint", 

    "pretest": "rimraf ./build/reports/tests && mkdirp ./build/reports/tests && npm run -s lint", 
    "test": "karma start ./src/test/resources/conf/karma.conf.js", 
    ... 

は私がその時に使用すてきなブログです:あなたが行うことはできません http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/

関連する問題