#!/usr/bin/env -S node -r esbin
import fs from "fs"
import { resolve } from "path"

const file = resolve(__dirname, "githubDark.json")
const json = JSON.parse(fs.readFileSync(file, "utf8"))

const ensureArray = <T>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value])

json.rules = (
  json.tokenColors as {
    scope: string | string[]
    settings: { [key: string]: string }
  }[]
).flatMap(({ settings, scope }) =>
  ensureArray(scope).map(token => ({ token, ...settings })),
)

fs.writeFileSync(file, JSON.stringify(json, null, 2))
