-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb23dff
commit 7421a49
Showing
3 changed files
with
42 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,17 @@ | ||
import * as dotenv from "dotenv"; | ||
import * as electron from "electron"; | ||
import * as fse from "fs-extra"; | ||
import * as log from "app/server/lib/log"; | ||
import * as net from 'net'; | ||
import * as packageJson from "desktop.package.json"; | ||
import * as path from "path"; | ||
import bluebird from 'bluebird'; | ||
import { commonUrls } from "app/common/gristUrls"; | ||
import { getAvailablePort } from "app/server/lib/serverUtils"; | ||
|
||
const NO_VALIDATION = () => true; | ||
|
||
const NO_VALIDATION = () => true; | ||
|
||
/** | ||
* Copied from grist-core, since it is unsafe to import core code at this point. | ||
*/ | ||
async function getAvailablePort(firstPort: number = 8000, optCount: number = 200): Promise<number> { | ||
const lastPort = firstPort + optCount - 1; | ||
async function checkNext(port: number): Promise<number> { | ||
if (port > lastPort) { | ||
throw new Error("No available ports between " + firstPort + " and " + lastPort); | ||
} | ||
return new bluebird((resolve: (p: number) => void, reject: (e: Error) => void) => { | ||
const server = net.createServer(); | ||
server.on('error', reject); | ||
server.on('close', () => resolve(port)); | ||
server.listen(port, 'localhost', () => server.close()); | ||
}) | ||
.catch(() => checkNext(port + 1)); | ||
} | ||
return bluebird.try(() => checkNext(firstPort)); | ||
} | ||
|
||
function check(envKey: string, validator: (value: string) => boolean, defaultValue: string,): void { | ||
function validateOrFallback(envKey: string, validator: (value: string) => boolean, defaultValue: string,): void { | ||
const envValue = process.env[envKey]; | ||
if (envValue === undefined) { | ||
log.warn(`${envKey} is not set, using default value ${defaultValue}`); | ||
|
@@ -45,6 +26,7 @@ function check(envKey: string, validator: (value: string) => boolean, defaultVal | |
|
||
|
||
export async function loadConfig() { | ||
dotenv.config(); | ||
if (process.env.GRIST_ELECTRON_AUTH !== undefined) { | ||
if (process.env.GRIST_DESKTOP_AUTH === undefined) { | ||
process.env.GRIST_DESKTOP_AUTH = process.env.GRIST_ELECTRON_AUTH; | ||
|
@@ -53,22 +35,22 @@ export async function loadConfig() { | |
log.warn("GRIST_DESKTOP_AUTH set, ignoring GRIST_ELECTRON_AUTH (deprecated)."); | ||
} | ||
} | ||
check( | ||
validateOrFallback( | ||
"GRIST_DEFAULT_USERNAME", | ||
NO_VALIDATION, | ||
"You" | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_DEFAULT_EMAIL", | ||
NO_VALIDATION, | ||
"[email protected]" | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_HOST", | ||
NO_VALIDATION, | ||
"localhost" | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_PORT", | ||
(portstr) => { | ||
if (! /^\d+$/.test(portstr)) { | ||
|
@@ -79,43 +61,43 @@ export async function loadConfig() { | |
}, | ||
(await getAvailablePort(47478)).toString() | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_DESKTOP_AUTH", | ||
(auth) => ["strict", "none", "mixed"].includes(auth), | ||
"strict" | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_SANDBOX_FLAVOR", | ||
(flavor) => ["pyodide", "unsandboxed", "gvisor", "macSandboxExec"].includes(flavor), | ||
"pyodide" | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_INST_DIR", | ||
NO_VALIDATION, | ||
electron.app.getPath("userData") | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_DATA_DIR", | ||
NO_VALIDATION, | ||
electron.app.getPath("documents") | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_USER_ROOT", | ||
NO_VALIDATION, | ||
path.join(electron.app.getPath("home"), ".grist") | ||
); | ||
check( | ||
validateOrFallback( | ||
"TYPEORM_DATABASE", | ||
NO_VALIDATION, | ||
path.join(electron.app.getPath("appData"), "landing.db") | ||
); | ||
check( | ||
validateOrFallback( | ||
"GRIST_WIDGET_LIST_URL", // Related to plugins (Would have to be changed if local custom widgets are used?) | ||
NO_VALIDATION, | ||
commonUrls.gristLabsWidgetRepository | ||
); | ||
|
||
const homeDBLocation = path.parse((process.env.TYPEORM_DATABASE as string)).dir; | ||
const homeDBLocation = path.parse(path.resolve(process.env.TYPEORM_DATABASE as string)).dir; | ||
if (!fse.existsSync(homeDBLocation)) { | ||
log.warn(`Directory to contain the home DB does not exist, creating ${homeDBLocation}`); | ||
fse.mkdirSync(homeDBLocation); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters