UNPKG

2.79 kBJavaScriptView Raw
1const Command = require('../utils/command')
2const { CLIError } = require('@oclif/errors')
3const pWaitFor = require('p-wait-for')
4const cli = require('cli-ux').default
5const prettyjson = require('prettyjson')
6const chalk = require('chalk')
7
8class 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 // wait for 1 sec for everything to kickoff
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 // Get latest commit and look for that
26 // git rev-parse HEAD
27
28 // if no sha, its a manual "triggered deploy"
29 /*
30 {
31 "id": "5b4e23db82d3f1780abd74f3",
32 "deploy_id": "5b4e23db82d3f1780abd74f2",
33 "sha": "pull/1/head",
34 "log": [],
35 "done": false,
36 "error": null,
37 "created_at": "2018-07-17T17:14:03.423Z"
38 }
39 */
40 cli.action.start('Waiting for active site deploys to complete')
41 try {
42 // Fetch all builds!
43 // const builds = await client.listSiteBuilds({siteId})
44 //
45 // // Filter down to any that are not done
46 // const buildsToWatch = builds.filter((build) => {
47 // return !build.done
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
72SitesWatchCommand.description = `Watch for site deploy to finish`
73
74SitesWatchCommand.examples = [`netlify watch`, `git push && netlify watch`]
75
76async function waitForBuildFinish(api, siteId) {
77 let firstPass = true
78
79 await pWaitFor(waitForBuildToFinish, {
80 interval: 1000,
81 timeout: 1.2e6, // 20 mins,
82 message: 'Timeout while waiting for deploy to finish'
83 })
84
85 // return only when build done or timeout happens
86 return firstPass
87
88 async function waitForBuildToFinish() {
89 const builds = await api.listSiteBuilds({ siteId })
90 const currentBuilds = builds.filter(build => {
91 // build.error
92 return !build.done
93 })
94
95 // if build.error
96 // @TODO implement build error messages into this
97
98 if (!currentBuilds || !currentBuilds.length) {
99 cli.action.stop()
100 return true
101 }
102 firstPass = false
103 return false
104 }
105}
106
107module.exports = SitesWatchCommand