bp-cli-testing
Version:
BP CLI
89 lines (76 loc) • 2.41 kB
JavaScript
import arg from "arg";
import inquirer from "inquirer";
import chalk from "chalk";
import { promptForMissingBuildOptions } from "./lib/build";
import { createSandbox, promptSandboxForMissingOptions } from "./lib/sandbox";
import {
BP_DEV_SANDBOX,
BP_KEYWORD_ERROR,
CREATE_KEYWORD_MISSING_ARGUMENT,
} from "./lib/constants";
const log = console.log;
function parseArgumentIntoOptions(rawArgs) {
const args = arg(
{
"--directory": String,
"--name": String,
"--pages": Number,
"--yes": Boolean,
"--git": String,
"-y": "--yes",
"-g": "--git",
"-n": "--name",
"-pgs": "--pages",
"-dir": "--directory",
},
{
argv: rawArgs.slice(2),
}
);
return {
argv: args._,
name: args["--name"],
pages: args["--pages"],
git: args["--git"],
directory: args["--directory"],
skipPrompts: args["--yes"] || false,
};
}
async function promptForMissingOptions(options, questions) {
if (questions.length < 1) {
log(chalk.white("Options set. \n"));
return options;
}
const answers = await inquirer.prompt(questions);
return {
...options,
name: options.name || answers.name,
git: options.git || answers.git,
pages: options.pages || answers.pages,
directory: options.directory || answers.directory,
};
}
export async function cli(args) {
let questions;
let options = parseArgumentIntoOptions(args);
if (options.argv[0] === "create" && options.argv[1] === "sandbox") {
log(chalk.bgGreen.white.bold("\n Create Sandbox \n"));
questions = promptSandboxForMissingOptions(options);
options = await promptForMissingOptions(options, questions);
log(chalk.green("\n Building sandbox. \n"));
createSandbox(options);
log(options);
}
if (options.argv[0] === "create" && options.argv[1] === "build") {
log(chalk.bgGreen.white.bold("\n Create Build \n"));
options = parseArgumentIntoOptions(args);
questions = promptForMissingBuildOptions(options);
options = await promptForMissingOptions(options, questions);
log(options);
}
if (options.argv[0] === "help")
log(chalk.white("bp [argument] [argument]: Example: bp create build"));
if (options.argv[0] === "create" && options.argv[1] === undefined)
log(chalk.red(CREATE_KEYWORD_MISSING_ARGUMENT));
if (options.argv[0] === undefined) log(chalk.red(BP_KEYWORD_ERROR));
}