From 879a464ed8d338c30ab8c7fe05775229c8889bca Mon Sep 17 00:00:00 2001 From: flippedround <734243792@qq.com> Date: Sat, 13 Apr 2024 11:38:10 +0800 Subject: [PATCH] feat: format print info --- README.md | 6 +++ src/commands/info/index.ts | 92 +++++++++++++++++++++++++++++--------- src/index.ts | 4 +- 3 files changed, 79 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 7838fbc..8bde676 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ npm create uni <项目名称> --ts -m pinia -m uniUse -u ano -e ``` +```shell +npm create uni --info +# --info 获取当前项目环境信息 +``` + ### 参数说明 | 配置项 | 参数 | 别名 | 可选值 | @@ -49,6 +54,7 @@ npm create uni <项目名称> --ts -m pinia -m uniUse -u ano -e | Module | moduleList | m | 见[模块列表](#模块列表) | | UI | ui | u | uni/ano | | Eslint | eslint | e | —— | +| info | info | —— | —— | #### 📦插件列表 diff --git a/src/commands/info/index.ts b/src/commands/info/index.ts index 847b165..d391004 100644 --- a/src/commands/info/index.ts +++ b/src/commands/info/index.ts @@ -2,8 +2,7 @@ import process from 'node:process' import { execSync } from 'node:child_process' import { getPackageInfo } from 'local-pkg' import envinfo from 'envinfo' - -// import { link } from 'kolorist' +import { gray, link } from 'kolorist' import { ora } from '../../utils' import { question } from './question' @@ -63,7 +62,13 @@ async function getVSCodeInfo() { } function getVSCodeExtensions(path: string) { - const list = execSync(`${path} --list-extensions --show-versions`) + let list + try { + list = execSync(`code --list-extensions --show-versions`) + } + catch (error) { + list = execSync(`${path} --list-extensions --show-versions`) + } return list.toString().split(/\r?\n/).filter(line => line.trim() !== '') } @@ -71,6 +76,10 @@ function getUniHelperExtensions(extensions: string[]) { return extensions.filter(item => item.includes('uni-helper.') || item.includes('mrmaoddxxaa.create-uniapp-view')) } +function getVolarExtensions(extensions: string[]) { + return extensions.filter(item => item.includes('vue.volar')) +} + function paserExtensionList(list: string[]) { return list.map((item) => { const [name_, version] = item.split('@') @@ -85,18 +94,22 @@ async function getErrorExtensions() { const { path } = (await getVSCodeInfo())! const extensions = getVSCodeExtensions(path) const uniHelperExtensions = paserExtensionList(getUniHelperExtensions(extensions)) + const volarExtensions = paserExtensionList(getVolarExtensions(extensions)) const choices = uniHelperExtensions.map(item => item.name) loading.finish() const { errorIndexList } = await question(choices, '请选择需要反馈的vscode插件') - return errorIndexList.map((index: number) => { - return { - name: uniHelperExtensions[index].name, - version: uniHelperExtensions[index].version, - bugs: uniHelperExtensions[index].bugs, - } - }) + return { + errorExtensions: errorIndexList.map((index: number) => { + return { + name: uniHelperExtensions[index].name, + version: uniHelperExtensions[index].version, + bugs: uniHelperExtensions[index].bugs, + } + }), + volarExtensions, + } } export async function getBaseEnvInfo() { @@ -112,21 +125,56 @@ export async function getBaseEnvInfo() { export async function getUniAppInfo() { const errorDependencies = await getErrorDependencies() - const errorExtensions = await getErrorExtensions() + const { errorExtensions, volarExtensions } = await getErrorExtensions() const baseEnvInfo = await getBaseEnvInfo() const baseDependencies = await getBaseDependencies() - console.log('\n') + const splitter = '----------------------------------------------' + + let baseEnvInfoStr = '' + for (const [key, value] of Object.entries(baseEnvInfo)) + baseEnvInfoStr += `- ${key}: \`${value}\`\n` + for (const { name, version } of volarExtensions) + baseEnvInfoStr += `- ${name}: \`${version}\`\n` + + let baseDependenciesStr = '' + for (const { name, version } of baseDependencies) + baseDependenciesStr += `- ${name}: \`${version}\`\n` + + let errorDependenciesStr = '' + for (const { name, version, bugs } of errorDependencies) + errorDependenciesStr += `- ${link(name, bugs!)}: \`${version}\`\n` + + let errorExtensionsStr = '' + for (const { name, version, bugs } of errorExtensions) + errorExtensionsStr += `- ${link(name, bugs)}: \`${version}\`\n` + + console.log(splitter) + console.log() console.log('基础环境信息:') - console.table(baseEnvInfo) - console.log('\n') + console.table(baseEnvInfoStr) + console.log('基础依赖信息:') - console.table(baseDependencies) - console.log('\n') - console.log('uni-helper依赖信息:') - console.table(errorDependencies) - console.log('\n') - console.log('uni-helper插件信息:') - console.table(errorExtensions) - console.log('\n') + console.log(baseDependenciesStr) + + if (errorDependencies.length > 0) { + console.log('uni-helper依赖信息:') + console.log(errorDependenciesStr) + } + + if (errorExtensions.length > 0) { + console.log('uni-helper插件信息:') + console.log(errorExtensionsStr) + } + + console.log(splitter) + + console.log( + `${[ + gray('感谢使用uni-helper,请提供以上信息以便我们排查问题。'), + '👉 uni-help官网: https://uni-helper.js.org/', + '👉 改进建议: https://github.com/uni-helper/create-uni/issues/new/choose', + ].join('\n')}\n`, + ) + process.exit(0) } diff --git a/src/index.ts b/src/index.ts index ccefaa1..7df171b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ import { import { join, resolve } from 'node:path' import process from 'node:process' import ejs from 'ejs' -import { bold, red } from 'kolorist' +import { bold, green, red } from 'kolorist' import minimist from 'minimist' import prompts from 'prompts' import figures from 'prompts/lib/util/figures.js' @@ -253,5 +253,7 @@ async function init() { init().catch(() => { console.log(`${red(figures.cross)} ${bold('操作已取消')}`) + console.log() + console.log(`🚀 遇到问题? 快速反馈:${green('https://github.com/uni-helper/create-uni/issues/new/choose')}`) process.exit(0) })