78 lines
2.1 KiB
JavaScript
78 lines
2.1 KiB
JavaScript
import { mkdir, writeFile } from 'node:fs/promises'
|
|
import { dirname, join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import * as prettier from 'prettier'
|
|
import labelsDef from '../../definitions/labels.json' with { type: 'json' }
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
|
|
export async function labels() {
|
|
const content = await gen()
|
|
|
|
const path = join(
|
|
__dirname,
|
|
'..',
|
|
'..',
|
|
'src',
|
|
'moderation',
|
|
'const',
|
|
'labels.ts',
|
|
)
|
|
|
|
await mkdir(dirname(path), { recursive: true })
|
|
await writeFile(path, content, 'utf8')
|
|
}
|
|
|
|
async function gen() {
|
|
const knownValues = new Set()
|
|
const flattenedLabelDefs = []
|
|
|
|
for (const { alias: aliases, ...label } of labelsDef) {
|
|
knownValues.add(label.identifier)
|
|
flattenedLabelDefs.push([label.identifier, { ...label, locales: [] }])
|
|
|
|
if (aliases) {
|
|
for (const alias of aliases) {
|
|
knownValues.add(alias)
|
|
flattenedLabelDefs.push([
|
|
alias,
|
|
{
|
|
...label,
|
|
identifier: alias,
|
|
locales: [],
|
|
comment: `@deprecated alias for \`${label.identifier}\``,
|
|
},
|
|
])
|
|
}
|
|
}
|
|
}
|
|
|
|
let labelDefsStr = `{`
|
|
for (const [key, { comment, ...value }] of flattenedLabelDefs) {
|
|
const commentStr = comment ? `\n/** ${comment} */\n` : ''
|
|
labelDefsStr += `${commentStr}'${key}': ${JSON.stringify(value, null, 2)},`
|
|
}
|
|
labelDefsStr += `}`
|
|
|
|
return prettier.format(
|
|
`/** this doc is generated by ./scripts/code/labels.mjs **/
|
|
import {InterpretedLabelValueDefinition, LabelPreference} from '../types'
|
|
|
|
export type KnownLabelValue = ${Array.from(knownValues)
|
|
.map((value) => `"${value}"`)
|
|
.join(' | ')}
|
|
|
|
export const DEFAULT_LABEL_SETTINGS: Record<string, LabelPreference> = ${JSON.stringify(
|
|
Object.fromEntries(
|
|
labelsDef
|
|
.filter((label) => label.configurable)
|
|
.map((label) => [label.identifier, label.defaultSetting]),
|
|
),
|
|
)}
|
|
|
|
export const LABELS: Record<KnownLabelValue, InterpretedLabelValueDefinition> = ${labelDefsStr}
|
|
`,
|
|
{ semi: false, parser: 'typescript', singleQuote: true },
|
|
)
|
|
}
|