Skip to content

Commit 26fcffb

Browse files
committedFeb 5, 2024
feat(cli): 简化命令 使得命令可以在任何路径下运行
1 parent 1b8324d commit 26fcffb

File tree

8 files changed

+74
-44
lines changed

8 files changed

+74
-44
lines changed
 

‎bin/lc-enter.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#! /usr/bin/env node
2+
3+
const init = async() => {
4+
await import('../common/scripts/create.js');
5+
6+
}
7+
init()
8+
9+

‎bin/lk-enter.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /usr/bin/env node
2+
const init = async() => {
3+
await import('../common/scripts/check.js');
4+
5+
}
6+
init()

‎common/resources/store.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"today":"2182","today-question-info":{"enName":"jump-game-vi","title":"跳跃游戏 VI","detail":"<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>\n\n<p>一开始你在下标 <code>0</code> 处。每一步,你最多可以往前跳 <code>k</code> 步,但你不能跳出数组的边界。也就是说,你可以从下标 <code>i</code> 跳到 <code>[i + 1, min(n - 1, i + k)]</code> <strong>包含</strong> 两个端点的任意位置。</p>\n\n<p>你的目标是到达数组最后一个位置(下标为 <code>n - 1</code> ),你的 <strong>得分</strong> 为经过的所有数字之和。</p>\n\n<p>请你返回你能得到的 <strong>最大得分</strong> 。</p>\n\n<p> </p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>nums = [<strong>1</strong>,<strong>-1</strong>,-2,<strong>4</strong>,-7,<strong>3</strong>], k = 2\n<b>输出:</b>7\n<b>解释:</b>你可以选择子序列 [1,-1,4,3] (上面加粗的数字),和为 7 。\n</pre>\n\n<p><strong>示例 2:</strong></p>\n\n<pre>\n<strong>输入:</strong>nums = [<strong>10</strong>,-5,-2,<strong>4</strong>,0,<strong>3</strong>], k = 3\n<b>输出:</b>17\n<b>解释:</b>你可以选择子序列 [10,4,3] (上面加粗数字),和为 17 。\n</pre>\n\n<p><strong>示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>nums = [1,-5,-20,4,-1,3,-6,-3], k = 2\n<b>输出:</b>0\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li> <code>1 <= nums.length, k <= 10<sup>5</sup></code></li>\n\t<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>\n</ul>\n","id":"1696","jsCode":"/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar maxResult = function(nums, k) {\n\n};","date":"2024-02-05"},"today-tag":"82","random-id":1314,"random-question-info":{"enName":"multiply-strings","title":"字符串相乘","detail":"<p>给定两个以字符串形式表示的非负整数&nbsp;<code>num1</code>&nbsp;和&nbsp;<code>num2</code>,返回&nbsp;<code>num1</code>&nbsp;和&nbsp;<code>num2</code>&nbsp;的乘积,它们的乘积也表示为字符串形式。</p>\n\n<p><strong>注意:</strong>不能使用任何内置的 BigInteger 库或直接将输入转换为整数。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre>\n<strong>输入:</strong> num1 = \"2\", num2 = \"3\"\n<strong>输出:</strong> \"6\"</pre>\n\n<p><strong>示例&nbsp;2:</strong></p>\n\n<pre>\n<strong>输入:</strong> num1 = \"123\", num2 = \"456\"\n<strong>输出:</strong> \"56088\"</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= num1.length, num2.length &lt;= 200</code></li>\n\t<li><code>num1</code>&nbsp;和 <code>num2</code>&nbsp;只能由数字组成。</li>\n\t<li><code>num1</code>&nbsp;和 <code>num2</code>&nbsp;都不包含任何前导零,除了数字0本身。</li>\n</ul>\n","id":"43","jsCode":"/**\n * @param {string} num1\n * @param {string} num2\n * @return {string}\n */\nvar multiply = function(num1, num2) {\n\n};"},"specified-question-info":{"enName":"reverse-nodes-in-k-group","title":"K 个一组翻转链表","detail":"<p>给你链表的头节点 <code>head</code> ,每&nbsp;<code>k</code><em>&nbsp;</em>个节点一组进行翻转,请你返回修改后的链表。</p>\n\n<p><code>k</code> 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是&nbsp;<code>k</code><em>&nbsp;</em>的整数倍,那么请将最后剩余的节点保持原有顺序。</p>\n\n<p>你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg\" style=\"width: 542px; height: 222px;\" />\n<pre>\n<strong>输入:</strong>head = [1,2,3,4,5], k = 2\n<strong>输出:</strong>[2,1,4,3,5]\n</pre>\n\n<p><strong>示例 2:</strong></p>\n\n<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg\" style=\"width: 542px; height: 222px;\" /></p>\n\n<pre>\n<strong>输入:</strong>head = [1,2,3,4,5], k = 3\n<strong>输出:</strong>[3,2,1,4,5]\n</pre>\n\n<p>&nbsp;</p>\n<strong>提示:</strong>\n\n<ul>\n\t<li>链表中的节点数目为 <code>n</code></li>\n\t<li><code>1 &lt;= k &lt;= n &lt;= 5000</code></li>\n\t<li><code>0 &lt;= Node.val &lt;= 1000</code></li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>进阶:</strong>你可以设计一个只用 <code>O(1)</code> 额外内存空间的算法解决此问题吗?</p>\n\n<ul>\n</ul>\n","id":"25","jsCode":"/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @param {number} k\n * @return {ListNode}\n */\nvar reverseKGroup = function(head, k) {\n\n};"}}
1+
{"today":"2182","today-question-info":{"enName":"jump-game-vi","title":"跳跃游戏 VI","detail":"<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>\n\n<p>一开始你在下标 <code>0</code> 处。每一步,你最多可以往前跳 <code>k</code> 步,但你不能跳出数组的边界。也就是说,你可以从下标 <code>i</code> 跳到 <code>[i + 1, min(n - 1, i + k)]</code> <strong>包含</strong> 两个端点的任意位置。</p>\n\n<p>你的目标是到达数组最后一个位置(下标为 <code>n - 1</code> ),你的 <strong>得分</strong> 为经过的所有数字之和。</p>\n\n<p>请你返回你能得到的 <strong>最大得分</strong> 。</p>\n\n<p> </p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>nums = [<strong>1</strong>,<strong>-1</strong>,-2,<strong>4</strong>,-7,<strong>3</strong>], k = 2\n<b>输出:</b>7\n<b>解释:</b>你可以选择子序列 [1,-1,4,3] (上面加粗的数字),和为 7 。\n</pre>\n\n<p><strong>示例 2:</strong></p>\n\n<pre>\n<strong>输入:</strong>nums = [<strong>10</strong>,-5,-2,<strong>4</strong>,0,<strong>3</strong>], k = 3\n<b>输出:</b>17\n<b>解释:</b>你可以选择子序列 [10,4,3] (上面加粗数字),和为 17 。\n</pre>\n\n<p><strong>示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>nums = [1,-5,-20,4,-1,3,-6,-3], k = 2\n<b>输出:</b>0\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li> <code>1 <= nums.length, k <= 10<sup>5</sup></code></li>\n\t<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>\n</ul>\n","id":"1696","jsCode":"/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar maxResult = function(nums, k) {\n\n};","date":"2024-02-05"},"today-tag":"82","random-id":1314,"random-question-info":{"enName":"multiply-strings","title":"字符串相乘","detail":"<p>给定两个以字符串形式表示的非负整数&nbsp;<code>num1</code>&nbsp;和&nbsp;<code>num2</code>,返回&nbsp;<code>num1</code>&nbsp;和&nbsp;<code>num2</code>&nbsp;的乘积,它们的乘积也表示为字符串形式。</p>\n\n<p><strong>注意:</strong>不能使用任何内置的 BigInteger 库或直接将输入转换为整数。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre>\n<strong>输入:</strong> num1 = \"2\", num2 = \"3\"\n<strong>输出:</strong> \"6\"</pre>\n\n<p><strong>示例&nbsp;2:</strong></p>\n\n<pre>\n<strong>输入:</strong> num1 = \"123\", num2 = \"456\"\n<strong>输出:</strong> \"56088\"</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= num1.length, num2.length &lt;= 200</code></li>\n\t<li><code>num1</code>&nbsp;和 <code>num2</code>&nbsp;只能由数字组成。</li>\n\t<li><code>num1</code>&nbsp;和 <code>num2</code>&nbsp;都不包含任何前导零,除了数字0本身。</li>\n</ul>\n","id":"43","jsCode":"/**\n * @param {string} num1\n * @param {string} num2\n * @return {string}\n */\nvar multiply = function(num1, num2) {\n\n};"},"specified-question-info":{"enName":"design-add-and-search-words-data-structure","title":"添加与搜索单词 - 数据结构设计","detail":"<p>请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。</p>\n\n<p>实现词典类 <code>WordDictionary</code> :</p>\n\n<ul>\n\t<li><code>WordDictionary()</code> 初始化词典对象</li>\n\t<li><code>void addWord(word)</code> 将 <code>word</code> 添加到数据结构中,之后可以对它进行匹配</li>\n\t<li><code>bool search(word)</code> 如果数据结构中存在字符串与&nbsp;<code>word</code> 匹配,则返回 <code>true</code> ;否则,返回&nbsp; <code>false</code> 。<code>word</code> 中可能包含一些 <code>'.'</code> ,每个&nbsp;<code>.</code> 都可以表示任何一个字母。</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>示例:</strong></p>\n\n<pre>\n<strong>输入:</strong>\n[\"WordDictionary\",\"addWord\",\"addWord\",\"addWord\",\"search\",\"search\",\"search\",\"search\"]\n[[],[\"bad\"],[\"dad\"],[\"mad\"],[\"pad\"],[\"bad\"],[\".ad\"],[\"b..\"]]\n<strong>输出:</strong>\n[null,null,null,null,false,true,true,true]\n\n<strong>解释:</strong>\nWordDictionary wordDictionary = new WordDictionary();\nwordDictionary.addWord(\"bad\");\nwordDictionary.addWord(\"dad\");\nwordDictionary.addWord(\"mad\");\nwordDictionary.search(\"pad\"); // 返回 False\nwordDictionary.search(\"bad\"); // 返回 True\nwordDictionary.search(\".ad\"); // 返回 True\nwordDictionary.search(\"b..\"); // 返回 True\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= word.length &lt;= 25</code></li>\n\t<li><code>addWord</code> 中的 <code>word</code> 由小写英文字母组成</li>\n\t<li><code>search</code> 中的 <code>word</code> 由 '.' 或小写英文字母组成</li>\n\t<li>最多调用 <code>10<sup>4</sup></code> 次 <code>addWord</code> 和 <code>search</code></li>\n</ul>\n","id":"211","jsCode":"\nvar WordDictionary = function() {\n\n};\n\n/** \n * @param {string} word\n * @return {void}\n */\nWordDictionary.prototype.addWord = function(word) {\n\n};\n\n/** \n * @param {string} word\n * @return {boolean}\n */\nWordDictionary.prototype.search = function(word) {\n\n};\n\n/**\n * Your WordDictionary object will be instantiated and called as such:\n * var obj = new WordDictionary()\n * obj.addWord(word)\n * var param_2 = obj.search(word)\n */"}}

