Skip to content

Commit c297938

Browse files
committed
chore: 优化文件结构,新增 try-flatten 依赖
1 parent d5d1e8b commit c297938

File tree

10 files changed

+50
-108
lines changed

10 files changed

+50
-108
lines changed

src/commands.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import chalk from 'chalk';
2-
import path from 'path';
2+
import * as path from 'path';
33
import * as process from 'process';
4+
import { normalizeError, tryFlatten } from 'try-flatten';
45
import { defineConfig } from './configure';
56
import { generate } from './generator';
67
import { UserConfig } from './types';
7-
import { isFile, isString, isUrl, normalizeError, syncPromise, tryCatch } from './utils';
8+
import { isFile } from './utils/fs2';
9+
import { isString, isUrl } from './utils/type-is';
810

911
interface StartConfig {
1012
// 指定配置文件绝对路径
@@ -41,7 +43,7 @@ export async function start(startConfig?: StartConfig) {
4143
}
4244

4345
// eslint-disable-next-line @typescript-eslint/no-var-requires
44-
const [err, config] = await tryCatch<UserConfig>(syncPromise(() => require(configPath)));
46+
const [err, config] = tryFlatten(() => require(configPath) as UserConfig | undefined);
4547

4648
if (err) {
4749
throw new Error(err.message);

src/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import path from 'path';
1+
import * as path from 'path';
22

33
export const pkgName = process.env.PKG_NAME!;
44
export const pkgVersion = process.env.PKG_VERSION!;

src/generator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import fs from 'fs/promises';
2-
import path from 'path';
1+
import * as fs from 'fs/promises';
2+
import * as path from 'path';
33
import { generateApi, GenerateApiParams } from 'swagger-typescript-api';
44
import { axiosImportDefault, helpersImport, templatesDir } from './const';
55
import { Generated, GeneratedCallback, OpenapiConfig, StrictConfig } from './types';
6-
import { isBoolean, isString, isUrl } from './utils';
6+
import { isBoolean, isString, isUrl } from './utils/type-is';
77

88
export function generateParams(openapiConfig: OpenapiConfig, config: StrictConfig): GenerateApiParams {
99
const { name, schema, unwrapResponseData: unwrapResponseDataScope } = openapiConfig;

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ContentKind } from './types';
2-
import { isBoolean, isDate, isNumber, isObject, isString } from './utils';
2+
import { isBoolean, isDate, isNumber, isObject, isString } from './utils/type-is';
33

44
/**
55
* 格式化请求头

src/utils.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/utils/fs2.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs from 'fs/promises';
2+
3+
export async function isFile(p: string) {
4+
try {
5+
const state = await fs.stat(p);
6+
return state.isFile();
7+
} catch (err) {
8+
return false;
9+
}
10+
}
11+
12+
export async function cleanDir(p: string) {
13+
await fs.rm(p, { recursive: true, force: true });
14+
}

src/utils/type-is.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function isString(any: unknown): any is string {
2+
return typeof any === 'string';
3+
}
4+
5+
export function isBoolean(any: unknown): any is boolean {
6+
return typeof any === 'boolean';
7+
}
8+
9+
export function isNumber(any: unknown): any is number {
10+
return typeof any === 'number';
11+
}
12+
13+
export function isObject(any: unknown): any is object {
14+
return typeof any === 'object' && any !== null;
15+
}
16+
17+
export function isDate(any: unknown): any is Date {
18+
return Boolean(any && any instanceof Date);
19+
}
20+
21+
export function isUrl(any: string): boolean {
22+
return /^https:\/\//i.test(any);
23+
}

test/commands.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from 'path';
22
import { start } from '../src';
33
import { test } from 'vitest';
4-
import { cleanDir } from '../src/utils';
4+
5+
import { cleanDir } from '../src/utils/fs2';
56

67
test('start', async () => {
78
await start({

test/generator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
2-
import { cleanDir, isFile } from 'src/utils';
32
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
43
import { generate, Generated, GenerateInfo, generateItem, OpenapiSpec, StrictConfig } from '../src';
4+
import { cleanDir, isFile } from '../src/utils/fs2';
55
import petstore3 from './petstore3.json';
66

77
function randomString(): string {

test/utils.test.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)