-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare-template.ts
145 lines (126 loc) · 5.54 KB
/
prepare-template.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
/**
* 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 { green, yellow, cyan, red, bgRed, bgGreen, white, bgBlue } from 'colors/safe'
/**
* Represents the PREPARE_MODULE for searching through files and updating the manifest.
*/
class PREPARE_MODULE {
/** The root directory of the module. */
ROOT_DIRECTORY: string = __dirname
/** An array of folder names to exclude from traversal. */
EXCLUDING_FOLDER: string[] = ['node_modules', 'venv', '.git', 'out']
/** An array of values to include in the search. */
INCLUDING_VALUES: string[] = ['FALCION', 'PATTERNU', 'PATTERNUGIT']
/**
* Creates an instance of the PREPARE_MODULE.
* @param {string[]} entries - An array of custom entries to include for searching.
*/
constructor(entries: string[]) {
if (entries[0] !== 'NO') {
for (const item of entries) this.INCLUDING_VALUES.push(item)
}
}
/**
* Searches for specified data within a file.
* @param {string} filepath - The path of the file to search.
* @param {string[]} data - An array of strings to search for within the file.
* @returns {Promise<void>} - A promise that resolves when the search is complete.
*/
async search(filepath: string, data: string[]): Promise<void> {
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(green(`Found "${target}" in L#${i} of:\n` + cyan(filepath)))
}
}
}
}
/**
* Recursively traverses a directory and searches for files.
* @param {string} directory - The directory path to traverse.
* @returns {Promise<void>} - A promise that resolves when the traversal is complete.
*/
async traverse(directory: string): Promise<void> {
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 if (filestat.isFile()) {
await this.search(filepath, this.INCLUDING_VALUES)
} else {
throw new Error(red('Invalid data format:') + bgRed(` ${filepath}`))
}
}
} catch (err) {
const error = err instanceof Error ? err : new Error('Unknown error.')
if (error.stack === 'ENOENT') {
console.error(red(`File or directory not found: ${error.message}`))
} else {
console.error(
red(
'Error reading directory: ' +
`${error.message}: ${error.stack !== undefined ? error.stack : ''}`
)
)
}
}
}
}
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 = JSON.parse(fs.readFileSync('package.json', { encoding: 'utf-8' }))
const MANIFEST = 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(bgGreen(white('Manifest is synced with package, keep everything as it was.')))
} else {
console.warn(bgBlue(yellow("Manifest is not synced with package's information, rewriting it.")))
fs.copyFileSync('manifest.json', 'manifest-backup.json')
const inputJson: Record<string, unknown> = {
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(inputJson, undefined, 4))
}
void new PREPARE_MODULE(['NO']).traverse(PREPARE_MODULE.prototype.ROOT_DIRECTORY).then(() => {
console.log(green('Traverse is finished.'))
})