Skip to content

Commit

Permalink
feat(create): modify ts config
Browse files Browse the repository at this point in the history
  • Loading branch information
liquan committed Sep 9, 2024
1 parent 87afc61 commit 14def86
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 88 deletions.
8 changes: 4 additions & 4 deletions packages/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
"@eljs/service": "0.24.0",
"@eljs/utils": "0.24.0",
"@types/glob": "^7.2.0",
"@types/prettier": "^2.7.0",
"dayjs": "^1.11.5",
"@types/prettier": "^3.0.0",
"dayjs": "^1.11.12",
"glob": "^8.0.3",
"prettier": "^2.7.1",
"sort-package-json": "^1.57.0"
"prettier": "^3.3.3",
"sort-package-json": "^2.10.0"
}
}
1 change: 0 additions & 1 deletion packages/create/src/core/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export class Create {
projectName: name,
targetDir,
args: this._opts.args,
isGenSchema: this._opts.schema,
})

await generator.create(templatePath)
Expand Down
3 changes: 1 addition & 2 deletions packages/create/src/core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Generator {

public async create(templatePath?: string) {
assert(templatePath, 'templatePath 不允许为空')
const { isLocalTemplate, targetDir, projectName, isGenSchema } = this._opts
const { isLocalTemplate, targetDir, projectName } = this._opts

if (templatePath) {
if (isLocalTemplate) {
Expand All @@ -37,7 +37,6 @@ export class Generator {
const service = new GenerateService({
cwd: templatePath,
plugins: generatorFile ? [require.resolve(generatorFile)] : [],
isGenSchema,
env: process.env.NODE_ENV as Env,
})

Expand Down
35 changes: 14 additions & 21 deletions packages/create/src/core/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ServicePluginAPI,
} from '@eljs/service'
import type { PkgJSON, RenderTemplateOpts } from '@eljs/utils'
import { logger, prompts } from '@eljs/utils'
import { prompts } from '@eljs/utils'

import {
AppData,
Expand Down Expand Up @@ -45,6 +45,10 @@ export class GenerateService extends Service {
* 用户输入
*/
public prompts: Prompts = Object.create(null)
/**
* tsconfig 配置
*/
public tsconfig = Object.create(null)
/**
* 项目的 package.json 对象
*/
Expand Down Expand Up @@ -75,22 +79,17 @@ export class GenerateService extends Service {
args: { cwd: this.cwd },
})

// 是否生成 schema 用于 vscode 创建项目使用
if (this.opts.isGenSchema) {
await this.applyPlugins({
key: 'onGenerateSchema',
args: {
questions,
},
})
}

// 修改用户提示
this.prompts = await this.applyPlugins({
key: 'modifyPrompts',
initialValue: {},
args: { questions },
})

this.tsconfig = await this.applyPlugins({
key: 'modifyTSConfig',
initialValue: {},
})
}

protected async afterRun(): Promise<void> {
Expand All @@ -103,12 +102,6 @@ export class GenerateService extends Service {
},
})

// 生成 schema 不做 文件生成
if (this.opts.isGenSchema) {
logger.info('跳过文件生成, 仅生成 schema')
return
}

await this.applyPlugins({
key: 'onGenerateFiles',
args: {
Expand Down Expand Up @@ -177,13 +170,13 @@ export interface GenerateServicePluginAPI extends ServicePluginAPI {
{ questions: prompts.PromptObject[] }
>
/**
* 添加命令行问询
* 修改用户输入数据
*/
addQuestions: ApplyAdd<{ cwd: string }, prompts.PromptObject[]>
modifyTSConfig: ApplyModify<typeof GenerateService.prototype.tsconfig, null>
/**
* 生成 Schema 事件
* 添加命令行问询
*/
onGenerateSchema: ApplyEvent<{ questions: prompts.PromptObject[] }>
addQuestions: ApplyAdd<{ cwd: string }, prompts.PromptObject[]>
/**
* 生成文件之前事件
*/
Expand Down
16 changes: 7 additions & 9 deletions packages/create/src/internal/features/built-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
import { writeFileSync } from 'fs'
import { join } from 'path'
import prettier from 'prettier'
import sortPackageJson from 'sort-package-json'
import { Api, ExtendPackageOpts } from '../../types'

function formatPkgJSON(pkg: string) {
const sortPkg = sortPackageJson(pkg)
import type { Api, ExtendPackageOpts } from '../../types'

async function formatPkgJSON(pkgJSONPath: string) {
// esm 语法需要使用动态 import 引入
const { default: sortPackageJson } = await import('sort-package-json')
// function getPrettierConfig() {
// const prettierPath = tryPaths([
// `${target}/prettier.config.js`,
Expand Down Expand Up @@ -42,14 +41,13 @@ function formatPkgJSON(pkg: string) {
// }
// }

return prettier.format(sortPkg, {
return prettier.format(sortPackageJson(pkgJSONPath), {
tabWidth: 2,
parser: 'json',
})
}

export default (api: Api) => {
// #region 更新package.json 的内容
api.registerMethod({
name: 'extendPackage',
async fn(opts: ExtendPackageOpts) {
Expand All @@ -62,7 +60,7 @@ export default (api: Api) => {
api.register({
key: 'onGenerateDone',
stage: Number.NEGATIVE_INFINITY,
fn() {
async fn() {
const pkgJSONPath = join(api.paths.target, 'package.json')
let pkgJSON = api.service.pkgJSON

Expand All @@ -78,7 +76,7 @@ export default (api: Api) => {

writeFileSync(
pkgJSONPath,
formatPkgJSON(JSON.stringify(pkgJSON, null, 2)),
await formatPkgJSON(JSON.stringify(pkgJSON, null, 2)),
)
},
})
Expand Down
31 changes: 0 additions & 31 deletions packages/create/src/internal/features/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { chalk, prompts } from '@eljs/utils'
import { execSync } from 'child_process'
import dayjs from 'dayjs'
import { writeFileSync } from 'fs'
import { join } from 'path'
import { Api } from '../../types'
import { author, email, getGitHref, getGitUrl } from '../const'

Expand Down Expand Up @@ -58,33 +56,4 @@ export default (api: Api) => {
...memo,
}
})

api.onGenerateSchema(({ questions }) => {
writeFileSync(
join(api.cwd, 'schema.json'),
JSON.stringify(
questions.map(question => {
const copied: Record<string, any> = { ...question }
// 生成 Schema 时,mail 和 author 的配置项不需要默认值
if (copied.name === 'mail' || copied.name === 'author') {
delete copied.initial
}

copied.message = copied.message.replace(/\033\[[0-9;]*m/g, '')

// 增加 isRequired
if (copied.initial) {
copied.isRequired = true
}

// initial 换成 value
if (copied.type === 'select' && typeof copied.initial === 'number') {
copied.initial = copied.choices?.[copied.initial]?.value ?? ''
}

return copied
}),
),
)
})
}
4 changes: 2 additions & 2 deletions packages/create/src/internal/features/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { author, email, getGitUrl } from '../const'

export default async (api: Api) => {
api.describe({
key: 'defaultPrompts',
key: 'defaultQuestions',
enableBy() {
return api.pluginConfig.defaultPrompts === true
return api.pluginConfig.defaultQuestions === true
},
})

Expand Down
2 changes: 1 addition & 1 deletion packages/create/src/internal/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Api } from '../types'
export default (api: Api) => {
;[
'addQuestions',
'onGenerateSchema',
'modifyPrompts',
'modifyTSConfig',
'onBeforeGenerateFiles',
'onGenerateFiles',
'onGenerateDone',
Expand Down
16 changes: 10 additions & 6 deletions packages/create/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ export interface CreateOpts {
* 当前路径
*/
cwd?: string
/**
* 是否生成 schema
*/
schema?: boolean
/**
* 命令行参数
*/
Expand Down Expand Up @@ -106,7 +102,9 @@ export interface Prompts {
* 创建时使用的文件夹名称
*/
dirname: string

/**
* 扩展字段
*/
[property: string]: any
}

Expand All @@ -115,7 +113,7 @@ export interface GeneratePluginConfig extends PluginConfig {
*
* 是否启用默认问询
*/
defaultPrompts?: boolean
defaultQuestions?: boolean
/**
* 是否启用 git 初始化
*/
Expand Down Expand Up @@ -156,13 +154,19 @@ export interface CopyFileOpts {
opts?: RenderTemplateOpts
}

/**
* 拷贝文件选项
*/
export interface CopyTplOpts extends CopyFileOpts {
/**
* 模板渲染需要的参数
*/
data: Record<string, any>
}

/**
* 拷贝文件夹选项
*/
export interface CopyDirectoryOpts extends CopyFileOpts {
/**
* 模板渲染需要的参数
Expand Down
Loading

0 comments on commit 14def86

Please # to comment.