Skip to content

Commit a879d29

Browse files
committed
feat: update project using github
1 parent 4755f3e commit a879d29

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

common/utils/etc/updateByEnv.js

+69
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
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";
26

37
/**
48
* 根据环境进行更新
@@ -50,4 +54,69 @@ export const updateProject = async ()=>{
5054
// 2.3 更新package.json文件中的版本号为最新的版本号.
5155
// 3. 创建一个git的commit update:Version [版本号]
5256
// 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+
}
53122
}

0 commit comments

Comments
 (0)