-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathbuild.ts
More file actions
134 lines (122 loc) · 3.67 KB
/
build.ts
File metadata and controls
134 lines (122 loc) · 3.67 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
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
import { DefinedError } from "ajv";
import stringify from "fast-json-stable-stringify";
import { execSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import winston from "winston";
import yargs from "yargs";
import * as data from "../index.js";
import { validate, validateProposed } from "./validate.js";
import { fdir } from "fdir";
import YAML from "yaml";
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple(),
),
transports: new winston.transports.Console(),
});
const rootDir = new URL("..", import.meta.url);
yargs(process.argv.slice(2))
.scriptName("build")
.command({
command: "package",
describe: "Generate the web-features npm package",
handler: buildPackage,
})
.parseSync();
function buildPackage() {
const packageDir = new URL("./packages/web-features/", rootDir);
const filesToCopy = [
"LICENSE.txt",
"types.quicktype.ts",
"types.ts",
"schemas/data.schema.json",
];
if (!valid(data)) {
logger.error("Data failed schema validation. No package built.");
process.exit(1);
}
const json = stringify(data);
const dataPath = new URL("data.json", packageDir);
fs.writeFileSync(dataPath, json);
// TODO: Remove the extended data artifact in the next major release.
const extendedPath = new URL("data.extended.json", rootDir);
fs.writeFileSync(extendedPath, json);
const proposedPath = new URL("data.proposed.json", rootDir);
const proposedData = buildProposed();
if (!validProposed(proposedData)) {
logger.error("Proposed data failed schema validation. No package built.");
process.exit(1);
}
fs.writeFileSync(proposedPath, stringify(proposedData));
for (const file of filesToCopy) {
fs.copyFileSync(
new URL(file, rootDir),
new URL(path.basename(file), packageDir),
);
}
execSync("npm install", {
cwd: "./packages/web-features",
encoding: "utf-8",
});
execSync("npm run prepare", {
cwd: "./packages/web-features",
encoding: "utf-8",
});
}
function valid(data: any): boolean {
const valid = validate(data);
if (!valid) {
// TODO: turn on strictNullChecks, fix all the errors, and replace this with:
// const errors = validate.errors;
const errors = validate.errors as DefinedError[];
for (const error of errors) {
logger.error(`${error.instancePath}: ${error.message}`);
}
return false;
}
return true;
}
function buildProposed() {
const features: any = {};
const filePaths = new fdir()
.withBasePath()
.filter((fp) => fp.endsWith(".yml"))
.crawl("features/draft/proposed")
.sync() as string[];
for (const fp of filePaths) {
const { name: key } = path.parse(fp);
let data;
try {
data = YAML.parse(fs.readFileSync(fp, { encoding: "utf-8" }));
} catch {
console.warn(`${fp} is not a valid YAML file. Skipping.`);
continue;
}
if (data.kind === undefined) {
data.kind = "proposed";
} else if (!["proposed", "moved", "split"].includes(data.kind)) {
console.log(
`${fp} uses an unexpected kind ${JSON.stringify(data.kind)}. Skipping.`,
);
continue;
}
features[key] = data;
}
const proposed = { features };
return proposed;
}
function validProposed(data: any): boolean {
const valid = validateProposed(data);
if (!valid) {
// TODO: turn on strictNullChecks, fix all the errors, and replace this with:
// const errors = validate.errors;
const errors = validate.errors as DefinedError[];
for (const error of errors) {
logger.error(`${error.instancePath}: ${error.message}`);
}
return false;
}
return true;
}