Skip to content

Commit

Permalink
fs 文件操作替换为 fs.promise
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Nov 1, 2023
1 parent 66a2769 commit 20f615e
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 260 deletions.
10 changes: 5 additions & 5 deletions back/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 * as fs from 'fs/promises';
import { celebrate, Joi } from 'celebrate';
import { join } from 'path';
const route = Router();
Expand All @@ -16,7 +16,7 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const fileList = fs.readdirSync(config.configPath, 'utf-8');
const fileList = await fs.readdir(config.configPath, 'utf-8');
res.send({
code: 200,
data: fileList
Expand All @@ -41,11 +41,11 @@ export default (app: Router) => {
res.send({ code: 403, message: '文件无法访问' });
}
if (req.params.file.includes('sample')) {
content = getFileContentByName(
content = await getFileContentByName(
join(config.samplePath, req.params.file),
);
} else {
content = getFileContentByName(
content = await getFileContentByName(
join(config.configPath, req.params.file),
);
}
Expand All @@ -72,7 +72,7 @@ export default (app: Router) => {
res.send({ code: 403, message: '文件无法访问' });
}
const path = join(config.configPath, name);
fs.writeFileSync(path, content);
await fs.writeFile(path, content);
res.send({ code: 200, message: '保存成功' });
} catch (e) {
return next(e);
Expand Down
10 changes: 3 additions & 7 deletions back/api/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Container } from 'typedi';
import { Logger } from 'winston';
import * as fs from 'fs';
import config from '../config';
import { emptyDir, getFileContentByName, readDirs } from '../config/util';
import { getFileContentByName, readDirs, rmPath } from '../config/util';
import { join } from 'path';
import { celebrate, Joi } from 'celebrate';
const route = Router();
Expand Down Expand Up @@ -39,7 +39,7 @@ export default (app: Router) => {
(req.query.path || '') as string,
req.params.file,
);
const content = getFileContentByName(filePath);
const content = await getFileContentByName(filePath);
res.send({ code: 200, data: content });
} catch (e) {
return next(e);
Expand All @@ -64,11 +64,7 @@ export default (app: Router) => {
type: string;
};
const filePath = join(config.logPath, path, filename);
if (type === 'directory') {
await emptyDir(filePath);
} else {
fs.unlinkSync(filePath);
}
await rmPath(filePath);
res.send({ code: 200 });
} catch (e) {
return next(e);
Expand Down
41 changes: 21 additions & 20 deletions back/api/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
readDirs,
getLastModifyFilePath,
readDir,
emptyDir,
rmPath,
} 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 * as fs from 'fs/promises';
import { celebrate, Joi } from 'celebrate';
import path, { join, parse } from 'path';
import ScriptService from '../services/script';
Expand Down Expand Up @@ -40,9 +40,13 @@ export default (app: Router) => {
config.scriptPath,
req.query.path as string,
);
result = readDir(targetPath, config.scriptPath, blacklist);
result = await readDir(targetPath, config.scriptPath, blacklist);
} else {
result = readDirs(config.scriptPath, config.scriptPath, blacklist);
result = await readDirs(
config.scriptPath,
config.scriptPath,
blacklist,
);
}
res.send({
code: 200,
Expand All @@ -64,7 +68,7 @@ export default (app: Router) => {
req.query.path as string,
req.params.file,
);
const content = getFileContentByName(filePath);
const content = await getFileContentByName(filePath);
res.send({ code: 200, data: content });
} catch (e) {
return next(e);
Expand Down Expand Up @@ -104,12 +108,12 @@ export default (app: Router) => {
}

if (req.file) {
fs.renameSync(req.file.path, join(path, req.file.filename));
await fs.rename(req.file.path, join(path, req.file.filename));
return res.send({ code: 200 });
}

if (directory) {
fs.mkdirSync(join(path, directory), { recursive: true });
await fs.mkdir(join(path, directory), { recursive: true });
return res.send({ code: 200 });
}

Expand All @@ -121,16 +125,17 @@ export default (app: Router) => {
`${originFilename.replace(/\//g, '')}`,
);
const filePath = join(path, `${filename.replace(/\//g, '')}`);
if (fs.existsSync(originFilePath)) {
fs.copyFileSync(
const fileExists = await fileExist(filePath);
if (fileExists) {
await fs.copyFile(
originFilePath,
join(config.bakPath, originFilename.replace(/\//g, '')),
);
if (filename !== originFilename) {
fs.unlinkSync(originFilePath);
await rmPath(originFilePath);
}
}
fs.writeFileSync(filePath, content);
await fs.writeFile(filePath, content);
return res.send({ code: 200 });
} catch (e) {
return next(e);
Expand All @@ -156,7 +161,7 @@ export default (app: Router) => {
path: string;
};
const filePath = join(config.scriptPath, path, filename);
fs.writeFileSync(filePath, content);
await fs.writeFile(filePath, content);
return res.send({ code: 200 });
} catch (e) {
return next(e);
Expand All @@ -182,11 +187,7 @@ export default (app: Router) => {
type: string;
};
const filePath = join(config.scriptPath, path, filename);
if (type === 'directory') {
await emptyDir(filePath);
} else {
fs.unlinkSync(filePath);
}
await rmPath(filePath);
res.send({ code: 200 });
} catch (e) {
return next(e);
Expand Down Expand Up @@ -239,7 +240,7 @@ export default (app: Router) => {
let { filename, content, path } = req.body;
const { name, ext } = parse(filename);
const filePath = join(config.scriptPath, path, `${name}.swap${ext}`);
fs.writeFileSync(filePath, content || '', { encoding: 'utf8' });
await fs.writeFile(filePath, content || '', { encoding: 'utf8' });

const scriptService = Container.get(ScriptService);
const result = await scriptService.runScript(filePath);
Expand Down Expand Up @@ -269,7 +270,7 @@ export default (app: Router) => {
const scriptService = Container.get(ScriptService);
const result = await scriptService.stopScript(filePath, pid);
setTimeout(() => {
emptyDir(logPath);
rmPath(logPath);
}, 3000);
res.send(result);
} catch (e) {
Expand Down Expand Up @@ -297,7 +298,7 @@ export default (app: Router) => {
};
const filePath = join(config.scriptPath, path, filename);
const newPath = join(config.scriptPath, path, newFilename);
fs.renameSync(filePath, newPath);
await fs.rename(filePath, newPath);
res.send({ code: 200 });
} catch (e) {
return next(e);
Expand Down
28 changes: 12 additions & 16 deletions back/api/system.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi';
import { Logger } from 'winston';
import * as fs from 'fs';
import * as fs from 'fs/promises';
import config from '../config';
import SystemService from '../services/system';
import { celebrate, Joi } from 'celebrate';
Expand Down Expand Up @@ -174,7 +174,7 @@ export default (app: Router) => {
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const command = req.body.command
const command = req.body.command;
const idStr = `cat ${config.crontabFile} | grep -E "${command}" | perl -pe "s|.*ID=(.*) ${command}.*|\\1|" | head -1 | awk -F " " '{print $1}' | xargs echo -n`;
let id = await promiseExec(idStr);
const uniqPath = await getUniqPath(command, id);
Expand All @@ -193,12 +193,12 @@ export default (app: Router) => {
onError: async (message: string) => {
res.write(`\n${message}`);
const absolutePath = await handleLogPath(logPath);
fs.appendFileSync(absolutePath, `\n${message}`);
await fs.appendFile(absolutePath, `\n${message}`);
},
onLog: async (message: string) => {
res.write(`\n${message}`);
const absolutePath = await handleLogPath(logPath);
fs.appendFileSync(absolutePath, `\n${message}`);
await fs.appendFile(absolutePath, `\n${message}`);
},
},
);
Expand Down Expand Up @@ -253,16 +253,12 @@ export default (app: Router) => {
},
);

route.get(
'/log',
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
await systemService.getSystemLog(res);
} catch (e) {
return next(e);
}
},
);

route.get('/log', async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
await systemService.getSystemLog(res);
} catch (e) {
return next(e);
}
});
};
Loading

0 comments on commit 20f615e

Please sign in to comment.