-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathcreate-probot-app.js
executable file
Β·165 lines (152 loc) Β· 4.79 KB
/
create-probot-app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
'use strict'
const path = require('path')
const inquirer = require('inquirer')
const program = require('commander')
const {generate} = require('egad')
const kebabCase = require('lodash.kebabcase')
const camelCase = require('lodash.camelcase')
const chalk = require('chalk')
const spawn = require('cross-spawn')
const stringifyAuthor = require('stringify-author')
const {guessEmail, guessAuthor, guessGitHubUsername} = require('conjecture')
const validatePackageName = require('validate-npm-package-name')
program
.usage('[options] [destination]')
.option('-n, --appName <app-name>', 'App name')
.option('-d, --desc "<description>"',
'Description (contain in quotes)')
.option('-a, --author "<full-name>"',
'Author name (contain in quotes)')
.option('-e, --email <email>', 'Author email address')
.option('-h, --homepage <homepage>', 'Author\'s homepage')
.option('-u, --user <username>', 'GitHub username or org (repo owner)')
.option('-r, --repo <repo-name>', 'Repository name')
.option('--overwrite', 'Overwrite existing files', false)
.option('-t, --template <template>', 'Name of use case template')
.parse(process.argv)
const destination = program.args.length
? path.resolve(process.cwd(), program.args.shift())
: process.cwd()
const templates = ['basic-js', 'checks-js', 'git-data-js', 'deploy-js', 'basic-ts']
const prompts = [
{
type: 'input',
name: 'name',
default (answers) {
return answers.repo || kebabCase(path.basename(destination))
},
message: 'App name:',
when: !program.appName,
validate (appName) {
const result = validatePackageName(appName)
if (result.errors && result.errors.length > 0) {
return result.errors.join(',')
}
return true
}
},
{
type: 'input',
name: 'description',
default () {
return 'A Probot app'
},
message: 'Description of app:',
when: !program.desc
},
{
type: 'input',
name: 'author',
default () {
return guessAuthor()
},
message: 'Author\'s full name:',
when: !program.author
},
{
type: 'input',
name: 'email',
default () {
return guessEmail()
},
message: 'Author\'s email address:',
when: !program.email
},
{
type: 'input',
name: 'homepage',
message: 'Homepage:',
when: !program.homepage
},
{
type: 'input',
name: 'user',
default (answers) {
return guessGitHubUsername(answers.email)
},
message: 'GitHub user or org name:',
when: !program.user
},
{
type: 'input',
name: 'repo',
default (answers) {
return answers.appName || kebabCase(path.basename(destination))
},
message: 'Repository name:',
when: !program.repo
},
{
type: 'list',
name: 'template',
choices: templates,
message: 'Which template would you like to use?',
when: !program.template
}
]
console.log(chalk.blue('\nLet\'s create a Probot app!\nHit enter to accept the suggestion.\n'))
inquirer.prompt(prompts)
.then(answers => {
answers.author = stringifyAuthor({
name: program.author || answers.author,
email: program.email || answers.email,
url: program.homepage || answers.homepage
})
answers.year = new Date().getFullYear()
answers.camelCaseAppName = camelCase(program.appName || answers.appName)
answers.template = program.template || answers.template
answers.appName = program.appName || answers.appName
answers.desc = program.desc || answers.desc
answers.user = program.user || answers.user
answers.repo = program.repo || answers.repo
answers.template = program.template || answers.template
// TODO: clean that up into nicer object combinging
console.log(answers, destination)
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)
})
})
.then(results => {
results.forEach(fileinfo => {
console.log(`${fileinfo.skipped ? chalk.yellow('skipped existing file')
: chalk.green('created file')}: ${fileinfo.path}`)
})
return console.log(chalk.blue('Finished scaffolding files!'))
})
.then(() => {
console.log(chalk.blue('\nInstalling Node dependencies!'))
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.`))
return
}
console.log(chalk.blue('\nDone! Enjoy building your Probot app!'))
})
})