优化初始化文件操作

This commit is contained in:
whyour 2025-01-05 00:28:08 +08:00
parent 05f8bbd26e
commit ffa8b25a66

View File

@ -38,108 +38,87 @@ const sshPath = path.resolve(homedir, '.ssh');
const sshdPath = path.join(dataPath, 'ssh.d'); const sshdPath = path.join(dataPath, 'ssh.d');
const systemLogPath = path.join(dataPath, 'syslog'); const systemLogPath = path.join(dataPath, 'syslog');
export default async () => { const directories = [
const confFileExist = await fileExist(confFile); configPath,
const scriptDirExist = await fileExist(scriptPath); scriptPath,
const preloadDirExist = await fileExist(preloadPath); preloadPath,
const logDirExist = await fileExist(logPath); logPath,
const configDirExist = await fileExist(configPath); tmpPath,
const uploadDirExist = await fileExist(uploadPath); uploadPath,
const sshDirExist = await fileExist(sshPath); sshPath,
const bakDirExist = await fileExist(bakPath); bakPath,
const sshdDirExist = await fileExist(sshdPath); sshdPath,
const systemLogDirExist = await fileExist(systemLogPath); systemLogPath,
const tmpDirExist = await fileExist(tmpPath); ];
const scriptNotifyJsFileExist = await fileExist(scriptNotifyJsFile);
const scriptNotifyPyFileExist = await fileExist(scriptNotifyPyFile);
const TaskBeforeFileExist = await fileExist(TaskBeforeFile);
const TaskBeforeJsFileExist = await fileExist(TaskBeforeJsFile);
const TaskBeforePyFileExist = await fileExist(TaskBeforePyFile);
const TaskAfterFileExist = await fileExist(TaskAfterFile);
if (!configDirExist) { const files = [
await fs.mkdir(configPath); {
} target: confFile,
source: sampleConfigFile,
if (!scriptDirExist) { checkExistence: true,
await fs.mkdir(scriptPath); },
} {
target: jsNotifyFile,
if (!preloadDirExist) { source: sampleNotifyJsFile,
await fs.mkdir(preloadPath); checkExistence: false,
} },
{
if (!logDirExist) { target: pyNotifyFile,
await fs.mkdir(logPath); source: sampleNotifyPyFile,
} checkExistence: false,
},
if (!tmpDirExist) { {
await fs.mkdir(tmpPath); target: scriptNotifyJsFile,
} source: sampleNotifyJsFile,
checkExistence: true,
if (!uploadDirExist) { },
await fs.mkdir(uploadPath); {
} target: scriptNotifyPyFile,
source: sampleNotifyPyFile,
if (!sshDirExist) { checkExistence: true,
await fs.mkdir(sshPath); },
} {
target: TaskBeforeFile,
if (!bakDirExist) { source: sampleTaskShellFile,
await fs.mkdir(bakPath); checkExistence: true,
} },
{
if (!sshdDirExist) { target: TaskBeforeJsFile,
await fs.mkdir(sshdPath); content:
}
if (!systemLogDirExist) {
await fs.mkdir(systemLogPath);
}
// 初始化文件
if (!confFileExist) {
await writeFileWithLock(confFile, await fs.readFile(sampleConfigFile));
}
await writeFileWithLock(jsNotifyFile, await fs.readFile(sampleNotifyJsFile));
await writeFileWithLock(pyNotifyFile, await fs.readFile(sampleNotifyPyFile));
if (!scriptNotifyJsFileExist) {
await writeFileWithLock(
scriptNotifyJsFile,
await fs.readFile(sampleNotifyJsFile),
);
}
if (!scriptNotifyPyFileExist) {
await writeFileWithLock(
scriptNotifyPyFile,
await fs.readFile(sampleNotifyPyFile),
);
}
if (!TaskBeforeFileExist) {
await writeFileWithLock(TaskBeforeFile, await fs.readFile(sampleTaskShellFile));
}
if (!TaskBeforeJsFileExist) {
await writeFileWithLock(
TaskBeforeJsFile,
'// The JavaScript code that executes before the JavaScript task execution will execute.', '// The JavaScript code that executes before the JavaScript task execution will execute.',
); checkExistence: true,
} },
{
if (!TaskBeforePyFileExist) { target: TaskBeforePyFile,
await writeFileWithLock( content:
TaskBeforePyFile,
'# The Python code that executes before the Python task execution will execute.', '# The Python code that executes before the Python task execution will execute.',
); checkExistence: true,
},
{
target: TaskAfterFile,
source: sampleTaskShellFile,
checkExistence: true,
},
];
export default async () => {
for (const dirPath of directories) {
if (!(await fileExist(dirPath))) {
await fs.mkdir(dirPath);
}
} }
if (!TaskAfterFileExist) { for (const item of files) {
await writeFileWithLock(TaskAfterFile, await fs.readFile(sampleTaskShellFile)); const exists = await fileExist(item.target);
if (!item.checkExistence || !exists) {
if (!item.content && !item.source) {
throw new Error(
`Neither content nor source specified for ${item.target}`,
);
}
const content = item.content || (await fs.readFile(item.source!));
await writeFileWithLock(item.target, content);
}
} }
Logger.info('✌️ Init file down'); Logger.info('✌️ Init file down');