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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:49:16  来源:igfitidea点击:

How can I write multiline scripts in npm scripts?

javascriptnpm

提问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 lintand build:servercommand are hard to read, so I want to break them into multiple lines.

如您所见,lintandbuild: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.shand 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.jswhich is in package node_modules/npm/node_modules/read-package-jsonwhich is used in run-script.jsto 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 keyin scriptpath_is like "build:server"in your code.

keyscriptpath_就像是"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 stringtype, in other words, if you don't write the stringtext in package.json, it will be an error unless you contribute to the package npm/read-package-json.

所以,如果你写的代码不是stringtype,换句话说,如果你不写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 npmcommand 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'

See: Run script on mac prompt "Permission denied"

请参阅:在 mac 提示“权限被拒绝”上运行脚本

回答by Bas Heerschop

You can use a tool like script-launcherto extend the features of you package.jsonfile.

您可以使用脚本启动器之类的工具来扩展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.

使用目录中的示例来更熟悉这些功能。