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: add -v command to check info #74

Merged
merged 2 commits into from
Mar 24, 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
1 change: 1 addition & 0 deletions bin/lc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ program
.option('-d, --directory <directory>', 'Set the question directory.')
.option('-l, --language [language]', 'Set/Get the code language of question.')
.option('-a, --all', 'Get all questions.')
.option('-v, --ver', 'Check the version info and some extra info about leetcode-practice.')
.option('-u, --update', 'Check the version to determine whether to update to the latest one.')
.parse(process.argv);

Expand Down
1 change: 1 addition & 0 deletions bin/lf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ program
.option('-e, --easy', 'Use easy mode.')
.option('-d, --directory <directory>', 'Set the question directory.')
.option('-l, --language [language]', 'Set/Get the code language of question.')
.option('-v, --ver', 'Check the version info and some extra info about leetcode-practice.')
.option('-u, --update', 'Check the version to determine whether to update to the latest one.')
.parse(process.argv);

Expand Down
1 change: 1 addition & 0 deletions bin/lk.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ program
.option('-e, --easy', 'Use easy mode.')
.option('-d, --directory <directory>', 'Set the question directory.')
.option('-l, --language [language]', 'Set/Get the code language of question.')
.option('-v, --ver', 'Check the version info and some extra info about leetcode-practice.')
.option('-u, --update', 'Check the version to determine whether to update to the latest one.')
.parse(process.argv);

Expand Down
5 changes: 4 additions & 1 deletion common/constants/question.const.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export const GITHUB_HOST = 'EternalHeartTeam';
// endregion
// region 域名列表
// npm 主域名
export const NPM_URL = 'https://registry.npmjs.org/';
export const NPM_URL = 'https://npmjs.org/';
// npm 仓库域名
export const NPM_REGISTRY_URL = 'https://registry.npmjs.org/';

// github raw 主域名
export const GITHUB_RAW = 'https://raw.githubusercontent.com/';
// github
Expand Down
6 changes: 6 additions & 0 deletions common/utils/cli-utils/commonMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { easyLanguageView } from '#common/view/language.view.js';
import { logger } from '#common/utils/logger/logger.js';
import { rootPath } from '#common/utils/file/getRootPath.js';
import { currentEnv } from '#common/utils/etc/checkEnv.js';
import { checkVisionInfo } from '#common/utils/etc/checkVisionInfo.js';

/**
* 执行逻辑:
Expand Down Expand Up @@ -45,10 +46,15 @@ export async function commonMode(cmdOpts, easyCallback) {
}
process.exit(0);
}
// 简单模式
if (cmdOpts.easy) {
await easyCallback(baseDir);
process.exit(0);
}
if (cmdOpts.ver) {
await checkVisionInfo();
process.exit(0);
}
// 检测更新
if (cmdOpts.update) {
await easyUpdateView();
Expand Down
16 changes: 16 additions & 0 deletions common/utils/etc/checkVisionInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DefaultVer, GITHUB_HOST, GITHUB_URL, NPM_URL, PackageName } from '#common/constants/question.const.js';
import { rootPath } from '#common/utils/file/getRootPath.js';
import { logger } from '#common/utils/logger/logger.js';
import { getQuestionLanguage } from '#common/utils/question-handler/questionLanguage.js';
import { url_join } from '#common/utils/http/urlJoin.js';
import { aim } from '#resources/text/aim.js';

/**
* 检查版本信息 加一些额外输出
*/
export async function checkVisionInfo() {
const version = process.env.VERSION ?? DefaultVer;
const location = rootPath;
const lang = await getQuestionLanguage();
logger.info(`version: ${version}\nlanguage: ${lang}\nlocation: file://${location}\ngithub: ${url_join(GITHUB_URL, GITHUB_HOST, PackageName)}\nnpm: ${url_join(NPM_URL, 'package', PackageName)}\n\n${aim}`);
}
6 changes: 3 additions & 3 deletions common/utils/logger/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LOGGER {
*/
info(message, ...args) {
if (this.forbidden) return;
console.log(chalk.blue(message, ...args));
console.log(chalk.whiteBright(message, ...args));
}

/**
Expand All @@ -44,7 +44,7 @@ class LOGGER {
*/
warn(message, ...args) {
if (this.forbidden) return;
console.log(chalk.yellow(message, ...args));
console.log(chalk.yellowBright(message, ...args));
}

/**
Expand All @@ -54,7 +54,7 @@ class LOGGER {
*/
error(message, ...args) {
if (this.forbidden) return;
console.log(chalk.red(message, ...args));
console.log(chalk.redBright(message, ...args));
}
}
const { env = null } = (await getStore('config')) ?? {};
Expand Down
4 changes: 2 additions & 2 deletions common/utils/update/update.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'node:fs';
import path from 'node:path';
import { rootPath } from '#common/utils/file/getRootPath.js';
import { GITEE_URL, GITHUB_HOST, GITHUB_RAW, NPM_URL, PackageName } from '#common/constants/question.const.js';
import { GITEE_URL, GITHUB_HOST, GITHUB_RAW, NPM_REGISTRY_URL, PackageName } from '#common/constants/question.const.js';
import { url_join } from '#common/utils/http/urlJoin.js';
import { fetch_ } from '#common/utils/http/fetch_.js';

