frontend: source: Prepare for class-based refactor

This commit is contained in:
Danny Lin 2021-12-18 02:05:08 -08:00
parent 1b9ed766f6
commit c956526f8f

View file

@ -176,54 +176,70 @@ async function searchLeafDir(
} }
} }
export async function wrapSystemSrc( class SourceResolver {
src: string, constructor(
device: string, readonly device: string,
buildId: string | null, readonly buildId: string | null,
tmp: TempState, readonly useTemp: boolean,
spinner: ora.Ora, readonly tmp: TempState,
): Promise<WrappedSource> { readonly spinner: ora.Ora,
let stat = await fs.stat(src) ) {}
if (stat.isDirectory()) {
// Directory
let tryDirs = [ async wrapSystemSrc(src: string) {
...(buildId != null && [ let stat = await fs.stat(src)
`${src}/${buildId}`, if (stat.isDirectory()) {
`${src}/${device}/${buildId}`, // Directory
`${src}/${buildId}/${device}`,
] || []),
`${src}/${device}`,
src,
]
// Also try to find extracted factory images first: device-buildId let tryDirs = [
if (buildId != null) { ...(this.buildId != null && [
tryDirs = [ `${src}/${this.buildId}`,
...tryDirs.map(p => `${p}/${device}-${buildId}`), `${src}/${this.device}/${this.buildId}`,
...tryDirs, `${src}/${this.buildId}/${this.device}`,
] || []),
`${src}/${this.device}`,
src,
] ]
}
for (let dir of tryDirs) { // Also try to find extracted factory images first: device-buildId
let { src: wrapped, factoryZip } = await searchLeafDir(dir, null, device, buildId, tmp, spinner) if (this.buildId != null) {
tryDirs = [
...tryDirs.map(p => `${p}/${this.device}-${this.buildId}`),
...tryDirs,
]
}
for (let dir of tryDirs) {
let { src: wrapped, factoryZip } = await searchLeafDir(dir, null, this.device, this.buildId, this.tmp, this.spinner)
if (wrapped != null) {
this.spinner.text = wrapped.startsWith(this.tmp.dir) ? path.relative(this.tmp.dir, wrapped) : wrapped
return { src: wrapped, factoryZip }
}
}
throw new Error(`No supported source format found in '${src}'`)
} else if (stat.isFile()) {
// File
// Attempt to extract factory images or OTA
let { src: wrapped, factoryZip } = await wrapLeafFile(src, null, this.tmp, this.spinner)
if (wrapped != null) { if (wrapped != null) {
spinner.text = wrapped.startsWith(tmp.dir) ? path.relative(tmp.dir, wrapped) : wrapped this.spinner.text = wrapped.startsWith(this.tmp.dir) ? path.relative(this.tmp.dir, wrapped) : wrapped
return { src: wrapped, factoryZip } return { src: wrapped, factoryZip }
} }
} }
throw new Error(`No supported source format found in '${src}'`) throw new Error(`Source '${src}' has unknown type`)
} else if (stat.isFile()) {
// File
// Attempt to extract factory images or OTA
let { src: wrapped, factoryZip } = await wrapLeafFile(src, null, tmp, spinner)
if (wrapped != null) {
spinner.text = wrapped.startsWith(tmp.dir) ? path.relative(tmp.dir, wrapped) : wrapped
return { src: wrapped, factoryZip }
}
} }
}
throw new Error(`Source '${src}' has unknown type`)
export async function wrapSystemSrc(
src: string,
device: string,
buildId: string | null,
useTemp: boolean,
tmp: TempState,
spinner: ora.Ora,
): Promise<WrappedSource> {
let resolver = new SourceResolver(device, buildId, useTemp, tmp, spinner)
return await resolver.wrapSystemSrc(src)
} }