1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-23 11:56:00 +01:00
bitwarden-browser/libs/node/src/node-utils.ts
Oscar Hinton a5e3432f85
Assign ownership to many libs files (#6928)
Assign ownership to many of the remaining libs/common files.

Criteria for ownership:
* Files used by a single team, is now owned by that team.
* Files related to a domain owned by a team is now owned by that team.
* Where ownership is unclear the "lowest level" service takes ownership.
2023-11-27 20:59:44 +00:00

35 lines
1.2 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));
});
}
// https://stackoverflow.com/a/31394257
static bufferToArrayBuffer(buf: Buffer): ArrayBuffer {
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
}
}