-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseSettings.js
48 lines (45 loc) · 1.54 KB
/
parseSettings.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
37
38
39
40
41
42
43
44
45
46
47
48
const fs = require('fs');
const path = require('path');
/**
* Reads and parses the settings.json file.
* @param {string} baseDir - The base directory where settings.json is located.
* @param {string} settingsFilePath - The custom path to the settings file.
* @returns {Object} - The parsed settings object.
*/
function readSettings(baseDir, settingsFilePath) {
const filePath = path.isAbsolute(settingsFilePath)
? settingsFilePath
: path.join(baseDir, settingsFilePath);
if (!fs.existsSync(filePath)) {
return {};
}
const settings = fs.readFileSync(filePath, 'utf8');
return JSON.parse(settings);
}
/**
* Reads and parses the settings.json file.
* @param {string} baseDir - The base directory where settings.json is located.
* @param {string} settingsFilePath - The custom path to the settings file.
* @param {string} setting - The setting to look for.
* @returns {Array} - The line and the position in the line respectively.
*/
function getSettingPosition(baseDir, settingsFilePath, setting, startingLine = 0) {
let res = [];
const filePath = path.isAbsolute(settingsFilePath)
? settingsFilePath
: path.join(baseDir, settingsFilePath);
if (!fs.existsSync(filePath)) {
return res;
}
const settings = fs.readFileSync(filePath, 'utf8').split(/\r?\n/);
settings.slice(startingLine).some((line, index) => {
const pos = line.indexOf(`"${setting}"`);
if(pos && pos >= 0) { res = [index + startingLine, pos]; return true; }
return false;
});
return res;
}
module.exports = {
readSettings,
getSettingPosition,
};