2016-03-28 17 views
19

mio package.json è simile al seguente:Come posso scrivere script multilinea in script 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*" 
    } 
} 

Come si può vedere, il comando lint, build:server sono difficili da leggere, voglio spezzare loro di righe.

ho cerco di usare \, ma getterò errore come

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!                 ^

Come posso fare questo?

Solo per scrivere un altro file bash come build.sh e utilizzarlo in script npm come ./build.sh server?

risposta

10

Non si può fare quella.

Il seguente codice è in read-json.js che è in pacchetto node_modules/npm/node_modules/read-package-json che viene utilizzato in run-script.js per eseguire $ npm run-script ~~ o $ npm run ~~ che è il suo alias.

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, '') 
    } 
} 

Il key in scriptpath_ è come "build:server" nel codice.

Il this[key] è come "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*" nel codice.

Quindi, se si scrive il codice che non è string tipo, in altre parole, se non si scrive il testo string in package.json, sarà un errore a meno che non contribuisce al pacchetto npm/read-package-json.

+0

sembra che il codice possa essere realizzato per supportare una serie di stringhe ... Spero che qualcuno lo faccia un giorno. –

39

è possibile compiti catena indipendenti:

Ecco un esempio:

"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", 
    ... 

ecco un bel blog che ho usato in quel momento: http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/

Problemi correlati