Skip to content

Commit 81a1439

Browse files
authored
feat: check update with first lc run (#41)
* docs: add contributors markdown * ci(release): modify ci to release when tag add and change the release-it to create new branch * feat(update): check update when first run lc and will be not allow until one day
1 parent 8ed9b2b commit 81a1439

File tree

6 files changed

+86
-7
lines changed

6 files changed

+86
-7
lines changed

common/constants/date.const.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// 以毫秒计算的时间单位
2+
export const Second = 1000
3+
export const Minute = 60 * Second
4+
export const Hour = 60 * Minute
5+
export const Day = 24 * Hour
6+
export const Week = 7 * Day
7+
export const Year = 365 * Day

common/constants/manager.const.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* npm 的安装指令
3+
* @param packageName
4+
* @param isUpdate
5+
* @param isGlobal
6+
* @returns {`npm ${string} ${string} ${string}`}
7+
* @constructor
8+
*/
9+
export function NpmInstall(packageName, isUpdate, isGlobal) {
10+
return `npm ${isUpdate ? 'update' : 'install'} ${isGlobal ? '-g' : ''} ${packageName}`
11+
}
12+
13+
/**
14+
* yarn 的安装指令
15+
* @param packageName
16+
* @param isUpdate
17+
* @param isGlobal
18+
* @returns {`yarn ${string} ${string} ${string}`}
19+
* @constructor
20+
*/
21+
export function YarnInstall(packageName, isUpdate, isGlobal) {
22+
return `yarn ${isGlobal ? 'global' : ''} ${isUpdate ? 'upgrade' : 'add'} ${packageName}`
23+
}
24+
25+
/**
26+
* pnpm 的安装指令
27+
* @param packageName
28+
* @param isUpdate
29+
* @param isGlobal
30+
* @returns {`pnpm ${string} ${string} ${string}`}
31+
* @constructor
32+
*/
33+
export function PnpmInstall(packageName, isUpdate, isGlobal) {
34+
return `pnpm ${isGlobal ? 'global' : ''} ${isUpdate ? 'update' : 'install'} ${packageName}`
35+
}

common/utils/cli-utils/checkUpdate.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { logger } from '#common/utils/logger/logger.js'
2+
import { checkUpdate } from '#common/utils/update/update.js'
3+
import { getStore, setStore } from '#common/utils/store/controller/store.js'
4+
import { Day } from '#common/constants/date.const.js'
5+
import {
6+
NpmInstall,
7+
PnpmInstall,
8+
YarnInstall
9+
} from '#common/constants/manager.const.js'
10+
import { PackageName } from '#common/constants/question.const.js'
11+
12+
const { timestamp, hasShow } = (await getStore('checkResult')) ?? {}
13+
if (Date.now() - timestamp <= Day || hasShow) process.exit(0)
14+
15+
const { localVersion, remoteVersion, isCliUpdate } = await checkUpdate()
16+
const needShow = false
17+
if (isCliUpdate) {
18+
const installInfo = [NpmInstall, YarnInstall, PnpmInstall]
19+
.map((fun) => fun(PackageName, true, true)) // 暂时先默认为全局
20+
.join('\n')
21+
logger.warn(
22+
`[leetcode-practice] 检测到新版本[ ${remoteVersion} ]已经发布! 您当前的版本为[ ${localVersion} ]! 您可以执行对应的指令进行手动更新~`
23+
)
24+
logger.info(`${installInfo}`)
25+
}
26+
await setStore('checkResult', { timestamp: Date.now(), hasShow: needShow })
27+
process.exit(0)

common/utils/cli-utils/commonMode.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import path from 'node:path'
2+
import { fork } from 'node:child_process'
23
import { easyUpdateView } from '#common/view/update.view.js'
34
import { getQuestionLanguage } from '#common/utils/question-handler/questionLanguage.js'
45
import { easyLanguageView } from '#common/view/language.view.js'
56
import { logger } from '#common/utils/logger/logger.js'
7+
import { rootPath } from '#common/utils/file/getRootPath.js'
68

79
/**
810
* 执行逻辑:
@@ -17,6 +19,14 @@ import { logger } from '#common/utils/logger/logger.js'
1719
* @returns {Promise<string>}
1820
*/
1921
export async function commonMode(cmdOpts, easyCallback) {
22+
// 启动一个额外的线程,并执行 worker.js 文件
23+
// const workerProcess =
24+
fork(path.resolve(rootPath, 'common/utils/cli-utils/checkUpdate.js'))
25+
// todo 监听额外线程的消息
26+
// workerProcess.on('message', (message) => {})
27+
// todo 监听额外线程的退出事件
28+
// workerProcess.on('exit', (code, signal) => {})
29+
2030
// 根据dir 参数来设置基本目录
2131
const baseDir = cmdOpts.directory
2232
? path.join(process.cwd(), cmdOpts.directory)

common/utils/etc/checkEnv.js

+7
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ export function currentEnv() {
99
const projectReg = /etc\/checkEnv.js$/im
1010
return projectReg.test(url) ? 'project' : 'cli'
1111
}
12+
13+
/**
14+
* 检查npm安装时的位置
15+
*/
16+
export function npmEnv() {
17+
return true ? 'global' : 'module'
18+
}

common/utils/update/update.js

-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from '#common/constants/question.const.js'
1111
import { url_join } from '#common/utils/http/urlJoin.js'
1212
import { fetch_ } from '#common/utils/http/fetch_.js'
13-
import { logger } from '#common/utils/logger/logger.js'
1413

1514
// npm 中的 包地址
1615
const npmUrl = url_join(NPM_URL, PackageName)
@@ -34,10 +33,8 @@ const giteeUrl = url_join(
3433
export async function getNpmVersion() {
3534
try {
3635
const res = await fetch_(npmUrl, { method: 'GET' })
37-
logger.info('获取NPM版本成功!======', res['dist-tags']?.latest)
3836
return res['dist-tags']?.latest
3937
} catch (e) {
40-
logger.info('获取NPM版本失败!')
4138
throw new Error(e)
4239
}
4340
}
@@ -53,10 +50,8 @@ export async function getGithubVersion() {
5350
fetch_(giteeUrl, { method: 'GET' })
5451
])
5552
const ver = github?.version ?? gitee?.version
56-
logger.info('获取Github版本成功!======', ver)
5753
return ver
5854
} catch (e) {
59-
logger.info('获取Github版本失败!', e)
6055
throw new Error(e)
6156
}
6257
}
@@ -65,10 +60,8 @@ export function getLocalVersion() {
6560
const { version } = JSON.parse(
6661
fs.readFileSync(path.resolve(rootPath, 'package.json'), 'utf-8')
6762
)
68-
logger.info('本地版本号获取成功!======', version)
6963
return version
7064
} catch (e) {
71-
logger.info('本地版本号获取失败!')
7265
return false
7366
}
7467
}

0 commit comments

Comments
 (0)