2016-03-28 12 views
19

Moja package.json wygląda następująco:Jak mogę pisać skrypty wielowierszowe w skryptach 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*" 
    } 
} 

Jak widać, komenda lint, build:server są trudne do odczytania, chcę złamać ich do multilinii.

ja spróbuje użyć \, ale będzie rzucać błąd jak

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

Jak mogę to zrobić?

Tylko do napisania kolejnego pliku bash, takiego jak build.sh i użycia go w skryptach npm, takich jak ./build.sh server?

Odpowiedz

10

nie można zrobić że.

Poniższy kod jest w read-json.js która jest w opakowaniu node_modules/npm/node_modules/read-package-json który jest używany w run-script.js wykonać $ npm run-script ~~ lub $ npm run ~~ który jest jego pseudonim.

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 w scriptpath_ jest jak "build:server" w kodzie.

Numer this[key] jest podobny do "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*" w kodzie.

Tak więc, jeśli napisać kod, który nie jest string typ, innymi słowy, jeśli nie napisać tekst w package.jsonstring, będzie to błąd, jeśli nie przyczyniają się do pakietu npm/read-package-json.

+0

Wygląda na to, że kod mógłby być obsługiwany dla szeregu łańcuchów ... Mam nadzieję, że ktoś to zrobi kiedyś. –

39

można zadania łańcuch niezaleznych:

Oto przykład:

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

tutaj jest miły blog którego użyłem w tym czasie: http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/

Powiązane problemy