1 | const Command = require('../base')
|
2 | const { CLIError } = require('@oclif/errors')
|
3 | const pWaitFor = require('p-wait-for')
|
4 | const cli = require('cli-ux').default
|
5 | const prettyjson = require('prettyjson')
|
6 | const chalk = require('chalk')
|
7 | const renderShortDesc = require('../utils/renderShortDescription')
|
8 |
|
9 | class SitesWatchCommand extends Command {
|
10 | async run() {
|
11 | await this.authenticate()
|
12 | const client = this.netlify.api
|
13 | const siteId = this.netlify.site.get('siteId')
|
14 |
|
15 |
|
16 | console.time('Deploy time')
|
17 | await cli.wait(1000)
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | cli.action.start('Waiting for active site deploys to complete')
|
35 | try {
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | const noActiveBuilds = await waitForBuildFinish(client, siteId)
|
45 |
|
46 | const siteData = await client.getSite({ siteId })
|
47 |
|
48 | const message = chalk.cyanBright.bold.underline(noActiveBuilds ? 'Last build' : 'Deploy complete')
|
49 | this.log()
|
50 | this.log(message)
|
51 | this.log(
|
52 | prettyjson.render({
|
53 | URL: siteData.ssl_url || siteData.url,
|
54 | Admin: siteData.admin_url
|
55 | })
|
56 | )
|
57 | console.timeEnd('Deploy time')
|
58 | } catch (err) {
|
59 | throw new CLIError(err)
|
60 | }
|
61 |
|
62 | this.exit()
|
63 | }
|
64 | }
|
65 |
|
66 | SitesWatchCommand.description = `${renderShortDesc('Watch for site deploy to finish')}`
|
67 |
|
68 | SitesWatchCommand.examples = [
|
69 | `netlify watch`,
|
70 | `git push && netlify watch`
|
71 | ]
|
72 |
|
73 | async function waitForBuildFinish(api, siteId) {
|
74 | let firstPass = true
|
75 |
|
76 | await pWaitFor(waitForBuildToFinish, {
|
77 | interval: 1000,
|
78 | timeout: 1.2e6,
|
79 | message: 'Timeout while waiting for deploy to finish'
|
80 | })
|
81 |
|
82 |
|
83 | return firstPass
|
84 |
|
85 | async function waitForBuildToFinish() {
|
86 | const builds = await api.listSiteBuilds({ siteId })
|
87 | const currentBuilds = builds.filter(build => {
|
88 |
|
89 | return !build.done
|
90 | })
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | if (!currentBuilds || !currentBuilds.length) {
|
96 | cli.action.stop()
|
97 | return true
|
98 | }
|
99 | firstPass = false
|
100 | return false
|
101 | }
|
102 | }
|
103 |
|
104 | module.exports = SitesWatchCommand
|