forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease-build.js
More file actions
100 lines (84 loc) · 2.36 KB
/
release-build.js
File metadata and controls
100 lines (84 loc) · 2.36 KB
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
'use strict';
const fs = require('fs').promises;
const path = require('path');
const directory = './build/';
const verbatimFiles = ['LICENSE', 'README.md', 'index.d.ts', 'types.d.ts'];
// Returns a string representing data ready for writing to JSON file
function createDataBundle() {
const bcd = require('../index.js');
const string = JSON.stringify(bcd);
return string;
}
// Returns a promise for writing the data to JSON file
async function writeData() {
const dest = path.resolve(directory, 'data.json');
const data = createDataBundle();
await fs.writeFile(dest, data);
}
async function writeIndex() {
const dest = path.resolve(directory, 'index.js');
const content = `module.exports = require("./data.json");\n`;
await fs.writeFile(dest, content);
}
// Returns an array of promises for copying of all files that don't need transformation
async function copyFiles() {
for (const file of verbatimFiles) {
const src = path.join('./', file);
const dest = path.join(directory, file);
await fs.copyFile(src, dest);
}
}
function createManifest() {
const full = require('../package.json');
const minimal = { main: 'index.js' };
const minimalKeys = [
'name',
'version',
'description',
'repository',
'keywords',
'author',
'license',
'bugs',
'homepage',
'types',
];
for (const key of minimalKeys) {
if (key in full) {
minimal[key] = full[key];
} else {
throw `Could not create a complete manifest! ${key} is missing!`;
}
}
return JSON.stringify(minimal);
}
async function writeManifest() {
const dest = path.resolve(directory, 'package.json');
const manifest = createManifest();
await fs.writeFile(dest, manifest);
}
async function main() {
// Remove existing files, if there are any
await fs
.rm(directory, {
force: true,
recursive: true,
})
.catch(e => {
// Missing folder is not an issue since we wanted to delete it anyway
if (e.code !== 'ENOENT') throw e;
});
// Crate a new directory
await fs.mkdir(directory);
await writeManifest();
await writeData();
await writeIndex();
await copyFiles();
console.log('Data bundle is ready');
}
// This is needed because NodeJS does not support top-level await.
// Also, make sure to log all errors and exit with failure code.
main().catch(e => {
console.error(e);
process.exit(1);
});