|
| 1 | +import { existsSync, writeFileSync } from 'node:fs' |
| 2 | +import { join } from 'node:path' |
| 3 | +import process from 'node:process' |
| 4 | +import { cancel, confirm, intro, isCancel, outro, select } from '@clack/prompts' |
| 5 | +import consola from 'consola' |
| 6 | +import { addDependency, addDevDependency, detectPackageManager } from 'nypm' |
| 7 | + |
| 8 | +const logger = consola.withTag('genapi:init') |
| 9 | + |
| 10 | +const CONFIG = { |
| 11 | + presets: { |
| 12 | + axios: { deps: ['axios'], schema: false }, |
| 13 | + fetch: { deps: [], schema: true }, |
| 14 | + ky: { deps: ['ky'], schema: false }, |
| 15 | + got: { deps: ['got'], schema: false }, |
| 16 | + ofetch: { deps: ['ofetch'], schema: true }, |
| 17 | + uni: { deps: ['@uni-helper/uni-network'], schema: false }, |
| 18 | + } as Record<string, { deps: string[], schema: boolean }>, |
| 19 | +} |
| 20 | + |
| 21 | +async function mandate<T>(promise: Promise<any>): Promise<T> { |
| 22 | + const res = await promise |
| 23 | + if (isCancel(res)) { |
| 24 | + cancel('Operation cancelled') |
| 25 | + process.exit(0) |
| 26 | + } |
| 27 | + return res as T |
| 28 | +} |
| 29 | + |
| 30 | +export async function initCommand() { |
| 31 | + intro('🚀 genapi init') |
| 32 | + |
| 33 | + const preset = await mandate<keyof typeof CONFIG.presets>(select({ |
| 34 | + message: 'Select preset:', |
| 35 | + options: Object.keys(CONFIG.presets).map(v => ({ value: v, label: v })), |
| 36 | + })) |
| 37 | + |
| 38 | + const mode = await mandate<('ts' | 'js' | 'schema')>(select({ |
| 39 | + message: 'Select mode:', |
| 40 | + options: [ |
| 41 | + { value: 'ts', label: 'TS' }, |
| 42 | + { value: 'js', label: 'JS' }, |
| 43 | + ...(CONFIG.presets[preset].schema ? [{ value: 'schema', label: 'Schema' }] : []), |
| 44 | + ], |
| 45 | + })) |
| 46 | + |
| 47 | + const isTS = mode !== 'js' |
| 48 | + const fileName = `genapi.config.${isTS ? 'ts' : 'js'}` |
| 49 | + const presetValue = `${preset}.${mode}` |
| 50 | + const content = isTS |
| 51 | + ? `import { defineConfig } from '@genapi/core' |
| 52 | +import { ${preset} } from '@genapi/presets' |
| 53 | +
|
| 54 | +export default defineConfig({ preset: ${presetValue}, input: '...', output: { main: 'src/api/index.ts' } })` |
| 55 | + : `const { defineConfig } = require('@genapi/core') |
| 56 | +const { ${preset} } = require('@genapi/presets') |
| 57 | +
|
| 58 | +module.exports = defineConfig({ preset: ${presetValue}, input: '...', output: { main: 'src/api/index.ts' } })` |
| 59 | + |
| 60 | + if (existsSync(join(process.cwd(), fileName)) && !await mandate(confirm({ message: 'Overwrite?' }))) |
| 61 | + return |
| 62 | + |
| 63 | + writeFileSync(join(process.cwd(), fileName), content) |
| 64 | + |
| 65 | + // 3. 依赖闭环 |
| 66 | + const deps = CONFIG.presets[preset].deps |
| 67 | + const devDeps = ['@genapi/core', '@genapi/presets', ...(mode === 'schema' ? ['fetchdts'] : [])] |
| 68 | + |
| 69 | + if (await mandate(confirm({ message: 'Install now?' }))) { |
| 70 | + addDependency(deps, { cwd: process.cwd() }) |
| 71 | + addDevDependency(devDeps, { cwd: process.cwd() }) |
| 72 | + } |
| 73 | + else { |
| 74 | + const pm = (await detectPackageManager(process.cwd()))?.name || 'npm' |
| 75 | + logger.info(`Manual: ${pm} add -D ${deps.join(' ')}`) |
| 76 | + } |
| 77 | + |
| 78 | + outro('✨ Success') |
| 79 | +} |
0 commit comments