appcenter-cli
Version:
Command line tool for Visual Studio App Center
74 lines (73 loc) • 3.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addFolderToZipRecursively = exports.writeZipToPath = exports.unpackZipToPath = void 0;
const Path = require("path");
const Pfs = require("./promisfied-fs");
const fs = require("fs");
const mkdirp = require("mkdirp");
/**
* Unpacks ZIP file contents to the specified folder (it should already exist)
* root parameter can be used to extract specific folder from the zip archive
*/
function unpackZipToPath(path, zip, root = "") {
return __awaiter(this, void 0, void 0, function* () {
const entries = zip.filter((relativePath, file) => file.name.startsWith(root));
for (const entry of entries) {
const zipPath = entry.name.substring(root.length);
if (entry.dir) {
yield mkdirp(Path.join(path, zipPath));
}
else {
const fileDirPath = Path.join(path, Path.dirname(zipPath));
// Creating directory path if needed
yield mkdirp(fileDirPath);
const buffer = yield entry.async("nodebuffer");
yield Pfs.writeFile(Path.join(fileDirPath, Path.basename(zipPath)), buffer);
}
}
});
}
exports.unpackZipToPath = unpackZipToPath;
/**
* Writes zip file to the specified location
*/
function writeZipToPath(path, zip, compression) {
return __awaiter(this, void 0, void 0, function* () {
const zipBuffer = yield zip.generateAsync({
type: "nodebuffer",
compression,
});
yield Pfs.writeFile(path, zipBuffer);
});
}
exports.writeZipToPath = writeZipToPath;
/**
* Adds the folder and it's content to the zip
*/
function addFolderToZipRecursively(path, zip) {
return __awaiter(this, void 0, void 0, function* () {
const subEntitiesNames = yield Pfs.readdir(path);
const folderZip = zip.folder(Path.basename(path));
for (const subEntityName of subEntitiesNames) {
const subEntityPath = Path.join(path, subEntityName);
const subEntityStats = yield Pfs.stat(subEntityPath);
if (subEntityStats.isDirectory()) {
yield addFolderToZipRecursively(subEntityPath, folderZip);
}
else {
const fileStream = yield fs.createReadStream(subEntityPath);
folderZip.file(subEntityName, fileStream);
}
}
});
}
exports.addFolderToZipRecursively = addFolderToZipRecursively;