message: Optionally clean up failed plugin directory
diff: @@ -12,7 +12,7 @@ export const createPluginFolder = (rootDir: string, id: string): string => {
  throw new Error(
  `A plugin with the same name already exists: ${chalk.cyan(
  destination.replace(rootDir, ''),
  - )}\nPlease try again with a different plugin Id`,
  + )}\nPlease try again with a different Plugin ID`,
  );
  }
  @@ -90,9 +90,40 @@ export const createFromTemplateDir = async (
  });
  };
  -const createPlugin = async (): Promise<any> => {
  +const cleanUp = async (rootDir: string, id: string) => {
  + const destination = path.join(rootDir, 'packages', 'plugins', id);
  +
  + const questions: Question[] = [
  + {
  + type: 'confirm',
  + name: 'cleanup',
  + message: chalk.yellow(
  + `Do you want to remove the created directory and all the files in it?\ndir: ${chalk.cyan(
  + destination,
  + )}`,
  + ),
  + },
  + ];
  + const answers: Answers = await inquirer.prompt(questions);
  +
  + if (answers.cleanup) {
  try {
  - const currentDir = process.argv[1];
  + // Not using recursion here, so only empty directories can be removed
  + fs.rmdirSync(destination);
  + console.log();
  + console.log(
  + chalk.green(`Removing ${chalk.cyan(destination.replace(rootDir, ''))}`),
  + );
  + console.log();
  + } catch (e) {
  + console.log();
  + console.log(chalk.red(`Failed to cleanup: ${e.message}`));
  + console.log();
  + }
  + }
  +};
  +
  +const createPlugin = async (): Promise<any> => {
  const questions: Question[] = [
  {
  type: 'input',
  @@ -103,10 +134,9 @@ const createPlugin = async (): Promise<any> => {
  },
  ];
  const answers: Answers = await inquirer.prompt(questions);
  - const destinationFolder = createPluginFolder(
  - path.join(currentDir, '..', '..', '..'),
  - answers.id,
  - );
  +
  + const currentDir = process.argv[1];
  + const rootDir = path.join(currentDir, '..', '..', '..');
  const templateFolder = path.join(
  currentDir,
  '..',
  @@ -117,6 +147,8 @@ const createPlugin = async (): Promise<any> => {
  'default-plugin',
  );
  + try {
  + const destinationFolder = createPluginFolder(rootDir, answers.id);
  await createFromTemplateDir(templateFolder, destinationFolder, answers);
  console.log();
  @@ -138,8 +170,10 @@ const createPlugin = async (): Promise<any> => {
  return destinationFolder;
  } catch (e) {
  console.log();
  - console.log(e.message);
  + console.log(chalk.red(e.message));
  console.log();
  +
  + await cleanUp(rootDir, answers.id);
  }
  };
  