2016-03-28 4 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*" 
    } 
} 

आप देख सकते हैं, lint, build: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 जैसे अन्य बैश फ़ाइल लिखने के लिए और इसे ./build.sh server जैसे एनपीएम स्क्रिप्ट में उपयोग करें?

उत्तर

10

आप ऐसा नहीं कर सकते:

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

यहाँ एक अच्छा ब्लॉग जो मुझे लगता है कि समय पर इस्तेमाल किया है:

यहाँ एक उदाहरण है उस।

निम्नलिखित कोड read-json.js जो पैकेज node_modules/npm/node_modules/read-package-json जो निष्पादित करने के लिए run-script.js में प्रयोग किया जाता है में है $ npm run-script ~~ या $ npm run ~~ जो अपने अन्य नाम है।

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

keyscriptpath_ में अपने कोड में "build:server" की तरह है।

this[key] अपने कोड में "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*" की तरह है।

इसलिए, यदि आप कोड जो string प्रकार, नहीं है, दूसरे शब्दों में, यदि आप package.json में string पाठ नहीं लिखते लिखते हैं, यह एक त्रुटि जब तक आप पैकेज npm/read-package-json के लिए योगदान किया जाएगा।

+0

लगता है कोड तार की एक सरणी का समर्थन करने के लिए बनाया जा सकता है ... मुझे आशा है कि किसी को है कि किसी दिन करता है। –

संबंधित मुद्दे

 संबंधित मुद्दे