1.0.7 • Published 3 months ago

lj-generator-cli v1.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

自定义脚手架

安装npm lj-generator-cli -g

查看模板lj-generator-cli list

查看版本lj-generator-cli -v

安装模板lj-generator-cli create [projectName]

脚手架配置

//入口js文件
#! /usr/bin/env noe
//指定node运行该脚本

package.json文件

//bin属性 脚本名称为 "lj-cli" ,执行脚本文件 "./index.js"
 "bin": {
    "lj-cli":"./index.js"
  },
具体步骤
  1. 书写脚本文件
  2. 编辑package.json文件
  3. 软链接npm link 删除软链接npm rm lj-generator-cli -g 全局命令 lj-cli 会执行脚本
  4. 使用 commander插件 读取命令行参数 option可配置
import { program } from 'commander';
program
    .command("create <app-name>",)
    .description("创建一个新的项目")
    .option('-t --template [template]', '输入的模板名称')//-t 配置 模板
    .option('-f --force [force]', '强制覆盖本地同名目录')//-f 配置 模板
    .option('-i --ignore [ignore]', '忽略描述')//-i 配置 模板
    .action((name, option) => {
        logTitle();
        //name是create输入的名字  
        // option { template: 'asd', force: '1232', ignore: true }
    })
program.parse(process.argv);//解析用户输入的参数
  1. 读取package.json文件查看版本号
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __dirname = dirname(fileURLToPath(import.meta.url));//当前文件夹
const pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8'));
program.version(pkg.version, "-v, -version");//参数查看版本号
  1. 已有文件删除文件夹
function deleteFolder(folderPath) {
    if (fs.existsSync(folderPath)) {
        fs.readdirSync(folderPath).forEach((file) => {
            const curPath = path.join(folderPath, file);
            if (fs.lstatSync(curPath).isDirectory()) {
                deleteFolder(curPath); // 递归调用删除子文件夹
            } else {
                fs.unlinkSync(curPath); // 直接删除文件
            }
        });
        fs.rmdirSync(folderPath); // 删除空文件夹
    }
}
await fs.rmdirSync(file);//删除文件夹 此操作只能删除空文件夹
  1. 交互命令行
import inquirer from 'inquirer';
export const inquirerConfirmHandle = async (message) => {
    return await inquirer
        .prompt({type: 'confirm', name: 'confirm',message, })
}
export const inquirerListHandle = async (message, choices) => {
    return await inquirer
        .prompt({ type: 'list', name: 'choices',  message,choices,  })
}   
export const inquirerPackageHandle = async (message) => {
    return await inquirer
        .prompt({ type: 'input',name: 'input',  message, })
}   
  1. 更改 package.json 文件信息
await changePackage(process.cwd() + '/' + name + '/package.json', pkgMessage, name);
const pkg = JSON.parse(fs.readFileSync(path, 'utf8'));  //读
await fs.writeFileSync(path, JSON.stringify(pkg, null, "\t"), { spaces: 2 }); //写 
  1. 安装依赖
await npmInstall(process.cwd() + '/' + name)
export const npmInstall = async (path) => {
const spinner = ora(chalk.blueBright(`正在安装依赖`)).start();
try {
    const res = shell.exec(`cd ${path} && npm install --force -d`);
    if (res?.code === 0) {
        spinner.succeed(chalk.blueBright((`~~~安装依赖成功!~~~`)));
        spinner.succeed(chalk.blueBright((`~~~项目安装成功!~~~`)));
    } else {
        spinner.fail(chalk.redBright((`安装依赖失败!请手动安装依赖!`)));
        shell.exec(1);
    }
} catch (error) {
    return false;
}
}
使用插件
  1. figlet(大标题)
  2. 进度条 ora
  3. chalk 字体美化
  4. shell 脚本控制
  5. table 命令行表格
  6. inquirer 命令行交互
  7. download-git-repo 下载包插件
  8. fs-extra 修改文件
1.0.7

3 months ago

1.0.6

3 months ago

1.0.5

3 months ago

1.0.4

3 months ago

1.0.3

3 months ago

1.0.2

3 months ago

1.0.1

4 months ago

1.0.0

4 months ago