forked from mozilla-extensions/firefox-translations
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-model-registry.js
94 lines (84 loc) · 2.47 KB
/
generate-model-registry.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
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
/* eslint-env node */
/* eslint-disable mozilla/balanced-listeners */
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const sha256File = require("sha256-file");
const zlib = require("zlib");
const getType = name => {
if (name.includes("model")) {
return "model";
}
if (name.includes("vocab")) {
return "vocab";
}
if (name.includes("lex")) {
return "lex";
}
return "unknown";
};
async function gunzip(file) {
const gunzippedFile = file.slice(0, -3);
return new Promise((resolve, reject) => {
const fileContents = fs.createReadStream(file);
const writeStream = fs.createWriteStream(gunzippedFile);
const unzip = zlib.createGunzip();
fileContents
.pipe(unzip)
.pipe(writeStream)
.on("finish", err => {
if (err) return reject(err);
return resolve(gunzippedFile);
});
});
}
async function generateModelRegistry() {
const modelRegistry = {};
await Promise.all(
glob.sync("firefox-translations-models/models/*/*").map(async directory => {
const languagePair = path.basename(directory);
modelRegistry[languagePair] = {};
return Promise.all(
glob.sync(`${directory}/*.gz`).map(async filePath => {
const gunzippedFilePath = await gunzip(filePath);
const name = path.basename(gunzippedFilePath);
const type = getType(name);
const stat = fs.statSync(filePath);
const estimatedCompressedSize = stat.size;
const gunzippedStat = fs.statSync(gunzippedFilePath);
const size = gunzippedStat.size;
const expectedSha256Hash = sha256File(gunzippedFilePath);
fs.unlinkSync(gunzippedFilePath);
modelRegistry[languagePair][type] = {
name,
size,
estimatedCompressedSize,
expectedSha256Hash,
};
}),
);
}),
);
const modelRegistryFileContents = `export interface ModelRegistry {
[languagePair: string]: {
[type: string]: {
name: string;
size: number;
estimatedCompressedSize: number;
expectedSha256Hash: string;
};
};
}
export const modelRegistry: ModelRegistry = ${JSON.stringify(
modelRegistry,
null,
2,
)};
`;
const targetPath = path.join("src", "core", "ts", "modelRegistry.ts");
await fs.promises.writeFile(targetPath, modelRegistryFileContents);
}
module.exports = { generateModelRegistry };
if (require.main === module) {
generateModelRegistry();
}