-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdateByEnv.js
121 lines (110 loc) · 3.72 KB
/
updateByEnv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { exec } from 'node:child_process'
import https from 'node:https'
import fs from 'node:fs'
import inquirer from 'inquirer'
import { getGithubVersion } from '#common/utils/update/update.js'
/**
* 根据环境进行更新
* @param {"cli"|"project"} env
*/
export async function updateByEnv(env) {
switch (env) {
case 'cli':
// 执行CLI更新逻辑
return await updateCli()
case 'project':
// 执行项目更新逻辑
return await updateProject()
default:
throw new Error(`Unsupported environment: ${env}`)
}
}
/**
* 更新CLI
* @returns {Promise<void>}
*/
export function updateCli() {
return new Promise((resolve, reject) => {
exec(`npm install -g leetcode-practice`, (err) => {
if (err)
reject(err)
else resolve()
})
})
}
/**
* 更新项目
* @returns {Promise<void>}
*/
export async function updateProject() {
// todo 更新项目
// 0. 询问是否存在自己修改过的内容
// 0.1 如果有过更改,终止更新脚本,并给出提示建议
// 0.2 如果没有更改,进行更新操作
// 1. 检测当前的git工作区
// 1.1 如果有内容,提示提交,终止更新
// 1.2 如果空,可以进行更新
// 2. 通过raw.github.com下载所有的文件并对应更新文件(除package.json)
// 2.0 所有的文件在更新后都进行git add [file-path]
// 2.1 如果文件不存在,则创建文件
// 2.2 如果文件存在,则覆盖文件
// 2.3 更新package.json文件中的版本号为最新的版本号.
// 3. 创建一个git的commit update:Version [版本号]
// 4. 完成更新
async function execCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout) => {
if (error)
reject(error)
else resolve(stdout.trim())
})
})
}
try {
const ltsVersion = await getGithubVersion()
// 0. 询问是否存在自己修改过的内容
const { hasChanges } = await inquirer.prompt([
{
type: 'confirm',
name: 'hasChanges',
message: '是否存在自己修改过的内容?',
default: false,
},
])
// 0.1 如果有过更改,终止更新脚本,并给出提示建议
if (hasChanges) {
console.log('您有未提交的更改,请先处理后再更新。')
return
}
// 1. 检测当前的git工作区
const gitStatus = await execCommand('git status --porcelain')
// 1.1 如果有内容,提示提交,终止更新
if (gitStatus) {
console.log('您有未提交的更改,请先提交后再更新。')
return
}
// 2. 下载所有的文件并对应更新文件(除package.json)
const response = await https.get(
'https://raw.githubusercontent.com/wh131462/leetcode-practice',
)
const filesToUpdate = response.data
// 2.0 所有的文件在更新后都进行git add [file-path]
for (const filePath in filesToUpdate) {
fs.writeFileSync(filePath, filesToUpdate[filePath])
// 2.1 如果文件不存在,则创建文件
// 2.2 如果文件存在,则覆盖文件
await execCommand(`git add ${filePath}`)
}
// 2.3 更新package.json文件中的版本号为最新的版本号
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8'))
packageJson.version = `${ltsVersion}` // 这里替换为实际的最新版本号
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2))
// 3. 创建一个git的commit update:Version [版本号]
await execCommand(`git commit -m "update:${ltsVersion}"`) // 这里替换为实际的最新版本号
// 4. 完成更新
console.log('项目更新完成。')
}
catch (error) {
console.error('更新过程中出现错误:', error)
}
}