blog
blog copied to clipboard
如何快速创建 Node.js 项目模版
开始创建 Node.js 项目模版
通常我们创建一个新的 Node.js 项目,第一件要做的事情是使用npm
进行一些初始化操作。
npm init
npm
会交互式的问一些问题,创建package.json
文件。
然后,从 .gitignore 模版集合拷贝一份.gitignore
的模版,可能还要为项目创建LICENSE
文件。
不够自动化,很没有效率
直到看到这个 twitter
https://twitter.com/bitandbang/status/1082331715471925250
这四行命令做了刚才手动的所有操作。
-
npx license mit > LICENSE
使用license package,下载并生成相应的 license 文件,这里是MIT license -
npx gitignore node
使用gitignore package,下载并生成相应的.gitignore
文件模版 -
npx covgen
使用covgen package,生成Contributor Covenant,为您的项目提供一个行为准则,欢迎所有贡献者
npx
是什么?它会查找本地 node_modules 中是否有可以执行的二进制命令行工具, 如果本地没有,它会先使用npm
安装,然后运行。npx
可以让我们避免在全局和本地安装一堆只在项目初始化时使用一次的npm
包。
-
npm init -y
使用默认配置生成package.json
文件
Tierney 建议定制你的npm init
默认配置,保证生成的package.json
文件是满足你需求的。
定制npm init
可是使用npm config list
命令来查看你当前的npm
配置。如果只想看和npm init
相关的配置,可以使用grep
命令过滤。
☁ nodejs-project npm config list -l | grep init
init-author-email = ""
init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "/Users/ldu020/.npm-init.js"
init-version = "1.0.0"
有很多默认配置你可以修改,比如:author name, author email, author url, license, version 等。要修改这些配置,可以使用npm config edit
命令在编辑器中打开npm
配置文件(.npmrc
)。
编者注:
阅读文档 https://docs.npmjs.com/misc/config#editor, 可知,npm
配置文件的默认编辑器是vi
☁ nodejs-project npm get editor
vi
vi
可能用不习惯,想用vscode
作为npm
配置文件的默认编辑器,如何设置?
如果是通过brew cask
安装的vscode
,先查看vscode
的二进制可执行文件
☁ nodejs-project brew cask info visual-studio-code
visual-studio-code: 1.30.2,61122f88f0bf01e2ac16bdb9e1bc4571755f5bd8 (auto_updates)
https://code.visualstudio.com/
/usr/local/Caskroom/visual-studio-code/1.22.2,3aeede733d9a3098f7b4bdc1f66b63b0f48c1ef9 (64B)
From: https://github.com/Homebrew/homebrew-cask/blob/master/Casks/visual-studio-code.rb
==> Names
Microsoft Visual Studio Code
VS Code
==> Artifacts
Visual Studio Code.app (App)
/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code (Binary)
由/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code (Binary)
可知,vscode
的二进制可执行文件名是code
。接着,使用下面的命令设置npm
的 editor:
☁ nodejs-project npm set editor code
☁ nodejs-project npm get editor
code
然后再运行npm config edit
,即可通过vscode
打开npm
配置文件.npmrc
。
或者像下面这样设置:
npm set init-author-name "Your name"
npm set init-author-email "[email protected]"
npm set init-author-url "https://your-url.com"
npm set init-license "MIT"
npm set init-version "1.0.0"
当定制好npm init
配置,npm init -y
将使用这些默认配置生成package.json
文件。
构建你的初始化脚本
尽管一些手动的操作我们使用命令代替了,但依次执行这些命令仍然是手动的,达到了“半自动化”,更进一步,通过shell
脚本将这些命令封装起来
function node-project {
git init
npx license $(npm get init-license) -o "$(npm get init-author-name)" > LICENSE
npx gitignore node
npx covgen "$(npm get init-author-email)"
npm init -y
git add -A
git commit -m "Initial commit"
}
这里使用从npm
配置文件中读取到的 license,author name, author email 作为参数传入相应的命令中来生成各自的文件。还加入了git
初始化仓库命令,最后使用git
提交项目初始化文件作为首次提交。
可以将该函数加入~/.bash_profile
或~/.zshrc
。然后在当前终端窗口执行source ~/.bash_profile
或source ~/.zshrc
,如果不想使用source
命令,可以新开一个终端窗口,然后执行node-project
函数。除了这些项目初始化命令,根据自身需求,可以加入或删除任何其他的命令。比如:npx tsc --init
生成tsconfig.json
文件,npx tslint --init
生成tslint.json
文件, .prettierrc
等等
测试结果:
☁ nodejs-project [master] ⚡ node-project
Reinitialized existing Git repository in /Users/ldu020/workspace/nodejs-project/.git/
npx: 5 安装成功,用时 2.225 秒
npx: 1 安装成功,用时 1.454 秒
Created .gitignore file for type Node :)
npx: 62 安装成功,用时 4.879 秒
Downloading Contributors Covenant...
Replacing e-mail address...
Done! Created file CODE_OF_CONDUCT.md.
Wrote to /Users/ldu020/workspace/nodejs-project/package.json:
{
"name": "nodejs-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "mrdulin <[email protected]>",
"license": "MIT"
}
[master 6bf97c6] Initial commit
1 file changed, 80 insertions(+)
编者注:
如果觉得各种项目脚手架太重,包含了太多用不到的东西,想自己慢慢定制,或者想快速生成一些demo项目,使用这种方式是一种比较好的方式。
原文
https://philna.sh/blog/2019/01/10/how-to-start-a-node-js-project/