From 2eed5cba14a622a351110002a7fd0f21cece7301 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 18 Nov 2021 21:47:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E7=9B=AE=E5=BD=95=E5=8F=8A=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/app.ts | 2 ++ back/config/index.ts | 6 ------ back/loaders/initFile.ts | 46 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 back/loaders/initFile.ts diff --git a/back/app.ts b/back/app.ts index ee2f64b6..68676bd8 100644 --- a/back/app.ts +++ b/back/app.ts @@ -9,6 +9,8 @@ import Logger from './loaders/logger'; async function startServer() { const app = express(); + await require('./loaders/initFile').default(); + await require('./loaders/sentry').default({ expressApp: app }); await require('./loaders/db').default(); diff --git a/back/config/index.ts b/back/config/index.ts index 02021b2f..61e89eb5 100644 --- a/back/config/index.ts +++ b/back/config/index.ts @@ -32,16 +32,10 @@ const authDbFile = path.join(rootPath, 'db/auth.db'); const dependenceDbFile = path.join(rootPath, 'db/dependence.db'); const versionFile = path.join(rootPath, 'src/version.ts'); -const configFound = dotenv.config({ path: confFile }); - if (envFound.error) { throw new Error("⚠️ Couldn't find .env file ⚠️"); } -if (configFound.error) { - throw new Error("⚠️ Couldn't find config.sh file ⚠️"); -} - export default { port: parseInt(process.env.PORT as string, 10), cronPort: parseInt(process.env.CRON_PORT as string, 10), diff --git a/back/loaders/initFile.ts b/back/loaders/initFile.ts new file mode 100644 index 00000000..09bddf13 --- /dev/null +++ b/back/loaders/initFile.ts @@ -0,0 +1,46 @@ +import fs from 'fs'; +import path from 'path'; +import dotenv from 'dotenv'; +import Logger from './logger'; +import { fileExist } from '../config/util'; + +const rootPath = process.cwd(); +const confFile = path.join(rootPath, 'config/config.sh'); +const sampleConfigFile = path.join(rootPath, 'sample/config.sample.sh'); +const sampleAuthFile = path.join(rootPath, 'sample/auth.sample.json'); +const authConfigFile = path.join(rootPath, 'config/auth.json'); +const configPath = path.join(rootPath, 'config/'); +const scriptPath = path.join(rootPath, 'scripts/'); +const logPath = path.join(rootPath, 'log/'); + +export default async () => { + const authFileExist = await fileExist(authConfigFile); + const confFileExist = await fileExist(confFile); + const scriptDirExist = await fileExist(scriptPath); + const logDirExist = await fileExist(logPath); + const configDirExist = await fileExist(configPath); + + if (!configDirExist) { + fs.mkdirSync(configPath); + } + + if (!authFileExist) { + fs.writeFileSync(authConfigFile, fs.readFileSync(sampleAuthFile)); + } + + if (!confFileExist) { + fs.writeFileSync(confFile, fs.readFileSync(sampleConfigFile)); + } + + if (!scriptDirExist) { + fs.mkdirSync(scriptPath); + } + + if (!logDirExist) { + fs.mkdirSync(logPath); + } + + dotenv.config({ path: confFile }); + + Logger.info('✌️ Init file down'); +};