support original manifest

This commit is contained in:
SrGooglo 2024-01-25 23:11:49 +01:00
parent f7818495e3
commit 33f73fefbb

View File

@ -1,11 +1,8 @@
import path from "node:path"
import fs from "node:fs"
import lodash from "lodash"
import got from "got"
export async function fetchAndCreateModule(manifest) {
console.log(`Fetching ${manifest}...`)
console.log(`[${manifest.id}] fetchAndCreateModule() | Fetching ${manifest}...`)
try {
const response = await got.get(manifest)
@ -20,36 +17,39 @@ export async function fetchAndCreateModule(manifest) {
}
}
export async function readManifest(manifest, { just_read = false } = {}) {
export async function readManifest(manifest) {
// check if manifest is a directory or a url
const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi
if (urlRegex.test(manifest)) {
const _module = await fetchAndCreateModule(manifest)
const remoteUrl = lodash.clone(manifest)
const target = manifest?.remote_url ?? manifest
manifest = _module.exports
if (urlRegex.test(target)) {
const _module = await fetchAndCreateModule(target)
manifest.remote_url = remoteUrl
if (typeof manifest === "object") {
manifest._original_manifest = {
...manifest,
}
manifest = {
...manifest,
..._module.exports,
}
} else {
if (!fs.existsSync(manifest)) {
throw new Error(`Manifest not found: ${manifest}`)
manifest = _module.exports
}
if (!fs.statSync(manifest).isFile()) {
throw new Error(`Manifest is not a file: ${manifest}`)
manifest.remote_url = target
} else {
if (!fs.existsSync(target)) {
throw new Error(`Manifest not found: ${target}`)
}
const manifestFilePath = lodash.clone(manifest)
manifest = require(manifest)
if (!just_read) {
// copy manifest
fs.copyFileSync(manifestFilePath, path.resolve(MANIFEST_PATH, path.basename(manifest)))
manifest.remote_url = manifestFilePath
if (!fs.statSync(target).isFile()) {
throw new Error(`Manifest is not a file: ${target}`)
}
manifest = require(target)
}
return manifest