ask-cli-x
Version:
Alexa Skills Kit (ASK) Command Line Interfaces
81 lines (80 loc) • 2.59 kB
JavaScript
;
const fs = require("fs");
const tmp = require("tmp");
const path = require("path");
const archiver = require("archiver");
const AdmZip = require("adm-zip");
const httpClient = require("../clients/http-client");
const Messenger = require("../view/messenger");
const CONSTANTS = require("./constants");
module.exports = {
createTempZip,
unzipRemoteZipFile,
};
/**
* function used to create a temporary zip file
* @param {string} src The file path of resource want to zip
* @param {callback} callback { error, filePath }
*/
function createTempZip(src, outputDir, callback) {
if (!src) {
return callback("Zip file path must be set.");
}
if (!outputDir) {
return callback("Zip file output path must be set.");
}
fs.access(src, (fs.constants || fs).W_OK, (err) => {
if (err) {
return callback(`File access error. ${err}`);
}
if (path.extname(src) === ".zip" || path.extname(src) === ".jar") {
Messenger.getInstance().debug(`The source file ${src} has already been compressed. Skip the zipping`);
return callback(null, src);
}
// Create the temp zip at the same level of the source file
const zipFilePath = tmp.tmpNameSync({
prefix: "askcli_temp_",
postfix: ".zip",
dir: outputDir,
});
const writeStream = fs.createWriteStream(zipFilePath);
const archive = archiver("zip");
const stats = fs.statSync(src);
archive.on("error", (archiveErr) => {
callback(`Archive error. ${archiveErr}`);
});
archive.pipe(writeStream);
if (stats.isFile()) {
archive.file(src, { name: path.basename(src) });
}
else if (stats.isDirectory()) {
archive.glob("**/*", {
cwd: src,
ignore: path.basename(zipFilePath),
}, {});
}
archive.finalize();
writeStream.on("close", () => {
callback(null, zipFilePath);
});
});
}
function unzipRemoteZipFile(url, targetPath, doDebug, callback) {
httpClient.request({
url,
method: CONSTANTS.HTTP_REQUEST.VERB.GET,
encoding: null,
}, "get-zip-file", doDebug, (err, response) => {
if (err) {
return callback(err);
}
const zip = new AdmZip(response.body);
try {
zip.extractAllTo(targetPath, false);
}
catch (unzipErr) {
return callback(unzipErr);
}
callback();
});
}