Use prettier

This commit is contained in:
Wingy 2024-08-17 06:04:33 -04:00
parent cef6bd975a
commit 48ab85b939
4 changed files with 93 additions and 75 deletions

4
.prettierrc.json Normal file
View file

@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}

BIN
bun.lockb

Binary file not shown.

View file

@ -1,5 +1,6 @@
{ {
"devDependencies": { "devDependencies": {
"@types/bun": "^1.1.6" "@types/bun": "^1.1.6",
"prettier": "^3.3.3"
} }
} }

View file

@ -5,102 +5,115 @@ import { spawn } from 'child_process'
const SOURCE_ROOTS = ['Core', 'Secret'] const SOURCE_ROOTS = ['Core', 'Secret']
type RojoSourcemap = { type RojoSourcemap = {
name: string name: string
className: string className: string
filePaths?: string[] filePaths?: string[]
children?: RojoSourcemap[] children?: RojoSourcemap[]
} }
const sourcemapCache = new Map<string, RojoSourcemap>() const sourcemapCache = new Map<string, RojoSourcemap>()
function getSourcemap(root: string): Promise<RojoSourcemap> { function getSourcemap(root: string): Promise<RojoSourcemap> {
const cached = sourcemapCache.get(root) const cached = sourcemapCache.get(root)
if (cached) { if (cached) {
return Promise.resolve(cached) return Promise.resolve(cached)
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let stdout = '' let stdout = ''
let stderr = '' let stderr = ''
const rojo = spawn('rojo', ['sourcemap', root]) const rojo = spawn('rojo', ['sourcemap', root])
rojo.stdout.on('data', (data) => { rojo.stdout.on('data', (data) => {
stdout += data.toString() stdout += data.toString()
})
rojo.stderr.on('data', (data) => {
stderr += data.toString()
})
rojo.on('close', (code) => {
if (code === 0) {
try {
const sourcemap = JSON.parse(stdout)
sourcemapCache.set(root, sourcemap)
resolve(sourcemap)
} catch {
reject(new Error(`Failed to parse sourcemap: ${stdout}`))
}
} else {
reject(stderr)
}
})
}) })
rojo.stderr.on('data', (data) => {
stderr += data.toString()
})
rojo.on('close', (code) => {
if (code === 0) {
try {
const sourcemap = JSON.parse(stdout)
sourcemapCache.set(root, sourcemap)
resolve(sourcemap)
} catch {
reject(new Error(`Failed to parse sourcemap: ${stdout}`))
}
} else {
reject(stderr)
}
})
})
} }
function prefixSourcemap(sourcemap: RojoSourcemap, prefix: string): RojoSourcemap { function prefixSourcemap(
return { sourcemap: RojoSourcemap,
name: sourcemap.name, prefix: string,
className: sourcemap.className, ): RojoSourcemap {
filePaths: sourcemap.filePaths?.map(path => `${prefix}/${path}`), return {
children: sourcemap.children?.map(child => prefixSourcemap(child, prefix)), name: sourcemap.name,
} className: sourcemap.className,
filePaths: sourcemap.filePaths?.map((path) => `${prefix}/${path}`),
children: sourcemap.children?.map((child) =>
prefixSourcemap(child, prefix),
),
}
} }
async function getPrefixedSourcemap(root: string): Promise<RojoSourcemap> { async function getPrefixedSourcemap(root: string): Promise<RojoSourcemap> {
const sourcemap = await getSourcemap(root) const sourcemap = await getSourcemap(root)
const prefixed = prefixSourcemap(sourcemap, root) const prefixed = prefixSourcemap(sourcemap, root)
return prefixed return prefixed
} }
function mergeSourcemaps(sourcemaps: RojoSourcemap[]): RojoSourcemap { function mergeSourcemaps(sourcemaps: RojoSourcemap[]): RojoSourcemap {
const merged: Required<RojoSourcemap> = { const merged: Required<RojoSourcemap> = {
name: '', name: '',
className: '', className: '',
filePaths: [], filePaths: [],
children: [] children: [],
}; }
for (const sourcemap of sourcemaps) { for (const sourcemap of sourcemaps) {
merged.name = sourcemap.name; merged.name = sourcemap.name
merged.className = sourcemap.className; merged.className = sourcemap.className
if (sourcemap.filePaths) { if (sourcemap.filePaths) {
merged.filePaths = sourcemap.filePaths; merged.filePaths = sourcemap.filePaths
}
for (const child of sourcemap.children ?? []) {
const existingChild = merged.children.findIndex(oldChild => oldChild.name === child.name);
if (existingChild !== -1) {
merged.children[existingChild] = mergeSourcemaps([merged.children[existingChild], child]);
} else {
merged.children.push(child);
}
}
} }
return merged; for (const child of sourcemap.children ?? []) {
const existingChild = merged.children.findIndex(
(oldChild) => oldChild.name === child.name,
)
if (existingChild !== -1) {
merged.children[existingChild] = mergeSourcemaps([
merged.children[existingChild],
child,
])
} else {
merged.children.push(child)
}
}
}
return merged
} }
async function rebuild() { async function rebuild() {
const sourcemaps = await Promise.all([ const sourcemaps = await Promise.all([
getPrefixedSourcemap('Secret'), getPrefixedSourcemap('Secret'),
getPrefixedSourcemap('Core'), getPrefixedSourcemap('Core'),
]) ])
const sourcemap = mergeSourcemaps(sourcemaps) const sourcemap = mergeSourcemaps(sourcemaps)
await fs.promises.writeFile('sourcemap.json', JSON.stringify(sourcemap, null, 2)) await fs.promises.writeFile(
'sourcemap.json',
JSON.stringify(sourcemap, null, 2),
)
} }
for (const root of SOURCE_ROOTS) { for (const root of SOURCE_ROOTS) {
fs.watch(root, {recursive:true}, () => { fs.watch(root, { recursive: true }, () => {
sourcemapCache.delete(root) sourcemapCache.delete(root)
rebuild() rebuild()
}) })
} }
rebuild() rebuild()