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

@@ -12,7 +12,7 @@ Generate a distribution.json for Helios.
1. Clone the repository 1. Clone the repository
2. Install the dependencies (`npm i`) 2. Install the dependencies (`npm i`)
3. Create a `.env` file at the root directory and set the required values. 3. Create a `.env` file at the root directory of the cloned folder and set the required values.
Example Example
```properties ```properties

50
package-lock.json generated
View File

@@ -60,15 +60,15 @@
"dev": true "dev": true
}, },
"@types/node": { "@types/node": {
"version": "12.12.25", "version": "12.12.26",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.25.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.26.tgz",
"integrity": "sha512-nf1LMGZvgFX186geVZR1xMZKKblJiRfiASTHw85zED2kI1yDKHDwTKMdkaCbTlXoRKlGKaDfYywt+V0As30q3w==", "integrity": "sha512-UmUm94/QZvU5xLcUlNR8hA7Ac+fGpO1EG/a8bcWVz0P0LqtxFmun9Y2bbtuckwGboWJIT70DoWq1r3hb56n3DA==",
"dev": true "dev": true
}, },
"@types/yargs": { "@types/yargs": {
"version": "15.0.1", "version": "15.0.3",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.3.tgz",
"integrity": "sha512-sYlwNU7zYi6eZbMzFvG6eHD7VsEvFdoDtlD7eI1JTg7YNnuguzmiGsc6MPSq5l8n+h21AsNof0je+9sgOe4+dg==", "integrity": "sha512-XCMQRK6kfpNBixHLyHUsGmXrpEmFFxzMrcnSXFMziHd8CoNJo8l16FkHyQq4x+xbM7E2XL83/O78OD8u+iZTdQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@types/yargs-parser": "*" "@types/yargs-parser": "*"
@@ -81,12 +81,12 @@
"dev": true "dev": true
}, },
"@typescript-eslint/eslint-plugin": { "@typescript-eslint/eslint-plugin": {
"version": "2.17.0", "version": "2.18.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.17.0.tgz", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.18.0.tgz",
"integrity": "sha512-tg/OMOtPeXlvk0ES8mZzEZ4gd1ruSE03nsKcK+teJhxYv5CPCXK6Mb/OK6NpB4+CqGTHs4MVeoSZXNFqpT1PyQ==", "integrity": "sha512-kuO8WQjV+RCZvAXVRJfXWiJ8iYEtfHlKgcqqqXg9uUkIolEHuUaMmm8/lcO4xwCOtaw6mY0gStn2Lg4/eUXXYQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@typescript-eslint/experimental-utils": "2.17.0", "@typescript-eslint/experimental-utils": "2.18.0",
"eslint-utils": "^1.4.3", "eslint-utils": "^1.4.3",
"functional-red-black-tree": "^1.0.1", "functional-red-black-tree": "^1.0.1",
"regexpp": "^3.0.0", "regexpp": "^3.0.0",
@@ -102,32 +102,32 @@
} }
}, },
"@typescript-eslint/experimental-utils": { "@typescript-eslint/experimental-utils": {
"version": "2.17.0", "version": "2.18.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/experimental-utils/-/experimental-utils-2.17.0.tgz", "resolved": "https://registry.npmjs.org/@typescript-eslint/experimental-utils/-/experimental-utils-2.18.0.tgz",
"integrity": "sha512-2bNf+mZ/3mj5/3CP56v+ldRK3vFy9jOvmCPs/Gr2DeSJh+asPZrhFniv4QmQsHWQFPJFWhFHgkGgJeRmK4m8iQ==", "integrity": "sha512-J6MopKPHuJYmQUkANLip7g9I82ZLe1naCbxZZW3O2sIxTiq/9YYoOELEKY7oPg0hJ0V/AQ225h2z0Yp+RRMXhw==",
"dev": true, "dev": true,
"requires": { "requires": {
"@types/json-schema": "^7.0.3", "@types/json-schema": "^7.0.3",
"@typescript-eslint/typescript-estree": "2.17.0", "@typescript-eslint/typescript-estree": "2.18.0",
"eslint-scope": "^5.0.0" "eslint-scope": "^5.0.0"
} }
}, },
"@typescript-eslint/parser": { "@typescript-eslint/parser": {
"version": "2.17.0", "version": "2.18.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-2.17.0.tgz", "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-2.18.0.tgz",
"integrity": "sha512-k1g3gRQ4fwfJoIfgUpz78AovicSWKFANmvTfkAHP24MgJHjWfZI6ya7tsQZt1sLczvP4G9BE5G5MgADHdmJB/w==", "integrity": "sha512-SJJPxFMEYEWkM6pGfcnjLU+NJIPo+Ko1QrCBL+i0+zV30ggLD90huEmMMhKLHBpESWy9lVEeWlQibweNQzyc+A==",
"dev": true, "dev": true,
"requires": { "requires": {
"@types/eslint-visitor-keys": "^1.0.0", "@types/eslint-visitor-keys": "^1.0.0",
"@typescript-eslint/experimental-utils": "2.17.0", "@typescript-eslint/experimental-utils": "2.18.0",
"@typescript-eslint/typescript-estree": "2.17.0", "@typescript-eslint/typescript-estree": "2.18.0",
"eslint-visitor-keys": "^1.1.0" "eslint-visitor-keys": "^1.1.0"
} }
}, },
"@typescript-eslint/typescript-estree": { "@typescript-eslint/typescript-estree": {
"version": "2.17.0", "version": "2.18.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-2.17.0.tgz", "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-2.18.0.tgz",
"integrity": "sha512-g0eVRULGnEEUakxRfJO0s0Hr1LLQqsI6OrkiCLpdHtdJJek+wyd8mb00vedqAoWldeDcOcP8plqw8/jx9Gr3Lw==", "integrity": "sha512-gVHylf7FDb8VSi2ypFuEL3hOtoC4HkZZ5dOjXvVjoyKdRrvXAOPSzpNRnKMfaUUEiSLP8UF9j9X9EDLxC0lfZg==",
"dev": true, "dev": true,
"requires": { "requires": {
"debug": "^4.1.1", "debug": "^4.1.1",
@@ -2090,9 +2090,9 @@
} }
}, },
"rimraf": { "rimraf": {
"version": "3.0.0", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.0.tgz", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.1.tgz",
"integrity": "sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg==", "integrity": "sha512-IQ4ikL8SjBiEDZfk+DFVwqRK8md24RWMEJkdSlgNLkyyAImcjf8SWvU1qFMDOb4igBClbTQ/ugPqXcRwdFTxZw==",
"dev": true, "dev": true,
"requires": { "requires": {
"glob": "^7.1.3" "glob": "^7.1.3"

View File

@@ -28,12 +28,12 @@
"devDependencies": { "devDependencies": {
"@types/adm-zip": "^0.4.32", "@types/adm-zip": "^0.4.32",
"@types/fs-extra": "^8.0.1", "@types/fs-extra": "^8.0.1",
"@types/node": "^12.12.25", "@types/node": "^12.12.26",
"@types/yargs": "^15.0.1", "@types/yargs": "^15.0.3",
"@typescript-eslint/eslint-plugin": "^2.17.0", "@typescript-eslint/eslint-plugin": "^2.18.0",
"@typescript-eslint/parser": "^2.17.0", "@typescript-eslint/parser": "^2.18.0",
"eslint": "^6.8.0", "eslint": "^6.8.0",
"rimraf": "^3.0.0", "rimraf": "^3.0.1",
"typescript": "^3.7.5" "typescript": "^3.7.5"
}, },
"dependencies": { "dependencies": {
@@ -43,6 +43,6 @@
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"toml": "^3.0.0", "toml": "^3.0.0",
"yargs": "^15.1.0", "yargs": "^15.1.0",
"helios-distribution-types": "1.0.0-pre.1" "helios-distribution-types": "^1.0.0-pre.1"
} }
} }

View File

@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-unused-vars */
import { Stats } from 'fs' import { Stats } from 'fs'
import { Type } from 'helios-distribution-types' import { Type, Module } from 'helios-distribution-types'
import { join } from 'path' import { resolve as resolveURL } from 'url'
import { resolve } from 'url'
import { ModuleStructure } from './module.struct' import { ModuleStructure } from './module.struct'
import { readdir, stat } from 'fs-extra'
import { join, resolve } from 'path'
export class MiscFileStructure extends ModuleStructure { export class MiscFileStructure extends ModuleStructure {
@@ -15,6 +16,29 @@ export class MiscFileStructure extends ModuleStructure {
super(absoluteRoot, relativeRoot, 'files', baseUrl, Type.File) 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> { protected async getModuleId(name: string, path: string, stats: Stats, buf: Buffer): Promise<string> {
return name return name
} }
@@ -22,10 +46,10 @@ export class MiscFileStructure extends ModuleStructure {
return name return name
} }
protected async getModuleUrl(name: string, path: string, stats: Stats): Promise<string> { 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> { 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 getModuleUrl(name: string, path: string, stats: Stats): Promise<string>
protected async abstract getModulePath(name: string, path: string, stats: Stats): Promise<string | null> 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[]> { private async _doModuleRetrieval(): Promise<Module[]> {
const accumulator: Module[] = [] const accumulator: Module[] = []
@@ -43,26 +66,7 @@ export abstract class ModuleStructure extends BaseModelStructure<Module> {
const filePath = resolve(this.containerDirectory, file) const filePath = resolve(this.containerDirectory, file)
const stats = await lstat(filePath) const stats = await lstat(filePath)
if (stats.isFile()) { if (stats.isFile()) {
const buf = await readFile(filePath) accumulator.push(await this.parseModule(file, filePath, stats))
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)
} }
} }
} }