packages/cli: move plugin diff prompts to separate file

This commit is contained in:
Patrik Oldsberg
2020-05-21 13:51:08 +02:00
parent b22a45d60c
commit 2d2710a7c4
2 changed files with 59 additions and 38 deletions
+6 -38
View File
@@ -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;
}
@@ -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;
};