Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check update with first lc run #41

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
jobs:
build:
runs-on: ubuntu-latest
if: github.repository == 'EternalHeartTeam/leetcode-practice' && github.ref == 'refs/heads/master'
if: github.repository == 'EternalHeartTeam/leetcode-practice'
steps:
- uses: actions/checkout@v3
# Setup .npmrc file to publish to npm
Expand Down
4 changes: 2 additions & 2 deletions .release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"git": {
"commitMessage": "chore: release v${version}",
"tagName": "cli-v${version}",
"branch": "stable-v${version}",
"tag": true,
"push": true,
"pushArgs": ["--follow-tags"]
Expand All @@ -16,7 +15,8 @@
"publish": false
},
"hooks": {
"after:bump": "git add CHANGELOG.md && echo 更新版本成功!添加changelog的commit成功!"
"after:bump": "git add CHANGELOG.md && echo 更新版本${version}成功!添加changelog的commit成功!",
"after:release": "git checkout master && git checkout -b stable-v${version} && git push origin stable-v${version}"
},
"plugins": {
"@release-it/bumper": {
Expand Down
7 changes: 7 additions & 0 deletions common/constants/date.const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// 以毫秒计算的时间单位
export const Second = 1000
export const Minute = 60 * Second
export const Hour = 60 * Minute
export const Day = 24 * Hour
export const Week = 7 * Day
export const Year = 365 * Day
35 changes: 35 additions & 0 deletions common/constants/manager.const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* npm 的安装指令
* @param packageName
* @param isUpdate
* @param isGlobal
* @returns {`npm ${string} ${string} ${string}`}
* @constructor
*/
export function NpmInstall(packageName, isUpdate, isGlobal) {
return `npm ${isUpdate ? 'update' : 'install'} ${isGlobal ? '-g' : ''} ${packageName}`
}

/**
* yarn 的安装指令
* @param packageName
* @param isUpdate
* @param isGlobal
* @returns {`yarn ${string} ${string} ${string}`}
* @constructor
*/
export function YarnInstall(packageName, isUpdate, isGlobal) {
return `yarn ${isGlobal ? 'global' : ''} ${isUpdate ? 'upgrade' : 'add'} ${packageName}`
}

/**
* pnpm 的安装指令
* @param packageName
* @param isUpdate
* @param isGlobal
* @returns {`pnpm ${string} ${string} ${string}`}
* @constructor
*/
export function PnpmInstall(packageName, isUpdate, isGlobal) {
return `pnpm ${isGlobal ? 'global' : ''} ${isUpdate ? 'update' : 'install'} ${packageName}`
}
27 changes: 27 additions & 0 deletions common/utils/cli-utils/checkUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { logger } from '#common/utils/logger/logger.js'
import { checkUpdate } from '#common/utils/update/update.js'
import { getStore, setStore } from '#common/utils/store/controller/store.js'
import { Day } from '#common/constants/date.const.js'
import {
NpmInstall,
PnpmInstall,
YarnInstall
} from '#common/constants/manager.const.js'
import { PackageName } from '#common/constants/question.const.js'

const { timestamp, hasShow } = (await getStore('checkResult')) ?? {}
if (Date.now() - timestamp <= Day || hasShow) process.exit(0)

const { localVersion, remoteVersion, isCliUpdate } = await checkUpdate()
const needShow = false
if (isCliUpdate) {
const installInfo = [NpmInstall, YarnInstall, PnpmInstall]
.map((fun) => fun(PackageName, true, true)) // 暂时先默认为全局
.join('\n')
logger.warn(
`[leetcode-practice] 检测到新版本[ ${remoteVersion} ]已经发布! 您当前的版本为[ ${localVersion} ]! 您可以执行对应的指令进行手动更新~`
)
logger.info(`${installInfo}`)
}
await setStore('checkResult', { timestamp: Date.now(), hasShow: needShow })
process.exit(0)
10 changes: 10 additions & 0 deletions common/utils/cli-utils/commonMode.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from 'node:path'
import { fork } from 'node:child_process'
import { easyUpdateView } from '#common/view/update.view.js'
import { getQuestionLanguage } from '#common/utils/question-handler/questionLanguage.js'
import { easyLanguageView } from '#common/view/language.view.js'
import { logger } from '#common/utils/logger/logger.js'
import { rootPath } from '#common/utils/file/getRootPath.js'

/**
* 执行逻辑:
Expand All @@ -17,6 +19,14 @@ import { logger } from '#common/utils/logger/logger.js'
* @returns {Promise<string>}
*/
export async function commonMode(cmdOpts, easyCallback) {
// 启动一个额外的线程,并执行 worker.js 文件
// const workerProcess =
fork(path.resolve(rootPath, 'common/utils/cli-utils/checkUpdate.js'))
// todo 监听额外线程的消息
// workerProcess.on('message', (message) => {})
// todo 监听额外线程的退出事件
// workerProcess.on('exit', (code, signal) => {})

// 根据dir 参数来设置基本目录
const baseDir = cmdOpts.directory
? path.join(process.cwd(), cmdOpts.directory)
Expand Down
7 changes: 7 additions & 0 deletions common/utils/etc/checkEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ export function currentEnv() {
const projectReg = /etc\/checkEnv.js$/im
return projectReg.test(url) ? 'project' : 'cli'
}

/**
* 检查npm安装时的位置
*/
export function npmEnv() {
return true ? 'global' : 'module'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这不是永远都是true

}
7 changes: 0 additions & 7 deletions common/utils/update/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from '#common/constants/question.const.js'
import { url_join } from '#common/utils/http/urlJoin.js'
import { fetch_ } from '#common/utils/http/fetch_.js'
import { logger } from '#common/utils/logger/logger.js'

// npm 中的 包地址
const npmUrl = url_join(NPM_URL, PackageName)
Expand All @@ -34,10 +33,8 @@ const giteeUrl = url_join(
export async function getNpmVersion() {
try {
const res = await fetch_(npmUrl, { method: 'GET' })
logger.info('获取NPM版本成功!======', res['dist-tags']?.latest)
return res['dist-tags']?.latest
} catch (e) {
logger.info('获取NPM版本失败!')
throw new Error(e)
}
}
Expand All @@ -53,10 +50,8 @@ export async function getGithubVersion() {
fetch_(giteeUrl, { method: 'GET' })
])
const ver = github?.version ?? gitee?.version
logger.info('获取Github版本成功!======', ver)
return ver
} catch (e) {
logger.info('获取Github版本失败!', e)
throw new Error(e)
}
}
Expand All @@ -65,10 +60,8 @@ export function getLocalVersion() {
const { version } = JSON.parse(
fs.readFileSync(path.resolve(rootPath, 'package.json'), 'utf-8')
)
logger.info('本地版本号获取成功!======', version)
return version
} catch (e) {
logger.info('本地版本号获取失败!')
return false
}
}
Expand Down
Loading