diff --git a/package.json b/package.json index 6f3e6c4..6ba41d7 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@antfu/ni": "^0.21.5", "@antfu/utils": "^0.7.5", "@types/ejs": "^3.1.2", + "@types/minimist": "^1.2.4", "@types/node": "^18.17.3", "@types/prompts": "^2.4.4", "@vue/create-eslint-config": "^0.3.1", @@ -51,6 +52,7 @@ "eslint": "^8.46.0", "kolorist": "^1.8.0", "lint-staged": "^13.2.3", + "minimist": "^1.2.8", "pnpm": "^8.6.12", "prompts": "^2.4.2", "simple-git-hooks": "^2.9.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f917d4f..b890ea1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ devDependencies: '@types/ejs': specifier: ^3.1.2 version: 3.1.2 + '@types/minimist': + specifier: ^1.2.4 + version: 1.2.4 '@types/node': specifier: ^18.17.3 version: 18.17.3 @@ -44,6 +47,9 @@ devDependencies: lint-staged: specifier: ^13.2.3 version: 13.2.3 + minimist: + specifier: ^1.2.8 + version: 1.2.8 pnpm: specifier: ^8.6.12 version: 8.6.12 @@ -495,6 +501,10 @@ packages: '@types/unist': 2.0.6 dev: true + /@types/minimist@1.2.4: + resolution: {integrity: sha512-Kfe/D3hxHTusnPNRbycJE1N77WHDsdS4AjUYIzlDzhDrS47NrwuL3YW4VITxwR7KCVpzwgy4Rbj829KSSQmwXQ==} + dev: true + /@types/node@18.17.3: resolution: {integrity: sha512-2x8HWtFk0S99zqVQABU9wTpr8wPoaDHZUcAkoTKH+nL7kPv3WUI9cRi/Kk5Mz4xdqXSqTkKP7IWNoQQYCnDsTA==} dev: true @@ -2512,6 +2522,10 @@ packages: brace-expansion: 2.0.1 dev: true + /minimist@1.2.8: + resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==} + dev: true + /minipass@3.3.6: resolution: {integrity: sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw==} engines: {node: '>=8'} diff --git a/src/index.ts b/src/index.ts index 93b2e3b..5bdeeb3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,12 @@ import { writeFileSync, } from 'node:fs' import ejs from 'ejs' +import minimist from 'minimist' +import prompts from 'prompts' +import { bold, red } from 'kolorist' +import figures from 'prompts/lib/util/figures.js' import { + canSkipEmptying, dowloadTemplate, preOrderDirectoryTraverse, printBanner, @@ -23,10 +28,21 @@ import { import { question } from './question' import type { BaseTemplateList } from './question/template/type' import { postOrderDirectoryTraverse } from './utils/directoryTraverse' +import filePrompt from './question/file' +import { templateList } from './question/template/templateDate' async function init() { printBanner() + const argv = minimist(process.argv.slice(2), { + alias: { + templateType: ['t'], + }, + string: ['_'], + }) + const projectName = argv._[0] + const templateType = templateList.find(item => item.value.type === argv?.t)?.value + let result: { projectName?: string shouldOverwrite?: boolean @@ -41,13 +57,29 @@ async function init() { needsUnocss?: boolean } = {} - try { - result = await question() - } - catch (cancelled) { + if (!projectName) { + try { + result = await question() + } + catch (cancelled) { // eslint-disable-next-line no-console - console.log((<{ message: string }>cancelled).message) - process.exit(1) + console.log((<{ message: string }>cancelled).message) + process.exit(1) + } + } + else { + if (!templateType) { + // eslint-disable-next-line no-console + console.log(`${red(figures.cross)} ${bold('未获取到指定模板')}`) + process.exit(1) + } + result = { + projectName, + shouldOverwrite: canSkipEmptying(projectName) + ? true + : (await prompts(filePrompt(projectName))).shouldOverwrite, + templateType, + } } const cwd = process.cwd() diff --git a/src/question/file.ts b/src/question/file.ts new file mode 100644 index 0000000..3b62d8f --- /dev/null +++ b/src/question/file.ts @@ -0,0 +1,31 @@ +import type { PromptObject } from 'prompts' +import { bold, red } from 'kolorist' +import figures from 'prompts/lib/util/figures.js' +import { canSkipEmptying } from '../utils' + +export default (targetDir: string): PromptObject[] => { + return [ + { + name: 'shouldOverwrite', + type: () => (canSkipEmptying(targetDir) ? null : 'toggle'), + message: () => { + const dirForPrompt + = targetDir === '.' ? '当前文件' : `目标文件"${targetDir}"` + + return `${dirForPrompt}不是空的。要删除现有文件并继续吗?` + }, + initial: false, + active: 'Yes', + inactive: 'No', + }, + { + name: 'overwriteChecker', + type: (_prev, values) => { + if (values.shouldOverwrite === false) + throw new Error(`${red(figures.cross)} ${bold('操作已取消')}`) + + return null + }, + }, + ] +} diff --git a/src/question/name.ts b/src/question/name.ts index 65f783a..a3643ae 100644 --- a/src/question/name.ts +++ b/src/question/name.ts @@ -1,7 +1,5 @@ import type { PromptObject } from 'prompts' -import { bold, red } from 'kolorist' -import figures from 'prompts/lib/util/figures.js' -import { canSkipEmptying } from '../utils' +import filePrompt from './file' export default (): PromptObject[] => { let targetDir = 'uni-app' @@ -13,27 +11,6 @@ export default (): PromptObject[] => { initial: targetDir, onState: state => (targetDir = String(state.value).trim() || targetDir), }, - { - name: 'shouldOverwrite', - type: () => (canSkipEmptying(targetDir) ? null : 'toggle'), - message: () => { - const dirForPrompt - = targetDir === '.' ? '当前文件' : `目标文件"${targetDir}"` - - return `${dirForPrompt}不是空的。要删除现有文件并继续吗?` - }, - initial: false, - active: 'Yes', - inactive: 'No', - }, - { - name: 'overwriteChecker', - type: (_prev, values) => { - if (values.shouldOverwrite === false) - throw new Error(`${red(figures.cross)} ${bold('操作已取消')}`) - - return null - }, - }, + ...filePrompt(targetDir), ] } diff --git a/src/question/template/templateDate.ts b/src/question/template/templateDate.ts index e1f50d8..61791ac 100644 --- a/src/question/template/templateDate.ts +++ b/src/question/template/templateDate.ts @@ -1,6 +1,4 @@ -import { readFileSync, rmSync, writeFileSync } from 'node:fs' -import { join } from 'node:path' -import { bold, dim, green, link } from 'kolorist' +import { dim, green, link } from 'kolorist' import type { TemplateList } from './type' export const templateList: TemplateList[] = [