【趣味项目】一键生成LICENSE

本文介绍了如何使用get-license这个Node.js项目,通过npm安装,自动生成并定制开源项目的LICENSE文件,支持MITLicense。项目利用了TypeScript、chalk、commander等技术,并解释了配置生成和主函数的工作原理。
摘要由CSDN通过智能技术生成

【趣味项目】一键生成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;
  1. 判断全局 git 是否存在
  2. 若存在,则通过 os.homedir 拼接出路径,读取文件后用 ini 进行解析
  3. 若不存在,则进行命令行交互得到需要的用户名和邮箱

主函数

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();
  1. 读取需要的命令行参数 license
  2. 根据 license 匹配相应的协议生成器
  3. 生成配置文件
  4. 将配置传入生成器得到协议
  5. 在当前文件夹创建 LICENSE 并写入协议内容

更新预期

涵盖 Github 支持的全部协议

  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ByeByeWorld02

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值