一个简单npm插件的开发步骤如下
1 找个文件夹,命令行里输入:
mkdir i8n-autoinsert
cd i18n-autoinsert
npm init
npm init
会要求填写一堆的信息,这些信息会在package.json
里体现,例如
{
"name": "i18n-autoinsert", //插件名,在https://www.npmjs.com里没有的
"version": "1.0.6", //版本号,每次npm publish 要改下这个版本号
"description": "it is quit trouble when dev ux need supportmultiple language ,need to write tranlation key in en.json and html ,this plugin is to resolve this problem.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"commander": "^2.11.0"
},
"bin":{
"i18n-autoinsert":"./index.js"
},
"author": "rechard",
"license": "ISC"
}
2 index.js
在i18n-autoinsert项目里建立一个index.js
index.js
#!/usr/bin/env node
console.info('hello world');
3 运行
node index.js
看到控制台输出
hello world
目录i18n-autoinsert里就只有如下
- index.js
- package.json
4 发布
到https://www.npmjs.com注册一个账号,注册完后
添加用户
npm adduser
发布自己的插件到https://www.npmjs.com
npm publish
5 使用
npm install -save-dev i18n-autoinsert