1
1
import { exec } from "child_process" ;
2
+ import inquirer from "inquirer" ;
3
+ import https from "https" ;
4
+ import fs from "fs" ;
5
+ import { getGithubVersion } from "#common/utils/etc/update.js" ;
2
6
3
7
/**
4
8
* 根据环境进行更新
@@ -50,4 +54,69 @@ export const updateProject = async ()=>{
50
54
// 2.3 更新package.json文件中的版本号为最新的版本号.
51
55
// 3. 创建一个git的commit update:Version [版本号]
52
56
// 4. 完成更新
57
+ async function execCommand ( command ) {
58
+ return new Promise ( ( resolve , reject ) => {
59
+ exec ( command , ( error , stdout , stderr ) => {
60
+ if ( error ) {
61
+ reject ( error ) ;
62
+ } else {
63
+ resolve ( stdout . trim ( ) ) ;
64
+ }
65
+ } ) ;
66
+ } ) ;
67
+ }
68
+
69
+ try {
70
+ const ltsVersion = await getGithubVersion ( ) ;
71
+ // 0. 询问是否存在自己修改过的内容
72
+ const { hasChanges } = await inquirer . prompt ( [
73
+ {
74
+ type : 'confirm' ,
75
+ name : 'hasChanges' ,
76
+ message : '是否存在自己修改过的内容?' ,
77
+ default : false
78
+ }
79
+ ] ) ;
80
+
81
+ // 0.1 如果有过更改,终止更新脚本,并给出提示建议
82
+ if ( hasChanges ) {
83
+ console . log ( '您有未提交的更改,请先处理后再更新。' ) ;
84
+ return ;
85
+ }
86
+
87
+ // 1. 检测当前的git工作区
88
+ const gitStatus = await execCommand ( 'git status --porcelain' ) ;
89
+
90
+ // 1.1 如果有内容,提示提交,终止更新
91
+ if ( gitStatus ) {
92
+ console . log ( '您有未提交的更改,请先提交后再更新。' ) ;
93
+ return ;
94
+ }
95
+
96
+ // 2. 下载所有的文件并对应更新文件(除package.json)
97
+ const response = await https . get ( 'https://raw.githubusercontent.com/wh131462/leetcode-practice' ) ;
98
+ const filesToUpdate = response . data ;
99
+
100
+ // 2.0 所有的文件在更新后都进行git add [file-path]
101
+ for ( const filePath in filesToUpdate ) {
102
+ fs . writeFileSync ( filePath , filesToUpdate [ filePath ] ) ;
103
+
104
+ // 2.1 如果文件不存在,则创建文件
105
+ // 2.2 如果文件存在,则覆盖文件
106
+ await execCommand ( `git add ${ filePath } ` ) ;
107
+ }
108
+
109
+ // 2.3 更新package.json文件中的版本号为最新的版本号
110
+ const packageJson = JSON . parse ( fs . readFileSync ( 'package.json' , 'utf-8' ) ) ;
111
+ packageJson . version = `${ ltsVersion } ` ; // 这里替换为实际的最新版本号
112
+ fs . writeFileSync ( 'package.json' , JSON . stringify ( packageJson , null , 2 ) ) ;
113
+
114
+ // 3. 创建一个git的commit update:Version [版本号]
115
+ await execCommand ( `git commit -m "update:${ ltsVersion } "` ) ; // 这里替换为实际的最新版本号
116
+
117
+ // 4. 完成更新
118
+ console . log ( '项目更新完成。' ) ;
119
+ } catch ( error ) {
120
+ console . error ( '更新过程中出现错误:' , error ) ;
121
+ }
53
122
}
0 commit comments