Skip to content

Commit 0c0a638

Browse files
authored
Prevent graphql and python combination (#10)
1 parent 1684719 commit 0c0a638

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

cli/index.ts

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ const OPTIONS: OptionConfigs = {
7979
},
8080
};
8181

82+
const OPTION_COMBINATION_DENY_LIST = [
83+
[
84+
{ optionType: "backend", value: "python" },
85+
{ optionType: "api", value: "graphql" },
86+
],
87+
];
88+
89+
const validateCommandLineOptions = (
90+
commandLineOptions: CommandLineOptions,
91+
): void => {
92+
OPTION_COMBINATION_DENY_LIST.forEach((combination) => {
93+
if (
94+
combination.every(
95+
(option) =>
96+
commandLineOptions[option.optionType as keyof CommandLineOptions] ===
97+
option.value,
98+
)
99+
) {
100+
const formattedCombination = combination.map((c) => c.value).join(", ");
101+
// TODO: custom error type would be a nice-to-have
102+
throw new Error(
103+
`Sorry, we currently do not support the following combination: ${formattedCombination}`,
104+
);
105+
}
106+
});
107+
};
108+
82109
const parseArguments = (args: CommandLineArgs): CommandLineOptions => {
83110
const { argv } = yargs(args.slice(2)).options({
84111
backend: {
@@ -122,22 +149,34 @@ const parseArguments = (args: CommandLineArgs): CommandLineOptions => {
122149
const promptOptions = async (
123150
options: CommandLineOptions,
124151
): Promise<UserResponse> => {
125-
const prompts = [];
152+
let prompts = [];
153+
const tsChoice = OPTIONS.backend.choices.find(
154+
(choice) => choice.value === "typescript",
155+
);
156+
126157
if (!options.backend) {
127158
prompts.push({
128159
type: "list",
129160
name: "backend",
130161
message: OPTIONS.backend.message,
131-
choices: OPTIONS.backend.choices,
162+
choices: options.api === "graphql" ? [tsChoice] : OPTIONS.backend.choices,
132163
});
133164
}
134165

166+
let answers = await inquirer.prompt(prompts);
167+
prompts = [];
168+
169+
const backend = options.backend || answers.backend;
170+
const restChoice = OPTIONS.api.choices.find(
171+
(choice) => choice.value === "rest",
172+
);
173+
135174
if (!options.api) {
136175
prompts.push({
137176
type: "list",
138177
name: "api",
139178
message: OPTIONS.api.message,
140-
choices: OPTIONS.api.choices,
179+
choices: backend === "python" ? [restChoice] : OPTIONS.api.choices,
141180
});
142181
}
143182

@@ -168,11 +207,11 @@ const promptOptions = async (
168207
});
169208
}
170209

171-
const answers = await inquirer.prompt(prompts);
210+
answers = await inquirer.prompt(prompts);
172211

173212
return {
174213
appOptions: {
175-
backend: options.backend || answers.backend,
214+
backend,
176215
api: options.api || answers.api,
177216
database: options.database || answers.database,
178217
auth: (options.auth || answers.auth ? "auth" : null) as AuthType,
@@ -210,7 +249,7 @@ const confirmPrompt = async (options: Options) => {
210249
return confirm;
211250
};
212251

213-
async function cli(args: CommandLineArgs): Promise<Options | null> {
252+
async function cli(args: CommandLineArgs): Promise<Options> {
214253
console.log(
215254
boxen(
216255
chalk.bold(
@@ -227,13 +266,16 @@ async function cli(args: CommandLineArgs): Promise<Options | null> {
227266

228267
const commandLineOptions: CommandLineOptions = parseArguments(args);
229268

269+
validateCommandLineOptions(commandLineOptions);
270+
230271
const { appOptions, outputDir } = await promptOptions(commandLineOptions);
231272

232273
const confirm = await confirmPrompt(appOptions);
233274

234275
if (!confirm) {
235-
console.log(chalk.red.bold("Blueprint app creation has been cancelled."));
236-
return null;
276+
return Promise.reject(
277+
new Error("Blueprint app creation has been cancelled."),
278+
);
237279
}
238280

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

244286
if (changeDirectory.code !== 0) {
245-
console.log("No directory exists. Exiting...");
246-
return null;
287+
return Promise.reject(new Error("No directory exists. Exiting..."));
247288
}
248289

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

253294
if (clone.code !== 0) {
254-
console.log("Git clone failed. Exiting...");
295+
return Promise.reject(new Error("Git clone failed. Exiting..."));
255296
}
256297

257298
return appOptions;

index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
import chalk from "chalk";
23
import cli from "./cli";
34
import { Options } from "./cli/optionTypes";
45

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

1819
async function run() {
1920
const rootDir = __dirname;
20-
const options = await cli(process.argv);
21-
if (options) {
21+
try {
22+
const options = await cli(process.argv);
2223
await scrub(rootDir, options);
24+
} catch (err) {
25+
console.log(chalk.red.bold(err.message));
2326
}
2427
}
2528

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"license": "MIT",
1010
"scripts": {
1111
"dev": "ts-node index.ts",
12-
"lint": "eslint . --ext .ts,.js",
13-
"lint-fix": "eslint . --ext .ts,.js --fix",
12+
"lint": "eslint . --ext .ts",
13+
"lint-fix": "eslint . --ext .ts --fix",
1414
"prod": "tsc -p . && node bin/index.js"
1515
},
1616
"devDependencies": {

0 commit comments

Comments
 (0)