@applicaster/zapplicaster-cli
Version:
CLI Tool for the zapp app and Quick Brick project
94 lines (82 loc) • 2.48 kB
JavaScript
const { exec, mv, rm } = require("shelljs");
const R = require("ramda");
const logger = require("../logger");
const getFileName = R.compose(R.last, R.split("/"));
/**
* runs a command in the shell. Will throw if exit code > 0
* @param {String} cmd command to run
*/
function runInShell(cmd) {
const { code, stdErr } = exec(cmd);
if (code > 0) {
throw new Error(`Shell execution failed for ${cmd} - ${stdErr}`);
}
}
/**
* runs a command in the shell and returns a promise which resolves with stout if
* exit code > 0 or rejects with stderr otherwise
* @param {String} cmd command to run
* @param {Object} options to pass to the shell command
* @returns {Promise<String>}
*/
function runInShellAsync(cmd, options) {
return new Promise((resolve, reject) => {
exec(cmd, options, (code, stdout, stderr) => {
if (code === 0) {
resolve(stdout.replace(/[\r\n]/, ""));
} else {
reject(stderr);
}
});
});
}
/**
* download a single file using cURL, and put it in the destination folder
* @param {String} url of the file to download
* @param {String} destination folder to put the files into
*/
function curlFile(url, destination) {
logger.log(`downloading ${url} with cURL`);
const fileName = getFileName(url);
runInShell(`curl -O ${url}`);
mv(fileName, destination);
}
/**
* downloads multiple files from a given remote folder into the destination folder
* @param {String} remoteFolder url of the remote folder
* @param {[String]} files array of file names to download
* @param {String} destination folder to put files into
*/
function curlMultipleFiles(remoteFolder, files, destination) {
const urls = R.compose(
R.concat(R.__, "}"),
R.concat(remoteFolder),
R.concat("/{"),
R.join(",")
)(files);
logger.log(`downloading ${urls} with cURL`);
runInShell(`curl -O --remote-name-all ${urls}`);
R.forEach((file) => mv(file, destination), files);
}
/**
* unzips a file in the destination path, and removes the zip
* @param {String} file to unzip
* @param {String} destinationPath to unpack the files
*/
function unzip(file, destinationPath) {
try {
logger.log(`unpacking ${file}`);
runInShell(`unzip -o ${file} -d ${destinationPath}`);
rm(file);
} catch (e) {
logger.warn(
"An error occured while unzipping assets - you may have to do this manually"
);
}
}
module.exports = {
curlFile,
curlMultipleFiles,
unzip,
runInShellAsync,
};