UNPKG

bp-cli-testing

Version:

BP CLI

99 lines (86 loc) 2.25 kB
"use strict"; import shell from "shelljs"; import chalk from "chalk"; import { GATSBY_THEME_PACKAGE_NAME, GATSBY_THEME_BACKPOCKET_GIT_URL, GATSBY_THEME_BACKPOCKET, BACKPOCKET_GIT_URL, BACKPOCKET, BP_DEV_SANDBOX, } from "./constants"; import { SANDBOX_MAIN_PACKAGE_JSON, SANDBOX_GATSBY_THEME_BACKPOCKET_PACKAGE_JSON, } from "./packageDotJSON"; import Listr from "listr"; import execa from "execa"; const log = console.log; function findDuplicateDirectories(options) { let dupFound = false; shell.ls().forEach((file) => { if (file === options.name) dupFound = true; }); return dupFound; } function createMainDirectory(options) { shell.mkdir(options.name); shell.cd(options.name); shell.touch("package.json"); shell.exec( `echo '${JSON.stringify(SANDBOX_MAIN_PACKAGE_JSON)}' >> package.json` ); } function getGatsbyTheme() { log(chalk.green("\n Setting up Gatsby core theme.\n")); shell.exec( `git clone ${GATSBY_THEME_BACKPOCKET_GIT_URL} ${GATSBY_THEME_BACKPOCKET}` ); } function getClient() { log(chalk.green("\n Setting up client directory. \n")); shell.exec(`git clone ${BACKPOCKET_GIT_URL} ${BACKPOCKET}`); } function setupGatsbyThemeConfig() { log(chalk.green("\n Configuring client for sandbox environment. \n")); shell.cd(GATSBY_THEME_BACKPOCKET); shell.sed( "-i", `"name": ${GATSBY_THEME_PACKAGE_NAME}`, `"name": ${GATSBY_THEME_BACKPOCKET}`, "package.json" ); } export async function createSandbox(options) { const duplicateDir = findDuplicateDirectories(options); if (duplicateDir) return log( chalk.red( `\n Can't perform action: Found existing project directory '${options.name}'. \n` ) ); createMainDirectory(options); getGatsbyTheme(); getClient(); setupGatsbyThemeConfig(); } export function promptSandboxForMissingOptions(options) { const questions = []; if (!options.name) { questions.push({ type: "input", message: "Dev sandbox name?", name: "name", default: BP_DEV_SANDBOX, }); } if (!options.directory) { questions.push({ type: "input", message: "Sandbox directory?", name: "directory", default: "./", }); } return questions; }