如何在 npm 脚本中编写多行脚本? [英] How can I write multiline scripts in npm scripts?
问题描述
我的 package.json 如下所示:
My package.json looks like the following:
{
"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
命令难以阅读,因此我想将它们分成多行.
As you can see, the lint
and build:server
command are hard to read, so I want to break them into multiple lines.
我尝试使用 \
,但它会抛出如下错误:
I've tried to use \
, but it throws errors like:
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 脚本中使用它?
Only to write another bash file like build.sh
and use it in npm scripts like ./build.sh server
?
推荐答案
你不能那样做.
以下代码在 read-json.js
中,它位于 node_modules/npm/node_modules/read-package-json
包中,用于 run-script.js
执行 $ npm run-script ~~
或 $ npm run ~~
,这是它的别名.
The following code is in read-json.js
which is in package node_modules/npm/node_modules/read-package-json
which is used in run-script.js
to execute $ npm run-script ~~
or $ npm run ~~
which is its 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, '')
}
}
scriptpath_
中的 key
就像代码中的 "build:server"
.
The key
in scriptpath_
is like "build:server"
in your code.
this[key]
就像 "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*"
在您的代码中.
The this[key]
is like "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*"
in your code.
所以,如果你写的代码不是string
类型,换句话说,如果你没有在package.json中写
,除非你贡献包string
文本npm/read-package-json
,否则会报错.
So, if you write the code which is not string
type, in other words, if you don't write the string
text in package.json
, it will be an error unless you contribute to the package npm/read-package-json
.
这篇关于如何在 npm 脚本中编写多行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!