markdown NPM脚本命令在课程的各个阶段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown NPM脚本命令在课程的各个阶段相关的知识,希望对你有一定的参考价值。

# npm-run-setups

The primary question is whether `npm start` and `npm test` should be setup for developer convenience and other commands like `npm run heroku` and `npm run travis` should be setup for services. Or should `npm start` and `npm test` should be kept for production and test environments respectively and customer scripts like `npm run dev:start` and `npm run dev:test` should be created for development. 

The secondary question is... what should they be named?

Below are examples of the scripts at the various phases of the course

## Basic - Node/Express

This version would be used at the beginning of the course, before testing has been introduced

```json
"scripts": {
  "start": "node server.js"
},
```

Introduce this setup during the during express but before mocha

```json
"scripts": {
  "start": "node server.js",
  "dev:start": "nodemon server.js"
}
```  

## Intermediate: Mocha Testing

Introduce this setup for mocha testing

- Keep `npm start` and `npm test` reserved for Heroku and Travis. Create `npm run dev:start` and `npm run dev:test` scripts for development.

```json
"scripts": {
  "start": "node server.js",
  "test": "cross-env NODE_ENV=test mocha",
  "dev:start": "nodemon server.js",
  "dev:test": "nodemon --exec npm test"
},
```

The proposed solutions use `process.env.NODE_ENV` to determine the correct database connection and suppress logging. It is considered bad practice to set `process.env.NODE_ENV` in code or use globals like so we'll set it in the command using `cross-env` so it works cross platform. This also assumes the use of a `.env` file and the `dotenv` package for database credentials and jwt secrets.

> Side Note: Never use `mocha --watch`. It may run faster but it causes a the app to create multiple processes and database connections. IMHO, the overhead of adding graceful shutdown scripts for `--watch` (which don't work for SIGINT or SIGTERM) is not worth the effort. 

## Advanced: Test Coverage

Propose: Introduce test coverage using `nyc` formerly known as `istanbul` as an advanced/optional lesson.

```json
"scripts": {
    "start": "node server.js",
    "test": "cross-env NODE_ENV=test mocha",
    "dev:start": "nodemon server.js",
    "dev:test": "nodemon --exec npm test",
    "dev:cover": "nodemon --exec nyc --reporter=lcov --reporter=text-summary npm test"
  },
```


以上是关于markdown NPM脚本命令在课程的各个阶段的主要内容,如果未能解决你的问题,请参考以下文章

markdown 更改npm脚本的工作目录

第二阶段 Linux与Bash脚本课程

markdown NPM脚本

markdown 使用npm运行bash脚本

markdown 有用的npm命令和技巧

markdown npm命令