clients/src/misc/nodeUtils.ts
Pasi Niemi fb7335b927
Enable alternative ways for settings passwords (#101)
* Enable alternative ways for settings passwords:
* the environment variable BW_PASSWORD
* prefix the command line argument with "file:" and the password will read from the first line of that file
* prefix the command line argument with "env:" and the password will be read from that environment variable

* Appveyor fixes

* Switch to using command options for password file and password env

* Lowercase options
2020-05-08 10:38:28 -04:00

30 lines
1.1 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import * as readline from 'readline';
export class NodeUtils {
static mkdirpSync(targetDir: string, mode = '700', relative = false, relativeDir: string = null) {
const initialDir = path.isAbsolute(targetDir) ? path.sep : '';
const baseDir = relative ? (relativeDir != null ? relativeDir : __dirname) : '.';
targetDir.split(path.sep).reduce((parentDir, childDir) => {
const dir = path.resolve(baseDir, parentDir, childDir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, mode);
}
return dir;
}, initialDir);
}
static readFirstLine(fileName: string) {
return new Promise<string>((resolve, reject) => {
const readStream = fs.createReadStream(fileName, {encoding: 'utf8'});
const readInterface = readline.createInterface(readStream);
readInterface
.on('line', (line) => {
readStream.close();
resolve(line);
})
.on('error', (err) => reject(err));
});
}
}