generated from Falcion/Patternugit
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.ts
132 lines (105 loc) · 4.91 KB
/
prepare.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @license MIT
* @author Falcion
* @year 2023-2024
*/
import * as fs from 'fs-extra';
import * as path from 'path';
import * as dotenv from 'dotenv';
import readline from 'readline';
import colors from 'colors/safe';
class PREPARE_MODULE {
ROOT_DIRECTORY: string = __dirname;
EXCLUDING_FOLDER: string[] = ['node_modules', 'venv', '.git', 'out'];
EXCLUDING_VALUES: string[] = [
'FALCION',
'PATTERNU',
'PATTERNUGIT'
];
constructor(entries: string[]) {
if (entries[0] != 'NO')
for (const item of entries)
this.EXCLUDING_VALUES.push(item);
}
async search(filepath: string,
data: string[]) {
const content = (await fs.readFile(filepath, 'utf-8')).split('\n');
for (let i = 0; i < content.length; i++) {
const line = content[i].toUpperCase();
for (const target of data)
if (line.includes(target))
console.info(colors.green(`Found "${target}" in L#${i} of:\n` + colors.cyan(filepath)))
}
}
async traverse(directory: string) {
try {
const files: string[] = await fs.readdir(directory);
for (const file of files) {
const filepath = path.join(directory, file);
const filestat = await fs.stat(filepath);
if (filestat.isDirectory()) {
if (!this.EXCLUDING_FOLDER.includes(file)) {
await this.traverse(filepath);
}
} else
await this.search(filepath, this.EXCLUDING_VALUES);
}
}
catch (err) { console.error(colors.red('Error via reading given directory: ' + `${err}`)); }
}
}
fs.ensureFileSync(path.join(__dirname, '.env'));
fs.ensureFileSync(path.join(__dirname, 'manifest.json'));
fs.writeFileSync(path.join(__dirname, '.env'), 'EXAMPLE_API_KEY=');
fs.writeFileSync(path.join(__dirname, 'manifest.json'), JSON.stringify({}, undefined, 4));
dotenv.config({
path: '.env',
encoding: 'utf-8'
});
const PACKAGE_JSON: any = JSON.parse(fs.readFileSync('package.json', { encoding: 'utf-8' }));
const MANIFEST: any = JSON.parse(fs.readFileSync('manifest.json', { encoding: 'utf-8' }));
if (PACKAGE_JSON.name === MANIFEST.id &&
PACKAGE_JSON.displayName === MANIFEST.name &&
PACKAGE_JSON.description === MANIFEST.description &&
PACKAGE_JSON.author.name === MANIFEST.author &&
PACKAGE_JSON.author.url === MANIFEST.authorUrl &&
PACKAGE_JSON.license === MANIFEST.license &&
PACKAGE_JSON.version === MANIFEST.version) {
console.warn(colors.bgGreen(colors.white('Manifest is synced with package, keep everything as it was.')));
}
else {
console.warn(colors.bgBlue(colors.yellow('Manifest is not synced with package\'s information, rewriting it.')));
fs.copyFileSync('manifest.json', 'manifest-backup.json');
const input_json: any = {
'id': PACKAGE_JSON.name,
'name': PACKAGE_JSON.displayName,
'description': PACKAGE_JSON.description,
'author': PACKAGE_JSON.author.name,
'authorUrl': PACKAGE_JSON.author.url,
'license': PACKAGE_JSON.license,
'version': PACKAGE_JSON.version,
};
fs.writeFileSync('manifest.json', JSON.stringify(input_json, undefined, 4));
}
const RL = readline.createInterface({ input: process.stdin, output: process.stdout });
RL.question(colors.bold('Change finding signatures (words) for the finder script? [Y/N]: '), (ASW1) => {
if (ASW1.toUpperCase() == 'Y') {
RL.question(colors.bold('Enter your custom signatures (words) separatedly by commas: '), (ASW2) => {
const input: string[] = ASW2.split(',');
new PREPARE_MODULE(input).traverse(__dirname);
RL.close();
});
}
else {
new PREPARE_MODULE(['NO']).traverse(__dirname);
RL.close();
}
});