2020-03-13 20:22:56 +00:00
|
|
|
import * as plugins from './smartarchive.plugins';
|
2020-03-16 14:38:17 +00:00
|
|
|
import * as paths from './smartarchive.paths';
|
2020-03-13 20:22:56 +00:00
|
|
|
|
|
|
|
export class SmartArchive {
|
|
|
|
public archiveDirectory: string;
|
|
|
|
constructor() {}
|
|
|
|
|
2020-03-16 14:38:17 +00:00
|
|
|
/**
|
|
|
|
* extracts an archive from a given url
|
|
|
|
*/
|
|
|
|
public async extractArchiveFromUrl(urlArg: string, targetDir: string) {
|
|
|
|
const parsedPath = plugins.path.parse(urlArg);
|
|
|
|
const uniqueFileName = plugins.smartunique.uni() + parsedPath.ext;
|
|
|
|
const downloadPath = plugins.path.join(paths.nogitDir, uniqueFileName);
|
2020-03-16 17:04:17 +00:00
|
|
|
const downloadedArchive = (await plugins.smartrequest.getBinary(urlArg)).body;
|
2020-03-16 14:38:17 +00:00
|
|
|
await plugins.smartfile.memory.toFs(downloadedArchive, downloadPath);
|
|
|
|
await this.extractArchiveFromFilePath(downloadPath, targetDir);
|
2020-03-16 17:04:17 +00:00
|
|
|
await plugins.smartfile.fs.remove(downloadPath);
|
2020-03-16 14:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* extracts an archive from a given filePath on disk
|
|
|
|
* @param filePathArg
|
|
|
|
* @param targetDir
|
|
|
|
*/
|
|
|
|
public async extractArchiveFromFilePath(filePathArg: string, targetDir: string) {
|
2020-03-13 20:22:56 +00:00
|
|
|
const parsedPath = plugins.path.parse(filePathArg);
|
|
|
|
switch (parsedPath.ext) {
|
2020-03-16 14:38:17 +00:00
|
|
|
case '.tgz':
|
|
|
|
console.log(`detected a .tgz archive`);
|
|
|
|
await plugins.tar.extract({
|
|
|
|
file: filePathArg,
|
|
|
|
cwd: targetDir
|
|
|
|
});
|
|
|
|
break;
|
2020-03-13 20:22:56 +00:00
|
|
|
}
|
2020-03-16 14:38:17 +00:00
|
|
|
}
|
|
|
|
}
|