UNPKG

4.71 kBJavaScriptView Raw
1/*
2* @adonisjs/mrm-preset
3*
4* (c) Harminder Virk <virk@adonisjs.com>
5*
6* For the full copyright and license information, please view the LICENSE
7* file that was distributed with this source code.
8*/
9
10const chalk = require('chalk')
11const inquirer = require('inquirer')
12const { json, ini } = require('mrm-core')
13const { execSync } = require('child_process')
14const debug = require('debug')('adonis:mrm-init')
15
16const gitOrigin = {
17 type: 'input',
18 message: 'Enter git origin url',
19 validate (input) {
20 return !input ? 'Please create a git project and enter it\'s remote origin' : true
21 },
22 when () {
23 const gitFile = ini('.git/config')
24 if (!gitFile.exists()) {
25 return true
26 }
27 const origin = gitFile.get('remote "origin"')
28 return !origin || !origin.url
29 },
30 name: 'gitOrigin'
31}
32
33/**
34 * Whether written by the core team or not
35 *
36 * @type {Object}
37 */
38const isCore = {
39 type: 'confirm',
40 message: 'Is it a package written by the AdonisJS core team',
41 name: 'core'
42}
43
44/**
45 * The minimum node version supported by the project.
46 *
47 * @type {Object}
48 */
49const minNodeVersion = {
50 type: 'list',
51 message: 'Select the minimum node version your project will support',
52 name: 'minNodeVersion',
53 choices: [
54 {
55 name: '14.17.0 (LTS)',
56 value: '14.17.0',
57 },
58 {
59 name: 'latest',
60 value: 'latest'
61 }
62 ]
63}
64
65/**
66 * The project license
67 *
68 * @type {Object}
69 */
70const license = {
71 type: 'list',
72 choices: ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'MIT', 'Unlicense'],
73 message: 'Select project license. Select Unlicense if not sure',
74 name: 'license'
75}
76
77/**
78 * Services to be used by the project
79 *
80 * @type {Object}
81 */
82const services = {
83 type: 'checkbox',
84 message: 'Select the CI services you want to use',
85 choices: [
86 {
87 name: 'Appveyor',
88 value: 'appveyor'
89 },
90 {
91 name: 'Circle CI',
92 value: 'circleci'
93 },
94 {
95 name: 'Github actions',
96 value: 'github-actions'
97 },
98 ],
99 name: 'services'
100}
101
102/**
103 * The appveyor username. Only asked when services
104 * has `appveyor` in it
105 *
106 * @type {Object}
107 */
108const appveyorUsername = {
109 type: 'input',
110 message: 'Enter appveyor username',
111 when: function (answers) {
112 return answers.services.indexOf('appveyor') > -1
113 },
114 name: 'appveyorUsername'
115}
116
117/**
118 * Ask if should configure github actions to run on windows too.
119 *
120 * @type {Object}
121 */
122const runGhActionsOnWindows = {
123 type: 'confirm',
124 message: 'Run github actions on windows?',
125 when: function (answers) {
126 return answers.services.indexOf('github-actions') > -1
127 },
128 name: 'runGhActionsOnWindows'
129}
130
131/**
132 * The probot applications to use
133 * @type {Object}
134 */
135const probotApps = {
136 type: 'checkbox',
137 message: 'Select the probot applications you want to use',
138 choices: [
139 {
140 name: 'Stale Issues',
141 value: 'stale'
142 },
143 {
144 name: 'Lock Issues',
145 value: 'lock'
146 }
147 ],
148 name: 'probotApps'
149}
150
151/**
152 * Running the task, asking questions and create a project
153 * specific config file.
154 *
155 * @method task
156 *
157 * @return {void}
158 */
159async function task () {
160 const file = json('config.json')
161 const existingAnswers = file.get()
162
163 /**
164 * Fill existing values
165 */
166 if (existingAnswers.minNodeVersion) {
167 minNodeVersion.choices.unshift({
168 name: `${existingAnswers.minNodeVersion} (from ./config.json)`,
169 value: existingAnswers.minNodeVersion,
170 })
171 minNodeVersion.default = 0
172 }
173
174 isCore.default = existingAnswers.core
175 license.default = existingAnswers.license
176 services.default = existingAnswers.services
177 appveyorUsername.default = existingAnswers.appveyorUsername
178 runGhActionsOnWindows.default = existingAnswers.runGhActionsOnWindows
179 probotApps.default = existingAnswers.probotApps
180
181 const answers = await inquirer.prompt([
182 gitOrigin,
183 minNodeVersion,
184 isCore,
185 license,
186 services,
187 appveyorUsername,
188 probotApps,
189 runGhActionsOnWindows
190 ])
191
192 const fileContent = {
193 core: answers.core,
194 license: answers.license,
195 services: answers.services,
196 appveyorUsername: answers.appveyorUsername,
197 minNodeVersion: answers.minNodeVersion,
198 probotApps: answers.probotApps,
199 runGhActionsOnWindows: answers.runGhActionsOnWindows
200 }
201
202 debug('init %o', fileContent)
203
204 file.set(fileContent)
205 file.save()
206
207 /**
208 * Initiate git repo, when answers has gitOrigin
209 */
210 if (answers.gitOrigin) {
211 console.log(chalk.yellow('git init'))
212 execSync('git init')
213
214 console.log(chalk.yellow(`git remote add origin ${answers.gitOrigin}`))
215 execSync(`git remote add origin ${answers.gitOrigin}`)
216 }
217}
218
219task.description = 'Initiate the project config file'
220module.exports = task