Skip to content

Commit a91c55b

Browse files
authored
Added script for releasing canary package (elastic#1338)
1 parent 5ed2548 commit a91c55b

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ certs
7070
.github
7171
CODE_OF_CONDUCT.md
7272
CONTRIBUTING.md
73+
74+
# CANARY-PACKAGE
75+
api/kibana.d.ts
76+
# /CANARY-PACKAGE

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"homepage": "http://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html",
1414
"version": "8.0.0-SNAPSHOT.9f33e3c7",
15+
"versionCanary": "8.0.0-canary.1",
1516
"keywords": [
1617
"elasticsearch",
1718
"elastic",
@@ -102,4 +103,4 @@
102103
"coverage": false,
103104
"jobs-auto": true
104105
}
105-
}
106+
}

scripts/release-canary.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
'use strict'
2+
3+
/**
4+
* Script for releasing the canary client to npm.
5+
* It should be executed from the top level directory of the repository.
6+
*
7+
* Usage:
8+
* node scripts/release-canary.js --otp <otp-code>
9+
*
10+
* You can reset the canary count via the `--reset` option
11+
* node scripts/release-canary.js --otp <otp-code> --reset
12+
*
13+
* You can also do a dry run with the `--dry-run` option
14+
* node scripts/release-canary.js --otp <otp-code> --dry-run
15+
*/
16+
17+
const readline = require('readline')
18+
const assert = require('assert')
19+
const { execSync } = require('child_process')
20+
const { writeFile, readFile } = require('fs').promises
21+
const { join } = require('path')
22+
const minimist = require('minimist')
23+
const chalk = require('chalk')
24+
25+
async function release (opts) {
26+
assert(process.cwd() !== __dirname, 'You should run the script from the top level directory of the repository')
27+
assert(typeof opts.otp === 'string', 'Missing OTP')
28+
const packageJson = JSON.parse(await readFile(join(__dirname, '..', 'package.json'), 'utf8'))
29+
30+
const originalName = packageJson.name
31+
const originalVersion = packageJson.version
32+
const currentCanaryVersion = packageJson.versionCanary
33+
const originalNpmIgnore = await readFile(join(__dirname, '..', '.npmignore'), 'utf8')
34+
35+
const newCanaryInteger = opts.reset ? 1 : (Number(currentCanaryVersion.split('-')[1].split('.')[1]) + 1)
36+
const newCanaryVersion = `${originalVersion.split('-')[0]}-canary.${newCanaryInteger}`
37+
38+
// Update the package.json with the correct name and new version
39+
packageJson.name = '@elastic/elasticsearch-canary'
40+
packageJson.version = newCanaryVersion
41+
packageJson.versionCanary = newCanaryVersion
42+
packageJson.commitHash = execSync('git log -1 --pretty=format:%h').toString()
43+
44+
// update the package.json
45+
await writeFile(
46+
join(__dirname, '..', 'package.json'),
47+
JSON.stringify(packageJson, null, 2),
48+
'utf8'
49+
)
50+
51+
// update the npmignore to publish the kibana types as well
52+
const newNpmIgnore = originalNpmIgnore.slice(0, originalNpmIgnore.indexOf('# CANARY-PACKAGE')) +
53+
originalNpmIgnore.slice(originalNpmIgnore.indexOf('# /CANARY-PACKAGE') + 17)
54+
await writeFile(
55+
join(__dirname, '..', '.npmignore'),
56+
newNpmIgnore,
57+
'utf8'
58+
)
59+
60+
// confirm the package.json changes with the user
61+
const diff = execSync('git diff').toString().split('\n').map(colorDiff).join('\n')
62+
console.log(diff)
63+
const answer = await confirm()
64+
// release on npm with provided otp
65+
if (answer) {
66+
execSync(`npm publish --otp ${opts.otp} ${opts['dry-run'] ? '--dry-run' : ''}`, { stdio: 'inherit' })
67+
} else {
68+
// the changes were not good, restore the previous canary version
69+
packageJson.versionCanary = currentCanaryVersion
70+
}
71+
72+
// restore the package.json to the original values
73+
packageJson.name = originalName
74+
packageJson.version = originalVersion
75+
delete packageJson.commitHash
76+
77+
await writeFile(
78+
join(__dirname, '..', 'package.json'),
79+
JSON.stringify(packageJson, null, 2),
80+
'utf8'
81+
)
82+
83+
await writeFile(
84+
join(__dirname, '..', '.npmignore'),
85+
originalNpmIgnore,
86+
'utf8'
87+
)
88+
}
89+
90+
function confirm (question) {
91+
return new Promise((resolve, reject) => {
92+
const rl = readline.createInterface({
93+
input: process.stdin,
94+
output: process.stdout
95+
})
96+
97+
rl.question('Does it look good? (y/n) ', (answer) => {
98+
resolve(answer === 'y')
99+
rl.close()
100+
})
101+
})
102+
}
103+
104+
function colorDiff (line) {
105+
if (line.startsWith('+')) {
106+
return chalk.green(line)
107+
} else if (line.startsWith('-')) {
108+
return chalk.red(line)
109+
} else {
110+
return line
111+
}
112+
}
113+
114+
release(
115+
minimist(process.argv.slice(2), {
116+
unknown (option) {
117+
console.log(`Unrecognized option: ${option}`)
118+
process.exit(1)
119+
},
120+
string: [
121+
// The otp code for publishing the package
122+
'otp'
123+
],
124+
boolean: [
125+
// Reset the canary version to '1'
126+
'reset',
127+
// run all the steps but publish
128+
'dry-run'
129+
]
130+
})
131+
)
132+
.catch(err => {
133+
console.log(err)
134+
process.exit(1)
135+
})

0 commit comments

Comments
 (0)