-
Notifications
You must be signed in to change notification settings - Fork 0
[PR] 新增题目文件创建器 #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
.eslintignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# EJS template | ||
**/*.ejs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
creator/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import inquirer from 'inquirer' | ||
import ejs from 'ejs' | ||
import path from 'path' | ||
import { fileURLToPath } from 'url' | ||
import * as fs from 'fs/promises' | ||
|
||
// 获取当前所在路径 | ||
const dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
/** EJS 模板参数 */ | ||
const templateParams = { | ||
/** 题目编号 */ | ||
titleNumber: 0, | ||
/** 题目中文标题 */ | ||
titleChinese: '', | ||
/** 题目英文标题 */ | ||
titleEnglish: '', | ||
/** LeetCode 入口函数名 */ | ||
leetCodeFunctionName: '', | ||
/** LeetCode 题目链接 */ | ||
problemLink: '', | ||
/** 题目难度(简单 中等 困难) */ | ||
difficulty: '', | ||
/** JS 模块名称,格式:[题目编号]_[英文小驼峰题目名称] */ | ||
moduleName: '' | ||
} | ||
|
||
/** | ||
* 将英文句子转换为小驼峰字符串 | ||
* @param {string} sentence - 英文句子 | ||
*/ | ||
function getLowerCamelCaseBySentence (sentence: string): string { | ||
let result: string = '' | ||
const words = sentence.split(' ').map(word => word.toLowerCase()) | ||
result += words.shift() ?? '' | ||
words.forEach(word => { result += word.charAt(0).toUpperCase() + word.slice(1) }) | ||
return result | ||
} | ||
|
||
/** | ||
* 通过用户输入设置模板参数 | ||
*/ | ||
async function setTemplateParams (): Promise<void> { | ||
// 通过控制台用户输入或选择参数 | ||
const answers = await inquirer.prompt( | ||
[ | ||
{ name: 'titleNumber', type: 'number', message: 'Title number:' }, | ||
{ name: 'titleChinese', type: 'string', message: 'Chinese title:' }, | ||
{ name: 'titleEnglish', type: 'string', message: 'English title:' }, | ||
{ name: 'leetCodeFunctionName', type: 'string', message: 'LeetCode function name:', default: 'main' }, | ||
{ name: 'problemLink', type: 'string', message: 'LeetCode link:' }, | ||
{ name: 'difficulty', type: 'list', message: 'Difficulty:', choices: ['Easy', 'Medium', 'Hard'], default: 'Medium' } | ||
] | ||
) | ||
|
||
// 设置难度 | ||
answers.difficulty = { | ||
Easy: '简单', | ||
Medium: '中等', | ||
Hard: '困难' | ||
}[answers.difficulty as string] | ||
|
||
// 设置模块名称 | ||
templateParams.moduleName = `${answers.titleNumber as string}_${getLowerCamelCaseBySentence(answers.titleEnglish)}` | ||
|
||
// 合并模板参数 | ||
Object.assign(templateParams, answers) | ||
} | ||
|
||
/** | ||
* 创建文件 | ||
* @description 创建源码、单元测试、测试用例文件 | ||
*/ | ||
function createFiles (): void { | ||
// 创建源码文件 | ||
const srcFilePath = path.join(dirname, `../src/${templateParams.moduleName}.ts`) | ||
ejs.renderFile(path.join(dirname, './templates/src.ts.ejs'), templateParams).then(result => { | ||
void fs.writeFile(srcFilePath, result) | ||
}) | ||
|
||
// 创建单元测试文件 | ||
const unitTestFilePath = path.join(dirname, `../tests/${templateParams.moduleName}.test.ts`) | ||
ejs.renderFile(path.join(dirname, './templates/unitTest.ts.ejs'), templateParams).then(result => { | ||
void fs.writeFile(unitTestFilePath, result) | ||
}) | ||
|
||
// 创建测试用例文件 | ||
const testCaseFilePath = path.join(dirname, `../tests/testCases/${templateParams.moduleName}.ts`) | ||
ejs.renderFile(path.join(dirname, './templates/testCase.ts.ejs'), templateParams).then(result => { | ||
void fs.writeFile(testCaseFilePath, result) | ||
}) | ||
} | ||
|
||
// 程序入口 | ||
void (async function (): Promise<void> { | ||
// 设置模板参数 | ||
await setTemplateParams() | ||
|
||
// 创建文件 | ||
createFiles() | ||
})() |
27 changes: 27 additions & 0 deletions
creator/templates/src.ts.ejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
<%= titleNumber %>. <%= titleChinese %> | ||
难度:<%= difficulty %> | ||
题目链接:<%= problemLink %> | ||
|
||
|
||
*/ | ||
|
||
import { fileURLToPath } from 'url' | ||
import testCases from '../tests/testCases/<%= moduleName %>.js' | ||
|
||
/** | ||
* <%= titleChinese %> | ||
* - LeetCode 入口 | ||
* @param {string} param1 - | ||
* @returns {string} | ||
*/ | ||
export function <%= leetCodeFunctionName %> (param1: string): string { | ||
|
||
} | ||
|
||
// Debug | ||
if (process.argv[1] === fileURLToPath(import.meta.url)) { | ||
const { input, expected } = testCases[0] | ||
const output = <%= leetCodeFunctionName %>(input) | ||
console.log({ input, output, expected }) | ||
} |
4 changes: 4 additions & 0 deletions
creator/templates/testCase.ts.ejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default [ | ||
{ input: 'input_1', expected: 'expected_1' }, | ||
{ input: 'input_2', expected: 'expected_2' } | ||
] |
11 changes: 11 additions & 0 deletions
creator/templates/unitTest.ts.ejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { describe, test, expect } from '@jest/globals' | ||
import testCases from './testCases/<%= moduleName %>.js' | ||
import { <%= leetCodeFunctionName %> } from '../src/<%= moduleName %>.js' | ||
|
||
describe('<%= titleNumber %>. <%= titleEnglish %>', () => { | ||
testCases.forEach((testCase, index) => { | ||
test(`Test case index: ${index}, Input: ${testCase.input}`, () => { | ||
expect(<%= leetCodeFunctionName %>(testCase.input)).toEqual(testCase.expected) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.