Skip to content

Commit

Permalink
♻️ refactor(dependencies): 重构 npx 代码逻辑 减少生产环境依赖 big deal
Browse files Browse the repository at this point in the history
  • Loading branch information
ErKeLost committed Aug 1, 2022
1 parent 7937711 commit 926a37c
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 181 deletions.
14 changes: 1 addition & 13 deletions packages/vite-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,14 @@
"bugs": "https://github.com/ErKeLost/vite-cli/issues",
"dependencies": {
"chalk": "4.1.0",
"chalk-animation": "^2.0.2",
"child_process": "^1.0.2",
"commander": "^9.3.0",
"download-git-repo": "^3.0.2",
"ejs": "^3.1.8",
"figlet": "^1.5.2",
"fs-extra": "^10.1.0",
"globby": "^13.1.2",
"gradient-string": "^2.0.1",
"handlebars": "^4.7.7",
"inquirer": "8.1.1",
"kolorist": "^1.5.1",
"log-symbols": "^5.1.0",
"nanospinner": "^1.1.0",
"ora": "5.4.1",
"pacote": "^13.6.1",
"prettier": "latest",
"prompts": "^2.4.2",
"shelljs": "^0.8.5",
"update-notifier": "^6.0.2"
"prompts": "^2.4.2"
},
"devDependencies": {
"@types/node": "^18.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-cli/src/core/command/version/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { magenta as color } from 'chalk'
// import { magenta as color } from 'chalk'
import program from '../../program'
import { magenta } from '../../../utils/log'
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -11,7 +11,7 @@ import {
BUILD_DATE
} from '../../../shared/constant'
function getVersionView() {
color(VITE_CLI_VERSION)
magenta(VITE_CLI_VERSION)
magenta(gradient.atlas(VALUE_ONLINE))
magenta(VERSION)
magenta(BUILD_DATE)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-cli/src/core/questions/creator/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
{ title: 'Element Plus', value: 'element-plus' },
{ title: 'Vuetify3 Beta', value: 'vuetify' },
{ title: 'Naive UI', value: 'naive-ui' },
{ title: 'Ant Design Vue 2.x', value: 'ant-design' },
{ title: 'Ant Design Vue 3.x', value: 'ant-design' },
{ title: 'DevUI', value: 'devui' },
{ title: 'arco-design', value: 'arco' },
{ title: 'TDesign', value: 'tencent' },
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-cli/src/core/questions/creator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { prompt, QuestionCollection } from 'inquirer'
import options from '../../../shared/options'
import precss from './precss'
import components from './components'
import PackageDevice from './packageDevice'
import PackageDevice from './packageManager'
import Plugins from './plugins'
import future from './feature'
import device from './device'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
name: 'package',
type: 'select',
message: 'Select your package manager (选择您的包管理器)',
message: 'Which package manager do you want to use? (选择您的包管理器)',
choices: [
{ title: 'Pnpm', value: 'pnpm' },
{ title: 'Yarn', value: 'yarn' },
Expand Down
58 changes: 29 additions & 29 deletions packages/vite-cli/src/core/questions/packages.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import path = require('path')
import { readdir } from 'fs/promises'
import fs = require('fs-extra')
import { prompt, QuestionCollection } from 'inquirer'
import options from '@/shared/options'
// import path = require('path')
// import { readdir } from 'fs/promises'
// import fs = require('fs-extra')
// import { prompt, QuestionCollection } from 'inquirer'
// import options from '@/shared/options'

async function createQuestion(quesiton) {
const result: QuestionCollection = await prompt([quesiton])
Object.assign(options, result)
return Promise.resolve()
}
// async function createQuestion(quesiton) {
// const result: QuestionCollection = await prompt([quesiton])
// Object.assign(options, result)
// return Promise.resolve()
// }

async function createQuestions(): Promise<void> {
const packages = []
const dirs = await readdir(options.src)
dirs.forEach((pluginPath) => {
const src = path.resolve(options.src, pluginPath, 'package.json')
packages.push(fs.readJsonSync(src))
})
options.allPackages = packages
await createQuestion({
name: 'plugins',
type: 'checkbox',
message: '选择需要安装的组件',
choices: packages.map((item) => ({
name: item.description,
value: item.name
}))
})
}
// async function createQuestions(): Promise<void> {
// const packages = []
// const dirs = await readdir(options.src)
// dirs.forEach((pluginPath) => {
// const src = path.resolve(options.src, pluginPath, 'package.json')
// packages.push(fs.readJsonSync(src))
// })
// options.allPackages = packages
// await createQuestion({
// name: 'plugins',
// type: 'checkbox',
// message: '选择需要安装的组件',
// choices: packages.map((item) => ({
// name: item.description,
// value: item.name
// }))
// })
// }

export default createQuestions
// export default createQuestions
15 changes: 8 additions & 7 deletions packages/vite-cli/src/utils/clearConsole.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import chalk from 'chalk'
import { bgCyan, cyan } from './log'
// import chalk from 'chalk'
// import { bgCyan, cyan } from './log'
import { cyan } from './log'
// import readline from 'readline'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const readline = require('readline')
Expand All @@ -8,11 +9,11 @@ export default function (color: string, str: string): void {
if (process.stdout.isTTY) {
console.log('')
const cutLine = ` VITE_CLI ${VITE_CLI_VERSION} `
bgCyan(
' ~'.repeat((process.stdout.columns - cutLine.length) / 4) +
cutLine +
'~ '.repeat((process.stdout.columns - cutLine.length) / 4)
)
// bgCyan(
// ' ~'.repeat((process.stdout.columns - cutLine.length) / 4) +
// cutLine +
// '~ '.repeat((process.stdout.columns - cutLine.length) / 4)
// )
const blank = '\n'.repeat(process.stdout.rows)
console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
Expand Down
166 changes: 83 additions & 83 deletions packages/vite-cli/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
import child_process from 'child_process'
import chalk from 'chalk'
import path from 'path'
import fs from 'fs'
import ora, { Color } from 'ora'
import util from 'util'
// import child_process from 'child_process'
// import chalk from 'chalk'
// import path from 'path'
// import fs from 'fs'
// import ora, { Color } from 'ora'
// import util from 'util'

const execSync = child_process.execSync
// eslint-disable-next-line @typescript-eslint/no-var-requires
const exec = util.promisify(require('child_process').exec)
// const execSync = child_process.execSync
// // eslint-disable-next-line @typescript-eslint/no-var-requires
// const exec = util.promisify(require('child_process').exec)

export class DefaultLogger {
private _spinner: ora.Ora
// export class DefaultLogger {
// private _spinner: ora.Ora

public constructor(startText: string) {
this._spinner = ora(startText).start()
}
// public constructor(startText: string) {
// this._spinner = ora(startText).start()
// }

public log(text: string, color?: Color): void {
if (color) {
this._spinner.color = color
}
this._spinner.text = text
}
// public log(text: string, color?: Color): void {
// if (color) {
// this._spinner.color = color
// }
// this._spinner.text = text
// }

public succeed(text: string): void {
this._spinner.succeed(text)
}
// public succeed(text: string): void {
// this._spinner.succeed(text)
// }

public fail(text: string): void {
this._spinner.fail(text)
}
}
// public fail(text: string): void {
// this._spinner.fail(text)
// }
// }

export const runSync = (command: string, spinner?: DefaultLogger) => {
try {
return execSync(command, { cwd: process.cwd(), encoding: 'utf8' })
} catch (error) {
spinner.fail('task fail')
process.exit(1)
}
}
// export const runSync = (command: string, spinner?: DefaultLogger) => {
// try {
// return execSync(command, { cwd: process.cwd(), encoding: 'utf8' })
// } catch (error) {
// spinner.fail('task fail')
// process.exit(1)
// }
// }

export const runAsync = async (command: string, spinner?: DefaultLogger) => {
try {
await exec(command, { cwd: process.cwd(), encoding: 'utf8' })
} catch (error) {
spinner.fail('task fail')
process.exit(1)
}
}
// export const runAsync = async (command: string, spinner?: DefaultLogger) => {
// try {
// await exec(command, { cwd: process.cwd(), encoding: 'utf8' })
// } catch (error) {
// spinner.fail('task fail')
// process.exit(1)
// }
// }

export const taskPre = (logInfo: string, type: 'start' | 'end') => {
if (type === 'start') {
return `task start(开始任务): ${logInfo} \r\n`
} else {
return `task end(任务结束): ${logInfo} \r\n`
}
}
// export const taskPre = (logInfo: string, type: 'start' | 'end') => {
// if (type === 'start') {
// return `task start(开始任务): ${logInfo} \r\n`
// } else {
// return `task end(任务结束): ${logInfo} \r\n`
// }
// }

// 获取项目文件
export const getProjectPath = (dir = './'): string => {
return path.join(process.cwd(), dir)
}
// // 获取项目文件
// export const getProjectPath = (dir = './'): string => {
// return path.join(process.cwd(), dir)
// }

export function compose(middleware) {
const otherOptions = {}
function dispatch(index, otherOptions) {
if (index == middleware.length) return
const currMiddleware = middleware[index]
currMiddleware((addOptions) => {
dispatch(++index, { ...otherOptions, ...addOptions })
}, otherOptions).catch((error) => {
console.log('💣 发布失败,失败原因:', error)
})
}
dispatch(0, otherOptions)
}
// export function compose(middleware) {
// const otherOptions = {}
// function dispatch(index, otherOptions) {
// if (index == middleware.length) return
// const currMiddleware = middleware[index]
// currMiddleware((addOptions) => {
// dispatch(++index, { ...otherOptions, ...addOptions })
// }, otherOptions).catch((error) => {
// console.log('💣 发布失败,失败原因:', error)
// })
// }
// dispatch(0, otherOptions)
// }

/**
* 获取当前package.json的版本号
*/
export const getOriginPackageJson = (): Record<string, any> => {
const packageJson = JSON.parse(
fs.readFileSync(getProjectPath('package.json'), 'utf-8')
)
return packageJson
}
// /**
// * 获取当前package.json的版本号
// */
// export const getOriginPackageJson = (): Record<string, any> => {
// const packageJson = JSON.parse(
// fs.readFileSync(getProjectPath('package.json'), 'utf-8')
// )
// return packageJson
// }

/**
* 工具函数,用来捕获并打印错误,返回false
*/
export const basicCatchError = (err: Error) => {
console.log(`\r\n ${chalk.red(err)}`)
return false
}
// /**
// * 工具函数,用来捕获并打印错误,返回false
// */
// export const basicCatchError = (err: Error) => {
// console.log(`\r\n ${chalk.red(err)}`)
// return false
// }
Loading

0 comments on commit 926a37c

Please sign in to comment.