修复脚本管理查询逻辑

This commit is contained in:
whyour 2025-04-25 01:40:37 +08:00
parent 1befa1bb8c
commit 40a831f3a2
2 changed files with 49 additions and 21 deletions

View File

@ -37,11 +37,11 @@ export default (app: Router) => {
'package-lock.json', 'package-lock.json',
]; ];
if (req.query.path) { if (req.query.path) {
const targetPath = path.join( result = await readDir(
config.scriptPath,
req.query.path as string, req.query.path as string,
config.scriptPath,
blacklist,
); );
result = await readDir(targetPath, config.scriptPath, blacklist);
} else { } else {
result = await readDirs( result = await readDirs(
config.scriptPath, config.scriptPath,

View File

@ -306,23 +306,51 @@ export async function readDir(
dir: string, dir: string,
baseDir: string = '', baseDir: string = '',
blacklist: string[] = [], blacklist: string[] = [],
) { ): Promise<IFile[]> {
const relativePath = path.relative(baseDir, dir); const absoluteDir = path.join(baseDir, dir);
const files = await fs.readdir(dir); const relativePath = path.relative(baseDir, absoluteDir);
const result: any = files
.filter((x) => !blacklist.includes(x)) try {
.map(async (file: string) => { const files = await fs.readdir(absoluteDir);
const subPath = path.join(dir, file); const result: IFile[] = [];
for (const file of files) {
const subPath = path.join(absoluteDir, file);
const stats = await fs.lstat(subPath); const stats = await fs.lstat(subPath);
const key = path.join(relativePath, file); const key = path.join(relativePath, file);
return {
title: file, if (blacklist.includes(file) || stats.isSymbolicLink()) {
type: stats.isDirectory() ? 'directory' : 'file', continue;
key, }
parent: relativePath,
}; if (stats.isDirectory()) {
}); result.push({
return result; title: file,
type: 'directory',
key,
parent: relativePath,
createTime: stats.birthtime.getTime(),
children: [],
});
} else {
result.push({
title: file,
type: 'file',
key,
parent: relativePath,
size: stats.size,
createTime: stats.birthtime.getTime(),
});
}
}
return result;
} catch (error: any) {
if (error.code === 'ENOENT') {
return [];
}
throw error;
}
} }
export async function promiseExec(command: string): Promise<string> { export async function promiseExec(command: string): Promise<string> {
@ -353,9 +381,9 @@ export function parseHeaders(headers: string) {
if (!headers) return {}; if (!headers) return {};
const parsed: any = {}; const parsed: any = {};
let key; let key: string;
let val; let val: string;
let i; let i: number;
headers && headers &&
headers.split('\n').forEach(function parser(line) { headers.split('\n').forEach(function parser(line) {