简体   繁体   English

如何为 `npm init` 设置默认测试命令?

[英]How do I set the default test command for `npm init`?

I know I can do:我知道我可以做到:

npm config set init.author.email me@mycompany.com
npm config set init.license UNLICENSED

To set the defaults used to create a new package.json with npm init .使用npm init设置用于创建新package.json的默认值。 But how can I set the default value for the test command ?但是如何设置test command的默认值呢? I've tried我试过了

npm config set init.scripts.test "mocha"

But it doesn't work.但它不起作用。 The npm docs don't seem to help. npm 文档似乎没有帮助。

Is there a list of all the init defaults?是否有所有初始化默认值的列表?

There is a list of all config defaults npm config list -l .有一个所有配置默认值的列表npm config list -l

As you can see there isn't anything like init.scripts.如您所见,没有像 init.scripts 这样的东西。 But there is another way to do it.但是还有另一种方法可以做到这一点。 If you first npm install mocha and then npm init you will get a package.json like:如果你先npm install mocha然后npm init你会得到一个 package.json 像:

{
  "name": "asd",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {
    "mocha": "^2.4.5"
  },
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "UNLICENSED"
}

Although you can't (currently) configure default scripts via npm settings, you can customise the entire npm init output by providing your own init-module .尽管您(当前)无法通过 npm 设置配置默认脚本,但您可以通过提供自己的init-module来自定义整个npm init输出。 The default is stored at ~/.npm-init.js - whatever you export becomes the result of running npm init .默认值存储在~/.npm-init.js - 无论您导出什么都将成为运行npm init的结果。

To make this useful, you'd probably want to hijack the existing default npm init module, which lives in <your npm install directory>/node_modules/init.js .为了使它有用,您可能想要劫持现有的默认 npm init 模块,该模块位于<your npm install directory>/node_modules/init.js中。 If you only care about providing a default test script, the simplest option is to use init-module , which provides a configuration parameter init-scripts-test .如果你只关心提供一个默认的测试脚本,最简单的选择是使用init-module ,它提供了一个配置参数init-scripts-test

I was searching for a way to programmatically create a package.json with dynamically defined values.我正在寻找一种以编程方式创建具有动态定义值的package.json的方法。 I noticed creating a package.json file before calling npm init -y works as expected.我注意到在调用npm init -y之前创建了一个package.json文件按预期工作。

// Require: fs
const fs = require('fs')

// Variables
const values = {
    "scripts": {
        "test": "mocha"
    }
    // as well as any other values you want
}

// Create package.json
fs.writeFileSync('package.json', values)

Running npm init -y after running the above code would generate a package.json with scripts.test = "mocha" defined.运行上述代码后运行npm init -y将生成一个package.json ,其中定义了scripts.test = "mocha"

If you're looking for a way to run npm init -y from within code as well, I recommend using shelljs : shell.exec('npm init -y') .如果您也在寻找一种在代码中运行npm init -y的方法,我建议您使用shelljsshell.exec('npm init -y')

npm config set init ...将为所有未来将被初始化的项目创建一个默认配置——如果你只想设置一些值——只需使用 shellscript 在npm init之前写入一些值,如下所示:

echo '{"version":"0.1.0","license":"UNLICENSED","private":true,"scripts":{"test":"npx jest"}}' > "./package.json" && npm init -y

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

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