【趣味项目】一键生成LICENSE
项目地址:GitHub(最新版本) | GitCode(旧版本)
项目介绍
一款用于自动生成开源项目协议的工具,可以通过 npm 进行安装后在命令行使用,非常方便
使用方式
npm install @xxhls/get-license -g get-license --license=mit
技术选型
- TypeScript: 支持类型体操
- chalk: 命令行输出色彩
- commander: 解析命令行参数
- fs-extra: 拓展原生 fs 的功能
- ini: 解析 .gitconfig 为对象
- parcel: 零配置打包工具
代码分析
- bin: 存放可执行文件
- dist: 存放打包后文件
- src: 源代码
- log: 日志显示
- templates: 协议生成器
- utils:
- getConfig: 生成配置文件
- getYear: 读取当前年份
- isGitConfigExists: 判断Git配置是否存在
- question: 命令行交互
- index.ts: 主函数
- package.json: npm 包配置文件
- tsconfig.json: ts 配置文件
思路详解
该部分我会挑选几个代码量相对多一些的文件进行思路解析,具体可以去代码仓库克隆下来查看
配置生成函数
import os from "os"; import ini from "ini"; import path from "path"; import fs from "fs-extra" import question from "./question"; import { error, info, warn } from "../log"; import isGitconfigExists from "./isGitconfigExists"; interface IConfig { name: string; email: string; } const getConfig = async () => { const config: IConfig = { name: "username", email: "xxx@email.com", }; // 检测是否存在 .gitconfig 文件 const isExists = isGitconfigExists(); if (isExists) { info("检测到 .gitconfig 文件"); // 读取配置文件 const configPath = path.join(os.homedir(), ".gitconfig"); const configStr = fs.readFileSync(configPath, "utf-8"); const configTemp = ini.parse(configStr); config.name = configTemp.user.name; config.email = configTemp.user.email; info(`用户名: ${config.name}`); info(`邮箱: ${config.email}`); } else { warn("未检测到 .gitconfig 文件"); // 创建配置文件 const name = await question("请输入用户名: "); const email = await question("请输入邮箱: "); config.name = name; config.email = email; } return config; }; export default getConfig;
- 判断全局 git 是否存在
- 若存在,则通过 os.homedir 拼接出路径,读取文件后用 ini 进行解析
- 若不存在,则进行命令行交互得到需要的用户名和邮箱
主函数
import fs from "fs-extra"; import getConfig from "./utils/getConfig"; import { info, debug, warn, error } from "./log"; import licenseMap from "./templates"; import { License } from "./templates"; import type { LicenseType } from "./templates"; import { Command } from "commander"; const main = async () => { const program = new Command(); program .name("get-license") .description("Get License") .version("0.1.7"); program .requiredOption("--license ", "Select License") .parse(process.argv); const options = program.opts(); const { license } = options; if (!license) { error("未选择 License 类型"); process.exit(1); } else if (license === "mit" || license === "MIT"){ info("成功选择 MIT License"); const generator = licenseMap[License.MIT]; const config = await getConfig(); const licenseStr = generator(config.name, config.email); const licensePath = `${process.cwd()}/LICENSE`; fs.outputFileSync(licensePath, licenseStr); } else { error("未知的 License 类型"); process.exit(1); } }; main();
- 读取需要的命令行参数 license
- 根据 license 匹配相应的协议生成器
- 生成配置文件
- 将配置传入生成器得到协议
- 在当前文件夹创建 LICENSE 并写入协议内容
更新预期
涵盖 Github 支持的全部协议