Skip to content

Commit

Permalink
Prevent graphql and python combination (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexguo8 authored Apr 23, 2021
1 parent 1684719 commit 0c0a638
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
63 changes: 52 additions & 11 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ const OPTIONS: OptionConfigs = {
},
};

const OPTION_COMBINATION_DENY_LIST = [
[
{ optionType: "backend", value: "python" },
{ optionType: "api", value: "graphql" },
],
];

const validateCommandLineOptions = (
commandLineOptions: CommandLineOptions,
): void => {
OPTION_COMBINATION_DENY_LIST.forEach((combination) => {
if (
combination.every(
(option) =>
commandLineOptions[option.optionType as keyof CommandLineOptions] ===
option.value,
)
) {
const formattedCombination = combination.map((c) => c.value).join(", ");
// TODO: custom error type would be a nice-to-have
throw new Error(
`Sorry, we currently do not support the following combination: ${formattedCombination}`,
);
}
});
};

const parseArguments = (args: CommandLineArgs): CommandLineOptions => {
const { argv } = yargs(args.slice(2)).options({
backend: {
Expand Down Expand Up @@ -122,22 +149,34 @@ const parseArguments = (args: CommandLineArgs): CommandLineOptions => {
const promptOptions = async (
options: CommandLineOptions,
): Promise<UserResponse> => {
const prompts = [];
let prompts = [];
const tsChoice = OPTIONS.backend.choices.find(
(choice) => choice.value === "typescript",
);

if (!options.backend) {
prompts.push({
type: "list",
name: "backend",
message: OPTIONS.backend.message,
choices: OPTIONS.backend.choices,
choices: options.api === "graphql" ? [tsChoice] : OPTIONS.backend.choices,
});
}

let answers = await inquirer.prompt(prompts);
prompts = [];

const backend = options.backend || answers.backend;
const restChoice = OPTIONS.api.choices.find(
(choice) => choice.value === "rest",
);

if (!options.api) {
prompts.push({
type: "list",
name: "api",
message: OPTIONS.api.message,
choices: OPTIONS.api.choices,
choices: backend === "python" ? [restChoice] : OPTIONS.api.choices,
});
}

Expand Down Expand Up @@ -168,11 +207,11 @@ const promptOptions = async (
});
}

const answers = await inquirer.prompt(prompts);
answers = await inquirer.prompt(prompts);

return {
appOptions: {
backend: options.backend || answers.backend,
backend,
api: options.api || answers.api,
database: options.database || answers.database,
auth: (options.auth || answers.auth ? "auth" : null) as AuthType,
Expand Down Expand Up @@ -210,7 +249,7 @@ const confirmPrompt = async (options: Options) => {
return confirm;
};

async function cli(args: CommandLineArgs): Promise<Options | null> {
async function cli(args: CommandLineArgs): Promise<Options> {
console.log(
boxen(
chalk.bold(
Expand All @@ -227,13 +266,16 @@ async function cli(args: CommandLineArgs): Promise<Options | null> {

const commandLineOptions: CommandLineOptions = parseArguments(args);

validateCommandLineOptions(commandLineOptions);

const { appOptions, outputDir } = await promptOptions(commandLineOptions);

const confirm = await confirmPrompt(appOptions);

if (!confirm) {
console.log(chalk.red.bold("Blueprint app creation has been cancelled."));
return null;
return Promise.reject(
new Error("Blueprint app creation has been cancelled."),
);
}

console.log(chalk.green.bold("Confirmed. Creating blueprint app..."));
Expand All @@ -242,16 +284,15 @@ async function cli(args: CommandLineArgs): Promise<Options | null> {
const changeDirectory = shell.cd(path);

if (changeDirectory.code !== 0) {
console.log("No directory exists. Exiting...");
return null;
return Promise.reject(new Error("No directory exists. Exiting..."));
}

const clone = shell.exec(
"git clone https://github.com/uwblueprint/starter-code-v2.git",
);

if (clone.code !== 0) {
console.log("Git clone failed. Exiting...");
return Promise.reject(new Error("Git clone failed. Exiting..."));
}

return appOptions;
Expand Down
7 changes: 5 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
import chalk from "chalk";
import cli from "./cli";
import { Options } from "./cli/optionTypes";

Expand All @@ -17,9 +18,11 @@ async function scrub(rootDir: string, options: Options) {

async function run() {
const rootDir = __dirname;
const options = await cli(process.argv);
if (options) {
try {
const options = await cli(process.argv);
await scrub(rootDir, options);
} catch (err) {
console.log(chalk.red.bold(err.message));
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"license": "MIT",
"scripts": {
"dev": "ts-node index.ts",
"lint": "eslint . --ext .ts,.js",
"lint-fix": "eslint . --ext .ts,.js --fix",
"lint": "eslint . --ext .ts",
"lint-fix": "eslint . --ext .ts --fix",
"prod": "tsc -p . && node bin/index.js"
},
"devDependencies": {
Expand Down

0 comments on commit 0c0a638

Please sign in to comment.