-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
36 lines (30 loc) · 911 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fs = require("fs");
const os = require("os");
const iconv = require("iconv-lite");
const path = require("path");
const detectCharacterEncoding = require("detect-character-encoding");
const DEBUG = true;
const log = (...args) => {
if (DEBUG === true) {
console.log(">>", new Date().toISOString(), ...args);
}
};
/*
* Read file given paths content as text. Automatically detects the encoding of the file
*/
const readTextFile = (path) => {
const contents = fs.readFileSync(path);
const encoding = detectCharacterEncoding(contents).encoding;
return iconv.decode(contents, encoding);
};
const createTempFile = (text) => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "comrade_temp"));
const tempFilePath = path.join(tmpDir, `comrade_temp_file`);
fs.writeFileSync(tempFilePath, text);
return tempFilePath;
};
module.exports = {
log,
readTextFile,
createTempFile,
};