UNPKG

@applicaster/zapplicaster-cli

Version:

CLI Tool for the zapp app and Quick Brick project

102 lines (87 loc) 3 kB
const R = require("ramda"); const axios = require("axios"); const { resolve } = require("path"); const { curlFile, curlMultipleFiles, unzip } = require("../shell"); const { getFileDestinationPath } = require("../settings/paths"); const { writeJsonToFile } = require("../file"); const logger = require("../logger"); const getFileName = R.ifElse( R.is(String), R.compose(R.last, R.split("/")), R.prop("name") ); /** * saves a remote json file in the app's configuration folder. * curried function of the shape saveRemoteJsonToFile(configuration)(fileUrl) * @param {configuration} configuration object from the CLI * @param {String} configuration.destinationPath of the QuickBrick template * @param {String} configuration.platform of the QuickBrick app * @param {String} fileUrl url of the json to save in the app's config folder * @returns {Promise<Object>} status of the file write operation */ function saveRemoteJsonToFile({ destinationPath, platform }) { return async function ([fileName, fileUrl]) { try { logger.log(`saving ${fileName} from ${fileUrl}`); const { data: fileContent } = await axios.get(fileUrl); const filePath = getFileDestinationPath({ destinationPath, platform, folder: "config", fileName, }); const result = await writeJsonToFile(filePath, fileContent); return result; } catch (e) { throw new Error( `An error occurred while trying to retrieve ${fileName}: ${e.message}` ); } }; } /** * downloads assets zip file into the appropriate destination folder, and unpacks them * @param {Object} configuration object from the cli * @param {String} configuration.destinationPath of the QuickBrick template * @param {String} configuration.platform of the QuickBrick app * @param {String} assetsUrl url of the assets zip */ function saveAssets({ destinationPath, platform }, assetsUrl) { logger.log(`saving assets from ${assetsUrl}`); const fileName = getFileName(assetsUrl); const filePath = getFileDestinationPath({ destinationPath, platform, folder: "assets", }); curlFile(assetsUrl, filePath); unzip(resolve(filePath, fileName), filePath); } /** * downloads fonts into the appropriate destination folder * @param {Object} configuration object from the cli * @param {String} configuration.destinationPath of the QuickBrick template * @param {String} configuration.platform of the QuickBrick app * @param {String} fontUrlPrefix url of the fonts folder on s3 * @param {[String]} fontFiles array of font files to download from that folder */ function saveFontFiles( { destinationPath, platform }, fontUrlPrefix, fontFiles ) { if (R.isEmpty(fontFiles)) { return; } const filePath = getFileDestinationPath({ destinationPath, platform, folder: "fonts", }); curlMultipleFiles(fontUrlPrefix, fontFiles, filePath); } module.exports = { saveRemoteJsonToFile, saveAssets, saveFontFiles, };