Skip to content

Commit 4755f3e

Browse files
committed
feat:update view basic process
1 parent 920dad4 commit 4755f3e

File tree

4 files changed

+146
-31
lines changed

4 files changed

+146
-31
lines changed

common/utils/etc/checkEnv.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* 检测当前的环境是 cli 还是 项目中
3+
* 通过meta信息中的url来判断
4+
* cli: file:///Users/mac-106/wh131462/workspace/leetcode-practice/pl-cli/.bin/lc.js
5+
* project: file:///Users/mac-106/wh131462/workspace/leetcode-practice/common/utils/etc/checkEnv.js
6+
*/
7+
export const currentEnv = ()=>{
8+
const url = import.meta.url;
9+
const projectReg = /etc\/checkEnv.js$/mi;
10+
return projectReg.test(url)?'project': 'cli';
11+
}

common/utils/etc/update.js

+25-17
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import https from "https";
22
import {rootPath} from "#common/utils/file/getRootPath.js";
33
import fs from "fs";
44
import path from "path";
5-
import { log } from "console";
6-
75
const npmUrl = `https://registry.npmjs.org/leetcode-practice`;
8-
const githubUrl = `https://api.github.com/repos/wh131462/leetcode-practice/commits?per_page=1`;
6+
const githubUrl = `https://raw.githubusercontent.com/wh131462/leetcode-practice/master/package.json`;
97
/**
108
* 获取远端npm库中的版本号
119
*/
12-
export const getRemoteVersion = ()=>{
10+
export const getNpmVersion = ()=>{
1311
return new Promise((resolve, reject) => {
12+
console.log("开始获取npm仓库中的版本号...")
1413
https.get(npmUrl, (res) => {
1514
let data = '';
1615
res.on('data', (chunk) => {
@@ -20,12 +19,15 @@ export const getRemoteVersion = ()=>{
2019
try {
2120
const packageInfo = JSON.parse(data);
2221
const latestVersion = packageInfo['dist-tags'].latest;
22+
console.log("npm仓库中的版本号获取成功!")
2323
resolve(latestVersion);
2424
} catch (error) {
25+
console.log("npm仓库中的版本号获取失败!")
2526
reject(error);
2627
}
2728
});
2829
}).on('error', (error) => {
30+
console.log("npm仓库中的版本号获取失败!")
2931
reject(error);
3032
});
3133
});
@@ -36,47 +38,53 @@ export const getRemoteVersion = ()=>{
3638
*/
3739
export const getGithubVersion = ()=>{
3840
return new Promise((resolve, reject) => {
39-
https.get(githubUrl,{
40-
headers: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
41-
},(res) => {
41+
console.log("开始获取github仓库的版本号...")
42+
https.get(githubUrl,(res) => {
4243
let data = '';
4344
res.on('data', (chunk) => {
4445
data += chunk;
4546
});
4647
res.on('end', () => {
4748
try {
48-
console.log(data);
4949
const jsonData = JSON.parse(data);
50-
const latestCommitSha = jsonData[0].sha;
51-
console.log(latestCommitSha)
52-
resolve(latestCommitSha);
50+
console.log("github仓库的版本号获取成功!")
51+
resolve(jsonData.version);
5352
} catch (error) {
53+
console.log("github仓库的版本号获取失败!")
5454
reject(error);
5555
}
5656
});
5757
}).on('error', (error) => {
58+
console.log("github仓库的版本号获取失败!")
5859
reject(error);
5960
});
6061
});
6162
}
6263
export const getLocalVersion = ()=>{
64+
console.log("开始获取本地版本号...")
6365
try{
6466
const {version} = JSON.parse(fs.readFileSync(path.resolve(rootPath,'package.json'),"utf-8"))
67+
console.log("本地版本号获取成功!")
6568
return version;
6669
}
6770
catch (e){
71+
console.log("本地版本号获取失败!")
6872
return false;
6973
}
7074
}
71-
75+
/**
76+
* 检测整体的更新状况
77+
* @returns {Promise<{localVersion: (any|boolean), githubVersion: *, isCliUpdate: boolean, remoteVersion: unknown, isGithubUpdate: boolean}>}
78+
*/
7279
export const checkUpdate = async ()=>{
73-
const remote = await getRemoteVersion();
74-
const local = getLocalVersion();
80+
const remote = await getNpmVersion();
7581
const github = await getGithubVersion()
76-
console.log(github)
82+
const local = getLocalVersion();
7783
return {
7884
localVersion: local,
79-
remoteVersion: remote,
80-
isUpdate : remote !== local
85+
npmVersion: remote,
86+
githubVersion: github,
87+
isCliUpdate : remote !== local,
88+
isGithubUpdate: github !== local
8189
};
8290
}

common/utils/etc/updateByEnv.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {exec} from "child_process";
2+
3+
/**
4+
* 根据环境进行更新
5+
* @param {"cli"|"project"} env
6+
*/
7+
export const updateByEnv = async (env)=>{
8+
switch (env){
9+
case "cli":
10+
// 执行CLI更新逻辑
11+
return await updateCli();
12+
case "project":
13+
// 执行项目更新逻辑
14+
return await updateProject();
15+
default:
16+
throw new Error(`Unsupported environment: ${env}`);
17+
}
18+
}
19+
/**
20+
* 更新CLI
21+
* @returns {Promise<void>}
22+
*/
23+
export const updateCli = ()=>{
24+
return new Promise((resolve,reject)=>{
25+
exec(`npm install -g leetcode-practice`, (err, stdout, stderr) => {
26+
if (err) {
27+
reject(err);
28+
} else {
29+
resolve();
30+
}
31+
});
32+
})
33+
}
34+
/**
35+
* 更新项目
36+
* @returns {Promise<void>}
37+
*/
38+
export const updateProject = async ()=>{
39+
// todo 更新项目
40+
// 0. 询问是否存在自己修改过的内容
41+
// 0.1 如果有过更改,终止更新脚本,并给出提示建议
42+
// 0.2 如果没有更改,进行更新操作
43+
// 1. 检测当前的git工作区
44+
// 1.1 如果有内容,提示提交,终止更新
45+
// 1.2 如果空,可以进行更新
46+
// 2. 通过raw.github.com下载所有的文件并对应更新文件(除package.json)
47+
// 2.0 所有的文件在更新后都进行git add [file-path]
48+
// 2.1 如果文件不存在,则创建文件
49+
// 2.2 如果文件存在,则覆盖文件
50+
// 2.3 更新package.json文件中的版本号为最新的版本号.
51+
// 3. 创建一个git的commit update:Version [版本号]
52+
// 4. 完成更新
53+
}

common/view/update.view.js

+57-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,65 @@
11
import {checkUpdate, getGithubVersion} from "#common/utils/etc/update.js";
22
import inquirer from "inquirer";
3+
import {currentEnv} from "#common/utils/etc/checkEnv.js";
4+
import {updateByEnv} from "#common/utils/etc/updateByEnv.js";
35

4-
export const easyUpdateView = async()=>{
5-
const checkQuestion = {
6-
type:"confirm",
7-
name:"willUpdate",
8-
message:"检测到更新,是否进行更新?"
6+
export const easyUpdateView = async () => {
7+
// 1. 询问当前的环境是啥 (自动检测一次)
8+
// 检测到的当前环境
9+
const env = currentEnv();
10+
const envQuestion = {
11+
type: "list",
12+
name: "choseEnv",
13+
message: `自动检测到的环境为[ ${env} ],如果不是,请进行选择,如是,请按下回车确认.`,
14+
choices: ["cli", "project"],
15+
default: env
916
};
10-
const {localVersion,remoteVersion,isUpdate} = {};
11-
console.log(`当前最新版本为: ${remoteVersion}`);
12-
console.log(`本地版本为: ${localVersion}`);
13-
if(isUpdate){
14-
const {willUpdate} = await inquirer.prompt(checkQuestion,null);
15-
if(willUpdate){
16-
console.log("确认更新!")
17-
}else{
18-
console.log("跳过此次更新!")
17+
const {choseEnv} = await inquirer.prompt(envQuestion, null);
18+
// 2. 检测版本更新
19+
const {
20+
localVersion,
21+
npmVersion,
22+
githubVersion,
23+
isCliUpdate,
24+
isGithubUpdate
25+
} = await checkUpdate();
26+
console.log(`当前版本:[ ${localVersion} ] npm包最新版本:[ ${npmVersion} ] github版本:[ ${githubVersion} ]`)
27+
let isUpdate = false;
28+
let version = "未知";
29+
switch (choseEnv) {
30+
case "project":
31+
isUpdate = isGithubUpdate;
32+
version = githubVersion;
33+
break;
34+
case "cli":
35+
isUpdate = isCliUpdate;
36+
version = remoteVersion;
37+
break;
38+
default:
39+
console.log("未知环境:", choseEnv)
40+
process.exit(0);
41+
break;
42+
}
43+
// 3. 询问是否更新
44+
if (isUpdate) {
45+
const checkQuestion = {
46+
type: "confirm",
47+
name: "willUpdate",
48+
message: `检测到[ ${env} ]可更新版本[ ${version} ],是否进行更新?`
49+
};
50+
const {willUpdate} = await inquirer.prompt(checkQuestion, null);
51+
if (willUpdate) {
52+
// 4.1 选择更新
53+
console.log("开始更新...");
54+
const result = await updateByEnv(env);
55+
} else {
56+
// 4.2 取消更新
57+
console.log("你选择跳过此次更新,如果想要进行更新,随时可以使用参数 -u 进行更新检测!祝你使用愉快~");
1958
}
59+
process.exit(0);
60+
} else {
61+
console.log("当前已是最新版本!祝你使用愉快~")
62+
process.exit(0);
2063
}
2164
}
2265

0 commit comments

Comments
 (0)