1 | const { flags } = require('@oclif/command')
|
2 | const chalk = require('chalk')
|
3 | const get = require('lodash.get')
|
4 | const Command = require('../base')
|
5 | const SitesWatchCommand = require('./watch')
|
6 | const configManual = require('../utils/init/config-manual')
|
7 | const configGithub = require('../utils/init/config-github')
|
8 | const renderShortDesc = require('../utils/renderShortDescription')
|
9 | const getRepoData = require('../utils/getRepoData')
|
10 | const inquirer = require('inquirer')
|
11 | const SitesCreateCommand = require('./sites/create')
|
12 | const LinkCommand = require('./link')
|
13 | const { track } = require('../utils/telemetry')
|
14 |
|
15 | class InitCommand extends Command {
|
16 | async run() {
|
17 | const { flags } = this.parse(InitCommand)
|
18 | const { site, api, state } = this.netlify
|
19 |
|
20 |
|
21 | await this.authenticate()
|
22 |
|
23 | const siteId = site.get('siteId')
|
24 |
|
25 |
|
26 | let siteData
|
27 | try {
|
28 | siteData = await api.getSite({ siteId })
|
29 | } catch (e) {
|
30 |
|
31 | }
|
32 |
|
33 | if (siteId && siteData) {
|
34 | const repoUrl = get(siteData, 'build_settings.repo_url')
|
35 | this.log()
|
36 | this.log(`${chalk.yellow('Warning:')} It looks like this site has already been initialized.`)
|
37 | this.log()
|
38 | this.log(`Site Name: ${chalk.cyan(siteData.name)}`)
|
39 | this.log(`Site Url: ${chalk.cyan(siteData.ssl_url || siteData.url)}`)
|
40 | if (repoUrl) {
|
41 | this.log(`Site Repo: ${chalk.cyan(repoUrl)}`)
|
42 | }
|
43 | this.log(`Site Id: ${chalk.cyan(siteData.id)}`)
|
44 | this.log(`Admin URL: ${chalk.cyan(siteData.admin_url)}`)
|
45 | this.log()
|
46 |
|
47 | this.log(`To disconnect this directory and create a new site (or link to another siteId)`)
|
48 | this.log(`1. Run ${chalk.cyanBright.bold('netlify unlink')}`)
|
49 | this.log(`2. Then run ${chalk.cyanBright.bold('netlify init')} again`)
|
50 | // TODO remove this.log(`Or delete the siteId from ${this.siteData.path}`)
|
51 | this.exit()
|
52 | }
|
53 |
|
54 | // Look for local repo
|
55 | const repo = await getRepoData()
|
56 |
|
57 | if (repo.error) {
|
58 | console.log()
|
59 | console.log(`${chalk.redBright('No git remote found. (╯°□°)╯︵ ┻━┻')}`)
|
60 | console.log(`
|
61 | It is recommended that you initialize a site that has a remote repository in Github.
|
62 |
|
63 | This will allow for Netlify Continuous deployment to build branch & PR previews.
|
64 |
|
65 | For more details on Netlify CI checkout the docs: http:
|
66 | `)
|
67 | let message
|
68 | switch (repo.error) {
|
69 | case 'Couldn\'t find origin url': {
|
70 | message = `Unable to find a remote origin url. Please add a git remote.
|
71 |
|
72 | git remote add origin https:
|
73 | `
|
74 | break
|
75 | }
|
76 | }
|
77 | if (message) {
|
78 | console.log(message)
|
79 | }
|
80 |
|
81 | const NEW_SITE_NO_GIT = 'Yes, create manually deploy site'
|
82 | const NO_ABORT = 'No, I will connect this with directory with github first'
|
83 |
|
84 | const { noGitRemoteChoice } = await inquirer.prompt([
|
85 | {
|
86 | type: 'list',
|
87 | name: 'noGitRemoteChoice',
|
88 | message: 'Do you want to create a netlify site without a git repository?',
|
89 | choices: [
|
90 | NEW_SITE_NO_GIT,
|
91 | NO_ABORT
|
92 | ]
|
93 | }
|
94 | ])
|
95 |
|
96 | // create site or search for one
|
97 | if (noGitRemoteChoice === NEW_SITE_NO_GIT) {
|
98 | // run site:create command
|
99 | siteData = await SitesCreateCommand.run([])
|
100 |
|
101 | console.log(`"${siteData.name}" site was created`)
|
102 | console.log()
|
103 | this.log(`To deploy to this site. Run your site build and then ${chalk.cyanBright.bold('netlify deploy')}`)
|
104 |
|
105 | // Save to .netlify/state.json file
|
106 | state.set('siteId', siteData.id)
|
107 |
|
108 | // no github remote
|
109 | this.exit()
|
110 |
|
111 | } else if (noGitRemoteChoice === NO_ABORT) {
|
112 | console.log()
|
113 | console.log(`${chalk.bold('To initialize a new git repo follow the steps below.')}
|
114 |
|
115 | 1. Initialize a new repo:
|
116 |
|
117 | ${chalk.cyanBright.bold('git init')}
|
118 |
|
119 | 2. Commit your files
|
120 |
|
121 | ${chalk.cyanBright.bold('git add .')}
|
122 |
|
123 | 3. Commit your files
|
124 |
|
125 | ${chalk.cyanBright.bold('git commit -m \'initial commit\'')}
|
126 |
|
127 | 4. Create a new repo in github ${chalk.cyanBright.bold('https://github.com/new')}
|
128 |
|
129 | 5. Link the remote repo with this local directory
|
130 |
|
131 | ${chalk.cyanBright.bold('git remote add origin git@github.com:YourGithubName/your-repo-slug.git')}
|
132 |
|
133 | 6. Push up your files
|
134 |
|
135 | ${chalk.cyanBright.bold('git push -u origin master')}
|
136 |
|
137 | 7. Initialize your Netlify Site
|
138 |
|
139 | ${chalk.cyanBright.bold('netlify init')}
|
140 | `)
|
141 | this.exit()
|
142 | }
|
143 |
|
144 | // Throw github remote error
|
145 | this.error(repo.error)
|
146 | }
|
147 |
|
148 | const NEW_SITE = '+ Create & configure a new site in Netlify'
|
149 | const EXISTING_SITE = '⇄ Link this directory to an existing site in your Netlify account'
|
150 |
|
151 | const initializeOpts = [
|
152 | NEW_SITE,
|
153 | EXISTING_SITE
|
154 | ]
|
155 |
|
156 | const { initChoice } = await inquirer.prompt([
|
157 | {
|
158 | type: 'list',
|
159 | name: 'initChoice',
|
160 | message: 'What would you like to do?',
|
161 | choices: initializeOpts
|
162 | }
|
163 | ])
|
164 |
|
165 | // create site or search for one
|
166 | if (initChoice === NEW_SITE) {
|
167 | await track('sites_initStarted', {
|
168 | type: 'new site'
|
169 | })
|
170 | // run site:create command
|
171 | siteData = await SitesCreateCommand.run([])
|
172 | } else if (initChoice === EXISTING_SITE) {
|
173 | // run link command
|
174 | siteData = await LinkCommand.run([], false)
|
175 | }
|
176 |
|
177 | // Check for existing CI setup
|
178 | const remoteBuildRepo = get(siteData, 'build_settings.repo_url')
|
179 | if (remoteBuildRepo) {
|
180 | this.log()
|
181 | this.log(chalk.underline.bold(`Existing Repo detected`))
|
182 | const siteName = get(siteData, 'name')
|
183 | this.log(`This site "${siteName}" is already configured to automatically deploy via ${remoteBuildRepo}`)
|
184 | // TODO add support for changing github repo in site:config command
|
185 |
|
186 | if (flags.watch) {
|
187 | await SitesWatchCommand.run([])
|
188 | }
|
189 | this.exit()
|
190 | }
|
191 |
|
192 | // Save to .netlify/state.json file
|
193 | state.set('siteId', siteData.id)
|
194 |
|
195 | if (flags.manual) {
|
196 | await configManual(this, siteData, repo)
|
197 | } else {
|
198 | switch (repo.provider) {
|
199 | case 'github': {
|
200 | await configGithub(this, siteData, repo)
|
201 | break
|
202 | }
|
203 | case 'gitlab':
|
204 | default: {
|
205 | this.error('No configurator found for the git hosting service')
|
206 | }
|
207 | }
|
208 | }
|
209 |
|
210 | // Success Message
|
211 | this.log()
|
212 | this.log(chalk.greenBright.bold.underline(`Netlify CI/CD Configured!`))
|
213 | this.log()
|
214 | this.log(`This site is now configured to automatically deploy from ${repo.provider} branches & pull requests`)
|
215 | this.log()
|
216 | this.log(`Next steps:
|
217 |
|
218 | ${chalk.cyanBright.bold('git push')} Push to your git repository to trigger new site builds
|
219 | ${chalk.cyanBright.bold('netlify open')} Open the netlify admin url of your site
|
220 | `)
|
221 |
|
222 | if (flags.watch) {
|
223 | await SitesWatchCommand.run([])
|
224 | }
|
225 | }
|
226 | }
|
227 |
|
228 | InitCommand.description = `${renderShortDesc('Configure continuous deployment for a new or existing site')}`
|
229 |
|
230 | InitCommand.flags = {
|
231 | manual: flags.boolean({
|
232 | description: 'Manually configure a git remote for CI'
|
233 | }),
|
234 | watch: flags.boolean({
|
235 | char: 'w',
|
236 | description: 'Make the CLI wait for the first deploy to complete after setting up CI'
|
237 | })
|
238 | }
|
239 |
|
240 | module.exports = InitCommand
|
241 |
|
\ | No newline at end of file |