Use prettier
This commit is contained in:
parent
cef6bd975a
commit
be07bb510f
4 changed files with 93 additions and 75 deletions
4
.prettierrc.json
Normal file
4
.prettierrc.json
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"semi": false,
|
||||
"singleQuote": true
|
||||
}
|
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"devDependencies": {
|
||||
"@types/bun": "^1.1.6"
|
||||
"@types/bun": "^1.1.6",
|
||||
"prettier": "^3.3.3"
|
||||
}
|
||||
}
|
161
src/index.ts
161
src/index.ts
|
@ -5,102 +5,115 @@ import { spawn } from 'child_process'
|
|||
const SOURCE_ROOTS = ['Core', 'Secret']
|
||||
|
||||
type RojoSourcemap = {
|
||||
name: string
|
||||
className: string
|
||||
filePaths?: string[]
|
||||
children?: RojoSourcemap[]
|
||||
name: string
|
||||
className: string
|
||||
filePaths?: string[]
|
||||
children?: RojoSourcemap[]
|
||||
}
|
||||
|
||||
const sourcemapCache = new Map<string, RojoSourcemap>()
|
||||
function getSourcemap(root: string): Promise<RojoSourcemap> {
|
||||
const cached = sourcemapCache.get(root)
|
||||
if (cached) {
|
||||
return Promise.resolve(cached)
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
const rojo = spawn('rojo', ['sourcemap', root])
|
||||
rojo.stdout.on('data', (data) => {
|
||||
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)
|
||||
}
|
||||
})
|
||||
const cached = sourcemapCache.get(root)
|
||||
if (cached) {
|
||||
return Promise.resolve(cached)
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
const rojo = spawn('rojo', ['sourcemap', root])
|
||||
rojo.stdout.on('data', (data) => {
|
||||
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)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function prefixSourcemap(sourcemap: RojoSourcemap, prefix: string): RojoSourcemap {
|
||||
return {
|
||||
name: sourcemap.name,
|
||||
className: sourcemap.className,
|
||||
filePaths: sourcemap.filePaths?.map(path => `${prefix}/${path}`),
|
||||
children: sourcemap.children?.map(child => prefixSourcemap(child, prefix)),
|
||||
}
|
||||
function prefixSourcemap(
|
||||
sourcemap: RojoSourcemap,
|
||||
prefix: string,
|
||||
): RojoSourcemap {
|
||||
return {
|
||||
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> {
|
||||
const sourcemap = await getSourcemap(root)
|
||||
const prefixed = prefixSourcemap(sourcemap, root)
|
||||
return prefixed
|
||||
const sourcemap = await getSourcemap(root)
|
||||
const prefixed = prefixSourcemap(sourcemap, root)
|
||||
return prefixed
|
||||
}
|
||||
|
||||
function mergeSourcemaps(sourcemaps: RojoSourcemap[]): RojoSourcemap {
|
||||
const merged: Required<RojoSourcemap> = {
|
||||
name: '',
|
||||
className: '',
|
||||
filePaths: [],
|
||||
children: []
|
||||
};
|
||||
const merged: Required<RojoSourcemap> = {
|
||||
name: '',
|
||||
className: '',
|
||||
filePaths: [],
|
||||
children: [],
|
||||
}
|
||||
|
||||
for (const sourcemap of sourcemaps) {
|
||||
merged.name = sourcemap.name;
|
||||
merged.className = sourcemap.className;
|
||||
for (const sourcemap of sourcemaps) {
|
||||
merged.name = sourcemap.name
|
||||
merged.className = sourcemap.className
|
||||
|
||||
if (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);
|
||||
}
|
||||
}
|
||||
if (sourcemap.filePaths) {
|
||||
merged.filePaths = sourcemap.filePaths
|
||||
}
|
||||
|
||||
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() {
|
||||
const sourcemaps = await Promise.all([
|
||||
getPrefixedSourcemap('Secret'),
|
||||
getPrefixedSourcemap('Core'),
|
||||
])
|
||||
const sourcemap = mergeSourcemaps(sourcemaps)
|
||||
await fs.promises.writeFile('sourcemap.json', JSON.stringify(sourcemap, null, 2))
|
||||
const sourcemaps = await Promise.all([
|
||||
getPrefixedSourcemap('Secret'),
|
||||
getPrefixedSourcemap('Core'),
|
||||
])
|
||||
const sourcemap = mergeSourcemaps(sourcemaps)
|
||||
await fs.promises.writeFile(
|
||||
'sourcemap.json',
|
||||
JSON.stringify(sourcemap, null, 2),
|
||||
)
|
||||
}
|
||||
|
||||
for (const root of SOURCE_ROOTS) {
|
||||
fs.watch(root, {recursive:true}, () => {
|
||||
sourcemapCache.delete(root)
|
||||
rebuild()
|
||||
})
|
||||
fs.watch(root, { recursive: true }, () => {
|
||||
sourcemapCache.delete(root)
|
||||
rebuild()
|
||||
})
|
||||
}
|
||||
|
||||
rebuild()
|
||||
rebuild()
|
||||
|
|
Loading…
Reference in a new issue