2021-05-21 15:17:21 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:49:47 +02:00
|
|
|
export async function readCredentialFile(filename: string, defaultValue: string = '') {
|
|
|
|
try {
|
|
|
|
const filePath = await credentialFile(filename);
|
|
|
|
const r = await fs.readFile(filePath);
|
2021-06-02 21:59:53 +02:00
|
|
|
// 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();
|
2021-05-25 11:49:47 +02:00
|
|
|
} catch (error) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2021-05-21 15:17:21 +02:00
|
|
|
}
|