|
| 1 | +import { coreBrowserSet, getStatus } from "compute-baseline"; |
| 2 | +import escapeHtml from "escape-html"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import YAML from "yaml"; |
| 7 | +import yargs from "yargs"; |
| 8 | +import { features } from ".."; |
| 9 | +import { defaultCompat } from "../packages/compute-baseline/dist/browser-compat-data/compat"; |
| 10 | +import { feature } from "../packages/compute-baseline/dist/browser-compat-data/feature"; |
| 11 | +import { SupportStatement } from "../packages/compute-baseline/dist/browser-compat-data/supportStatements"; |
| 12 | +import { convertHTML } from "../text"; |
| 13 | +import { FeatureData } from "../types"; |
| 14 | + |
| 15 | +interface Args { |
| 16 | + paths: string[]; |
| 17 | + verbose: number; |
| 18 | +} |
| 19 | + |
| 20 | +const argv = yargs(process.argv.slice(2)) |
| 21 | + .scriptName("dist") |
| 22 | + .usage("$0 [paths..]", "Inspect a .yml or .dist file", (yargs) => |
| 23 | + yargs.positional("paths", { |
| 24 | + describe: "Directories or files to inspect.", |
| 25 | + default: ["features/a.yml"], |
| 26 | + }), |
| 27 | + ) |
| 28 | + .option("verbose", { |
| 29 | + alias: "v", |
| 30 | + describe: "Show more detail", |
| 31 | + type: "count", |
| 32 | + default: 0, |
| 33 | + defaultDescription: "warn", |
| 34 | + }).argv as Args; |
| 35 | + |
| 36 | +function main(): void { |
| 37 | + for (const filePath of argv.paths) { |
| 38 | + const { name, ext, dir } = path.parse(filePath); |
| 39 | + const id = ext === ".dist" ? path.basename(name, ".yml") : name; |
| 40 | + |
| 41 | + const yamlFile = path.join(dir, `${id}.yml`); |
| 42 | + const distFile = `${yamlFile}.dist`; |
| 43 | + |
| 44 | + const feature = features[id]; |
| 45 | + if (!feature) { |
| 46 | + throw new Error(`No feature found for ID ${id}`); |
| 47 | + } |
| 48 | + |
| 49 | + const { compat_features } = feature; |
| 50 | + |
| 51 | + console.log(`${escapeHtml(feature.name)} (${yamlFile}, ${distFile})\n`); |
| 52 | + |
| 53 | + // Iterate over the compat keys in the order given by the dist file |
| 54 | + const yml = YAML.parse( |
| 55 | + fs.readFileSync(distFile, { encoding: "utf-8" }), |
| 56 | + ) as Partial<FeatureData>; |
| 57 | + let compatKeys: string[]; |
| 58 | + if (yml.compat_features?.length > 0) { |
| 59 | + compatKeys = yml.compat_features; |
| 60 | + } else if (compat_features && compat_features.length > 0) { |
| 61 | + compatKeys = compat_features; |
| 62 | + } else { |
| 63 | + compatKeys = []; |
| 64 | + } |
| 65 | + |
| 66 | + for (const compatKey of compatKeys) { |
| 67 | + const status = getStatus(id, compatKey); |
| 68 | + console.log( |
| 69 | + `- ${compatKey}${status.baseline ? ` (${status.baseline})` : ""}`, |
| 70 | + ); |
| 71 | + for (const [browser, notes] of getNotes(compatKey).entries()) { |
| 72 | + console.log(` - ${browser}`); |
| 73 | + for (const note of notes) { |
| 74 | + console.log(` - ${convertHTML(note).text}`); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function getNotes(compatKey: string): Map<string, string[]> { |
| 82 | + const browserNotes = new Map<string, string[]>(); |
| 83 | + const f = feature(compatKey); |
| 84 | + for (const browserKey of coreBrowserSet) { |
| 85 | + const statements = f |
| 86 | + .supportStatements(defaultCompat.browser(browserKey)) |
| 87 | + .filter(isEligibleStatement); |
| 88 | + for (const statement of statements) { |
| 89 | + const notes = Array.isArray(statement.data.notes!) |
| 90 | + ? statement.data.notes |
| 91 | + : [statement.data.notes]; |
| 92 | + for (const note of notes) { |
| 93 | + browserNotes.set(statement.browser.id, [ |
| 94 | + ...(browserNotes.get(statement.browser.id) ?? []), |
| 95 | + `${statement.partial_implementation ? `[partial_implementation] ` : ""}${statement.version_added}+: ${note}`, |
| 96 | + ]); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return browserNotes; |
| 101 | +} |
| 102 | + |
| 103 | +function isEligibleStatement(statement: SupportStatement): boolean { |
| 104 | + if (statement.version_added === false) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + if (statement.version_removed) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + if (statement.flags.length) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + if (statement.data.alternative_name) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + if (statement.data.prefix) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + const { notes } = statement.data; |
| 120 | + if (notes === undefined) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + if (notes.length === 0) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + return true; |
| 127 | +} |
| 128 | + |
| 129 | +if (import.meta.url.startsWith("file:")) { |
| 130 | + if (process.argv[1] === fileURLToPath(import.meta.url)) { |
| 131 | + main(); |
| 132 | + } |
| 133 | +} |
0 commit comments