UNPKG

@applicaster/zapplicaster-cli

Version:

CLI Tool for the zapp app and Quick Brick project

160 lines (136 loc) 4.43 kB
const R = require("ramda"); const { resolve } = require("path"); const ejs = require("ejs"); const { promisify } = require("util"); const { getFileDestinationPath } = require("../settings/paths"); const { writeFileAsync, writeJsonToFile } = require("../file"); /** * module render * @module @applicaster/zapplicaster-cli/src/render */ const ejsAsync = promisify(ejs.renderFile); /** * injects plugin dependencies in package.json * * @export * @param {Array} dependencies to inject * @param {String} packageJson (optional) full path to package.json * defaults to the package.json file in the template folder * @returns promise with result of writing the package.json */ async function injectDependencies(dependencies, templatePath) { const packageJsonPath = resolve(templatePath, "package.json"); const packageJson = require(packageJsonPath); if (!packageJson.dependencies) { packageJson.dependencies = {}; } if (!packageJson.devDependencies) { packageJson.devDependencies = {}; } dependencies.forEach(({ type, name, version }) => { packageJson[type][name] = version; }); return await writeJsonToFile(packageJsonPath, packageJson); } /** * injects scripts in package.json * * @export * @param {String} packageJson (optional) full path to package.json * defaults to the package.json file in the template folder * @returns promise with result of writing the package.json */ async function injectScripts(scripts, templatePath) { const packageJsonPath = resolve(templatePath, "package.json"); const packageJson = require(packageJsonPath); if (!packageJson.scripts) { packageJson.scripts = {}; } scripts.forEach(({ name, command }) => { packageJson.scripts[name] = command; }); return await writeJsonToFile(packageJsonPath, packageJson); } /** * Renders a template to a specific path * * @param {String} templatePath to render (full path) * @param {String} filePath to save the rendered file (full path) * @param {String} renderData to inject into the template * @returns result of file write operation */ async function renderFile(templatePath, filePath, renderData) { const fileData = await ejsAsync(resolve(templatePath), renderData); return await writeFileAsync(filePath, fileData); } /** * Saves JSON object as a configuration file in the matching /config folder * according to the platform specified in the configuration file. * @param {Object} configuration object from the CLI * @param {String} configuration.destinationPath path of the app * @param {String} configuration.platform platform of the app * @param {String} fileName of the config file to save * @param {Object} jsonObject of the data to save as config */ async function saveConfigFile( { destinationPath, platform }, fileName, jsonObject ) { const path = getFileDestinationPath({ destinationPath, platform, folder: "config", fileName, }); return await writeJsonToFile(path, jsonObject); } /** * renders a config file - curried function of the form renderConfigFile(config)(configFileOption) * @param {Object} config object * @param {String} config.destinationPath path of the workspace * @param {String} config.platform platform of the app * @param {Object} configFileOption * @param {String} configFileOption.name name of the file * @param {Function} configFileOption.getJsonContent function to get the json content */ function renderConfigFile(config) { const { destinationPath, platform } = config; return async function ({ name, getJsonContent }) { const data = getJsonContent(config); const path = getFileDestinationPath({ destinationPath, platform, folder: "config", fileName: name, }); return await writeJsonToFile(path, data); }; } /** */ function renderTemplateFile(configuration, quickBrickProjectPath, plugins) { return async function ({ templatePath, filePath }) { const applyIfNeeded = R.when( (f) => typeof f === "function", R.apply(R.__, [configuration]) ); const fileDestinationPath = applyIfNeeded(filePath); return await renderFile( templatePath, resolve(quickBrickProjectPath, fileDestinationPath), { configuration, plugins, } ); }; } module.exports = { injectDependencies, injectScripts, renderFile, saveConfigFile, renderConfigFile, renderTemplateFile, };