@@ -23,9 +23,8 @@ const CONFIG: Config = {
2323 MAX_ROWS : parseInt ( process . env . MAX_ROWS || '0' ) ,
2424} ;
2525
26- // Main function
27- async function main ( ) : Promise < void > {
28- let logger : ImportLogger | null = null ;
26+ async function main ( ) {
27+ const logger = new ImportLogger ( ) ;
2928
3029 // Handle process signals to ensure logs are saved on forced exit
3130 const handleExit = ( signal : string ) => {
@@ -36,7 +35,6 @@ async function main(): Promise<void> {
3635 }
3736 process . exit ( 0 ) ;
3837 } ;
39-
4038 process . on ( 'SIGINT' , ( ) => handleExit ( 'SIGINT' ) ) ;
4139 process . on ( 'SIGTERM' , ( ) => handleExit ( 'SIGTERM' ) ) ;
4240 process . on ( 'SIGQUIT' , ( ) => handleExit ( 'SIGQUIT' ) ) ;
@@ -45,18 +43,11 @@ async function main(): Promise<void> {
4543 console . log ( '=== Strapi 数据导入工具 ===\n' ) ;
4644
4745 // Validate configuration
48- if ( ! CONFIG . STRAPI_TOKEN && ! CONFIG . DRY_RUN ) {
46+ if ( ! CONFIG . STRAPI_TOKEN && ! CONFIG . DRY_RUN )
4947 throw new Error ( '请设置 STRAPI_TOKEN 环境变量或使用 DRY_RUN=true' ) ;
50- }
51-
52- if ( CONFIG . DRY_RUN ) {
53- console . log ( '🔥 DRY RUN 模式 - 不会实际创建数据\n' ) ;
54- }
5548
56- // Initialize logger
57- logger = new ImportLogger ( ) ;
49+ if ( CONFIG . DRY_RUN ) console . log ( '🔥 DRY RUN 模式 - 不会实际创建数据\n' ) ;
5850
59- // Create migrator instance
6051 const migrator = new RestMigrator (
6152 ( ) => ExcelReader . readExcelFile ( CONFIG . EXCEL_FILE , CONFIG . SHEET_NAME ) ,
6253 TargetOrganizationModel ,
@@ -70,14 +61,12 @@ async function main(): Promise<void> {
7061 for await ( const organization of migrator . boot ( {
7162 dryRun : CONFIG . DRY_RUN ,
7263 } ) )
73- count ++ ;
64+ if ( ++ count === CONFIG . MAX_ROWS && CONFIG . MAX_ROWS > 0 ) break ;
7465
75- // Print final statistics
7666 logger . printStats ( ) ;
7767
78- console . log ( '\n导入完成 !' ) ;
68+ console . log ( '导入完成 !' ) ;
7969
80- // Save logs to files
8170 await logger . saveToFiles ( ) ;
8271 } catch ( error : any ) {
8372 console . error ( '导入失败:' , error . message ) ;
@@ -89,8 +78,7 @@ async function main(): Promise<void> {
8978 }
9079}
9180
92- // Handle command line arguments
93- function parseArgs ( ) : void {
81+ function parseArgs ( ) {
9482 const args = process . argv . slice ( 2 ) ;
9583
9684 if ( args . includes ( '--help' ) || args . includes ( '-h' ) ) {
@@ -116,24 +104,25 @@ Strapi 数据导入工具
116104 VERBOSE_LOGGING 详细日志 (true/false, 默认: false)
117105
118106示例:
119- # 基本使用
120- STRAPI_TOKEN=your_token tsx scripts/import-data.ts
107+ # 正常导入
108+ STRAPI_TOKEN=your_token tsx import-data.ts
109+
110+ # 模拟导入
111+ DRY_RUN=true tsx import-data.ts
121112
122- # 指定工作表
123- SHEET_NAME="甘肃省" STRAPI_TOKEN=your_token tsx scripts/ import-data.ts
113+ # 导入指定工作表
114+ SHEET_NAME="甘肃省" STRAPI_TOKEN=your_token tsx import-data.ts
124115
125116 # 仅测试前10行
126- MAX_ROWS=10 DRY_RUN=true tsx scripts/ import-data.ts
117+ MAX_ROWS=10 DRY_RUN=true tsx import-data.ts
127118
128119 # 设置详细日志
129- VERBOSE_LOGGING=true STRAPI_TOKEN=your_token tsx scripts/ import-data.ts
120+ VERBOSE_LOGGING=true STRAPI_TOKEN=your_token tsx import-data.ts
130121` ) ;
131122 process . exit ( 0 ) ;
132123 }
133124
134- if ( args . includes ( '--dry-run' ) || args . includes ( '-d' ) ) {
135- CONFIG . DRY_RUN = true ;
136- }
125+ if ( args . includes ( '--dry-run' ) || args . includes ( '-d' ) ) CONFIG . DRY_RUN = true ;
137126}
138127
139128parseArgs ( ) ;
0 commit comments