Skip to content

Commit d80364c

Browse files
committed
🎨 style(app): 代码优化
1 parent 409a261 commit d80364c

File tree

2 files changed

+34
-134
lines changed

2 files changed

+34
-134
lines changed

src/helper.ts

Lines changed: 34 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -134,96 +134,6 @@ export const printError = (content: string | unknown) => {
134134
);
135135
};
136136

137-
/**
138-
* 根据后缀列表过滤获取合法的文件列表
139-
* @param fileList
140-
* @param suffixes
141-
* @returns
142-
*/
143-
export function getFiilesBySuffixes(
144-
fileList: string[],
145-
suffixes: string[],
146-
): string[] {
147-
const paths: string[] = [];
148-
149-
for (let i = 0; i < fileList.length; i++) {
150-
const file = fileList[i];
151-
152-
for (let j = 0; j < suffixes.length; j++) {
153-
const extension = suffixes[j];
154-
155-
if (file.endsWith(extension)) {
156-
paths.push(file);
157-
break;
158-
}
159-
}
160-
}
161-
return paths;
162-
}
163-
164-
/**
165-
* 获取目录列表下所有的文件列表
166-
*
167-
* @param paths
168-
* @returns
169-
*/
170-
export function getEveryFiles(paths: string[]): string[] {
171-
const fileList: string[] = [];
172-
function traverseDirectory(dirPath: string) {
173-
const files = fs.readdirSync(dirPath);
174-
175-
files.forEach((file) => {
176-
const filePath = path.join(dirPath, file);
177-
const stat = fs.statSync(filePath);
178-
179-
if (stat.isDirectory()) {
180-
traverseDirectory(filePath);
181-
} else {
182-
fileList.push(filePath);
183-
}
184-
});
185-
}
186-
187-
paths.forEach((dirPath) => {
188-
traverseDirectory(dirPath);
189-
});
190-
191-
return Array.from(new Set(fileList));
192-
}
193-
194-
/**
195-
* 获取指定目录后暂存区所有符合给定后缀的文件列表
196-
* @param cwd
197-
* @param staged
198-
* @param paths
199-
* @param suffix
200-
* @returns
201-
*/
202-
export const getEveryFilesBySuffixes = async (
203-
cwd: string,
204-
staged: boolean,
205-
paths: string[],
206-
suffix: string[],
207-
) => {
208-
let files: string[] = [];
209-
if (staged) {
210-
const result = await execCommand("git", [
211-
"diff",
212-
"--name-only",
213-
"--diff-filter=d",
214-
"--cached",
215-
]);
216-
files = result?.split("\n").map((path: string) => `${cwd}/${path}`) || [];
217-
} else {
218-
files = getEveryFiles(paths.map((path) => `${cwd}/${path}`));
219-
}
220-
return getFiilesBySuffixes(files, suffix);
221-
};
222-
223-
export function formatTargetDir(targetDir: string | undefined) {
224-
return targetDir?.trim().replace(/\/+$/g, "");
225-
}
226-
227137
export function isValidPackageName(projectName: string) {
228138
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
229139
projectName,
@@ -284,6 +194,11 @@ export function isValidVariant(framework: string) {
284194
return variants.length > 0;
285195
}
286196

197+
/**
198+
* 生成随机串
199+
* @param length
200+
* @returns
201+
*/
287202
export function generateRandom(length: number) {
288203
let result = "";
289204
const characters =
@@ -295,9 +210,19 @@ export function generateRandom(length: number) {
295210
return result;
296211
}
297212

213+
/**
214+
* 用户配置工厂函数
215+
* @param config
216+
* @returns
217+
*/
298218
export const defineConfig = (config: CodeGeniusOptions): CodeGeniusOptions =>
299219
config;
300220

221+
/**
222+
* 用于循环注册指令
223+
* @param cli
224+
* @param config
225+
*/
301226
export async function cmdInstaller(cli: CAC, config: CodeGeniusOptions) {
302227
const { plugins } = config;
303228
if (plugins) {
@@ -307,6 +232,11 @@ export async function cmdInstaller(cli: CAC, config: CodeGeniusOptions) {
307232
}
308233
}
309234

235+
/**
236+
* 用于转换 scripts 结构
237+
* @param scripts
238+
* @returns
239+
*/
310240
export function genScriptConfig(scripts: { [key: string]: string }) {
311241
return Object.keys(scripts).map((key) => {
312242
return {
@@ -317,6 +247,12 @@ export function genScriptConfig(scripts: { [key: string]: string }) {
317247
});
318248
}
319249

250+
/**
251+
* 用于合并两份 scripts.config.json 数据
252+
* @param pkgScripts
253+
* @param configScripts
254+
* @returns
255+
*/
320256
export function syncScripts(
321257
pkgScripts: Array<CommandOptions>,
322258
configScripts: Array<CommandOptions>,
@@ -337,13 +273,16 @@ export function syncScripts(
337273
}
338274
}
339275

340-
const syncScripts = mergedScripts.filter((configScript) => {
276+
const scripts = mergedScripts.filter((configScript) => {
341277
return pkgScripts.find((i) => i.cmd === configScript.cmd);
342278
});
343279

344-
return syncScripts;
280+
return scripts;
345281
}
346282

283+
/**
284+
* 读取 package.json 解析并生成 scripts.config.json 配置文件
285+
*/
347286
export const generateScripts = async () => {
348287
const pkg = await fsExtra.readJSONSync(
349288
path.join(process.cwd(), "package.json"),
@@ -367,6 +306,9 @@ export const generateScripts = async () => {
367306
printInfo("代理脚本 scripts.config.json 已完成同步");
368307
};
369308

309+
/**
310+
* 动态加载用户配置文件
311+
*/
370312
export async function loadConfigModule(): Promise<
371313
CodeGeniusOptions | undefined
372314
> {

src/types.ts

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { CAC } from "cac";
22

33
export type ColorFunc = (str: string | number) => string;
4-
export interface CommandSet {
5-
[key: string]: (cli: CAC) => void;
6-
}
74

85
export interface CommitType {
96
emoji: string;
@@ -59,45 +56,6 @@ export interface CommitOptions {
5956
gitCommitScopes?: Array<CommitScope>;
6057
}
6158

62-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
63-
export interface VerifyOptions {
64-
// TODO
65-
}
66-
67-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
68-
export interface HooksOptions {
69-
// TODO
70-
}
71-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
72-
export interface DepcheckOptions {
73-
// TODO
74-
}
75-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
76-
export interface RegistryOptions {
77-
// TODO
78-
}
79-
80-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
81-
export interface TemplateOptions {
82-
// TODO
83-
}
84-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
85-
export interface FixOptions {
86-
// TODO
87-
}
88-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
89-
export interface FormatOptions {
90-
// TODO
91-
}
92-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
93-
export interface ImpsortOptions {
94-
// TODO
95-
}
96-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
97-
export interface ScriptOptions {
98-
// TODO
99-
}
100-
10159
export interface CodeGeniusOptions {
10260
plugins?: Array<
10361
(cli: CAC) => {

0 commit comments

Comments
 (0)