mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-03 08:35:29 +02:00
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
const fs = require('fs-extra');
|
|
|
|
export async function credentialDir() {
|
|
const username = require('os').userInfo().username;
|
|
|
|
const toTry = [
|
|
`c:/Users/${username}/joplin-credentials`,
|
|
`/mnt/c/Users/${username}/joplin-credentials`,
|
|
`/home/${username}/joplin-credentials`,
|
|
`/Users/${username}/joplin-credentials`,
|
|
];
|
|
|
|
for (const dirPath of toTry) {
|
|
if (await fs.pathExists(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();
|
|
const output = `${rootDir}/${filename}`;
|
|
if (!(await fs.pathExists(output))) throw new Error(`No such file: ${output}`);
|
|
return output;
|
|
}
|
|
|
|
export async function readCredentialFile(filename: string, defaultValue: string = '') {
|
|
try {
|
|
const filePath = await credentialFile(filename);
|
|
const r = await fs.readFile(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.
|
|
return r.toString().trim();
|
|
} catch (error) {
|
|
return defaultValue;
|
|
}
|
|
}
|