diff --git a/.gitignore b/.gitignore index 01b1635a96..2915e7bdef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,15 @@ +# Dependencies node_modules + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* +debug.log + +# Lockfiles package-lock.json +yarn.lock + +# Temporary directory for testing tmp diff --git a/bin/create-probot-app.js b/bin/create-probot-app.js index 6f776c877b..fa8cb89056 100755 --- a/bin/create-probot-app.js +++ b/bin/create-probot-app.js @@ -2,16 +2,18 @@ 'use strict' +const fs = require('fs') const path = require('path') const inquirer = require('inquirer') const program = require('commander') -const {generate} = require('egad') +const { generate } = require('egad') const kebabCase = require('lodash.kebabcase') const camelCase = require('lodash.camelcase') const chalk = require('chalk') +const jsesc = require('jsesc') const spawn = require('cross-spawn') const stringifyAuthor = require('stringify-author') -const {guessEmail, guessAuthor, guessGitHubUsername} = require('conjecture') +const { guessEmail, guessAuthor, guessGitHubUsername } = require('conjecture') const validatePackageName = require('validate-npm-package-name') program @@ -35,6 +37,22 @@ const destination = program.args.length const templates = ['basic-js', 'checks-js', 'git-data-js', 'deploy-js', 'basic-ts'] +/** + * Partially sanitizes keys by escaping double-quotes. + * + * @param {Object} object The object to mutate. + * @param {String[]} keys The keys on `object` to sanitize. + */ +function sanitizeBy(object, keys) { + keys.forEach(key => { + if (key in object) { + object[key] = jsesc(object[key], { + quotes: 'double' + }) + } + }) +} + const prompts = [ { type: 'input', @@ -131,13 +149,14 @@ inquirer.prompt(prompts) answers.repo = program.repo || answers.repo answers.template = program.template || answers.template - // TODO: clean that up into nicer object combinging + // TODO: clean that up into nicer object combining + sanitizeBy(answers, ['author', 'description']) + if (!templates.includes(answers.template)) { console.log(chalk.red(`Please use an existing use case template: ${templates.join(', ')}`)) process.exit(1) } - const relativePath = path.join(__dirname, '/../templates/', answers.template) return generate(relativePath, destination, answers, { overwrite: Boolean(program.overwrite) @@ -145,6 +164,26 @@ inquirer.prompt(prompts) }) .then(results => { results.forEach(fileinfo => { + if (fileinfo.skipped === false && + path.basename(fileinfo.path) === 'gitignore' + ) { + try { + const gitignorePath = path.join(path.dirname(fileinfo.path), '.gitignore') + + if (fs.existsSync(gitignorePath)) { + const data = fs.readFileSync(fileinfo.path, { encoding: 'utf8' }) + fs.appendFileSync(gitignorePath, data) + fs.unlinkSync(fileinfo.path) + } else { + fs.renameSync(fileinfo.path, gitignorePath) + } + + fileinfo.path = gitignorePath + } catch (err) { + throw err + } + } + console.log(`${fileinfo.skipped ? chalk.yellow('skipped existing file') : chalk.green('created file')}: ${fileinfo.path}`) }) @@ -152,7 +191,7 @@ inquirer.prompt(prompts) }) .then(() => { console.log(chalk.blue('\nInstalling Node dependencies!')) - const child = spawn('npm', ['install', '--prefix', destination], {stdio: 'inherit'}) + const child = spawn('npm', ['install', '--prefix', destination], { stdio: 'inherit' }) child.on('close', code => { if (code !== 0) { console.log(chalk.red(`Could not install npm dependencies. Try running ${chalk.bold('npm install')} yourself.`)) diff --git a/package.json b/package.json index f13904d7ca..3d7bc6fca6 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "conjecture": "^0.1.2", "egad": "^0.2.0", "inquirer": "^6.2.2", + "jsesc": "^2.5.2", "lodash.camelcase": "^4.3.0", "lodash.kebabcase": "^4.1.1", "stringify-author": "^0.1.3", diff --git a/templates/basic-js/.gitignore b/templates/basic-js/gitignore similarity index 100% rename from templates/basic-js/.gitignore rename to templates/basic-js/gitignore diff --git a/templates/basic-ts/.gitignore b/templates/basic-ts/gitignore similarity index 100% rename from templates/basic-ts/.gitignore rename to templates/basic-ts/gitignore diff --git a/templates/checks-js/.gitignore b/templates/checks-js/gitignore similarity index 100% rename from templates/checks-js/.gitignore rename to templates/checks-js/gitignore diff --git a/templates/deploy-js/.gitignore b/templates/deploy-js/gitignore similarity index 100% rename from templates/deploy-js/.gitignore rename to templates/deploy-js/gitignore diff --git a/templates/git-data-js/.gitignore b/templates/git-data-js/gitignore similarity index 100% rename from templates/git-data-js/.gitignore rename to templates/git-data-js/gitignore