赞
踩
项目地址:GitHub(最新版本) | GitCode(旧版本)
一款用于自动生成开源项目协议的工具,可以通过 npm 进行安装后在命令行使用,非常方便
npm install @xxhls/get-license -g
get-license --license=mit
该部分我会挑选几个代码量相对多一些的文件进行思路解析,具体可以去代码仓库克隆下来查看
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;
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 <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();
涵盖 Github 支持的全部协议
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。