@@ -3,9 +3,11 @@ import type { IPluginContext } from "./plugins/typeDeclaration.ts";
33import Logs from "./console.ts" ;
44import { PluginManager } from "./plugins/index.ts" ;
55import { getApiPath , getDefinition } from "./core.ts" ;
6- import { createFile , emptyDirectory , readFile , removeFile } from "./utils.ts" ;
6+ import { createDiffFile , createFile , readFile , removeFile } from "./utils.ts" ;
77import { getT } from "./i18n/index.ts" ;
88
9+ const LOCK_FILE = ".stc.lock" ;
10+
911/**
1012 * 初始化插件管理器
1113 */
@@ -36,22 +38,49 @@ const createContext = (context: IPluginContext) => {
3638 * @param urlOrPath - 远程地址或本地
3739 * @returns
3840 */
39- const getData = async ( urlOrPath : string ) : Promise < ISwaggerResult > => {
40- if ( ! / ^ h t t p ( s ? ) : \/ \/ / . test ( urlOrPath ) ) {
41- const content = await readFile ( urlOrPath ) ;
42-
43- try {
44- return JSON . parse ( content ) as unknown as ISwaggerResult ;
45- } catch ( error ) {
46- throw new Error ( getT ( "$t(app.apiJsonFileError)" , { error } ) ) ;
47- }
48- }
41+ const getData = async (
42+ urlOrPath : string ,
43+ _outDir : string ,
44+ ) : Promise < ISwaggerResult > => {
45+ try {
46+ // const lockFile = await readFile(`${outDir}/${LOCK_FILE}`);
47+ let data : ISwaggerResult ;
4948
50- // 从远程地址获取 Swagger 数据
51- const res = await fetch ( urlOrPath ) ;
49+ // 从本地文件获取 Swagger 数据
50+ if ( ! / ^ h t t p ( s ? ) : \/ \/ / . test ( urlOrPath ) ) {
51+ const content = await readFile ( urlOrPath ) ;
5252
53- try {
54- const data = await res . json ( ) ;
53+ data = JSON . parse ( content ) as unknown as ISwaggerResult ;
54+ } else {
55+ // 从远程地址获取 Swagger 数据
56+ const res = await fetch ( urlOrPath ) ;
57+
58+ data = await res . json ( ) ;
59+ }
60+
61+ // 对比 path 和 definitions/schemas 数据是否有变化,有变化则使用新数据
62+ // if (lockFile) {
63+ // const oldData = JSON.parse(lockFile) as unknown as ISwaggerResult;
64+ // createFile(
65+ // `${outDir}/.stc_new.lock`,
66+ // JSON.stringify(data, null, 2),
67+ // {
68+ // banner: false,
69+ // },
70+ // );
71+ // const isChange = diff["diffLines"](
72+ // JSON.stringify(oldData, null, 2),
73+ // JSON.stringify(data, null, 2),
74+ // );
75+
76+ // createFile(
77+ // `${outDir}/.stc_diff.lock`,
78+ // JSON.stringify(isChange, null, 2),
79+ // {
80+ // banner: false,
81+ // },
82+ // );
83+ // }
5584
5685 return data ;
5786 } catch ( error ) {
@@ -67,7 +96,7 @@ export const start = async (options: DefaultConfigOptions): Promise<void> => {
6796 // 创建上下文
6897 const context = createContext ( { options } ) ;
6998
70- const data = await getData ( options . url ) ;
99+ const data = await getData ( options . url , options . outDir ) ;
71100
72101 // 触发插件 onload 事件
73102 context . onLoad ?.( data , context . options ) ;
@@ -81,12 +110,15 @@ export const start = async (options: DefaultConfigOptions): Promise<void> => {
81110 // 触发插件 onAction 事件
82111 context . onAction ?.( actionData , context . options ) ;
83112
84- if ( options . shared ) {
85- // 清空输出目录
86- await emptyDirectory ( options . outDir ) ;
87- } else {
113+ if ( options . clean ) {
114+ const exclude = [ `${ options . outDir } /${ LOCK_FILE } ` ] ;
115+
116+ if ( ! options . shared ) {
117+ exclude . push ( `${ options . outDir } /shared/**/*` ) ;
118+ }
119+
88120 await removeFile ( `${ options . outDir } /**/*.*` , {
89- exclude : [ ` ${ options . outDir } /shared/**/*` ] ,
121+ exclude,
90122 } ) ;
91123 }
92124
@@ -99,22 +131,24 @@ export const start = async (options: DefaultConfigOptions): Promise<void> => {
99131
100132 // 写入类型定义文件
101133 if ( transformData ?. definition ?. content ) {
102- createFile (
103- ` ${ options . outDir } / ${ transformData . definition . filename } ` ,
104- transformData . definition . content ,
105- ) ;
134+ const name = ` ${ options . outDir } / ${ transformData . definition . filename } ` ;
135+ const content = transformData . definition . content ;
136+
137+ createDiffFile ( name , content , options . clean ) ;
106138 }
107139
108140 // 写入 API 文件
109141 if ( transformData ?. action ) {
110142 transformData . action . forEach ( ( content , filename ) => {
111- createFile (
112- `${ options . outDir } /${ filename } ` ,
113- content ,
114- ) ;
143+ createDiffFile ( `${ options . outDir } /${ filename } ` , content , options . clean ) ;
115144 } ) ;
116145 }
117146
147+ // 保存数据
148+ createFile ( `${ options . outDir } /${ LOCK_FILE } ` , JSON . stringify ( data , null , 2 ) , {
149+ banner : false ,
150+ } ) ;
151+
118152 console . log ( "\n" ) ;
119153 Logs . success ( `${ getT ( "$t(app.generateFileDone)" ) } \n\t${ options . outDir } \n` ) ;
120154 // 触发插件 onEnd 事件
0 commit comments