-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmake-index.js
41 lines (38 loc) · 1.47 KB
/
make-index.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
'use strict';
const { promises: fs } = require('fs');
const path = require('path');
const bson = require('bson');
const zlib = require('zlib');
const util = require('util');
(async() => {
const snippetsDir = path.join(__dirname, '..', 'snippets');
const index = [];
for await (const dir of await fs.opendir(snippetsDir)) {
if (!dir.isDirectory()) continue;
const pjsonPath = path.join(snippetsDir, dir.name, 'package.json');
const pjson = JSON.parse(await fs.readFile(pjsonPath, 'utf8'));
if (pjson.errorMatchers) {
pjson.errorMatchers = require(path.join(snippetsDir, dir.name, pjson.errorMatchers));
}
try {
pjson.readme = await fs.readFile(path.join(snippetsDir, dir.name, 'README.md'), 'utf8');
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
index.push(pjson);
}
const ownPjsonPath = path.join(__dirname, '..', 'package.json');
const ownPjson = JSON.parse(await fs.readFile(ownPjsonPath, 'utf8'));
const metadata = (({ homepage, repository, bugs }) => ({ homepage, repository, bugs }))(ownPjson);
const indexFileContents = {
indexFileVersion: 1,
index,
metadata
};
const data = await util.promisify(zlib.brotliCompress)(bson.serialize(indexFileContents), {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY
}
});
await fs.writeFile(path.join(__dirname, '..', 'index.bson.br'), data);
})().catch(err => { process.nextTick(() => { throw err; }); });