2023-04-25 11:01:25 +00:00
|
|
|
import { readFile, writeFile as writeFileUnchecked } from 'fs/promises'
|
2023-05-05 13:10:36 +00:00
|
|
|
import json5 from 'json5'
|
2023-04-25 11:01:25 +00:00
|
|
|
import { basename, dirname, join, relative } from 'path'
|
|
|
|
import prettier from 'prettier'
|
|
|
|
import { fileURLToPath } from 'url'
|
2023-06-01 18:01:49 +00:00
|
|
|
import { nicelog } from './nicelog'
|
2023-04-25 11:01:25 +00:00
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
|
|
const __dirname = dirname(__filename)
|
|
|
|
const isBublic = basename(join(__dirname, '../..')) === 'bublic'
|
2023-06-23 14:23:14 +00:00
|
|
|
export const REPO_ROOT = join(__dirname, isBublic ? '../../../' : '../..')
|
2023-04-25 11:01:25 +00:00
|
|
|
export const BUBLIC_ROOT = join(__dirname, '../..')
|
|
|
|
|
|
|
|
export async function readJsonIfExists(file: string) {
|
2023-05-05 13:10:36 +00:00
|
|
|
const fileContents = await readFileIfExists(file)
|
|
|
|
if (fileContents === null) {
|
2023-04-25 11:01:25 +00:00
|
|
|
return null
|
|
|
|
}
|
2023-05-05 13:10:36 +00:00
|
|
|
return json5.parse(fileContents)
|
2023-04-25 11:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function readFileIfExists(file: string) {
|
|
|
|
try {
|
|
|
|
return await readFile(file, 'utf8')
|
|
|
|
} catch {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const prettierConfigPromise = prettier.resolveConfig(__dirname)
|
2023-05-09 16:08:38 +00:00
|
|
|
export async function writeCodeFile(
|
|
|
|
generator: string,
|
|
|
|
language: 'typescript' | 'javascript',
|
|
|
|
filePath: string,
|
|
|
|
code: string
|
|
|
|
) {
|
2023-04-25 11:01:25 +00:00
|
|
|
const formattedCode = prettier.format(
|
|
|
|
`
|
2023-05-09 16:08:38 +00:00
|
|
|
// This file is automatically generated by ${generator}.
|
2023-04-25 11:01:25 +00:00
|
|
|
// Do not edit manually.
|
|
|
|
|
|
|
|
${code}
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
...(await prettierConfigPromise),
|
2023-05-09 16:08:38 +00:00
|
|
|
parser: language === 'typescript' ? 'typescript' : 'babel',
|
2023-04-25 11:01:25 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
await writeStringFile(filePath, formattedCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function writeStringFile(filePath: string, contents: string) {
|
|
|
|
await writeFile(filePath, Buffer.from(contents, 'utf-8'))
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function writeFile(filePath: string, contents: Buffer) {
|
|
|
|
if (process.env.CI) {
|
|
|
|
let existingContents: Buffer | null = null
|
|
|
|
try {
|
|
|
|
existingContents = await readFile(filePath)
|
|
|
|
} catch {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
if (existingContents && !existingContents.equals(contents)) {
|
2023-06-01 18:01:49 +00:00
|
|
|
nicelog(
|
2023-04-25 11:01:25 +00:00
|
|
|
`Asset file ${relative(
|
|
|
|
REPO_ROOT,
|
|
|
|
filePath
|
|
|
|
)} has changed. Please run this script again and commit the changes.`
|
|
|
|
)
|
2023-06-01 18:01:49 +00:00
|
|
|
nicelog('Contents before:')
|
|
|
|
nicelog(existingContents.toString('utf-8'))
|
|
|
|
nicelog('\nContents after:')
|
|
|
|
nicelog(contents.toString('utf-8'))
|
2023-04-25 11:01:25 +00:00
|
|
|
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await writeFileUnchecked(filePath, contents, 'utf-8')
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function writeJsonFile(filePath: string, contents: unknown) {
|
|
|
|
const formattedJson = prettier.format(JSON.stringify(contents, null, '\t'), {
|
|
|
|
...(await prettierConfigPromise),
|
|
|
|
parser: 'json',
|
|
|
|
})
|
|
|
|
await writeStringFile(filePath, formattedJson)
|
|
|
|
}
|