From ffa8b25a661ead11a0a883595a69591e590fae9f Mon Sep 17 00:00:00 2001 From: whyour Date: Sun, 5 Jan 2025 00:28:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/loaders/initFile.ts | 177 +++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 99 deletions(-) diff --git a/back/loaders/initFile.ts b/back/loaders/initFile.ts index 4fb20a03593..b8629bc0cff 100644 --- a/back/loaders/initFile.ts +++ b/back/loaders/initFile.ts @@ -38,108 +38,87 @@ const sshPath = path.resolve(homedir, '.ssh'); const sshdPath = path.join(dataPath, 'ssh.d'); const systemLogPath = path.join(dataPath, 'syslog'); -export default async () => { - const confFileExist = await fileExist(confFile); - const scriptDirExist = await fileExist(scriptPath); - const preloadDirExist = await fileExist(preloadPath); - const logDirExist = await fileExist(logPath); - const configDirExist = await fileExist(configPath); - const uploadDirExist = await fileExist(uploadPath); - const sshDirExist = await fileExist(sshPath); - const bakDirExist = await fileExist(bakPath); - const sshdDirExist = await fileExist(sshdPath); - const systemLogDirExist = await fileExist(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) { - await fs.mkdir(configPath); - } - - if (!scriptDirExist) { - await fs.mkdir(scriptPath); - } - - if (!preloadDirExist) { - await fs.mkdir(preloadPath); - } - - if (!logDirExist) { - await fs.mkdir(logPath); - } - - if (!tmpDirExist) { - await fs.mkdir(tmpPath); - } - - if (!uploadDirExist) { - await fs.mkdir(uploadPath); - } - - if (!sshDirExist) { - await fs.mkdir(sshPath); - } - - if (!bakDirExist) { - await fs.mkdir(bakPath); - } - - if (!sshdDirExist) { - await fs.mkdir(sshdPath); - } - - 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, +const directories = [ + configPath, + scriptPath, + preloadPath, + logPath, + tmpPath, + uploadPath, + sshPath, + bakPath, + sshdPath, + systemLogPath, +]; + +const files = [ + { + target: confFile, + source: sampleConfigFile, + checkExistence: true, + }, + { + target: jsNotifyFile, + source: sampleNotifyJsFile, + checkExistence: false, + }, + { + target: pyNotifyFile, + source: sampleNotifyPyFile, + checkExistence: false, + }, + { + target: scriptNotifyJsFile, + source: sampleNotifyJsFile, + checkExistence: true, + }, + { + target: scriptNotifyPyFile, + source: sampleNotifyPyFile, + checkExistence: true, + }, + { + target: TaskBeforeFile, + source: sampleTaskShellFile, + checkExistence: true, + }, + { + target: TaskBeforeJsFile, + content: '// The JavaScript code that executes before the JavaScript task execution will execute.', - ); - } - - if (!TaskBeforePyFileExist) { - await writeFileWithLock( - TaskBeforePyFile, + checkExistence: true, + }, + { + target: TaskBeforePyFile, + content: '# The Python code that executes before the Python task execution will execute.', - ); - } + checkExistence: true, + }, + { + target: TaskAfterFile, + source: sampleTaskShellFile, + checkExistence: true, + }, +]; - if (!TaskAfterFileExist) { - await writeFileWithLock(TaskAfterFile, await fs.readFile(sampleTaskShellFile)); +export default async () => { + for (const dirPath of directories) { + if (!(await fileExist(dirPath))) { + await fs.mkdir(dirPath); + } + } + + for (const item of files) { + 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');