Implement recursive scan for File modules.

Ex. store resourcepacks at /files/resourcepacks/MyPack.zip
Added as a module with destination path resourcepacks/MyPack.zip
This commit is contained in:
Daniel Scalzi
2020-02-02 17:30:53 -05:00
parent 064a664687
commit 54cc93d499
5 changed files with 85 additions and 57 deletions

View File

@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Stats } from 'fs'
import { Type } from 'helios-distribution-types'
import { join } from 'path'
import { resolve } from 'url'
import { Type, Module } from 'helios-distribution-types'
import { resolve as resolveURL } from 'url'
import { ModuleStructure } from './module.struct'
import { readdir, stat } from 'fs-extra'
import { join, resolve } from 'path'
export class MiscFileStructure extends ModuleStructure {
@@ -15,6 +16,29 @@ export class MiscFileStructure extends ModuleStructure {
super(absoluteRoot, relativeRoot, 'files', baseUrl, Type.File)
}
public async getSpecModel(): Promise<Module[]> {
if (this.resolvedModels == null) {
this.resolvedModels = await this.recursiveModuleScan(this.containerDirectory)
}
return this.resolvedModels
}
protected async recursiveModuleScan(dir: string): Promise<Module[]> {
let acc: Module[] = []
const subdirs = await readdir(dir)
for (const file of subdirs) {
const filePath = resolve(dir, file)
const stats = await stat(filePath)
if (stats.isDirectory()) {
acc = acc.concat(await this.recursiveModuleScan(filePath))
} else {
acc.push(await this.parseModule(file, filePath, stats))
}
}
return acc
}
protected async getModuleId(name: string, path: string, stats: Stats, buf: Buffer): Promise<string> {
return name
}
@@ -22,10 +46,10 @@ export class MiscFileStructure extends ModuleStructure {
return name
}
protected async getModuleUrl(name: string, path: string, stats: Stats): Promise<string> {
return resolve(this.baseUrl, join(this.relativeRoot, name))
return resolveURL(this.baseUrl, join(this.relativeRoot, name))
}
protected async getModulePath(name: string, path: string, stats: Stats): Promise<string | null> {
return name
return path.substr(this.containerDirectory.length+1).replace(/\\/g, '/')
}
}

View File

@@ -33,6 +33,29 @@ export abstract class ModuleStructure extends BaseModelStructure<Module> {
protected async abstract getModuleUrl(name: string, path: string, stats: Stats): Promise<string>
protected async abstract getModulePath(name: string, path: string, stats: Stats): Promise<string | null>
protected async parseModule(file: string, filePath: string, stats: Stats): Promise<Module> {
const buf = await readFile(filePath)
const mdl: Module = {
id: await this.getModuleId(file, filePath, stats, buf),
name: await this.getModuleName(file, filePath, stats, buf),
type: this.type,
required: {
value: false,
def: false
},
artifact: {
size: stats.size,
MD5: createHash('md5').update(buf).digest('hex'),
url: await this.getModuleUrl(file, filePath, stats)
}
}
const pth = await this.getModulePath(file, filePath, stats)
if (pth) {
mdl.artifact.path = pth
}
return mdl
}
private async _doModuleRetrieval(): Promise<Module[]> {
const accumulator: Module[] = []
@@ -43,26 +66,7 @@ export abstract class ModuleStructure extends BaseModelStructure<Module> {
const filePath = resolve(this.containerDirectory, file)
const stats = await lstat(filePath)
if (stats.isFile()) {
const buf = await readFile(filePath)
const mdl: Module = {
id: await this.getModuleId(file, filePath, stats, buf),
name: await this.getModuleName(file, filePath, stats, buf),
type: this.type,
required: {
value: false,
def: false
},
artifact: {
size: stats.size,
MD5: createHash('md5').update(buf).digest('hex'),
url: await this.getModuleUrl(file, filePath, stats)
}
}
const pth = await this.getModulePath(file, filePath, stats)
if (pth) {
mdl.artifact.path = pth
}
accumulator.push(mdl)
accumulator.push(await this.parseModule(file, filePath, stats))
}
}
}