Skip to content

Commit

Permalink
feat(core): use clack instead of inquire
Browse files Browse the repository at this point in the history
  • Loading branch information
yzh990918 committed Mar 4, 2023
1 parent 82fcf09 commit febb364
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 131 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"rules": {
"no-console": "off",
"no-prototype-builtins":"off",
"no-return-await":"off"
"no-return-await":"off",
"promise/no-promise-in-callback": "off"
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<a href="https://magic-cli.netlify.app/">🧑‍💻 Document <sup>Beta</sup></a>
</p>

## New version of Magic preview, Stay tuned!

![](https://jsd.cdn.zzko.cn/gh/yzh990918/static@master/Kapture-2023-03-04-at-14.q6m1ppb4psg.gif)

## Getting Start

This section will help you learn how to use [Magic-cli](https://github.com/vbs-plus/magic-cli). If you already have an existing project and would like to keep project inside the project, start from Step 2.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"deploy": "zr build && zr docs:build"
},
"dependencies": {
"@clack/prompts": "^0.6.1",
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@vbs/eslint-config": "^1.0.0",
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ export const InitCommander = () => {
.name(Object.keys(pkg.bin)[0])
.usage('<command> [options]')
.version(pkg.version)
.option('-d, --debug', '是否开启 Debug 模式', false)
.option('-tp, --targetPath <targetPath>', '指定目标安装目录', '')
.option('-d, --debug', 'Whether to turn on Debug mode', false)
.option('-tp, --targetPath <targetPath>', 'Specifies the command working directory', '')

program
.command('init [projectName]')
.option('-f, --force', '是否强制初始化项目', false)
.option('-f, --force', 'Whether to force initialization of the project', false)
.action((projectName: string, { force }: { force: boolean }, cmd: Command) => {
exec(projectName, force, cmd)
})

program
.command('add [templateName]')
.option('-f, --force', '是否强制添加模板', false)
.option('-f, --force', 'Whether to force the addition of a template', false)
.action((templateName: string, { force }: { force: boolean }, cmd: Command) => {
info(templateName)
info(force)
Expand All @@ -40,7 +40,7 @@ export const InitCommander = () => {
try {
const HOME_PATH = process.env.MAGIC_CLI_HOME_PATH || ''
fse.removeSync(HOME_PATH)
consola.success('重启应用成功! \n')
consola.success('The app is restarted successfully! \n')
} catch (error) {
consola.error(error)
}
Expand All @@ -50,7 +50,7 @@ export const InitCommander = () => {
program.on('option:debug', () => {
if (program.opts().debug) {
process.env.DEBUG = 'debug'
info('开启 DEBUG 模式')
info('Turn on DEBUG mode!')
} else {
process.env.DEBUG = ''
}
Expand All @@ -65,7 +65,7 @@ export const InitCommander = () => {
const avaliableCommands = program.commands.map(item => item.name())
consola.error(`未知命令${cmd[0]}`)

if (avaliableCommands.length) echo(' 可用命令 ', avaliableCommands.join(','))
if (avaliableCommands.length) echo(' Available commands ', avaliableCommands.join(','))
})

program.parse(process.argv)
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ export const exec = async(...args: any[]) => {

// 判断缓存目录是否已存在
if (await pkg.exists()) {
debug('执行更新')
debug('Perform the update')
await pkg.update()
} else {
debug('执行初始化')
debug('Perform initialization')
await pkg.init()
}
}

const execFilePath = await pkg.getRootFilePath()
if (!execFilePath)
throw new Error(error('当前指定文件夹路径有误', { needConsole: false }))
throw new Error(error('The folder path specified is incorrect!', { needConsole: false }))

debug(`execFilePath:${execFilePath}`)
debug(`TP_PATH:${TP_PATH}`)
Expand Down Expand Up @@ -86,12 +86,12 @@ export const exec = async(...args: any[]) => {
)

child.on('error', (e: Error) => {
error(`多进程代码执行异常: ${e.message}`)
error(`Multi-process code execution exception: ${e.message}`)
process.exit(1)
})

child.on('exit', (e: number) => {
debug(`${curCommand} 命令执行成功`)
debug(`${curCommand} The command was executed successfully`)
process.exit(e)
})
} catch (e: any) {
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const homePath = os.homedir()

export function checkUserHome(homePath: string) {
if (!homePath || !fse.existsSync(homePath))
consola.error(new Error('当前登录用户主目录不存在'))
consola.error(new Error('The home directory of the currently logged-on user does not exist!'))
}

export function initDefaultConfig() {
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function checkPackageUpdate() {
if (latestVersion && semver.gt(latestVersion, version)) {
console.log()
consola.warn(
`最新版本已发布,请手动更新脚手架版本,当前版本为:${version},最新版本为:${latestVersion} []~( ̄▽ ̄)~*,探索跟多关于 Magic,请访问: https://magic-cli.netlify.app/\n`,
`The latest version is released, please manually update the scaffolding version, the current version is: ${version}, the latest version is: ${latestVersion} []~( ̄▽ ̄)~*, to explore and learn more about Magic, please visit: https://magic-cli.netlify.app/\n`,
)
console.log()
}
Expand All @@ -53,14 +53,14 @@ export async function checkPackageUpdate() {
export function checkNodeVersion() {
const currentVersion = process.version
if (!semver.gte(currentVersion, LOWEST_NODE_VERSION))
throw new Error(error(`当前 Node 版本过低,推荐安装 v${LOWEST_NODE_VERSION} 以上 Node 版本`, { needConsole: false }))
throw new Error(error(`The current Node version is too low, it is recommended to install v${LOWEST_NODE_VERSION} or above Node version`, { needConsole: false }))
}

export async function prepare() {
printMagicLogo(pkg.version)

const spinner = ora({
text: '👉 检查构建环境... \n',
text: '👉 Check the build environment... \n',
spinner: 'material',
})

Expand All @@ -71,9 +71,9 @@ export async function prepare() {
checkEnv()
await checkPackageUpdate()
checkNodeVersion()
spinner.succeed('构建环境正常!\n')
spinner.succeed('The build environment is normal!\n')
} catch (error) {
spinner.fail('检查构建环境异常! \n')
spinner.fail('Check for build environment exceptions! \n')
console.log(error)
process.exit(-1)
}
Expand Down
Loading

0 comments on commit febb364

Please sign in to comment.