-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from nomo-app/extractAndCheckFiles
Extract and check files(validate manifest)
- Loading branch information
Showing
5 changed files
with
147 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { logFatal } from "../util/util"; | ||
import * as tar from "tar"; | ||
import { resolve, join } from "path"; | ||
import { existsSync, mkdirSync } from "fs"; | ||
|
||
const requiredFiles = ["index.html", "nomo_icon.svg", "nomo_manifest.json"]; | ||
const cacheDirectory = "./cache"; | ||
const cacheOutDirectory = "./cache/out"; | ||
|
||
export async function extractAndCache(args: { | ||
tarFilePath: string; | ||
destinationDir?: string; | ||
}) { | ||
const { tarFilePath, destinationDir = cacheDirectory } = args; | ||
|
||
if (!existsSync(destinationDir)) { | ||
console.log(`Creating cache directory: ${destinationDir}`); | ||
mkdirSync(destinationDir); | ||
} | ||
|
||
try { | ||
await tar.x({ | ||
file: tarFilePath, | ||
cwd: resolve(destinationDir), | ||
}); | ||
|
||
/* Maybe possible to fetch fileNames as stream before extracting: | ||
const getEntryFilenamesSync = (tarFilePath as any) => { | ||
const filenames = requiredFiles; | ||
tar.t({ | ||
file: tarFilePath, | ||
onentry: (entry) => filenames.push(entry.path), | ||
sync: true, | ||
}); | ||
return filenames | ||
};*/ | ||
|
||
const missingFiles = requiredFiles.filter((file) => { | ||
const filePath = join(resolve(destinationDir), "/out/", file); | ||
return !existsSync(filePath); | ||
}); | ||
if (missingFiles.length > 0) { | ||
logFatal( | ||
`Error: The following required files are missing: ${missingFiles.join( | ||
", " | ||
)}` | ||
); | ||
} | ||
console.log(`Tar.gz file extracted successfully to: ${destinationDir}`); | ||
} catch (error) { | ||
logFatal(`Error extracting tar.gz file: ${error}`); | ||
} | ||
} | ||
|
||
export function getCachedIndexHtmlPath(): string { | ||
const path = join(resolve(cacheOutDirectory), "index.html"); | ||
if (!existsSync(path)) { | ||
logFatal(`Error: ${path} is missing.`); | ||
} | ||
return path; | ||
} | ||
|
||
export function getCachedNomoIconPath(): string { | ||
const path = join(resolve(cacheOutDirectory), "nomo_icon.svg"); | ||
if (!existsSync(path)) { | ||
logFatal(`Error: ${path} is missing.`); | ||
} | ||
return path; | ||
} | ||
|
||
export function getCachedNomoManifestPath(): string { | ||
const path = join(resolve(cacheOutDirectory), "nomo_manifest.json"); | ||
if (!existsSync(path)) { | ||
logFatal(`Error: ${path} is missing.`); | ||
} | ||
return path; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters