Skip to content

Commit 2184f59

Browse files
committed
chore:update modify
1 parent 25c83a0 commit 2184f59

19 files changed

+100
-2099
lines changed

common/constants/question.const.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,18 @@
22
export const DefaultLang = "javascript";
33

44
// 默认版本号
5-
export const DefaultVer = "0.0.0";
5+
export const DefaultVer = "0.0.0";
6+
7+
// 包名
8+
export const PackageName = "leetcode-practice";
9+
// github 主账号
10+
export const GITHUB_HOST = "wh131462";
11+
// region 域名列表
12+
// npm 主域名
13+
export const NPM_URL = "https://registry.npmjs.org/";
14+
// github raw 主域名
15+
export const GITHUB_RAW = "https://raw.githubusercontent.com/";
16+
// github
17+
export const GITHUB_URL = "https://github.com/";
18+
19+
//endregion

common/utils/etc/update.js

-90
This file was deleted.

common/utils/http/fetch_.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* 基础请求
2+
* 基础请求-直接返回JSON格式的值
33
* @param url
44
* @param options
55
* @returns {Promise<any>}

common/utils/http/urlJoin.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* 拼接域名和地址
3+
* @param host
4+
* @param rest
5+
* @returns {string}
6+
*/
7+
export const url_join = (host,...rest)=>{
8+
const host_base = host.replace(/\/$/,"");
9+
const path = rest.join('/').replace(/(\/){1,3}/gmi,'/').replace(/^\//,'');
10+
return [host_base,path].join("/");
11+
}

common/utils/update/update.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import https from "https";
2+
import {rootPath} from "#common/utils/file/getRootPath.js";
3+
import fs from "fs";
4+
import path from "path";
5+
import {GITHUB_HOST, GITHUB_RAW, NPM_URL, PackageName} from "#common/constants/question.const.js";
6+
import {url_join} from "#common/utils/http/urlJoin.js";
7+
import {fetch_} from "#common/utils/http/fetch_.js";
8+
// npm 中的 包地址
9+
const npmUrl = url_join(NPM_URL,PackageName);
10+
const githubUrl = url_join(GITHUB_RAW,GITHUB_HOST,PackageName,"master/package.json");
11+
/**
12+
* 获取远端npm库中的版本号
13+
*/
14+
export const getNpmVersion = async ()=>{
15+
try{
16+
const res = await fetch_(npmUrl,{method:"GET"});
17+
console.log("获取NPM版本成功!======",res['dist-tags']?.latest)
18+
return res['dist-tags']?.latest;
19+
}
20+
catch(e){
21+
console.log("获取NPM版本失败!")
22+
throw new Error(e);
23+
}
24+
}
25+
/**
26+
* 获取github的最新提交sha
27+
* @returns {Promise<unknown>}
28+
*/
29+
export const getGithubVersion = async ()=>{
30+
try{
31+
const res = await fetch_(githubUrl,{method:"GET"});
32+
console.log("获取Github版本成功!======",res?.version)
33+
return res?.version;
34+
}
35+
catch(e){
36+
console.log("获取Github版本失败!",e)
37+
throw new Error(e);
38+
}
39+
}
40+
export const getLocalVersion = ()=>{
41+
console.log("开始获取本地版本号...")
42+
try{
43+
const {version} = JSON.parse(fs.readFileSync(path.resolve(rootPath,'package.json'),"utf-8"))
44+
console.log("本地版本号获取成功!")
45+
return version;
46+
}
47+
catch (e){
48+
console.log("本地版本号获取失败!")
49+
return false;
50+
}
51+
}
52+
/**
53+
* 检测整体的更新状况
54+
* @returns {Promise<{localVersion: (any|boolean), githubVersion: *, isCliUpdate: boolean, remoteVersion: unknown, isGithubUpdate: boolean}>}
55+
*/
56+
export const checkUpdate = async ()=>{
57+
const remote = await getNpmVersion();
58+
const github = await getGithubVersion()
59+
const local = getLocalVersion();
60+
return {
61+
localVersion: local,
62+
npmVersion: remote,
63+
githubVersion: github,
64+
isCliUpdate : remote !== local,
65+
isGithubUpdate: github !== local
66+
};
67+
}

common/utils/etc/updateByEnv.js common/utils/update/updateByEnv.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {exec} from "child_process";
22
import inquirer from "inquirer";
33
import https from "https";
44
import fs from "fs";
5-
import {getGithubVersion} from "#common/utils/etc/update.js";
5+
import {getGithubVersion} from "#common/utils/update/update.js";
66

77
/**
88
* 根据环境进行更新

common/view/update.view.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {checkUpdate, getGithubVersion} from "#common/utils/etc/update.js";
1+
import {checkUpdate, getGithubVersion} from "#common/utils/update/update.js";
22
import inquirer from "inquirer";
33
import {currentEnv} from "#common/utils/etc/checkEnv.js";
4-
import {updateByEnv} from "#common/utils/etc/updateByEnv.js";
4+
import {updateByEnv} from "#common/utils/update/updateByEnv.js";
55

66
export const easyUpdateView = async () => {
77
// 1. 询问当前的环境是啥 (自动检测一次)

0 commit comments

Comments
 (0)