|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const path = require('path'); |
| 6 | +const inquirer = require('inquirer'); |
| 7 | +const program = require('commander'); |
| 8 | +const {scaffold} = require('egad'); |
| 9 | +const kebabCase = require('lodash.kebabcase'); |
| 10 | +const chalk = require('chalk'); |
| 11 | +const stringifyAuthor = require('stringify-author'); |
| 12 | +const {guessEmail, guessAuthor, guessGitHubUsername} = require('conjecture'); |
| 13 | + |
| 14 | +const PLUGIN_REPO_URL = 'https://github.com/probot/plugin-template.git'; |
| 15 | + |
| 16 | +program |
| 17 | + .usage('[options] [destination]') |
| 18 | + .option('-p, --pkgname <package-name>', 'Plugin package name') |
| 19 | + .option('-d, --desc "<description>"', |
| 20 | + 'Plugin description (contain in quotes)') |
| 21 | + .option('-a, --author "<full-name>"', |
| 22 | + 'Plugin author name (contain in quotes)') |
| 23 | + .option('-e, --email <email>', 'Plugin author email address') |
| 24 | + .option('-h, --homepage <homepage>', 'Plugin author\'s homepage') |
| 25 | + .option('-u, --user <username>', 'GitHub username or org (repo owner)') |
| 26 | + .option('-r, --repo <repo-name>', 'Plugin repo name') |
| 27 | + .option('--overwrite', 'Overwrite existing files', false) |
| 28 | + .option('--template <template-url>', 'URL of custom plugin template', |
| 29 | + PLUGIN_REPO_URL) |
| 30 | + .parse(process.argv); |
| 31 | + |
| 32 | +const destination = program.args.length ? |
| 33 | + path.resolve(process.cwd(), program.args.shift()) : |
| 34 | + process.cwd(); |
| 35 | + |
| 36 | +const prompts = [ |
| 37 | + { |
| 38 | + type: 'input', |
| 39 | + name: 'name', |
| 40 | + default(answers) { |
| 41 | + return answers.repo || kebabCase(path.basename(destination)); |
| 42 | + }, |
| 43 | + message: 'Plugin\'s package name:', |
| 44 | + when: !program.pkgname |
| 45 | + }, |
| 46 | + { |
| 47 | + type: 'input', |
| 48 | + name: 'desc', |
| 49 | + default() { |
| 50 | + return 'A Probot plugin'; |
| 51 | + }, |
| 52 | + message: 'Description of plugin:', |
| 53 | + when: !program.desc |
| 54 | + }, |
| 55 | + { |
| 56 | + type: 'input', |
| 57 | + name: 'author', |
| 58 | + default() { |
| 59 | + return guessAuthor(); |
| 60 | + }, |
| 61 | + message: 'Plugin author\'s full name:', |
| 62 | + when: !program.author |
| 63 | + }, |
| 64 | + { |
| 65 | + type: 'input', |
| 66 | + name: 'email', |
| 67 | + default() { |
| 68 | + return guessEmail(); |
| 69 | + }, |
| 70 | + message: 'Plugin author\'s email address:', |
| 71 | + when: !program.email |
| 72 | + }, |
| 73 | + { |
| 74 | + type: 'input', |
| 75 | + name: 'homepage', |
| 76 | + message: 'Plugin author\'s homepage:', |
| 77 | + when: !program.homepage |
| 78 | + }, |
| 79 | + { |
| 80 | + type: 'input', |
| 81 | + name: 'owner', |
| 82 | + default(answers) { |
| 83 | + return guessGitHubUsername(answers.email); |
| 84 | + }, |
| 85 | + message: 'Plugin\'s GitHub user or org name:', |
| 86 | + when: !program.user |
| 87 | + }, |
| 88 | + { |
| 89 | + type: 'input', |
| 90 | + name: 'repo', |
| 91 | + default(answers) { |
| 92 | + return answers.pkgname || kebabCase(path.basename(destination)); |
| 93 | + }, |
| 94 | + message: 'Plugin\'s repo name:', |
| 95 | + when: !program.repo |
| 96 | + } |
| 97 | +]; |
| 98 | + |
| 99 | +console.log(chalk.blue('Let\'s create a Probot plugin!')); |
| 100 | + |
| 101 | +inquirer.prompt(prompts) |
| 102 | + .then(answers => { |
| 103 | + answers.author = stringifyAuthor({ |
| 104 | + name: answers.author, |
| 105 | + email: answers.email, |
| 106 | + url: answers.homepage |
| 107 | + }); |
| 108 | + return scaffold(program.template, destination, answers, { |
| 109 | + overwrite: Boolean(program.overwrite) |
| 110 | + }); |
| 111 | + }) |
| 112 | + .then(results => { |
| 113 | + results.forEach(fileinfo => { |
| 114 | + console.log(`${fileinfo.skipped ? chalk.yellow('skipped existing file') : |
| 115 | + chalk.green('created file')}: ${fileinfo.path}`); |
| 116 | + }); |
| 117 | + console.log(chalk.blue('Done!')); |
| 118 | + }); |
0 commit comments