// npm 中的 包地址
const npmUrl = url_join(NPM_URL, PackageName);
const npmUrl = url_join(NPM_REGISTRY_URL, PackageName);
const githubUrl = url_join(GITHUB_RAW, GITHUB_HOST, PackageName, 'master/package.json');
const giteeUrl = url_join(GITEE_URL, GITHUB_HOST, PackageName, 'raw', 'master/package.json');

Expand Down
26 changes: 22 additions & 4 deletions common/view/finder.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuesti
import { logger } from '#common/utils/logger/logger.js';
import { getQuestionListCodeBySlug, getQuestionListCodeByTag } from '#common/utils/question-handler/getQuestionListCodeBy.js';
import { getQuestionTagType } from '#common/utils/question-getter/getQuestionTagType.js';
import { getAllQuestion } from '#common/utils/store/controller/allQuestion.js';
import { getAllQuestion, setAllQuestion } from '#common/utils/store/controller/allQuestion.js';
import { getAllQuestionList } from '#common/utils/question-getter/getAllQuestionList.js';

function handleQuestionList(list) {
const questionList = [];
Expand Down Expand Up @@ -126,16 +127,33 @@ async function selectMode(baseDir = process.cwd()) {
};
const chooseTag = await select(tagQuestion);
const allQuestion = await getAllQuestion();
const tagQuestionList = await allQuestion.filter((question) => question.topicTags.some((topic) => topic.slug === chooseTag));

// 未发现题目 所以先自动拉取题目
if (!allQuestion?.length) {
logger.info('本地数据库未初始化,自动执行初始化流程,请稍等~');
try {
const allQuestionData = await getAllQuestionList();
await setAllQuestion(allQuestionData);
const newData = await getAllQuestion();
allQuestion.push(...newData);
} catch (e) {
logger.error('初始化失败!终止.');
process.exit(0);
} finally {
logger.info('本地数据库初始化完成.');
}
}
const tagQuestionList = allQuestion.filter((question) => question.topicTags?.some((topic) => topic.slug === chooseTag));
if (!tagQuestionList?.length) {
logger.info('您选择的类型暂无可拉取题目~');
process.exit(0);
}
const createMode = await select({
message: '拉题模式',
choices: [
{ name: '单个选择(不穩定)', value: 'single' },
{ name: '全部拉取(不穩定)', value: 'all' }
]
});

if (createMode === 'single') {
const singleMode = {
type: 'list',
Expand Down
4 changes: 3 additions & 1 deletion resources/text/description.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GITHUB_HOST, PackageName } from '#common/constants/question.const.js';

export const description = `
A powerful practice platform for leetcode.
CLI / Template Project / Plugin, you can create question by any way you like.
See https://github.com/wh131462/leetcode-practice
See https://github.com/${GITHUB_HOST}/${PackageName}
`;
11 changes: 6 additions & 5 deletions resources/text/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Examples:
$ lc -u // Check the version to determine whether to update to the latest one.
$ lc -l // Get the code language of question.
$ lc -l java // Set the code language of question.
$ lc -v // Check the CLI version.
$ lc -a // Fetch all questions from server and store them locally, prepare database for lf command.
$ lc -v // Check the version info and some extra info about leetcode-practice.
$ lc -h // Check the help information.

`;
Expand All @@ -33,24 +34,24 @@ Examples:
$ lk -l // Get the code language of question.
$ lk -l java // Set the code language of question.
$ lk -u // Check the version to determine whether to update to the latest one.
$ lk -v // Check the CLI version.
$ lk -v // Check the version info and some extra info about leetcode-practice.
$ lk -h // Display the help information.

`;

export const lfExamples = `
Examples:
# Command with no parameters

nothing...
# Exclusive commands

nothing...
# Other instructions
$ lf -e // Easy mode to check a question.
$ lf -d src // Use the relative path to the source folder.
$ lf -l // Get the code language of question.
$ lf -l java // Set the code language of question.
$ lf -u // Check the version to determine whether to update to the latest one.
$ lf -v // Check the CLI version.
$ lf -v // Check the version info and some extra info about leetcode-practice.
$ lf -h // Display the help information.

`;
Loading