修复脚本管理数据

This commit is contained in:
whyour 2021-11-12 23:18:24 +08:00
parent ee0b47d101
commit a2ce3304de
4 changed files with 67 additions and 54 deletions

View File

@ -1,10 +1,15 @@
import { getFileContentByName, getLastModifyFilePath } from '../config/util';
import {
fileExist,
getFileContentByName,
getLastModifyFilePath,
} from '../config/util';
import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi';
import { Logger } from 'winston';
import config from '../config';
import * as fs from 'fs';
import { celebrate, Joi } from 'celebrate';
import path from 'path';
const route = Router();
export default (app: Router) => {
@ -16,49 +21,55 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger');
try {
const fileList = fs.readdirSync(config.scriptPath, 'utf-8');
let result = [];
for (let i = 0; i < fileList.length; i++) {
const fileOrDir = fileList[i];
const fPath = path.join(config.scriptPath, fileOrDir);
const dirStat = fs.statSync(fPath);
if (['node_modules'].includes(fileOrDir)) {
continue;
}
if (dirStat.isDirectory()) {
const childFileList = fs.readdirSync(fPath, 'utf-8');
let children = [];
for (let j = 0; j < childFileList.length; j++) {
const childFile = childFileList[j];
const sPath = path.join(config.scriptPath, fileOrDir, childFile);
const _fileExist = await fileExist(sPath);
if (_fileExist && fs.statSync(sPath).isFile()) {
const statObj = fs.statSync(sPath);
children.push({
title: childFile,
value: childFile,
key: childFile,
mtime: statObj.mtimeMs,
parent: fileOrDir,
});
}
}
result.push({
title: fileOrDir,
value: fileOrDir,
key: fileOrDir,
mtime: dirStat.mtimeMs,
disabled: true,
children: children.sort((a, b) => b.mtime - a.mtime),
});
} else {
result.push({
title: fileOrDir,
value: fileOrDir,
key: fileOrDir,
mtime: dirStat.mtimeMs,
});
}
}
res.send({
code: 200,
data: fileList
.map((x) => {
if (fs.lstatSync(config.scriptPath + x).isDirectory()) {
const childFileList = fs.readdirSync(
config.scriptPath + x,
'utf-8',
);
const dirStat = fs.statSync(`${config.scriptPath}${x}`);
return {
title: x,
value: x,
key: x,
mtime: dirStat.mtimeMs,
disabled: true,
children: childFileList
.filter(
(y) =>
!fs
.lstatSync(`${config.scriptPath}${x}/${y}`)
.isDirectory(),
)
.map((y) => {
const statObj = fs.statSync(
`${config.scriptPath}${x}/${y}`,
);
return {
title: y,
value: y,
key: y,
mtime: statObj.mtimeMs,
parent: x,
};
})
.sort((a, b) => b.mtime - a.mtime),
};
} else {
const statObj = fs.statSync(config.scriptPath + x);
return { title: x, value: x, key: x, mtime: statObj.mtimeMs };
}
})
.sort((a, b) => b.mtime - a.mtime),
data: result,
});
} catch (e) {
logger.error('🔥 error: %o', e);

View File

@ -220,3 +220,14 @@ export function getPlatform(userAgent: string): 'mobile' | 'desktop' {
return platform as 'mobile' | 'desktop';
}
export async function fileExist(file: any) {
return new Promise((resolve) => {
try {
fs.accessSync(file);
resolve(true);
} catch (error) {
resolve(false);
}
});
}

View File

@ -2,6 +2,7 @@ import DataStore from 'nedb';
import config from '../config';
import Logger from './logger';
import fs from 'fs';
import { fileExist } from '../config/util';
interface Dbs {
cronDb: DataStore;
@ -13,17 +14,6 @@ interface Dbs {
const db: Dbs = {} as any;
async function fileExist(file: any) {
return new Promise((resolve) => {
try {
fs.accessSync(file);
resolve(true);
} catch (error) {
resolve(false);
}
});
}
async function truncateDb() {
return new Promise(async (resolve) => {
const files = [

View File

@ -104,7 +104,8 @@ add_cron() {
local detail=""
cd $dir_scripts
for file in $(cat $list_add); do
local file_name=${file/${path}\_/}
local file_name=${file/${path}\//}
file_name=${file/${path}\_/}
if [[ -f $file ]]; then
cron_line=$(
perl -ne "{