-
Notifications
You must be signed in to change notification settings - Fork 2
/
environment.ts
257 lines (220 loc) · 7.69 KB
/
environment.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* MIT License
*
* Copyright (c) 2023-2024 Falcion
*
* 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.
*
* Any code and/or API associated with OBSIDIAN behaves as stated in their distribution policy.
*/
import * as ndos from 'node:os';
import * as path from 'node:path';
import * as fsxt from 'fs-extra';
import * as rcli from 'readline';
import colors from 'colors/safe';
const PROMPTS: string[] =
[
'DO YOU WANT TO UPDATE AND CHECK YOUR MANIFEST FOR THE SYNC',
'DO YOU WANT TO ADD WORDS TO SEARCH FOR THEM IN THE PROJECT',
'WRITE THE WORDS SEPARATED BY COMMA',
]
/*
* Declaring unsupport for macOS, iOS and any related types of platforms.
*/
if(ndos.type() === 'Darwin') process.abort();
/**
* @class
* Represents a logger utility for logging messages with different severity levels and colors.
*/
export class LOCALE_LOGGER {
/**
* A process ID which represents session of localized logger instance.
* @type {number}
*/
private static session_id: number = process.ppid;
/**
* Logs the info message.
* @param {...any} data - The data to be logged.
*/
public static info(...data: any[]): void {
const datetime = new Date().toLocaleString();
console.info(
colors.blue(`[${datetime}] <${this.session_id}> \t - ${data.join(' ')}`));
}
/**
* Logs the warn message.
* @param {...any} data - The data to be logged.
*/
public static warn(...data: any[]): void {
const datetime = new Date().toLocaleString();
console.warn(
colors.yellow(`[${datetime}] <${this.session_id}> \t - ${data.join(' ')}`));
}
/**
* Logs the error message.
* @param {...any} data - The data to be logged.
*/
public static error(...data: any[]): void {
const datetime = Date.now().toLocaleString();
console.error(
colors.bgRed(colors.white(`[${datetime}] <${this.session_id}> \t - ${data}`)));
}
/**
* Logs the success message.
* @param {...any} data - The data to be logged.
*/
public static success(...data: any[]): void {
const datetime = Date.now().toLocaleString();
console.log(
colors.green(`[${datetime}] <${this.session_id}> \t - ${data}`));
}
/**
* Logs the message with custom color.
* @param {(str: string) => string} color - The color function.
* @param {...any} data - The data to be logged.
*/
public static raw(color: (str: string) => string, ...data: any[]): void {
console.debug(
color(data.join(' ')));
}
/**
* Formats a message with custom color.
* @param {(str: string) => string} color - The color function.
* @param {string} message - The message to be formatted.
* @returns {string} The formatted message.
*/
public static msg(color: (str: string) => string, message: string): string {
return color(message);
}
}
/**
* @class
* Represents a module for searching and updating files.
*/
export default class LOCALE_MODULE {
/**
* The root directory of the module.
* @type {string}
*/
public ROOT_DIRECTORY: string = __dirname;
/**
* Directories to be excluded from traversal.
* @type {string[]}
*/
private EXCLUDING_FOLDERS: string[] = [
'node_modules',
'dist',
'venv',
'.git',
'$git',
'$',
'out',
'bin',
];
/**
* Values to be excluded from file content search.
* @type {string[]}
*/
private EXCLUDING_VALUES: string[] = [
'FALCION',
'PATTERNU',
'PATTERNUGIT',
'PATTERNUGIT.NET'
];
/**
* Updates the exclusion settings based on user input.
* @param {string[]} entries - Entries to be added to the exclusion list.
* @param {string} actions - User action (Y or N).
*/
public update(
entries: string[],
actions: string): void {
if(actions.length > 1) {
throw new RangeError('Action input must be a char.', {
cause: actions
});
}
if(actions === 'Y') {
for(const entry of entries) {
this.EXCLUDING_VALUES.push(entry);
}
}
if(actions === 'N') {
this.EXCLUDING_FOLDERS = entries;
}
}
/**
* Searches for specified words in file contents.
* @param {string} filepath - The path of the file to search.
* @param {string[]} data - Words to search for.
* @returns {Promise<void>} A promise representing the search operation.
*/
public async search(filepath: string, data: string[]): Promise<void> {
const buffer: string = await fsxt.readFile(filepath, { 'encoding': 'utf-8' });
const contents: string[] = buffer.split(ndos.EOL);
for(var i = 0; i < contents.length; i++) {
const line = contents[i].toUpperCase();
for(const target of data)
if(line.includes(target)) {
LOCALE_LOGGER.raw(colors.green, `Found "${target}" in L#${i} of:`);
LOCALE_LOGGER.raw(colors.cyan, filepath);
}
}
}
/**
* Traverses directories and searches files for specified words.
* @param {string} directory - The directory to start traversal from.
* @returns {Promise<void>} A promise representing the traversal operation.
*/
public async traverse(directory: string = __dirname): Promise<void> {
try {
const items: string[] = await fsxt.readdir(directory);
for(const item of items) {
const itempath = path.join(directory, item);
const itemstats = await fsxt.stat(itempath);
if(itemstats.isDirectory()) {
if(!this.EXCLUDING_FOLDERS.includes(item))
await this.traverse(itempath);
} else if(itemstats.isFile()) {
await this.search(itempath, this.EXCLUDING_VALUES);
} else {
continue;
}
}
} catch(err: any) {
LOCALE_LOGGER.error(err);
}
}
}
(() => {
const RL = rcli.createInterface({ input: process.stdin, output: process.stdout });
RL.setPrompt(PROMPTS[1]);
RL.prompt(false);
const mod = new LOCALE_MODULE();
RL.question('Y/N/IGNORE:', (mode) => {
if(mode.toUpperCase() != 'IGNORE') {
RL.question('', (params) => {
const diction: string[] = params.split(',').map(str => str.trim());
mod.update(diction, mode);
})
}
mod.traverse();
});
RL.close();
})();