Skip to content

Commit 6c0e69c

Browse files
committed
refactor(all): instead commonjs to module
1 parent 439fff9 commit 6c0e69c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+159
-198
lines changed

common/resources/codeDetailJson.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function getCodeDetailJson(todayQuestionEnName) {
1+
export function getCodeDetailJson(todayQuestionEnName) {
22
return {
33
headers: {
44
accept: '*/*',
@@ -22,6 +22,4 @@ function getCodeDetailJson(todayQuestionEnName) {
2222
body: `{"query":"\\n query questionEditorData($titleSlug: String!) {\\n question(titleSlug: $titleSlug) {\\n questionId\\n questionFrontendId\\n codeSnippets {\\n lang\\n langSlug\\n code\\n }\\n envInfo\\n enableRunCode\\n hasFrontendPreview\\n frontendPreviews\\n }\\n}\\n ","variables":{"titleSlug":"${todayQuestionEnName}"},"operationName":"questionEditorData"}`,
2323
method: 'POST',
2424
};
25-
}
26-
27-
module.exports = { getCodeDetailJson };
25+
}

common/resources/questionDetailJson.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function getQuestionDetailJson(slug) {
1+
export function getQuestionDetailJson(slug) {
22
return {
33
headers: {
44
accept: '*/*',
@@ -23,5 +23,3 @@ function getQuestionDetailJson(slug) {
2323
method: 'POST',
2424
};
2525
}
26-
27-
module.exports = { getQuestionDetailJson };

common/resources/questionListJson.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function getQuestionListJson(skip = 0, limit = 50) {
1+
export function getQuestionListJson(skip = 0, limit = 50) {
22
return {
33
headers: {
44
accept: '*/*',
@@ -24,4 +24,3 @@ function getQuestionListJson(skip = 0, limit = 50) {
2424
};
2525
}
2626

27-
module.exports = { getQuestionListJson };

common/resources/questionSearchJson.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function getQuestionSearchJson(keyword) {
1+
export function getQuestionSearchJson(keyword) {
22
return {
33
headers: {
44
accept: '*/*',
@@ -24,4 +24,3 @@ function getQuestionSearchJson(keyword) {
2424
};
2525
}
2626

27-
module.exports = { getQuestionSearchJson };

common/resources/questionTodayJson.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function getQuestionTodayJson() {
1+
export function getQuestionTodayJson() {
22
return {
33
headers: {
44
accept: '*/*',
@@ -24,4 +24,3 @@ function getQuestionTodayJson() {
2424
};
2525
}
2626

27-
module.exports = { getQuestionTodayJson };

common/scripts/check.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
/**
22
* 执行脚本
33
*/
4-
const { temExe } = require('../utils/temExe');
5-
const {readStore} = require("../utils/store");
4+
import {temExe} from "#common/utils/temExe.js";
5+
import {readStore} from "#common/utils/store.js";
66
const args = process.argv.slice(2);
77
let name;
88
switch (args[0]) {
9-
case "-r": {
9+
case "-r": {
1010
const random = readStore("random-question-info")
1111
name = `${random.id}.${random.enName}`;
1212
console.log(`[leet-check]检测当前随机题目:${name}`)
1313
break;
1414
}
1515

1616
case "-i": {
17-
let id = args[1];
17+
let id = args[1];
1818
const specified = readStore("specified-question-info")
1919
name = `${specified.id}.${specified.enName}`;
20-
if(id===undefined&&name.startsWith("undefined")) {
20+
if (id === undefined && name.startsWith("undefined")) {
2121
console.warn("请指定对应的编号进行检查!")
22-
return;
2322
}
2423
console.log(`[leet-check]检测题目:${name}`)
2524
break;
2625
}
27-
26+
2827
case "-t":
2928
default: {
3029
const question = readStore("today-question-info")
@@ -33,6 +32,6 @@ switch (args[0]) {
3332
}
3433
break;
3534
}
36-
temExe('node ./src/{0}/index.js',name)
35+
temExe('node ./src/{0}/index.js', name)
3736
.then(res => console.log(`执行结果:\n${res}`))
3837
.catch(e => console.log("执行报错: ", e));

common/scripts/create.js

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const { createQuestion } = require("../utils/createQuestion");
2-
const {getQuestionToday} = require("../utils/getQuestionToday");
3-
const {fulfillQuestion} = require("../utils/fulfillQuestion");
4-
const { writeStore} = require("../utils/store");
5-
const {getQuestionById} = require("../utils/getQuestionById");
6-
const {getRandomId} = require("../utils/getRandomId");
1+
import {createQuestion} from "#common/utils/createQuestion.js";
2+
import {getQuestionToday} from "#common/utils/getQuestionToday.js";
3+
import {fulfillQuestion} from "#common/utils/fulfillQuestion.js";
4+
import {writeStore} from "#common/utils/store.js";
5+
import {getQuestionById} from "#common/utils/getQuestionById.js";
6+
import {getRandomId} from "#common/utils/getRandomId.js";
7+
78
/**
89
* leet-create [-t|-r|-i [id]]
910
* 默认参数 -t
@@ -15,10 +16,10 @@ const {getRandomId} = require("../utils/getRandomId");
1516
const args = process.argv.slice(2);
1617
switch (args[0]) {
1718
case "-r":
18-
getRandomId().then(id=>{
19+
getRandomId().then(id => {
1920
getQuestionById(id).then(question => {
2021
const random = `${question.id}.${question.enName}`;
21-
writeStore("random-question-info",question);
22+
writeStore("random-question-info", question);
2223
createQuestion(random).then((filePath) => {
2324
fulfillQuestion(filePath, question);
2425
})
@@ -27,18 +28,18 @@ switch (args[0]) {
2728
break;
2829
case "-i":
2930
const id = args[1];
30-
if(id===undefined) {
31+
if (id === undefined) {
3132
console.warn("请指定对应的编号!")
32-
return;
33-
}
34-
console.log(`获取指定编号[${id}]的题目...`)
35-
getQuestionById(id).then(question => {
36-
const specified = `${question.id}.${question.enName}`;
37-
writeStore("specified-question-info",question);
38-
createQuestion(specified).then((filePath) => {
39-
fulfillQuestion(filePath, question);
33+
}else{
34+
console.log(`获取指定编号[${id}]的题目...`)
35+
getQuestionById(id).then(question => {
36+
const specified = `${question.id}.${question.enName}`;
37+
writeStore("specified-question-info", question);
38+
createQuestion(specified).then((filePath) => {
39+
fulfillQuestion(filePath, question);
40+
})
4041
})
41-
})
42+
}
4243
break;
4344
case "-t":
4445
default:

common/scripts/test.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const { getQuestionListJson } = require('../resources/questionListJson');
21
// fetch("https://leetcode.cn/graphql/", getQuestionListJson(1)).then((res => res.json())).then((res)=>{
32
// return res?.data?.problemsetQuestionList?.questions?.map(q=>q.frontendQuestionId);
43
// });

common/structures/ListNode.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @constructor
66
*/
77

8-
class ListNode {
8+
export class ListNode {
99
constructor(val, next) {
1010
this.val = (val === undefined ? 0 : val);
1111
this.next = (next === undefined ? null : next);
@@ -33,5 +33,3 @@ class ListNode {
3333
return ListNode.toArray(listNodes.next, arr);
3434
}
3535
}
36-
37-
module.exports = ListNode;

common/structures/Node.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Node {
1+
export class Node {
22
constructor(val, neighbors) {
33
this.val = val === undefined ? 0 : val;
44
this.neighbors = neighbors === undefined ? [] : neighbors;
@@ -54,4 +54,3 @@ class Node {
5454
return result;
5555
}
5656
}
57-
module.exports = Node;

common/structures/TreeNode.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class TreeNode {
1+
export class TreeNode {
22
constructor(val, left, right) {
33
this.left = null;
44
this.val = (val === undefined ? 0 : val);
@@ -50,4 +50,3 @@ class TreeNode {
5050
return result;
5151
}
5252
}
53-
module.exports = TreeNode;

common/template/template.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { showLogs } = require('../utils/showLogs');
1+
import {showLogs} from "#common/utils/showLogs.js";
2+
23
/**
34
* @题目
45
* @描述

common/utils/createQuestion.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const readlinePromises = require('node:readline/promises');
4-
const { getCountBySameName } = require('./getCountBySameName');
5-
6-
const rl = readlinePromises.createInterface({
7-
input: process.stdin,
8-
output: process.stdout,
9-
});
10-
const sourceFilePath = path.normalize('./common/template/template.js');
11-
12-
function createQuestion(newPath) {
1+
import fs from "fs";
2+
import path from "path";
3+
import readlinePromises from "node:readline/promises";
4+
import {getCountBySameName} from "./getCountBySameName.js";
5+
export function createQuestion(newPath) {
6+
const rl = readlinePromises.createInterface({
7+
input: process.stdin,
8+
output: process.stdout,
9+
});
10+
const sourceFilePath = path.normalize('./common/template/template.js');
1311
return new Promise((resolve, reject) => {
1412
let newDir = path.normalize(`./src/${newPath}`);
1513
let newFilePath = path.join(newDir, 'index.js');
@@ -53,5 +51,3 @@ function createQuestion(newPath) {
5351
});
5452
});
5553
}
56-
57-
module.exports = { createQuestion, sourceFilePath };

common/utils/fulfillQuestion.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const fs = require('fs');
2-
const { removeDomTags } = require('./removeDomTags');
3-
const { getTestCase } = require('./getTestCase');
4-
const { getQuestionUrl } = require('./getQuestionUrl');
5-
// import {getQuestion} from "./getQuestion";
6-
1+
import {removeDomTags} from "./removeDomTags.js";
2+
import fs from "fs";
3+
import {getTestCase} from "./getTestCase.js";
4+
import {getQuestionUrl} from "./getQuestionUrl.js";
75
/**
86
* @typedef {Object} Question
97
* @property {string} title
@@ -21,7 +19,7 @@ const { getQuestionUrl } = require('./getQuestionUrl');
2119
* @param {Question} question
2220
*
2321
*/
24-
const generateTemplateContent = (data, question) => data.replace('@题目', `${question.id}.${question.title} ${question.date ? `[${question.date}]` : ''}`)
22+
export const generateTemplateContent = (data, question) => data.replace('@题目', `${question.id}.${question.title} ${question.date ? `[${question.date}]` : ''}`)
2523
.replace('@描述', removeDomTags(question.detail)
2624
.replace('@url', question.url)
2725
.replace(/\n+/g, '\n')
@@ -34,7 +32,7 @@ const generateTemplateContent = (data, question) => data.replace('@题目', `${q
3432
* @param questionPath
3533
* @param question
3634
*/
37-
const fulfillQuestion = (questionPath, question) => {
35+
export const fulfillQuestion = (questionPath, question) => {
3836
// 开始填充内容
3937
fs.readFile(questionPath, 'utf8', (err, data) => {
4038
if (err) throw err;
@@ -46,4 +44,3 @@ const fulfillQuestion = (questionPath, question) => {
4644
});
4745
});
4846
};
49-
module.exports = { fulfillQuestion, generateTemplateContent };

common/utils/getCountBySameName.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
const { readdirSync } = require('fs');
1+
import {readdirSync} from "fs";
22

33
/**
44
* 根据指定的目录和文件名 给出存在的数量
55
* @param dir
66
* @param name
77
* @returns {number}
88
*/
9-
function getCountBySameName(dir, name) {
9+
export function getCountBySameName(dir, name) {
1010
return readdirSync(dir).filter((filename) => filename.includes(name)).length;
11-
}
12-
13-
module.exports = { getCountBySameName };
11+
}

common/utils/getJSCode.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { getCodeDetailJson } = require('../resources/codeDetailJson');
1+
import {getCodeDetailJson} from "../resources/codeDetailJson.js";
22
/**
33
* 获取JS code 必须在获取基本信息之后调用 需要传入enName
44
* @returns {Promise<unknown>}
55
*/
6-
function getJSCode(enName) {
6+
export function getJSCode(enName) {
77
return new Promise((resolve) => {
88
fetch('https://leetcode.cn/graphql/', getCodeDetailJson(enName)).then(((res) => res.json())).then((res) => {
99
const id = res.data.question.questionFrontendId;
@@ -12,7 +12,4 @@ function getJSCode(enName) {
1212
resolve({ id, jsCode });
1313
});
1414
});
15-
}
16-
17-
18-
module.exports = { getJSCode };
15+
}

common/utils/getQuestionById.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
const { getQuestionSearchJson } = require('../resources/questionSearchJson');
2-
const { getQuestionDetail } = require('./getQuestionDetail');
3-
const { writeStore } = require('./store');
4-
5-
async function getQuestionById(id) {
1+
import {getQuestionSearchJson} from "../resources/questionSearchJson.js";
2+
import {getQuestionDetail} from "./getQuestionDetail.js";
3+
export async function getQuestionById(id) {
64
const base = await fetch('https://leetcode.cn/graphql/', getQuestionSearchJson(id.toString())).then((res) => res.json());
75
const questionContent = base.data.problemsetQuestionList.questions.find((o) => o.frontendQuestionId === id.toString());
86
if(!questionContent) {
@@ -15,5 +13,3 @@ async function getQuestionById(id) {
1513
const question = await getQuestionDetail(slug);
1614
return question;
1715
}
18-
19-
module.exports = { getQuestionById };

common/utils/getQuestionDetail.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { getJSCode } = require('./getJSCode');
2-
const { getQuestionDetailJson } = require('../resources/questionDetailJson');
1+
import {getJSCode} from "./getJSCode.js";
2+
import {getQuestionDetailJson} from "../resources/questionDetailJson.js";
33

4-
async function getQuestionDetail(slug, extra) {
4+
export async function getQuestionDetail(slug, extra) {
55
// 标题的英文字符串
66
const questionDetail = await fetch('https://leetcode.cn/graphql/', getQuestionDetailJson(slug)).then(((res) => res.json()));
77
const detail = questionDetail.data.question;
@@ -15,4 +15,3 @@ async function getQuestionDetail(slug, extra) {
1515
};
1616
}
1717

18-
module.exports = { getQuestionDetail };

common/utils/getQuestionRandom.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
const { getQuestionSearchJson } = require('../resources/questionSearchJson');
2-
const { getQuestionDetail } = require('./getQuestionDetail');
3-
const { writeStore } = require('./store');
1+
import {getQuestionSearchJson} from "../resources/questionSearchJson.js";
2+
import {getQuestionDetail} from "./getQuestionDetail.js";
43

5-
async function getQuestionRandom(id) {
4+
export async function getQuestionRandom(id) {
65
const base = await fetch('https://leetcode.cn/graphql/', getQuestionSearchJson(id.toString())).then((res) => res.json());
76
const slug = base.data.problemsetQuestionList.questions.find((o) => o.frontendQuestionId === id.toString()).titleSlug;
87
const question = await getQuestionDetail(slug);
98
return question;
109
}
11-
12-
module.exports = { getQuestionRandom };

common/utils/getQuestionToday.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { writeStore } = require('./store');
2-
const { getQuestionTodayJson } = require('../resources/questionTodayJson');
3-
const { getQuestionDetail } = require('./getQuestionDetail');
1+
import {writeStore} from "./store.js";
2+
import {getQuestionTodayJson} from "../resources/questionTodayJson.js";
3+
import {getQuestionDetail} from "./getQuestionDetail.js";
44

5-
async function getQuestionToday() {
5+
export async function getQuestionToday() {
66
const question = await fetch('https://leetcode.cn/graphql/', getQuestionTodayJson()).then(((res) => res.json()));
77
const today = question.data.todayRecord[0].question;
88
const { date } = question.data.todayRecord[0];
@@ -11,4 +11,3 @@ async function getQuestionToday() {
1111
return questionInfo;
1212
}
1313

14-
module.exports = { getQuestionToday };

common/utils/getQuestionUrl.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
function getQuestionUrl(enName) {
1+
export function getQuestionUrl(enName) {
22
return `https://leetcode.cn/problems/${enName}/`;
33
}
4-
5-
module.exports = { getQuestionUrl };

0 commit comments

Comments
 (0)