feat: automated most of operator config detection

This commit is contained in:
Haoyu Xu
2025-05-06 14:04:17 +08:00
parent ea7947d900
commit f3f84068da
89 changed files with 667 additions and 849 deletions

View File

@@ -3,6 +3,11 @@ import path from 'node:path'
import yauzl from 'yauzl-promise'
import yazl from 'yazl'
type ReadOpts = {
encoding?: BufferEncoding
useAsPrefix?: boolean
}
export async function write(
content: string | NodeJS.ArrayBufferView,
filePath: string
@@ -21,14 +26,32 @@ export function writeSync(
export async function read(
filePath: string,
encoding: BufferEncoding = 'utf8'
opts: ReadOpts = { encoding: 'utf8', useAsPrefix: false }
) {
return await fsP.readFile(filePath, { encoding, flag: 'r' })
return await fsP.readFile(filePath, { ...opts, flag: 'r' })
}
export function readSync(filePath: string, encoding: BufferEncoding = 'utf8') {
export function readSync(
filePath: string,
opts: ReadOpts = { encoding: 'utf8', useAsPrefix: false }
) {
const useAsPrefix = opts.useAsPrefix
delete opts.useAsPrefix
if (useAsPrefix) {
const parent = path.dirname(filePath)
const filename = path.parse(filePath).name
const ext = path.parse(filePath).ext
const file = readdirSync(parent).find(
(e) => e.startsWith(filename) && e.endsWith(ext)
)
if (!file) return null
return fs.readFileSync(path.join(parent, file), {
...opts,
flag: 'r',
})
}
if (exists(filePath)) {
return fs.readFileSync(filePath, { encoding, flag: 'r' })
return fs.readFileSync(filePath, { ...opts, flag: 'r' })
}
return null
}

View File

@@ -17,7 +17,7 @@ export function read(
return data
},
}
const file = fs.readFileSync(file_dir, 'utf8')
const file = fs.readFileSync(file_dir, { encoding: 'utf8' })
return parse(file, {
customTags: [include, ...customTags],
} as SchemaOptions)