‎common/scripts/create.js

100644100755
+40-35
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,49 @@ import {getRandomId} from "#common/utils/getRandomId.js";
1313
* -i id 指定题目id
1414
* 工作区为 根目录
1515
*/
16-
const args = process.argv.slice(2);
17-
switch (args[0]) {
18-
case "-r":
19-
getRandomId().then(id => {
20-
getQuestionById(id).then(question => {
21-
const random = `${question.id}.${question.enName}`;
22-
writeStore("random-question-info", question);
23-
createQuestion(random).then((filePath) => {
24-
fulfillQuestion(filePath, question);
16+
export const create = () => {
17+
const args = process.argv.slice(2);
18+
switch (args[0]) {
19+
case "-r":
20+
getRandomId().then(id => {
21+
getQuestionById(id).then(question => {
22+
const random = `${question.id}.${question.enName}`;
23+
writeStore("random-question-info", question);
24+
createQuestion(random).then((filePath) => {
25+
fulfillQuestion(filePath, question);
26+
})
2527
})
2628
})
27-
})
28-
break;
29-
case "-i":
30-
const id = args[1];
31-
if (id === undefined) {
32-
console.warn("请指定对应的编号!")
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) => {
29+
break;
30+
case "-i":
31+
const id = args[1];
32+
if (id === undefined) {
33+
console.warn("请指定对应的编号!")
34+
}else{
35+
console.log(`获取指定编号[${id}]的题目...`)
36+
getQuestionById(id).then(question => {
37+
const specified = `${question.id}.${question.enName}`;
38+
writeStore("specified-question-info", question);
39+
createQuestion(specified).then((filePath) => {
40+
fulfillQuestion(filePath, question);
41+
})
42+
})
43+
}
44+
break;
45+
case "-t":
46+
default:
47+
console.log("开始获取今日题目")
48+
// 获取问题的全部信息
49+
getQuestionToday().then(question => {
50+
const today = `${question.id}.${question.enName}`;
51+
createQuestion(today).then((filePath) => {
3952
fulfillQuestion(filePath, question);
4053
})
4154
})
42-
}
43-
break;
44-
case "-t":
45-
default:
46-
console.log("开始获取今日题目")
47-
// 获取问题的全部信息
48-
getQuestionToday().then(question => {
49-
const today = `${question.id}.${question.enName}`;
50-
createQuestion(today).then((filePath) => {
51-
fulfillQuestion(filePath, question);
52-
})
53-
})
54-
break;
55+
break;
56+
}
57+
58+
5559
}
56-
60+
create()
61+

‎common/utils/createQuestion.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ import fs from "fs";
22
import path from "path";
33
import readlinePromises from "node:readline/promises";
44
import {getCountBySameName} from "./getCountBySameName.js";
5-
6-
7-
export const sourceFilePath = path.normalize('./common/template/template.js');
5+
import { __dirname} from '#common/utils/getDirname.js'
6+
export const sourceFilePath = path.normalize(path.resolve(__dirname, '../template/template.js'));
87
export function createQuestion(newPath) {
98
const rl = readlinePromises.createInterface({
109
input: process.stdin,
1110
output: process.stdout,
1211
});
1312
return new Promise((resolve, reject) => {
14-
let newDir = path.normalize(`./src/${newPath}`);
13+
let newDir = path.normalize(`${process.cwd()}/src/${newPath}`);
1514
let newFilePath = path.join(newDir, 'index.js');
1615
// 判断是否存在
1716
fs.exists(newFilePath, async (exists) => {

‎common/utils/getDirname.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { fileURLToPath } from 'url';
2+
import { dirname } from 'path';
3+
const __filename = fileURLToPath(import.meta.url);
4+
export const __dirname = dirname(__filename);

‎common/utils/store.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
*/
44
import fs from "fs";
55
import path from "path";
6-
6+
import { __dirname} from '#common/utils/getDirname.js'
7+
const absolutePath = path.resolve(__dirname, '../../common/resources/store.json');
78
export function readStore(key) {
8-
const rawData = fs.readFileSync(path.normalize('common/resources/store.json'));
9+
const rawData = fs.readFileSync(path.normalize(absolutePath));
910
return JSON.parse(rawData)?.[key];
1011
}
1112

1213
export function writeStore(key, value) {
13-
const raw = fs.readFileSync(path.normalize('common/resources/store.json'));
14+
const raw = fs.readFileSync(path.normalize(absolutePath));
1415
const json = JSON.parse(raw ?? {});
15-
fs.writeFileSync(path.normalize('common/resources/store.json'), JSON.stringify(Object.assign(json, { [key]: value })));
16+
fs.writeFileSync(path.normalize(absolutePath), JSON.stringify(Object.assign(json, { [key]: value })));
1617
console.log(`[store]数据存储成功[${key}]:[${value}]`);
1718
}
1819

‎package.json

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"imports": {
88
"#common/*": "./common/*"
99
},
10+
"bin": {
11+
"lc": "./bin/lc-enter.js",
12+
"lk": "./bin/lk-enter.js"
13+
},
1014
"scripts": {
1115
"leet-create": "node common/scripts/create.js",
1216
"leet-create:i": "node common/scripts/create.js -i",
@@ -30,7 +34,9 @@
3034
"eslint": "^8.56.0",
3135
"eslint-config-airbnb-base": "^15.0.0",
3236
"eslint-plugin-import": "^2.29.1",
37+
"inquirer": "^9.2.14",
3338
"rimraf": "^5.0.5",
39+
"shelljs": "^0.8.5",
3440
"vitest": "^1.2.2"
3541
},
3642
"config": {

0 commit comments

Comments
 (0)
Please sign in to comment.