From 33407801572d69c1f9febf49035dc241bc114d9c Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 12 Apr 2020 14:23:17 +0200 Subject: [PATCH] Migrate config templates to TypeScript Signed-off-by: David Mehren --- lib/config/{default.js => default.ts} | 6 ++--- lib/config/{defaultSSL.js => defaultSSL.ts} | 8 +++---- .../{dockerSecret.js => dockerSecret.ts} | 12 +++++----- lib/config/enum.js | 16 ------------- lib/config/enum.ts | 14 +++++++++++ lib/config/{environment.js => environment.ts} | 6 ++--- ...kmdEnvironment.js => hackmdEnvironment.ts} | 6 ++--- lib/config/{oldDefault.js => oldDefault.ts} | 4 +--- .../{oldEnvironment.js => oldEnvironment.ts} | 6 ++--- lib/config/{utils.js => utils.ts} | 24 +++++++++---------- 10 files changed, 43 insertions(+), 59 deletions(-) rename lib/config/{default.js => default.ts} (98%) rename lib/config/{defaultSSL.js => defaultSSL.ts} (77%) rename lib/config/{dockerSecret.js => dockerSecret.ts} (60%) delete mode 100644 lib/config/enum.js create mode 100644 lib/config/enum.ts rename lib/config/{environment.js => environment.ts} (97%) rename lib/config/{hackmdEnvironment.js => hackmdEnvironment.ts} (97%) rename lib/config/{oldDefault.js => oldDefault.ts} (96%) rename lib/config/{oldEnvironment.js => oldEnvironment.ts} (63%) rename lib/config/{utils.js => utils.ts} (66%) diff --git a/lib/config/default.js b/lib/config/default.ts similarity index 98% rename from lib/config/default.js rename to lib/config/default.ts index 14bac8a73..4e96b237f 100644 --- a/lib/config/default.js +++ b/lib/config/default.ts @@ -1,8 +1,6 @@ -'use strict' +import os from 'os' -const os = require('os') - -module.exports = { +export const defaultConfig = { domain: '', urlPath: '', host: '0.0.0.0', diff --git a/lib/config/defaultSSL.js b/lib/config/defaultSSL.ts similarity index 77% rename from lib/config/defaultSSL.js rename to lib/config/defaultSSL.ts index ba020466d..053932ff5 100644 --- a/lib/config/defaultSSL.js +++ b/lib/config/defaultSSL.ts @@ -1,15 +1,13 @@ -'use strict' +import fs from 'fs' -const fs = require('fs') - -function getFile (path) { +function getFile (path): string | undefined { if (fs.existsSync(path)) { return path } return undefined } -module.exports = { +export const defaultSSL = { sslKeyPath: getFile('/run/secrets/key.pem'), sslCertPath: getFile('/run/secrets/cert.pem'), sslCAPath: getFile('/run/secrets/ca.pem') !== undefined ? [getFile('/run/secrets/ca.pem')] : [], diff --git a/lib/config/dockerSecret.js b/lib/config/dockerSecret.ts similarity index 60% rename from lib/config/dockerSecret.js rename to lib/config/dockerSecret.ts index ef2a2cf98..3bf2c47ff 100644 --- a/lib/config/dockerSecret.js +++ b/lib/config/dockerSecret.ts @@ -1,18 +1,18 @@ -'use strict' - -const fs = require('fs') -const path = require('path') +import fs from 'fs' +import path from 'path' const basePath = path.resolve('/run/secrets/') -function getSecret (secret) { +function getSecret (secret): string | undefined { const filePath = path.join(basePath, secret) if (fs.existsSync(filePath)) return fs.readFileSync(filePath, 'utf-8') return undefined } +export let dockerSecret: { s3: { accessKeyId: string | undefined; secretAccessKey: string | undefined }; github: { clientID: string | undefined; clientSecret: string | undefined }; facebook: { clientID: string | undefined; clientSecret: string | undefined }; google: { clientID: string | undefined; hostedDomain: string | undefined; clientSecret: string | undefined }; sessionSecret: string | undefined; sslKeyPath: string | undefined; twitter: { consumerSecret: string | undefined; consumerKey: string | undefined }; dropbox: { clientID: string | undefined; clientSecret: string | undefined; appKey: string | undefined }; gitlab: { clientID: string | undefined; clientSecret: string | undefined }; imgur: string | undefined; sslCertPath: string | undefined; sslCAPath: string | undefined; dhParamPath: string | undefined; dbURL: string | undefined; azure: { connectionString: string | undefined } } + if (fs.existsSync(basePath)) { - module.exports = { + dockerSecret = { dbURL: getSecret('dbURL'), sessionSecret: getSecret('sessionsecret'), sslKeyPath: getSecret('sslkeypath'), diff --git a/lib/config/enum.js b/lib/config/enum.js deleted file mode 100644 index 07cdfcfe3..000000000 --- a/lib/config/enum.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict' - -exports.Environment = { - development: 'development', - production: 'production', - test: 'test' -} - -exports.Permission = { - freely: 'freely', - editable: 'editable', - limited: 'limited', - locked: 'locked', - protected: 'protected', - private: 'private' -} diff --git a/lib/config/enum.ts b/lib/config/enum.ts new file mode 100644 index 000000000..83b81d5fc --- /dev/null +++ b/lib/config/enum.ts @@ -0,0 +1,14 @@ +export enum Environment { + development = 'development', + production = 'production', + test = 'test' +} + +export enum Permission { + freely = 'freely', + editable = 'editable', + limited = 'limited', + locked = 'locked', + protected = 'protected', + private = 'private' +} diff --git a/lib/config/environment.js b/lib/config/environment.ts similarity index 97% rename from lib/config/environment.js rename to lib/config/environment.ts index 7aa55dec8..021e3ccde 100644 --- a/lib/config/environment.js +++ b/lib/config/environment.ts @@ -1,8 +1,6 @@ -'use strict' +import { toArrayConfig, toBooleanConfig, toIntegerConfig } from './utils' -const { toBooleanConfig, toArrayConfig, toIntegerConfig } = require('./utils') - -module.exports = { +export const environment = { sourceURL: process.env.CMD_SOURCE_URL, domain: process.env.CMD_DOMAIN, urlPath: process.env.CMD_URL_PATH, diff --git a/lib/config/hackmdEnvironment.js b/lib/config/hackmdEnvironment.ts similarity index 97% rename from lib/config/hackmdEnvironment.js rename to lib/config/hackmdEnvironment.ts index 0bc1a7f7d..19b390b3e 100644 --- a/lib/config/hackmdEnvironment.js +++ b/lib/config/hackmdEnvironment.ts @@ -1,8 +1,6 @@ -'use strict' +import { toArrayConfig, toBooleanConfig, toIntegerConfig } from './utils' -const { toBooleanConfig, toArrayConfig, toIntegerConfig } = require('./utils') - -module.exports = { +export const hackmdEnvironment = { domain: process.env.HMD_DOMAIN, urlPath: process.env.HMD_URL_PATH, port: toIntegerConfig(process.env.HMD_PORT), diff --git a/lib/config/oldDefault.js b/lib/config/oldDefault.ts similarity index 96% rename from lib/config/oldDefault.js rename to lib/config/oldDefault.ts index 738ad9f7d..7c350bb62 100644 --- a/lib/config/oldDefault.js +++ b/lib/config/oldDefault.ts @@ -1,6 +1,4 @@ -'use strict' - -module.exports = { +export const oldDefault = { urlpath: undefined, urladdport: undefined, alloworigin: undefined, diff --git a/lib/config/oldEnvironment.js b/lib/config/oldEnvironment.ts similarity index 63% rename from lib/config/oldEnvironment.js rename to lib/config/oldEnvironment.ts index 060475533..f68bb3183 100644 --- a/lib/config/oldEnvironment.js +++ b/lib/config/oldEnvironment.ts @@ -1,8 +1,6 @@ -'use strict' +import { toBooleanConfig } from './utils' -const { toBooleanConfig } = require('./utils') - -module.exports = { +export const oldEnvironment = { debug: toBooleanConfig(process.env.DEBUG), dburl: process.env.DATABASE_URL, urlpath: process.env.URL_PATH, diff --git a/lib/config/utils.js b/lib/config/utils.ts similarity index 66% rename from lib/config/utils.js rename to lib/config/utils.ts index 9646f8c0e..0ee109168 100644 --- a/lib/config/utils.js +++ b/lib/config/utils.ts @@ -1,32 +1,30 @@ -'use strict' +import fs from 'fs' +import path from 'path' -const fs = require('fs') -const path = require('path') - -exports.toBooleanConfig = function toBooleanConfig (configValue) { - if (configValue && typeof configValue === 'string') { +export function toBooleanConfig (configValue: string | boolean | undefined): boolean { + if (typeof configValue === 'string') { return (configValue === 'true') } - return configValue + return configValue || false } -exports.toArrayConfig = function toArrayConfig (configValue, separator = ',', fallback) { - if (configValue && typeof configValue === 'string') { +export function toArrayConfig (configValue: string | undefined, separator = ',', fallback = []): any[] { + if (configValue) { return (configValue.split(separator).map(arrayItem => arrayItem.trim())) } return fallback } -exports.toIntegerConfig = function toIntegerConfig (configValue) { +export function toIntegerConfig (configValue): number { if (configValue && typeof configValue === 'string') { return parseInt(configValue) } return configValue } -exports.getGitCommit = function getGitCommit (repodir) { +export function getGitCommit (repodir): string { if (!fs.existsSync(repodir + '/.git/HEAD')) { - return undefined + return '' } let reference = fs.readFileSync(repodir + '/.git/HEAD', 'utf8') if (reference.startsWith('ref: ')) { @@ -37,7 +35,7 @@ exports.getGitCommit = function getGitCommit (repodir) { return reference } -exports.getGitHubURL = function getGitHubURL (repo, reference) { +export function getGitHubURL (repo, reference): string { // if it's not a github reference, we handle handle that anyway if (!repo.startsWith('https://github.com') && !repo.startsWith('git@github.com')) { return repo