-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils.js
154 lines (137 loc) · 4.19 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const http = require('https');
const qs = require('querystring');
const execa = require('execa');
/**
* helper function to get gitSha without needing a GITHUB_TOKEN (for local dev);
* @returns {string} git sha of last commit
*/
const gitSha = execa.sync('git', ['rev-parse', '--short', 'HEAD']).stdout;
/**
* @param {Object} opt
* @param {string} opt.path
* @param {Object} opt.requestBody
* @param {string} opt.hostname
* @param {string} opt.TOKEN
* @param {Object} [opt.query]
* @return {Promise<any>}
*/
function post({ path, requestBody, query, TOKEN }) {
return new Promise((resolve, reject) => {
const options = {
method: 'POST',
hostname,
path: query ? `${path}?${qs.stringify(query)}` : path,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${TOKEN || process.env.NOW_TOKEN}`,
},
};
const req = http.request(options, res => {
const chunks = [];
res.on('data', chunk => {
chunks.push(chunk);
});
res.on('end', () => {
const responseBody = Buffer.concat(chunks);
resolve(JSON.parse(responseBody.toString()));
});
});
// console.log({ requestBody });
req.write(JSON.stringify(requestBody));
req.end();
});
}
/**
* @param {Object} opt
* @param {string} opt.path
* @param {string} opt.hostname
* @param {string} opt.TOKEN
* @param {Object} [opt.query]
* @return {Promise<any>}
*/
function get({ path, query, hostname, TOKEN }) {
return new Promise((resolve, reject) => {
const options = {
method: 'GET',
hostname,
path: query ? `${path}?${qs.stringify(query)}` : path,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${TOKEN || process.env.NOW_TOKEN}`,
},
};
const req = http.request(options, res => {
const chunks = [];
res.on('data', chunk => {
chunks.push(chunk);
});
res.on('end', () => {
const responseBody = Buffer.concat(chunks);
resolve(JSON.parse(responseBody.toString()));
});
});
req.end();
});
}
/**
* @link https://zeit.co/docs/api/v1/#endpoints/deployments/list-all-the-deployments
* @return {Promise<string>} - URL of latest deploymennt
*/
function getLatestDeploy() {
if (!process.env.NOW_TOKEN) {
process.stderr.write('NOW_TOKEN env var required and is missing');
process.exit(1);
}
return new Promise((resolve, reject) => {
get({
path: '/v4/now/deployments',
hostname: 'api.zeit.co',
query: {
teamId: 'team_etXPus2wqbe3W15GcdHsbAs8', // boltdesignsystem
},
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${process.env.NOW_TOKEN}`,
},
})
.then(results => {
if (results.error) {
process.stderr.write(
`Error getting latest now.sh deploy: ${results.error.message}`,
);
process.exit(1);
}
// If a deployment hasn't finished uploading (is incomplete), the url property will have a value of null.
const resultsWithGitSha = results.deployments.filter(
d => d.meta.gitSha === gitSha,
);
const fallbackResultsWithGitSha = results.deployments.filter(d =>
gitSha.includes(d.meta.gitSha),
);
const result = resultsWithGitSha.find(d => d.url);
// if an exact match isn't found, check partial matches and sort by most recent
fallbackResultsWithGitSha.sort(function(a, b) {
// Turn created unix time strings into dates, and then sort by what happened most recently
return new Date(a.created) - new Date(b.created);
});
const fallbackResults = fallbackResultsWithGitSha.find(d => d.url);
if (result) {
resolve(`https://${result.url}`);
} else if (fallbackResults) {
resolve(`https://${fallbackResults.url}`);
} else {
reject(new Error('No deployments found'));
}
})
.catch(error => {
reject(error);
});
});
}
module.exports = {
getLatestDeploy,
gitSha,
};