From a2ce3304de89e470b3bca4561a6ebc96be564b93 Mon Sep 17 00:00:00 2001 From: whyour Date: Fri, 12 Nov 2021 23:18:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=84=9A=E6=9C=AC=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/script.ts | 95 +++++++++++++++++++++++++-------------------- back/config/util.ts | 11 ++++++ back/loaders/db.ts | 12 +----- shell/update.sh | 3 +- 4 files changed, 67 insertions(+), 54 deletions(-) diff --git a/back/api/script.ts b/back/api/script.ts index 83391e28..ce85d2f8 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -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); diff --git a/back/config/util.ts b/back/config/util.ts index c9938277..30f04a1e 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -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); + } + }); +} diff --git a/back/loaders/db.ts b/back/loaders/db.ts index e89de4f9..95089f5d 100644 --- a/back/loaders/db.ts +++ b/back/loaders/db.ts @@ -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 = [ diff --git a/shell/update.sh b/shell/update.sh index 2f902947..1d93c05b 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -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 "{