Skip to content

Commit

Permalink
feat: 新增配置项 noUpdateTimeComment 是否不生成接口的更新时间注释
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Feb 1, 2021
1 parent fcdfc69 commit 72c8afa
Show file tree
Hide file tree
Showing 4 changed files with 2,443 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,13 @@ export class Generator {
const interfaceTitle = `[${escapedTitle}↗](${syntheticalConfig.serverUrl}/project/${extendedInterfaceInfo.project_id}/interface/api/${extendedInterfaceInfo._id})`

// 接口摘要
const interfaceSummary: Array<{
label: string
value: string | string[]
}> = [
const interfaceSummary: Array<
| false
| {
label: string
value: string | string[]
}
> = [
{
label: '分类',
value: `[${extendedInterfaceInfo._category.name}↗](${syntheticalConfig.serverUrl}/project/${extendedInterfaceInfo.project_id}/interface/api/cat_${extendedInterfaceInfo.catid})`,
Expand All @@ -758,7 +761,7 @@ export class Generator {
extendedInterfaceInfo.path
}\``,
},
{
!syntheticalConfig.noUpdateTimeComment && {
label: '更新时间',
value: process.env.JEST_WORKER_ID // 测试时使用 unix 时间戳
? String(extendedInterfaceInfo.up_time)
Expand All @@ -769,8 +772,11 @@ export class Generator {
},
]
const interfaceExtraComments: string = interfaceSummary
.filter(item => !isEmpty(item.value))
.map(item => `* @${item.label} ${castArray(item.value).join(', ')}`)
.filter(item => typeof item !== 'boolean' && !isEmpty(item.value))
.map(item => {
const _item: Exclude<typeof interfaceSummary[0], boolean> = item as any
return `* @${_item.label} ${castArray(_item.value).join(', ')}`
})
.join('\n')

return dedent`
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ export interface SharedConfig {
*/
jsonSchema?: JsonSchemaConfig

/**
* 是否不生成接口的更新时间注释。
*/
noUpdateTimeComment?: boolean

/**
* 预处理接口信息,返回新的接口信息。可返回 false 排除当前接口。
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const generatorFactory = ({
token = 'hello',
jsonSchema,
onlyMatchPath,
noUpdateTimeComment,
}: {
id: OneOrMany<0 | 82 | 87 | 151 | -82 | -87 | -151>
typesOnly?: boolean
Expand All @@ -26,6 +27,7 @@ const generatorFactory = ({
token?: string | string[]
jsonSchema?: ServerConfig['jsonSchema']
onlyMatchPath?: RegExp
noUpdateTimeComment?: ServerConfig['noUpdateTimeComment']
}) => {
const apiDir = tempy.directory()
return new Generator({
Expand All @@ -39,6 +41,7 @@ const generatorFactory = ({
enabled: enableReactHooks,
},
jsonSchema: jsonSchema,
noUpdateTimeComment: noUpdateTimeComment,
projects: [
{
token: token,
Expand Down Expand Up @@ -380,4 +383,23 @@ describe('Generator', () => {
)
})
})

test('无更新时间注释', async () => {
const generator = generatorFactory({
id: 82,
noUpdateTimeComment: true,
})
await generator.prepare()
const output = await generator.generate()
forOwn(output, ({ content }) => {
expect(content).toMatchSnapshot('输出内容')
})

await generator.write(output)
forOwn(output, (_, outputFilePath) => {
expect(fs.readFileSync(outputFilePath).toString()).toMatchSnapshot(
'接口文件',
)
})
})
})
Loading

0 comments on commit 72c8afa

Please sign in to comment.