# npm 发布自己的包
- 注册 npm 账号:https://www.npmjs.com/
npm init
修改 package.json , 例如这样:
| { |
| "name": "test-exec-generator", |
| "version": "1.0.0", |
| "description": "a test generator", |
| "main": "index.js", |
| "scripts": { |
| "test": "echo \"Error: no test specified\" && exit 1" |
| }, |
| "author": "nekoaimer", |
| "license": "MIT", |
| "repository": { |
| "type": "git", |
| "url": "https://github.com/nekoaimer" |
| }, |
| "homepage": "https://github.com/nekoaimer", |
| "keywords": [ |
| "nekoaimer", |
| "generator" |
| ] |
| } |
| |
| export default function execGenerator(genFn) { |
| const generator = genFn() |
| function exec(res) { |
| const result = generator.next(res) |
| if (result.done) return result.value |
| result.value.then(res => exec(res)) |
| } |
| exec() |
| } |
# 更新仓库
- 加入您更新了代码重新发布时不可直接
npm publish
, 否则会报类似下面的错
- 需要进行操作
- 修改版本号 (最好符合 semver 规范)
- 重新发布
| { |
| "name": "exec-generator", |
| "version": "1.1.1", |
| "description": "a test execGenerator", |
| "main": "index.js", |
| "scripts": { |
| "test": "echo \"Error: no test specified\" && exit 1" |
| }, |
| "author": "nekoaimer", |
| "license": "MIT", |
| "repository": { |
| "type": "git", |
| "url": "https://github.com/nekoaimer" |
| }, |
| "homepage": "https://github.com/nekoaimer", |
| "keywords": [ |
| "nekoaimer", |
| "generator", |
| "utils" |
| ], |
| "dependencies": { |
| "exec-generator": "^1.0.0" |
| } |
| } |
- 修改了版本号之后执行
npm publish
, 出现类似下面这种说明更新发布成功啦~
# 使用自己的包
| |
| { |
| "name": "test-exec-generator", |
| "version": "1.0.0", |
| "description": "", |
| "main": "main.js", |
| "dependencies": { |
| "test-exec-generator": "^1.1.0" |
| }, |
| "scripts": { |
| "test": "echo \"Error: no test specified\" && exit 1" |
| }, |
| "author": "", |
| "license": "ISC" |
| } |
| import execGenerator from './node_modules/test-exec-generator/index.js' |
| |
| function requsetDate(url) { |
| return new Promise(resolve => setTimeout(() => resolve(url), 2000)) |
| } |
| |
| |
| function* getData() { |
| const res1 = yield requsetDate('https://neko') |
| const res2 = yield requsetDate(res1 + 'aimer') |
| const res3 = yield requsetDate(res2 + '.com') |
| console.log(res3) |
| } |
| |
| execGenerator(getData) |
# 删除发布的包
| npm unpublish --force xxx |
# 让发布的包过期