Skip to content

Commit 32901d8

Browse files
authored
chore: tiny modify some files for better performance (#53)
* chore: tiny modify some files for better performance * chore: lint * chore: add lf -e
1 parent 6b710a1 commit 32901d8

File tree

13 files changed

+27
-250
lines changed

13 files changed

+27
-250
lines changed

.release-it.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"publish": false
1616
},
1717
"hooks": {
18-
"after:bump": "git add CHANGELOG.md && echo 更新版本${version}成功!添加changelog的commit成功!",
19-
"after:release": "git checkout master && git push origin master:stable-v${version} || true"
18+
"after:bump": "prettier --write CHANGELOG.md && git add CHANGELOG.md ",
19+
"after:release": "git checkout master && git push origin master:stable-v${version} || true && echo 更新版本 ${version} 成功!"
2020
},
2121
"plugins": {
2222
"@release-it/bumper": {

CHANGELOG.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
2-
31
## [1.0.9-2](https://github.com/wh131462/leetcode-practice/compare/cli-v1.0.9-1...cli-v1.0.9-2) (2024-03-14)
42

5-
63
### Features
74

8-
* add loading and logger on/off ([#51](https://github.com/wh131462/leetcode-practice/issues/51)) ([bbff9b1](https://github.com/wh131462/leetcode-practice/commit/bbff9b1b80aea18255b089c5ad9c836bcbd18dae))
5+
- add loading and logger on/off ([#51](https://github.com/wh131462/leetcode-practice/issues/51)) ([bbff9b1](https://github.com/wh131462/leetcode-practice/commit/bbff9b1b80aea18255b089c5ad9c836bcbd18dae))
96

107
## [1.0.9-1](https://github.com/wh131462/leetcode-practice/compare/cli-v1.0.9-0...cli-v1.0.9-1) (2024-03-14)
118

bin/lf.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ program
1515
.version(version)
1616
.description(`${description}\n${artFontLogo}\n${aim}`)
1717
.addHelpText('after', lfExamples + love)
18-
.option('-l, --language [language]', 'Set/Get the code language of question.')
18+
.option('-e, --easy', 'Use easy mode.')
1919
.option('-d, --directory <directory>', 'Set the question directory.')
20+
.option('-l, --language [language]', 'Set/Get the code language of question.')
2021
.option(
2122
'-u, --update',
2223
'Check the version to determine whether to update to the latest one.'

bin/lk.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function check(mode, question) {
6565
logger.info(`文件[${filePath}]不存在,请确保已经创建!`)
6666
} else {
6767
logger.info(
68-
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检测结果:`
68+
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检查结果:`
6969
)
7070
await checkQuestionByPath(filePath)
7171
}
@@ -94,7 +94,7 @@ const callModeAction = {
9494
const needToSelect = {
9595
type: 'list',
9696
name: 'need',
97-
message: '在当前目录下存在多个题目,请选择你要检测的题目:',
97+
message: `在当前目录下存在id为[${id}]的题目副本,请选择你要检查的副本:`,
9898
choices: []
9999
}
100100
/**
@@ -106,7 +106,7 @@ const callModeAction = {
106106
const needToCheck = {
107107
type: 'list',
108108
name: 'check',
109-
message: '存在多个题目文件,请选择:',
109+
message: '当前题目目录中存在多个题目文件副本,请选择一个进行检查:',
110110
choices: [],
111111
default: null
112112
}
@@ -135,6 +135,7 @@ const callModeAction = {
135135
}
136136

137137
let files
138+
let which
138139
switch (typeof question) {
139140
case 'undefined':
140141
logger.warn(`当前目录下未找到题目id为[${id}]的题目!`)
@@ -147,9 +148,8 @@ const callModeAction = {
147148
needToSelect.choices = question.map((o) => {
148149
return { name: o, value: o }
149150
})
150-
files = getQuestionFileInDir(
151-
path.resolve(baseDir, await select(needToSelect))
152-
)
151+
which = await select(needToSelect)
152+
files = getQuestionFileInDir(path.resolve(baseDir, which))
153153
break
154154
}
155155
await checkOne(files)

common/constants/manager.const.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param packageName
44
* @param isUpdate
55
* @param isGlobal
6-
* @returns {`npm ${string} ${string} ${string}`}
6+
* @returns {string}
77
* @constructor
88
*/
99
export function NpmInstall(packageName, isUpdate, isGlobal) {
@@ -15,7 +15,7 @@ export function NpmInstall(packageName, isUpdate, isGlobal) {
1515
* @param packageName
1616
* @param isUpdate
1717
* @param isGlobal
18-
* @returns {`yarn ${string} ${string} ${string}`}
18+
* @returns {string}
1919
* @constructor
2020
*/
2121
export function YarnInstall(packageName, isUpdate, isGlobal) {
@@ -27,7 +27,7 @@ export function YarnInstall(packageName, isUpdate, isGlobal) {
2727
* @param packageName
2828
* @param isUpdate
2929
* @param isGlobal
30-
* @returns {`pnpm ${string} ${string} ${string}`}
30+
* @returns {string}
3131
* @constructor
3232
*/
3333
export function PnpmInstall(packageName, isUpdate, isGlobal) {

common/utils/cli-utils/commonMode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import { currentEnv } from '#common/utils/etc/checkEnv.js'
1515
*/
1616
/**
1717
* 通用参数的执行逻辑
18-
* @param cmdOpts{{directory:string,language:string|boolean,easy:boolean,update:boolean,[key:string]:*}}
19-
* @param easyCallback{(baseDir:string)=>Promise<any>}
18+
* @param cmdOpts {{directory:string,language:string|boolean,easy:boolean,update:boolean,[key:string]:*}}
19+
* @param easyCallback {(baseDir:string)=>Promise<any>}
2020
* @returns {Promise<string>}
2121
*/
2222
export async function commonMode(cmdOpts, easyCallback) {

common/utils/loading/loading.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class Loading {
1313
if (this.interval) clearInterval(this.interval)
1414
this.interval = setInterval(() => {
1515
process.stdout.write(
16-
chalk.yellow(`\r${this.frames[this.currentFrame]} ${this.text}`)
16+
chalk.blueBright(`\r${this.frames[this.currentFrame]} ${this.text}`)
1717
)
1818

1919
this.currentFrame++

common/utils/update/updateByEnv.js

-107
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
11
import { exec } from 'node:child_process'
2-
import https from 'node:https'
3-
import fs from 'node:fs'
4-
import inquirer from 'inquirer'
5-
import { getGithubVersion } from '#common/utils/update/update.js'
6-
import { logger } from '#common/utils/logger/logger.js'
72

8-
/**
9-
* 根据环境进行更新
10-
* @param {"cli"|"project"} env
11-
*/
12-
export async function updateByEnv(env) {
13-
switch (env) {
14-
case 'cli':
15-
// 执行CLI更新逻辑
16-
return await updateCli()
17-
case 'project':
18-
// 执行项目更新逻辑
19-
return await updateProject()
20-
default:
21-
throw new Error(`Unsupported environment: ${env}`)
22-
}
23-
}
243
/**
254
* 更新CLI
265
* @returns {Promise<void>}
@@ -33,89 +12,3 @@ export function updateCli() {
3312
})
3413
})
3514
}
36-
/**
37-
* 更新项目
38-
* @returns {Promise<void>}
39-
*/
40-
export async function updateProject() {
41-
logger.info('脚本升级中,请等待后续版本更新...')
42-
return
43-
// todo 更新项目
44-
// 0. 询问是否存在自己修改过的内容
45-
// 0.1 如果有过更改,终止更新脚本,并给出提示建议
46-
// 0.2 如果没有更改,进行更新操作
47-
// 1. 检测当前的git工作区
48-
// 1.1 如果有内容,提示提交,终止更新
49-
// 1.2 如果空,可以进行更新
50-
// 2. 通过raw.github.com下载所有的文件并对应更新文件(除package.json)
51-
// 2.0 所有的文件在更新后都进行git add [file-path]
52-
// 2.1 如果文件不存在,则创建文件
53-
// 2.2 如果文件存在,则覆盖文件
54-
// 2.3 更新package.json文件中的版本号为最新的版本号.
55-
// 3. 创建一个git的commit update:Version [版本号]
56-
// 4. 完成更新
57-
async function execCommand(command) {
58-
return new Promise((resolve, reject) => {
59-
exec(command, (error, stdout) => {
60-
if (error) reject(error)
61-
else resolve(stdout.trim())
62-
})
63-
})
64-
}
65-
66-
try {
67-
const ltsVersion = await getGithubVersion()
68-
// 0. 询问是否存在自己修改过的内容
69-
const { hasChanges } = await inquirer.prompt([
70-
{
71-
type: 'confirm',
72-
name: 'hasChanges',
73-
message: '是否存在自己修改过的内容?',
74-
default: false
75-
}
76-
])
77-
78-
// 0.1 如果有过更改,终止更新脚本,并给出提示建议
79-
if (hasChanges) {
80-
logger.info('您有未提交的更改,请先处理后再更新。')
81-
return
82-
}
83-
84-
// 1. 检测当前的git工作区
85-
const gitStatus = await execCommand('git status --porcelain')
86-
87-
// 1.1 如果有内容,提示提交,终止更新
88-
if (gitStatus) {
89-
logger.info('您有未提交的更改,请先提交后再更新。')
90-
return
91-
}
92-
93-
// 2. 下载所有的文件并对应更新文件(除package.json)
94-
const response = await https.get(
95-
'https://raw.githubusercontent.com/wh131462/leetcode-practice'
96-
)
97-
const filesToUpdate = response.data
98-
99-
// 2.0 所有的文件在更新后都进行git add [file-path]
100-
for (const filePath in filesToUpdate) {
101-
fs.writeFileSync(filePath, filesToUpdate[filePath])
102-
103-
// 2.1 如果文件不存在,则创建文件
104-
// 2.2 如果文件存在,则覆盖文件
105-
await execCommand(`git add ${filePath}`)
106-
}
107-
108-
// 2.3 更新package.json文件中的版本号为最新的版本号
109-
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8'))
110-
packageJson.version = `${ltsVersion}` // 这里替换为实际的最新版本号
111-
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2))
112-
113-
// 3. 创建一个git的commit update:Version [版本号]
114-
await execCommand(`git commit -m "update:${ltsVersion}"`) // 这里替换为实际的最新版本号
115-
116-
// 4. 完成更新
117-
logger.info('项目更新完成。')
118-
} catch (error) {
119-
console.error('更新过程中出现错误:', error)
120-
}
121-
}

common/view/update.view.js

+6-39
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,27 @@
11
import inquirer from 'inquirer'
22
import { checkUpdate } from '#common/utils/update/update.js'
3-
import { currentEnv } from '#common/utils/etc/checkEnv.js'
4-
import { updateByEnv } from '#common/utils/update/updateByEnv.js'
53
import { logger } from '#common/utils/logger/logger.js'
4+
import { updateCli } from '#common/utils/update/updateByEnv.js'
65

76
export async function easyUpdateView() {
8-
// 1. 询问当前的环境是啥 (自动检测一次)
9-
// 检测到的当前环境
10-
const env = currentEnv()
11-
const envQuestion = {
12-
type: 'list',
13-
name: 'choseEnv',
14-
message: `自动检测到的环境为[ ${env} ],如果不是,请进行选择,如是,请按下回车确认.`,
15-
choices: ['cli', 'project'],
16-
default: env
17-
}
18-
const { choseEnv } = await inquirer.prompt(envQuestion, null)
19-
// 2. 检测版本更新
207
const {
218
localVersion = '未检出',
229
npmVersion = '未检出',
23-
githubVersion = '未检出',
24-
isCliUpdate,
25-
isGithubUpdate
10+
isCliUpdate
2611
} = await checkUpdate()
27-
logger.info(
28-
`当前版本:[ ${localVersion} ] npm包最新版本:[ ${npmVersion} ] github版本:[ ${githubVersion} ]`
29-
)
30-
let isUpdate = false
31-
let version = '未知'
32-
switch (choseEnv) {
33-
case 'project':
34-
isUpdate = isGithubUpdate
35-
version = githubVersion
36-
break
37-
case 'cli':
38-
isUpdate = isCliUpdate
39-
version = npmVersion
40-
break
41-
default:
42-
logger.warn('未知环境:', choseEnv)
43-
process.exit(0)
44-
break
45-
}
12+
logger.info(`当前版本:[ ${localVersion} ] npm包最新版本:[ ${npmVersion} ]`)
4613
// 3. 询问是否更新
47-
if (isUpdate) {
14+
if (isCliUpdate) {
4815
const checkQuestion = {
4916
type: 'confirm',
5017
name: 'willUpdate',
51-
message: `检测到[ ${env} ]可更新版本[ ${version} ],是否进行更新?`
18+
message: `检测到可更新版本[ ${npmVersion} ],是否进行更新?`
5219
}
5320
const { willUpdate } = await inquirer.prompt(checkQuestion, null)
5421
if (willUpdate) {
5522
// 4.1 选择更新
5623
logger.info('开始更新...')
57-
await updateByEnv(env)
24+
await updateCli()
5825
logger.info('更新完成~祝你使用愉快~')
5926
} else {
6027
// 4.2 取消更新

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
"lc": "node bin/lc.js -d src",
3030
"lk": "node bin/lk.js -d src",
3131
"lf": "node bin/lf.js -d src",
32-
"release:cli": "node scripts/release.js",
33-
"update": "node scripts/update.js",
3432
"build-cli": "node esbuild.config.js",
3533
"publish-cli": "cd pl-cli && npm publish --registry https://registry.npmjs.org/",
3634
"unpublish-cli": "cd pl-cli && npm unpublish --force --registry https://registry.npmjs.org/",

scripts/loading.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Loading } from '#common/utils/loading/loading.js'
2+
3+
const loader = new Loading('旋转跳跃~我闭着眼~')
4+
loader.start()

scripts/release.js

-52
This file was deleted.

0 commit comments

Comments
 (0)