Javascript 如何在npm脚本中编写多行脚本?
My package.json如下所示:Javascript 如何在npm脚本中编写多行脚本?,javascript,npm,Javascript,Npm,My package.json如下所示: { "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\" .", "buil
{
"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! ^
我该怎么做
只需编写另一个bash文件,如
build.sh
,并在npm脚本中使用它,如/build.sh server
?就可以链接独立的任务
以下是一个例子:
"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\u modules/npm/node\u 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, '')
}
}
脚本路径
中的键类似于代码中的“build:server”
此[键]
类似于代码中的“/node\u modules/babel cli/bin/babel.js.-d dist/server——忽略node\u modules、dist、client、public、webpack*”
因此,如果您编写的代码不是
string
类型,换句话说,如果您没有在package.json
中编写string
文本,除非您对包npm/read package json
做出贡献,否则这将是一个错误。另一个常见的替代方法是编写一个引用本地bash脚本的npm
命令(在该脚本中,您有更大的权限执行您想要的操作)
i、 e
注意:确保您有权运行该文件;否则您将遇到权限问题
sudo chmod 755 'build-server.sh'
请参阅:代码似乎可以用来支持字符串数组……我希望有一天会有人这样做。这种类型回答了问题,但没有解决问题。就我个人而言,我倾向于运行外部脚本(
“build:server”:“node build\u server.js”
),因为npm“脚本”非常有限。@snarf您有一个简单的例子来说明build\u server.js是什么样子的吗?它在道德上等同于您的npm脚本。您可以使用exec
或spawn
调用通常在命令行上调用的相同外部程序,将逻辑分散到尽可能多的行上,以保持可读性和可维护性。请注意,此代码使用&
而不是&
。前者意味着在后台运行脚本,这意味着它们并行执行,并且它们的输出被混合在一起。后者意味着只有在当前命令退出且没有错误代码的情况下才执行下一个命令。感谢您的评论,链接文章中确实有更多的配置和信息,值得所有的赞扬,因此我链接了它。因为我在Windows上开发的时间很长,我假设你遇到的任何问题都与其他问题有关。在上面的示例中,命令npm run lint
工作正常(刚刚再次测试)。不幸的是,它不能跨平台工作。
# build-server.sh
#!/bin/bash
./node_modules/babel-cli/bin/babel.js . \
-d dist/server \
--ignore \
node_modules,\
dist,\
client,\
public,\
webpack*
sudo chmod 755 'build-server.sh'