diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2020-04-27 18:17:06 -0400 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2020-04-27 18:17:06 -0400 |
| commit | fd2013a7bf4129b05080f01ef199d69b9ef9ec68 (patch) | |
| tree | 8f8eae08b2884a0e066e01138aa6a104625d952f /src/util/config.ts | |
| parent | 32db5f6e5e8b6f6d2baffbec44cc34daedd24533 (diff) | |
| download | ao-coverage-fd2013a7bf4129b05080f01ef199d69b9ef9ec68.tar.gz ao-coverage-fd2013a7bf4129b05080f01ef199d69b9ef9ec68.tar.bz2 ao-coverage-fd2013a7bf4129b05080f01ef199d69b9ef9ec68.zip | |
Refactor startup to passthrough values
Even more refactoring - however there were some small
troubles using path in the nested scripts/files, so referencing
them via the index should be a bit more stable. Plus, the config
unit tests won't just exit because of configOrError constants
strewn about the file.
Diffstat (limited to 'src/util/config.ts')
| -rw-r--r-- | src/util/config.ts | 53 |
1 files changed, 19 insertions, 34 deletions
diff --git a/src/util/config.ts b/src/util/config.ts index 195d85b..3eb8d1c 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -45,48 +45,33 @@ export const persistTemplate = async (input: Template): Promise<void> => { } }; -const MONGO_URI = configOrError("MONGO_URI"); -const TARGET_URL = process.env.TARGET_URL ?? "http://localhost:3000"; -const HOST_DIR = configOrError("HOST_DIR"); - -export const handleStartup = async (): Promise<MongoClient> => { +export const handleStartup = async ( + mongoUri: string, + hostDir: string, + publicPath: string, + targetUrl: string +): Promise<MongoClient> => { try { - await fs.promises.access(HOST_DIR, fs.constants.R_OK | fs.constants.W_OK); - if (!path.isAbsolute(HOST_DIR)) { - logger.error("HOST_DIR must be an absolute path"); - process.exit(1); + await fs.promises.access(hostDir, fs.constants.R_OK | fs.constants.W_OK); + if (!path.isAbsolute(hostDir)) { + await Promise.reject("hostDir must be an absolute path"); } - const mongo = await MongoClient.connect(MONGO_URI, { + const mongo = await MongoClient.connect(mongoUri, { useUnifiedTopology: true - }).catch((err: MongoError) => { - logger.error(err.message ?? "Unable to connect to database"); - process.exit(1); - }); + }).catch((err: MongoError) => + Promise.reject(err.message ?? "Unable to connect to database") + ); await persistTemplate({ - inputFile: path.join( - __dirname, - "..", - "..", - "public", - "templates", - "bash.template" - ), - outputFile: path.join(HOST_DIR, "bash"), - context: { TARGET_URL } + inputFile: path.join(publicPath, "templates", "bash.template"), + outputFile: path.join(hostDir, "bash"), + context: { targetUrl } } as Template); await persistTemplate({ - inputFile: path.join( - __dirname, - "..", - "..", - "public", - "templates", - "index.html.template" - ), - outputFile: path.join(HOST_DIR, "index.html"), - context: { TARGET_URL } + inputFile: path.join(publicPath, "templates", "index.html.template"), + outputFile: path.join(hostDir, "index.html"), + context: { targetUrl } } as Template); return mongo; |
