-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathrelease-post.js
executable file
·222 lines (194 loc) · 6.7 KB
/
release-post.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env node
/**
* What's this?? It will help you create release blog
* posts so you won't have to do the tedious work
* of stitching together data from changelog, shasums etc,
* but get a more or less complete release blog ready to go.
*
* Usage: $ node release-post.js [version]
*
* If the version argument is omitted, the latest version number
* will be picked from https://nodejs.org/dist/index.json.
*
* It'll create a file with the blog post content
* into ../locale/en/blog/release/vX.md ready for you to commit
* or possibly edit by hand before committing.
*
* Happy releasing!
*/
'use strict'
const fs = require('fs')
const path = require('path')
const Handlebars = require('handlebars')
const fetch = require('node-fetch')
const downloads = require('./helpers/downloads')
function sendRequest (opts) {
const options = {
headers: { 'User-Agent': 'nodejs.org release blog post script' },
...opts
}
return fetch(options.url, options).then(resp => {
if (resp.status !== 200) {
throw new Error(`Invalid status code (!= 200) while retrieving ${options.url}: ${resp.status}`)
}
return options.json ? resp.json() : resp.text()
})
}
function explicitVersion (version) {
return version
? Promise.resolve(version)
: Promise.reject(new Error('Invalid "version" argument'))
}
function findLatestVersion () {
return sendRequest({ url: 'https://nodejs.org/dist/index.json', json: true })
.then((versions) => versions[0].version.substr(1))
}
function fetchDocs (version) {
return Promise.all([
fetchChangelogBody(version),
fetchAuthor(version),
fetchVersionPolicy(version),
fetchShasums(version),
verifyDownloads(version)
]).then((results) => {
const [changelog, author, versionPolicy, shasums, files] = results
return {
version,
changelog,
author,
versionPolicy,
shasums,
files
}
})
}
function fetchAuthor (version) {
return fetchChangelog(version)
.then((section) => findAuthorLogin(version, section))
.then((author) => sendRequest({
url: `https://api.github.com/users/${author}`,
json: true
}))
.then((githubRes) => githubRes.name)
}
function fetchChangelog (version) {
const parts = version.split('.')
const releaseLine = parts[0] === '0'
? parts.slice(0, 2).join('')
: parts[0]
return sendRequest({
url: `https://raw.githubusercontent.com/nodejs/node/master/doc/changelogs/CHANGELOG_V${releaseLine}.md`
}).then((data) => {
// matches a complete release section
const rxSection = new RegExp(`<a id="${version}"></a>\\n([\\s\\S]+?)(?:\\n<a id="|$)`)
const matches = rxSection.exec(data)
return matches
? matches[1].trim()
: Promise.reject(new Error(`Couldn't find matching changelog for ${version}`))
})
}
function fetchChangelogBody (version) {
return fetchChangelog(version).then((section) => {
const rxSectionBody = /(### Notable [\s\S]*)/
// Make sure that all the console has been replaced
// by "```shell-session" for metalsmith-prism's check to pass
const rxSectionConsole = /```console/igm
const matches = rxSectionBody.exec(section)
return matches
? matches[1].trim().replace(rxSectionConsole, '```shell-session')
: Promise.reject(new Error(`Could not find changelog body of ${version} release`))
})
}
function fetchVersionPolicy (version) {
return fetchChangelog(version).then((section) => {
// matches the policy for a given version (Stable, LTS etc) in the changelog
// ## 2015-10-07, Version 4.2.0 'Argon' (LTS), @jasnell
// ## 2015-12-04, Version 0.12.9 (LTS), @rvagg
const rxPolicy = /^## ?\d{4}-\d{2}-\d{2}, Version [^(].*\(([^)]+)\)/
const matches = rxPolicy.exec(section)
return matches
? matches[1]
: Promise.reject(new Error(`Could not find version policy of ${version} in its changelog`))
})
}
function fetchShasums (version) {
return sendRequest({ url: `https://nodejs.org/dist/v${version}/SHASUMS256.txt.asc` })
.then(null, () => '[INSERT SHASUMS HERE]')
}
function verifyDownloads (version) {
const allDownloads = downloads(version)
const reqs = allDownloads.map(urlOrComingSoon)
return Promise.all(reqs)
}
function findAuthorLogin (version, section) {
// looking for the @author part of the release header, eg:
// ## 2016-03-08, Version 5.8.0 (Stable). @Fishrock123
// ## 2015-10-13, Version 4.2.1 'Argon' (LTS), @jasnell
// ## 2015-09-08, Version 4.0.0 (Stable), @rvagg
const rxReleaseAuthor = /^## .*? \([^)]+\)[,.] @(\S+)/
const matches = rxReleaseAuthor.exec(section)
return matches
? matches[1]
: Promise.reject(new Error(`Couldn't find @author of ${version} release :(`))
}
function urlOrComingSoon (binary) {
return sendRequest({ url: binary.url, method: 'HEAD' }).then(
() => `${binary.title}: ${binary.url}`,
() => `${binary.title}: *Coming soon*`
)
}
function renderPost (results) {
const templateStr = fs.readFileSync(path.resolve(__dirname, 'release.hbs')).toString('utf8')
const template = Handlebars.compile(templateStr, { noEscape: true })
const view = Object.assign({
date: new Date().toISOString(),
versionSlug: slugify(results.version)
}, results)
return Object.assign({
content: template(view)
}, results)
}
function writeToFile (results) {
const filepath = path.resolve(__dirname, '..', 'locale', 'en', 'blog', 'release', `v${results.version}.md`)
return new Promise((resolve, reject) => {
fs.access(filepath, fs.F_OK, (err) => {
if (!err && process.argv[3] !== '--force') {
return reject(new Error(`Release post for ${results.version} already exists!`))
}
fs.writeFile(filepath, results.content, (err1) => {
if (err1) {
return reject(new Error(`Failed to write Release post: Reason: ${err1.message}`))
}
resolve(filepath)
})
})
})
}
function slugify (str) {
return str.replace(/\./g, '-')
}
exports.explicitVersion = explicitVersion
exports.fetchShasums = fetchShasums
exports.writeToFile = writeToFile
exports.findLatestVersion = findLatestVersion
exports.verifyDownloads = verifyDownloads
exports.fetchChangelog = fetchChangelog
exports.fetchChangelogBody = fetchChangelogBody
exports.fetchAuthor = fetchAuthor
exports.fetchVersionPolicy = fetchVersionPolicy
// when script is executed directly,
// not required by another module, e.g:
// $ node scripts/release-post.js
if (require.main === module) {
explicitVersion(process.argv[2])
.then(null, findLatestVersion)
.then(fetchDocs)
.then(renderPost)
.then(writeToFile)
.then((filepath) => {
console.log('Release post created:', filepath)
}, (err) => {
console.error('Some error occurred here!', err.stack)
process.exit(1)
})
}