1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-13 00:10:37 +02:00

Tools: Allow setting website build environment from config file

This commit is contained in:
Laurent Cozic
2022-04-09 12:32:19 +01:00
parent bdd9c6cf35
commit a0d77d10ba
2 changed files with 38 additions and 14 deletions

View File

@ -1,6 +1,10 @@
const fs = require('fs-extra');
import { pathExistsSync, readFileSync } from 'fs-extra';
export async function credentialDir() {
// All these calls used to be async but certain scripts need to load config
// files early, so they've been converted to sync calls. Do not convert them
// back to async.
export function credentialDir() {
const username = require('os').userInfo().username;
const toTry = [
@ -11,23 +15,23 @@ export async function credentialDir() {
];
for (const dirPath of toTry) {
if (await fs.pathExists(dirPath)) return dirPath;
if (pathExistsSync(dirPath)) return dirPath;
}
throw new Error(`Could not find credential directory in any of these paths: ${JSON.stringify(toTry)}`);
}
export async function credentialFile(filename: string) {
const rootDir = await credentialDir();
export function credentialFile(filename: string) {
const rootDir = credentialDir();
const output = `${rootDir}/${filename}`;
if (!(await fs.pathExists(output))) throw new Error(`No such file: ${output}`);
if (!(pathExistsSync(output))) throw new Error(`No such file: ${output}`);
return output;
}
export async function readCredentialFile(filename: string, defaultValue: string = '') {
export function readCredentialFile(filename: string, defaultValue: string = '') {
try {
const filePath = await credentialFile(filename);
const r = await fs.readFile(filePath);
const filePath = credentialFile(filename);
const r = readFileSync(filePath);
// There's normally no reason to keep the last new line character and it
// can cause problems in certain scripts, so trim it. Any other white
// space should also not be relevant.
@ -36,3 +40,16 @@ export async function readCredentialFile(filename: string, defaultValue: string
return defaultValue;
}
}
export function readCredentialFileJson<T>(filename: string, defaultValue: T = null): T {
const v = readCredentialFile(filename);
if (!v) return defaultValue;
try {
const o = JSON.parse(v);
return o;
} catch (error) {
error.message = `Could not parse JSON file ${filename}: ${error.message}`;
throw error;
}
}