Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronMax1 committed Mar 10, 2024
0 parents commit a00e33c
Show file tree
Hide file tree
Showing 4,566 changed files with 656,963 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file added .DS_Store
Binary file not shown.
30 changes: 30 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#! /usr/bin/env node

const {Command} = require('commander');

const program = new Command()

// 定义创建项目
// 定义创建项目
program
.command('create <projectName>')
.description('create a new project, 创建一个新项目')
.option('-f, --force', '如果创建的目录存在则直接覆盖')
.action((name, option) => {
// 引入create.js 模块并传递参数
require('../lib/create')(name, option)
})




// 配置版本信息
program
.version(`v${require('../package.json').version}`)
.description('使用说明')
.usage('<command> [option]')


// 解析用户执行命令传入参数
program.parse(process.argv)

Binary file added lib/.DS_Store
Binary file not shown.
102 changes: 102 additions & 0 deletions lib/CreateGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// lib/CreateGenerator.js
const ora = require('ora'); // Ora 在控制台显示当前加载状态的(下载5.x版本)
const inquirer = require('inquirer'); // 用户与命令行交互
const chalk = require('chalk'); // ‘粉笔’ 用于设置终端字体颜色的库(下载4.x版本)
const path = require('path');
const child_process = require('child_process');
const extra = require('fs-extra'); // fs-extra 是 node fs 的扩展
const log = ctx => console.log(chalk.green(ctx));
// 使用 ora 初始化,传入提示信息 message
const spinner = ora()

/**
* 复制模板文件
*/
copyTempl = async(dist)=> {
let desPath = path.join(__dirname, '../template')
await extra.copy(desPath,dist);
}



/**
* @wrapLoading 交互加载动画
* @param {*} fn 在 wrapLoading 函数中执行的方法
* @param {*} message 执行动画时的提示信息
* @param {...any} args 传递给 fn 方法的参数
* @returns
*/
async function wrapLoading( dist,message) {
spinner.text = message.loadingMsg
// 开始加载动画
spinner.start()

try {
// 执行传入的方法 fn
const result = await copyTempl(dist);
// 动画修改为成功
spinner.succeed(message.seccessfulMsg)
return result
} catch (error) {
// 动画修改为失败
spinner.fail(message.failedMsg + ': ', error)
}
}

class CreateGenerator {
constructor(name, targetDir) {
// 目录名称
this.name = name;
// 创建位置
this.targetDir = targetDir;
/**
* 对 download-git-repo 进行 promise 化改造
* 使用 child_process 的 execSync 方法拉取仓库模板
*/
this.downloadGitRepo = child_process.execSync

}

/**
* @download 下载远程模板
*/
async download() {
// 设置模板下载地址
// const modelUrl = `https://github.com/AaronAbelx/hardhat-cli.git`

// child_process.spawn 参数
/**
* @param masterBranch master分支
*/
const masterBranch = "./template"

// 调用动画加载效果,加载master分支
await wrapLoading(
this.targetDir,
{ loadingMsg: '加载hardhat-cli中...', seccessfulMsg: 'hardhat-cli加载成功', failedMsg: 'hardhat-cli加载失败' }
// modelUrl,
// path.resolve(process.cwd(), this.targetDir),

)
}

// 核心创建逻辑
async create() {
// console.log(chalk.yellow(`name: ${this.name}`))
// console.log(chalk.yellow(`targetDir: ${this.targetDir}`))
log(this.targetDir)
await this.download()
log(`
👌安装完成
To get start
======================
cd ${this.name}
yarn
======================
`)
// 写环境变量文件(用在模板编译启动、打包时使用)
extra.outputFileSync(path.resolve(this.targetDir, '.env'), `PROJECT = ${this.name}`)
}
}

module.exports = CreateGenerator;
69 changes: 69 additions & 0 deletions lib/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const {promisify} = require('util');
const figlet = promisify(require("figlet"));
const clear = require('clear');
const path = require('path');
const extra = require('fs-extra'); // fs-extra 是 node fs 的扩展
const inquirer = require('inquirer'); // 命令行交互
const chalk = require('chalk'); // ‘粉笔’
const CreateGenerator = require('./CreateGenerator');
const log = ctx => console.log(chalk.green(ctx));
module.exports = async function (name, options) {
clear();
const data = await figlet("hello "+name)
log(data)
// 验证是否正常取到值
// console.log('create success', name, options);
const cwd = process.cwd(); // 执行目录
const targetAir = path.join(cwd, name); // 需要创建的目录地址
// 判断目录是否已经存在?
if (extra.existsSync(targetAir)) {
// 是否为强制创建?
if (options.force) {
await extra.remove(targetAir)
makeGenerator(name, targetAir)
}
else {
// 在终端输出询问用户是否覆盖:
const inquirerParams = [{
name: 'aboutProject',
type: 'list',
message: '目标文件目录已经存在,请选择如下操作:',
choices: [
{ name: '替换当前目录', value: 'replace' },
{ name: '移除已有目录', value: 'remove' },
{ name: '取消当前操作', value: 'cancel' }
]
}]
let inquirerData = await inquirer.prompt(inquirerParams);
// console.log('inquirerData', inquirerData)
if (!inquirerData?.aboutProject) {
return
}
else if (inquirerData.aboutProject === 'remove') {
// 移除已存在的目录
await extra.remove(targetAir)
console.log('目录 <' + chalk.green(name) + '> 已移除')
makeGenerator(name, targetAir)
}
else if (inquirerData.aboutProject === 'replace') {
await extra.remove(targetAir)
makeGenerator(name, targetAir)
}
}
}
else {
makeGenerator(name, targetAir)
}
};

/**
* 创建项目
* @param {string} name 项目名称
* @param {string} targetAir 需要创建的目录地址
*/
const makeGenerator = (name, targetAir) => {
const generator = new CreateGenerator(name, targetAir)

generator.create()
}

Binary file added node_modules/.DS_Store
Binary file not shown.
107 changes: 107 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a00e33c

Please sign in to comment.