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
|
import fs from 'fs-extra';
import { glob } from 'glob';
import { minimatch } from 'minimatch';
import upath from 'upath';
import { hashFile, hashFromArray } from './utils/hash.mjs';
console.log('generating imports');
const newFiles = new Set();
if (!fs.existsSync('lib')) {
console.log('> missing sources');
process.exit(0);
}
if (!fs.existsSync('data')) {
console.log('> missing data folder');
process.exit(0);
}
/**
*
* @param {string} file
* @param {string} code
*/
async function updateFile(file, code) {
const oldCode = fs.existsSync(file) ? await fs.readFile(file, 'utf8') : null;
if (code !== oldCode) {
await fs.writeFile(file, code);
}
newFiles.add(file);
}
const dataPaths = [
'data',
'node_modules/emojibase-data/en/shortcodes/github.json',
];
/**
*
* @param {string[]} paths
* @returns {string[]}
*/
function expandPaths(paths) {
return paths
.map((pathName) => {
const stat = fs.statSync(pathName);
if (stat.isFile()) {
return [pathName];
}
if (stat.isDirectory()) {
const dirPaths = fs
.readdirSync(pathName, { withFileTypes: true })
.filter(
(dirent) =>
!(dirent.isFile() && ['.DS_Store'].includes(dirent.name)),
)
.map((dirent) => upath.join(pathName, dirent.name));
return expandPaths(dirPaths);
}
return [];
})
.reduce((x, y) => x.concat(y));
}
/**
* @param {string} filePath
* @returns {Promise<string>}
*/
async function getFileHash(filePath) {
try {
const hash = await hashFile(filePath, 'sha256');
return hash;
} catch {
throw new Error(`ERROR: Unable to generate hash for ${filePath}`);
}
}
/**
*
* @param {string} managerName
* @param {boolean} isCustomManager
* @returns {Promise<string>}
*/
export async function getManagerHash(managerName, isCustomManager) {
/** @type {string[]} */
let hashes = [];
let folderPattern = `lib/modules/manager/${managerName}/**`;
if (isCustomManager) {
folderPattern = `lib/modules/manager/custom/${managerName}/**`;
}
const files = (await glob(folderPattern)).filter((fileName) =>
minimatch(fileName, '*.+(snap|spec.ts)', { matchBase: true }),
);
// sort files in case glob order changes
files.sort();
for (const fileAddr of files) {
const hash = await getFileHash(fileAddr);
hashes.push(hash);
}
if (hashes.length) {
return hashFromArray(hashes, 'sha256');
}
throw new Error(`Unable to generate hash for manager/${managerName}`);
}
async function generateData() {
const files = expandPaths(dataPaths).sort();
const importDataFileType = files.map((x) => ` | '${x}'`).join('\n');
const contentMapDecl = 'const data = new Map<DataFile, string>();';
/** @type {string[]} */
const contentMapAssignments = [];
for (const file of files) {
const key = file.replace(/\\/g, '/');
const rawFileContent = await fs.readFile(file, 'utf8');
const value = JSON.stringify(rawFileContent);
console.log(`> ${key}`);
contentMapAssignments.push(`data.set('${key}', ${value});`);
}
await updateFile(
`lib/data-files.generated.ts`,
[
`export type DataFile =\n${importDataFileType};`,
contentMapDecl,
contentMapAssignments.join('\n'),
`export default data;\n`,
].join('\n\n'),
);
}
async function generateHash() {
console.log('generating hashes');
try {
const hashMap = `export const hashMap = new Map<string, string>();`;
/** @type {Record<string, string>[]} */
let hashes = [];
// get managers list
const managers = (
await fs.readdir('lib/modules/manager', { withFileTypes: true })
)
.filter((file) => file.isDirectory())
.map((file) => file.name)
.filter((mgr) => mgr !== 'custom');
const customManagers = (
await fs.readdir('lib/modules/manager/custom', { withFileTypes: true })
)
.filter((file) => file.isDirectory())
.map((file) => file.name);
for (const manager of managers) {
const hash = await getManagerHash(manager, false);
hashes.push({ manager, hash });
}
for (const manager of customManagers) {
const hash = await getManagerHash(manager, true);
hashes.push({ manager, hash });
}
//add manager hashes to hashMap {key->manager, value->hash}
const hashStrings = (await Promise.all(hashes)).map(
({ manager, hash }) => `hashMap.set('${manager}','${hash}');`,
);
//write hashMap to fingerprint.generated.ts
await updateFile(
'lib/modules/manager/fingerprint.generated.ts',
[hashMap, hashStrings.join('\n')].join('\n\n'),
);
} catch (err) {
console.log('ERROR:', err.message);
process.exit(1);
}
}
await (async () => {
try {
// data-files
await generateData();
await generateHash();
await Promise.all(
(await glob('lib/**/*.generated.ts'))
.map((f) => upath.join(f))
.filter((f) => !newFiles.has(f))
.map(async (file) => {
await fs.remove(file);
}),
);
} catch (e) {
console.log(e.toString());
process.exit(1);
}
})();
|