简体   繁体   English

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

[英]How can I write multiline scripts in npm scripts?

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.如您所见, lintbuild: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 ?只是为了编写另一个像build.sh这样的 bash 文件并在像./build.sh server这样的 npm 脚本中使用它?

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/

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.以下代码位于node_modules/npm/node_modules/read-package-json包中的read-json.js ,该包在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. scriptpath_key就像代码中的"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类型的,换句话说,如果你没有在package.jsonstring text ,除非你对包npm/read-package-json做出贡献,否则它将是一个错误。

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).另一种常见的替代方法是编写一个引用本地 bash 脚本的npm命令(在那里你有更多的能力做你想做的事)。

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 提示“权限被拒绝”上运行脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM