当前位置:   article > 正文

【趣味项目】一键生成LICENSE_license授权文件生成器

license授权文件生成器

【趣味项目】一键生成LICENSE

项目地址:GitHub(最新版本) | GitCode(旧版本)
在这里插入图片描述

项目介绍

一款用于自动生成开源项目协议的工具,可以通过 npm 进行安装后在命令行使用,非常方便

使用方式

npm install @xxhls/get-license -g

get-license --license=mit
  • 1
  • 2
  • 3

技术选型

  • 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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  1. 读取需要的命令行参数 license
  2. 根据 license 匹配相应的协议生成器
  3. 生成配置文件
  4. 将配置传入生成器得到协议
  5. 在当前文件夹创建 LICENSE 并写入协议内容

更新预期

涵盖 Github 支持的全部协议

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/663851
推荐阅读
相关标签
  

闽ICP备14008679号