-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan_keys.js
69 lines (59 loc) · 1.95 KB
/
scan_keys.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const fs = require('fs');
require('dotenv').config();
function getEnvVariablesAsArray() {
const variables = [];
for (let key in process.env) {
if (process.env.hasOwnProperty(key)) {
const value = process.env[key];
variables.push(value);
}
}
return variables;
}
function scanSourceCode(directory, staticStrings = [], envVariablesArray = []) {
if (!staticStrings.length) {
console.log('No static strings provided.');
}
if (!envVariablesArray.length) {
console.log('No environment variables provided.');
}
// const additionalTargetStrings = readTargetStrings(envVariables);
// Concatenate the two arrays and convert to a Set to remove duplicates
let targetStrings = [...new Set([...staticStrings, ...envVariablesArray])];
const foundLines = [];
const files = fs.readdirSync(directory, { withFileTypes: true });
files.forEach((file) => {
const filePath = directory + '/' + file.name;
const fileContent = fs.readFileSync(filePath, 'utf8').split('\n');
fileContent.forEach((line, lineNum) => {
targetStrings.forEach((targetString) => {
if (line.includes(targetString)) {
foundLines.push({
filePath,
lineNum: lineNum + 1,
lineContent: line.trim(),
targetString,
});
return; // Once a target string is found in a line, no need to check further
}
});
});
});
return foundLines;
}
const directoryToScan = './build';
const staticStrings = [];
const envVariablesArray = getEnvVariablesAsArray();
const foundLines = scanSourceCode(
directoryToScan,
staticStrings,
envVariablesArray
);
if (foundLines.length > 0) {
console.log('Found the following strings in the source files:');
foundLines.forEach(({ filePath, lineNum, lineContent, targetString }) => {
console.log(`File: ${filePath}, Line: ${lineNum}, Content: ${lineContent}`);
});
} else {
console.log('No strings were found in any files.');
}