Javascript 如何在 npm 脚本中编写多行脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36258456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I write multiline scripts in npm scripts?
提问by user2331095
My package.json looks like the following:
我的 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\" .",
"build:server": "./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*"
}
}
As you can see, the lint
and build:server
command are hard to read, so I want to break them into multiple lines.
如您所见,lint
andbuild:server
命令难以阅读,因此我想将它们分成多行。
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! ^
How can I do this?
我怎样才能做到这一点?
Only to write another bash file like build.sh
and use it in npm scripts like ./build.sh server
?
只是为了编写另一个 bash 文件build.sh
并在 npm 脚本中使用它,例如./build.sh server
?
采纳答案by ha?r?uka?re?r?u
You can't do that.
你不能那样做。
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.
下面的代码在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, '')
}
}
The key
in scriptpath_
is like "build:server"
in your code.
该key
中scriptpath_
就像是"build:server"
在你的代码。
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.
该this[key]
就像"./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*"
在你的代码。
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
.
所以,如果你写的代码不是string
type,换句话说,如果你不写string
文本package.json
,除非你对包做出贡献,否则就会出错npm/read-package-json
。
回答by asa
You can chain independent tasks.
您可以链接独立的任务。
Here is an example:
下面是一个例子:
"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",
...
Here is a nice blog which I used at that time: https://www.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/
这是我当时使用的一个不错的博客:https: //www.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/
回答by FrostyDog
Another common alternative is to write an npm
command that references a local bash script (where you have more power to do what you want).
另一种常见的替代方法是编写一个npm
引用本地 bash 脚本的命令(在那里你有更多的能力做你想做的事)。
i.e.
IE
# 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\" .",
"build:server": "./build-server.sh"
}
}
# build-server.sh
#!/bin/bash
./node_modules/babel-cli/bin/babel.js . \
-d dist/server \
--ignore \
node_modules,\
dist,\
client,\
public,\
webpack*
NOTE: make sure you give yourself permission to run the file; otherwise you'll run into permission issues
注意:确保您授予自己运行该文件的权限;否则你会遇到权限问题
sudo chmod 755 'build-server.sh'
回答by Bas Heerschop
You can use a tool like script-launcherto extend the features of you package.json
file.
您可以使用脚本启动器之类的工具来扩展package.json
文件的功能。
With script-launcheryou can use arrays as scripts and reference another script with different arguments.
使用脚本启动器,您可以使用数组作为脚本并使用不同的参数引用另一个脚本。
Array Exampleand Lint script reference example
数组示例和Lint 脚本参考示例
{
"scripts": {
"lint": [
"jshint --verbose --show-non-errors ./src/main/js",
"eslint ./src/main/js ./src/test/js",
"csslint ./src/main/js"
],
"pretest": [
"rimraf ./build/reports/tests",
"mkdirp ./build/reports/tests",
"lint"
]
}
}
Use the examples from the Table of Contentsto get more familiar with these features.
使用目录中的示例来更熟悉这些功能。