如何在npm脚本中编写多行脚本?

时间:2016-03-28 08:12:43

标签: npm

我的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*"
  }
}

如您所见,lintbuild: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,并在./build.sh server等npm脚本中使用它?

4 个答案:

答案 0 :(得分:65)

您可以将独立任务链接起来。

以下是一个例子:

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

这是我当时使用的一个不错的博客: https://www.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/

答案 1 :(得分:15)

你做不到。

以下代码位于read-json.js中,node_modules/npm/node_modules/read-package-json位于run-script.js中,用于执行$ npm run-script ~~$ npm run ~~,它是别名。< / p>

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

key中的scriptpath_与代码中的"build:server"类似。

this[key]与代码中的"./node_modules/babel-cli/bin/babel.js . -d dist/server --ignore node_modules,dist,client,public,webpack*"类似。

因此,如果您编写的代码不是string类型,换句话说,如果您不在string中编写package.json文本,那么除非您参与了包npm/read-package-json

答案 2 :(得分:8)

另一种常见的替代方法是编写一个引用本地bash脚本的npm命令(您可以在其中执行更多操作)。

# 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*

注意:确保您授予自己运行文件的权限;否则会遇到权限问题

sudo chmod 755 'build-server.sh'

请参阅:Run script on mac prompt "Permission denied"

答案 3 :(得分:1)

您可以使用script-launcher之类的工具来扩展package.json文件的功能。

使用 script-launcher ,您可以将数组用作脚本,并引用具有不同参数的另一个脚本。

数组示例 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"
    ]
  }
}

使用Table of Contents中的示例来更熟悉这些功能。

相关问题