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