UNPKG

@kcjpop/x-browser-compat

Version:

A Web Component to display browser compatibility data

97 lines (81 loc) 1.95 kB
#!/usr/bin/env node import process from "node:process"; import fs from "node:fs/promises"; import bcd from "@mdn/browser-compat-data" assert { type: "json" }; const mock = { html: { bar: { blah: { __compat: { what: 999 }, more: { __compat: { yes: 1 } }, where: { who: { __compat: { yes: 2 } }, whom: { __compat: { yes: 2 } }, }, }, haha: { __compat: { yes: 1 } }, }, }, }; function get(obj, path) { const attrs = path.split("/"); let cur = obj; for (const attr of attrs) { cur = cur[attr]; } return cur; } const rootPath = new URL(`./bcd`, import.meta.url).pathname; async function writeToFile(path) { try { const content = get(bcd, path); const folder = rootPath + "/" + path; await fs.mkdir(folder, { recursive: true }); await fs.writeFile( folder + "/index.json", JSON.stringify(content.__compat) ); } catch (e) { console.error("Failed to write to " + path + "/index.json", e); } } function doTheJob(stack) { const path = stack.pop(); const obj = get(bcd, path); const entries = Object.entries(obj); for (const [key, val] of entries) { const target = `${path}/${key}`; if (key === "__compat") { writeToFile(path); } else if (val.__compat != null && Object.values(val).length === 1) { writeToFile(target); } else stack.push(target); } } async function run() { const validSections = [ "api", "css", "html", "http", "javascript", "mathml", "svg", "webassembly", "webdriver", "webextensions", ]; const section = process.argv[2]; if (validSections.includes(section) === false) { console.error( "Incorrect usage of cli.js. Must be one of those: " + validSections.join(",") ); process.exit(1); } const stack = [section]; while (stack.length > 0) { doTheJob(stack); } } run();