UNPKG

@applicaster/zapplicaster-cli

Version:

CLI Tool for the zapp app and Quick Brick project

108 lines (86 loc) 2.88 kB
const R = require("ramda"); const { getFileDestinationPath, quickBrickDirectory, } = require("../../settings/paths"); const { resolve } = require("path"); const { mkdirSync, existsSync } = require("fs"); const { exec } = require("shelljs"); const camelize = require("camelize"); const logger = require("../../logger"); /** * gets the parent directory of a file or directory * @param {String} directory or file to get the parent of * @returns {String} parent directory */ const getParentDirectory = R.compose(R.join("/"), R.init, R.split("/")); /** * Creates a directory if it doesn't exists. If the directory doesn't exist, calls this * function recursively on the directory's parent folder. * acts like mkdir -p, which doesn't have a built-in implementation in node's fs API * @param {String} dir: path of the directory to be created * @returns {Object} response from fs.mkdir */ function createDirUnlessItExists(dir) { if (!existsSync(dir)) { createDirUnlessItExists(getParentDirectory(dir)); return mkdirSync(dir); } } const getWorkspaceName = R.compose(camelize, R.replace(/ /g, "_")); /** * Creates a new workspace to prepare the app into. This workspace * can be used to start working on plugins or other components on a specific zapp app * @param {configuration} configuration object for the CLI command * @returns {string} newWorkspacePath */ async function createNewWorkspace(configuration) { const { destinationPath, newWorkspace, buildParams: { app_name }, } = configuration; // get workspace name const workspaceName = typeof newWorkspace === "boolean" ? getWorkspaceName(app_name) : newWorkspace; const workspacePath = resolve( destinationPath || process.cwd(), workspaceName ); logger.log(`\u{1f4e6} Creating new workspace at ${workspacePath}`); createDirUnlessItExists(workspacePath); // initialize yarn workspace // initialize git repo exec("git init", { cwd: workspacePath }); logger.log( "Git repository created. run `git remote set origin <url>` to set the remote url" ); // return path return workspacePath; } /** * Creates the necessary folders for the workspace * @param {configuration} configuration object for the CLI command * @returns {void} */ async function workspaceCreator(configuration) { const { destinationPath, platform, newWorkspace } = configuration; const quickBrickProjectPath = quickBrickDirectory( newWorkspace ? await createNewWorkspace(configuration) : destinationPath ); const folders = ["config", "fonts", "assets"]; R.forEach(createDirUnlessItExists, [ quickBrickProjectPath, ...R.map( R.compose( getFileDestinationPath, R.assoc("folder", R.__, { destinationPath, platform }) ), folders ), ]); return true; } module.exports = { workspaceCreator };