Skip to content

Commit

Permalink
fix: 修复create脚本在某些情况卡死的问题,统一create脚本入口 (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtrbo committed Jul 4, 2024
1 parent 28cf194 commit 80ee16a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 41 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
"node": ">=18.0.0"
},
"scripts": {
"create:subpackage": "esno scripts/create-subpackage.ts",
"create:function": "esno scripts/create-function.ts",
"create": "esno scripts/create.ts",
"build": "pnpm -r --filter=./packages/* run build",
"docs": "pnpm -F xparcai-utils-docs run docs:dev",
Expand Down
17 changes: 8 additions & 9 deletions scripts/create-function.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import process from 'node:process'
import fs from 'node:fs'
import process from 'node:process'
import prompts from '@posva/prompts'
import ora from 'ora'
import { loadFunction, loadSubpackage, packagePrefix, resolveRealPath, spinnersPrefixText, toCamelCase, writeSubpackageFunction, writeSubpackageFunctionTest } from './utils'

const [, , ...args] = process.argv

// 写子包的index.ts - 这里侧重的是增加导出
function writeSubpackageIndex(subpackageName: string, functionName: string) {
const exportPath = resolveRealPath(`../packages/${subpackageName}/index.ts`)
Expand All @@ -15,9 +13,7 @@ function writeSubpackageIndex(subpackageName: string, functionName: string) {
}

// 创建函数
export async function createFunction() {
let subpackageName = args[0]
let functionName = args[1]
export async function createFunction(subpackageName?: string, functionName?: string) {
const { choices } = loadSubpackage()
if (!subpackageName || !functionName) {
// 无参调起交互式命令行
Expand All @@ -40,6 +36,10 @@ export async function createFunction() {
},
},
])
if (!response?.subpackage || !response.name) {
console.log('未能获取明确的包名和函数名,终止创建!')
process.exit(1)
}
subpackageName = response.subpackage.trim()
functionName = response.name.trim()
}
Expand All @@ -51,7 +51,8 @@ export async function createFunction() {

// 校验方法是否存在
if (functionDirNames.includes(functionName)) {
return console.error(`方法${functionName}已存在`)
console.log(`子包${subpackageName}中,方法${functionName}已存在,终止创建!`)
process.exit(1)
}

const spinners = ora()
Expand All @@ -62,5 +63,3 @@ export async function createFunction() {
writeSubpackageFunctionTest(subpackageName!, functionName)
spinners.succeed(`${spinnersPrefixText}创建[${packagePrefix}/${subpackageName}] => ${functionName}成功`)
}

await createFunction()
19 changes: 8 additions & 11 deletions scripts/create-subpackage.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import process from 'node:process'
import fs from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import process from 'node:process'
import ora from 'ora'
import MagicString from 'magic-string'
import prompts from '@posva/prompts'
import { loadFunction, loadSubpackage, packagePrefix, resolveRealPath, runCommand, sortJsonObject, spinnersPrefixText, toCamelCase, toLinesCase, writeSubpackageFunction, writeSubpackageFunctionTest } from './utils'

const [, , ...args] = process.argv

// 复制模板
function copyTemplate(templatePath: string, subpackagePath: string, functionName: string) {
const templateDirPath = resolveRealPath(templatePath)
Expand Down Expand Up @@ -90,10 +88,7 @@ function pnpmInstall() {
}

// 创建子包
export async function createSubpackage() {
let subpackageName = args[0]
let functionName = args[1]

export async function createSubpackage(subpackageName?: string, functionName?: string) {
if (!subpackageName || !functionName) {
// 无参调起交互式命令行
const response = await prompts([
Expand All @@ -120,6 +115,10 @@ export async function createSubpackage() {
},
},
])
if (!response?.subpackage || !response.function) {
console.log('未能获取明确的包名和函数名,终止创建!')
process.exit(1)
}
subpackageName = response.subpackage.trim()
functionName = response.function.trim()
}
Expand All @@ -133,14 +132,14 @@ export async function createSubpackage() {

// 校验子包是否存在
if (subpackageDirNames.includes(subpackageName)) {
return console.error(`子包${subpackageName}已存在`)
return console.log(`子包${subpackageName}已存在,终止创建!`)
}

const { dirNames: functionDirNames } = loadFunction(subpackageName)

// 校验方法是否存在
if (functionDirNames.includes(functionName)) {
return console.error(`方法${functionName}已存在`)
return console.log(`子包${subpackageName}中,方法${functionName}已存在,终止创建!`)
}

const spinners = ora()
Expand All @@ -167,5 +166,3 @@ export async function createSubpackage() {
spinners.fail(`${spinnersPrefixText}创建[${packagePrefix}/${subpackageName}]子包失败`)
})
}

await createSubpackage()
40 changes: 21 additions & 19 deletions scripts/create.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
import process from 'node:process'
import prompts from '@posva/prompts'
import { loadSubpackage, runCommand } from './utils'
import { loadSubpackage } from './utils'
import { createSubpackage } from './create-subpackage'
import { createFunction } from './create-function'

const [, , ...args] = process.argv

const choices = [
{ title: '创建子包', value: 'subpackage' },
{ title: '创建函数', value: 'function' },
]

const fnMap = {
subpackage: createSubpackage,
function: createFunction,
}

async function create() {
console.log('--------')
console.log(args.length, process.cwd())
if (args.length !== 2) {
const response = await prompts([
{
name: 'type',
type: 'select',
message: '请选择子包',
choices: [
{ title: '创建子包', value: 'subpackage' },
{ title: '创建函数', value: 'function' },
],
choices,
},
])
if (!response?.type) {
console.log('未能获取明确的创建类型,终止创建!')
process.exit(1)
}
const type: 'subpackage' | 'function' = response.type
runCommand(`pnpm run create:${type}`, process.cwd(), { silent: false })
await fnMap[type]()
}
else {
const subpackageName = args[0]
const functionName = args[1]

const { dirNames: subpackageDirNames } = loadSubpackage()

// 校验子包是否存在
if (!subpackageDirNames.includes(subpackageName)) {
// 不存在: 创建子包
runCommand(`pnpm run create:subpackage ${subpackageName} ${functionName}`, process.cwd(), { silent: false })
}
else {
// 存在: 创建函数
runCommand(`pnpm run create:function ${subpackageName} ${functionName}`, process.cwd(), { silent: false })
}
const type = !subpackageDirNames.includes(subpackageName) ? 'subpackage' : 'function'
await fnMap[type](subpackageName, functionName)
}
}

Expand Down

0 comments on commit 80ee16a

Please sign in to comment.