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

use*_*095 43 javascript 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*"
  }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的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!                                                                   ^
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

只写了另一个bash文件build.sh,并在npm脚本中使用它./build.sh server

小智 76

您可以链接独立的任务.

这是一个例子:

"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",
    ...
Run Code Online (Sandbox Code Playgroud)

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

  • 请注意,此代码使用`&`而不是`&&`。前者意味着在后台运行脚本,这意味着它们并行执行,并且其输出被混在一起。后者意味着仅当* current *命令退出且没有错误代码时才执行* next *命令。 (4认同)

haͣ*_*rͬu 20

你不能这样做.

以下代码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, '')
  }
}
Run Code Online (Sandbox Code Playgroud)

keyscriptpath_就像是"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.

  • 这种做法只是回答了问题,却没有解决问题。就我个人而言,考虑到 npm“脚本”的有限性,我倾向于运行外部脚本(“build:server”:“node build_server.js”)。 (4认同)

Fro*_*Dog 16

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).

i.e.

# 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"
  }
}
Run Code Online (Sandbox Code Playgroud)
# build-server.sh
#!/bin/bash

./node_modules/babel-cli/bin/babel.js . \
  -d dist/server \
  --ignore \
    node_modules,\
    dist,\
    client,\
    public,\
    webpack*
Run Code Online (Sandbox Code Playgroud)

NOTE: make sure you give yourself permission to run the file; otherwise you'll run into permission issues

sudo chmod 755 'build-server.sh'
Run Code Online (Sandbox Code Playgroud)

See: Run script on mac prompt "Permission denied"

  • 不幸的是不能跨平台工作。 (7认同)