|
| 1 | +import assert from 'assert'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { fdir } from 'fdir'; |
| 5 | +import features from '../index.js'; |
| 6 | + |
| 7 | +const BCD_PATH = '/Users/foolip/mdn/browser-compat-data'; |
| 8 | + |
| 9 | +// Map from api.CoolThing.something to cool-thing |
| 10 | +const bcdToFeature = new Map(); |
| 11 | + |
| 12 | +// Set of the first part of the BCD path encountered. |
| 13 | +const bcdDirs = new Set(); |
| 14 | + |
| 15 | +for (const [feature, {compat_features}] of Object.entries(features)) { |
| 16 | + if (!compat_features) { |
| 17 | + continue; |
| 18 | + } |
| 19 | + for (const key of compat_features) { |
| 20 | + assert(!bcdToFeature.has(key)); |
| 21 | + bcdToFeature.set(key, feature); |
| 22 | + bcdDirs.add(key.split('.')[0]); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +const bcdJsons = new fdir() |
| 27 | + .withBasePath() |
| 28 | + .filter((fp) => { |
| 29 | + const dir = path.relative(BCD_PATH, fp).split(path.sep)[0]; |
| 30 | + return bcdDirs.has(dir); |
| 31 | + }) |
| 32 | + .filter((fp) => fp.endsWith('.json')) |
| 33 | + .crawl(BCD_PATH) |
| 34 | + .sync(); |
| 35 | + |
| 36 | +const lookup = (root, key) => { |
| 37 | + const parts = key.split('.'); |
| 38 | + let node = root; |
| 39 | + for (const part of parts) { |
| 40 | + if (Object.hasOwn(node, part)) { |
| 41 | + node = node[part]; |
| 42 | + //console.log(`${part} found`); |
| 43 | + } else { |
| 44 | + //console.log(`${part} not found`); |
| 45 | + return undefined; |
| 46 | + } |
| 47 | + } |
| 48 | + return node; |
| 49 | +}; |
| 50 | + |
| 51 | +for (const fp of bcdJsons) { |
| 52 | + const src = fs.readFileSync(fp, { encoding: 'utf-8' }); |
| 53 | + const data = JSON.parse(src); |
| 54 | + let updated = false; |
| 55 | + for (const [key, feature] of bcdToFeature.entries()) { |
| 56 | + const node = lookup(data, key); |
| 57 | + if (!node || !node.__compat) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + //console.log(`${key} found in ${fp}`); |
| 61 | + const compat = node.__compat; |
| 62 | + //assert(!compat.tags, 'tags already present'); |
| 63 | + compat.tags = [`web-features:${feature}`]; |
| 64 | + updated = true; |
| 65 | + bcdToFeature.delete(key); |
| 66 | + } |
| 67 | + if (updated) { |
| 68 | + const src = JSON.stringify(data, null, ' ') + '\n'; |
| 69 | + fs.writeFileSync(fp, src, { encoding: 'utf-8' }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +for (const [key, feature] of bcdToFeature) { |
| 74 | + console.warn('Not migrated:', feature, key); |
| 75 | +} |
0 commit comments