UNPKG

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