From 2d2710a7c486ab2dc5c005695df1089d5d1d03a5 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 21 May 2020 13:51:08 +0200 Subject: [PATCH] packages/cli: move plugin diff prompts to separate file --- .../cli/src/commands/plugin/diff/index.ts | 44 +++------------ .../cli/src/commands/plugin/diff/prompts.ts | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+), 38 deletions(-) create mode 100644 packages/cli/src/commands/plugin/diff/prompts.ts diff --git a/packages/cli/src/commands/plugin/diff/index.ts b/packages/cli/src/commands/plugin/diff/index.ts index 5338594a3b..6300ff64d8 100644 --- a/packages/cli/src/commands/plugin/diff/index.ts +++ b/packages/cli/src/commands/plugin/diff/index.ts @@ -14,12 +14,14 @@ * limitations under the License. */ -import chalk from 'chalk'; -import inquirer from 'inquirer'; import { Command } from 'commander'; import { readTemplateFiles } from './read'; import { handlers, handleAllFiles } from './handlers'; -import { PromptFunc } from './types'; +import { + inquirerPromptFunc, + makeCheckPromptFunc, + yesPromptFunc, +} from './prompts'; const fileHandlers = [ { @@ -41,46 +43,12 @@ const fileHandlers = [ }, ]; -const inquirerPromptFunc: PromptFunc = async (msg) => { - const { result } = await inquirer.prompt({ - type: 'confirm', - name: 'result', - message: chalk.blue(msg), - }); - return result; -}; - -const makeCheck = () => { - let failed = false; - - const promptFunc: PromptFunc = async (msg) => { - failed = true; - console.log(chalk.red(`[Check Failed] ${msg}`)); - return false; - }; - - const finalize = () => { - if (failed) { - throw new Error( - 'Check failed, the plugin is not in sync with the latest template', - ); - } - }; - - return [promptFunc, finalize] as const; -}; - -const yesPromptFunc: PromptFunc = async (msg) => { - console.log(`Accepting: "${msg}"`); - return true; -}; - export default async (cmd: Command) => { let promptFunc = inquirerPromptFunc; let finalize = () => {}; if (cmd.check) { - [promptFunc, finalize] = makeCheck(); + [promptFunc, finalize] = makeCheckPromptFunc(); } else if (cmd.yes) { promptFunc = yesPromptFunc; } diff --git a/packages/cli/src/commands/plugin/diff/prompts.ts b/packages/cli/src/commands/plugin/diff/prompts.ts new file mode 100644 index 0000000000..ac04fc3559 --- /dev/null +++ b/packages/cli/src/commands/plugin/diff/prompts.ts @@ -0,0 +1,53 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import chalk from 'chalk'; +import inquirer from 'inquirer'; +import { PromptFunc } from './types'; + +export const inquirerPromptFunc: PromptFunc = async msg => { + const { result } = await inquirer.prompt({ + type: 'confirm', + name: 'result', + message: chalk.blue(msg), + }); + return result; +}; + +export const makeCheckPromptFunc = () => { + let failed = false; + + const promptFunc: PromptFunc = async msg => { + failed = true; + console.log(chalk.red(`[Check Failed] ${msg}`)); + return false; + }; + + const finalize = () => { + if (failed) { + throw new Error( + 'Check failed, the plugin is not in sync with the latest template', + ); + } + }; + + return [promptFunc, finalize] as const; +}; + +export const yesPromptFunc: PromptFunc = async msg => { + console.log(`Accepting: "${msg}"`); + return true; +};