1.0.0 • Published 3 years ago
create-cli-dolphin v1.0.0
create-cli
package初始化:
"type": "module",
"test": "rm -rf ./hei && node index.js"
重构:
将根路径抽离
单一职责:
生成独立的indextemplate模板
ejs:
Embedded JavaScript templates
https://www.npmjs.com/package/ejs
程序:
数据结构+算法
在终端与用户交互
https://www.npmjs.com/package/inquirer
全局使用:
npm link
package.json中bin字段指定执行代码
// 重新设置路径
import fs from 'fs';
import ejs from 'ejs';
import path from 'path';
import { fileURLToPath } from 'url';
export default (inputConfig)=>{
// nodejs esm __dirname
const __dirname = fileURLToPath(import.meta.url);
const templateCode = fs.readFileSync(
path.resolve(__dirname,"../template/index.ejs")
);
const code = ejs.render(templateCode.toString(),{
middleware: inputConfig.middleware,
port: inputConfig.port,
});
return code;
}
Shebang:#!/usr/bin/env node
execa
https://www.npmjs.com/package/execa
execa("yarn", {
cwd: getRootPath(),
stdio: ['inherit', 'inherit', 'inherit'],
});
https://nodejs.org/dist/latest-v14.x/docs/api/child_process.html#child_process_subprocess_stdio
prettier格式化
https://www.prettier.cn/docs/api.html
import prettier from "prettier";
return prettier.format(code, { parser: "babel" });
return prettier.format(code, { parser: "json" });
chalk 提示
https://www.npmjs.com/package/chalk
发布:
设置发布内容
"files": [
"bin",
"package.json"
],
同时要确保包名是唯一的,可以增加前后缀,如在package.json中配置name为create-cli-dol
注意:files没有指定readme.md,也会自动把README.MD文件上传
登录npmjs
# 登录
npm login --registry https://registry.npmjs.org
# 发布
npm publish --registry https://registry.npmjs.org
# 删除包
npm unpublish --force --registry https://registry.npmjs.org
# 删除后24小时内不能上传同名包...
usage
npm i create-cli-dol -g
1.0.0
3 years ago