Skip to content

Commit 78bc0b9

Browse files
author
amazon-meaisiah
committed
Add scripts to automate versioning
1 parent 58504d9 commit 78bc0b9

File tree

5 files changed

+135
-10
lines changed

5 files changed

+135
-10
lines changed

BUILDING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ Gets the output from the CFN stack, writes a local version of the config.js file
200200

201201
Lint all code in the repo using ESLint and `cfn-lint`.
202202

203+
### `node run version --inc=patch|minor|major|prepatch|preminor|premajor|prerelease --type=beta`
204+
205+
Update all package versions with a version increment and optional prerelease identifier (as used by [semver](https://www.npmjs.com/package/semver)), and create a commit with the updated files and all files that were already staged prior to running this command.
206+
203207
### `node run test`
204208

205209
Run all the unit tests.

package-lock.json

Lines changed: 68 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"memorystream": "^0.3.1",
3131
"node-fetch": "^2.3.0",
3232
"request-promise": "^4.2.5",
33+
"semver": "^7.3.2",
3334
"xml-js": "^1.6.11"
3435
},
3536
"dependencies": {}

scripts/internal/util.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ function computeExtra ({ status, signal }) {
3636
}
3737

3838
async function run (name, args, { action, target, ...opts }) {
39-
console.error(green(`${action} for `) + blue(target) + green(' started'))
39+
console.error(green(`${action} for `) + (target ? blue(target) : green('root')) + green(' started'))
4040
const result = await exec(name, args, opts)
4141

4242
if (result.error != null) {
43-
console.error(red(`${action} for ${target} errored\n${inspect(result.error, { colors: true })}`))
43+
console.error(red(`${action} for ${target || 'root'} errored\n${inspect(result.error, { colors: true })}`))
4444
return false
4545
} else {
4646
console.error(
47-
green(`${action} for `) + blue(target) + green(' completed') +
47+
green(`${action} for `) + (target ? blue(target) : green('root')) + green(' completed') +
4848
computeExtra(result)
4949
)
5050
return result.status === 0
@@ -58,11 +58,12 @@ const packageList = [
5858
'dev-portal',
5959
'lambdas/backend',
6060
'lambdas/catalog-updater',
61+
'lambdas/cloudfront-security',
6162
'lambdas/listener',
6263
'lambdas/static-asset-uploader',
6364
'lambdas/user-group-importer'
6465
].map(rel => ({
65-
target: `/${rel}`,
66+
target: rel,
6667
resolved: p(rel)
6768
}))
6869

scripts/run.js

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Note: this *MUST NOT* globally depend on any module installed in `node_modules`, as it could be
44
// loaded before they're installed.
55

6+
const path = require('path')
67
const { p, run, exec, packageList } = require('./internal/util.js')
78
const { blue, green } = require('./internal/color').stdout
89
const deployTemplate = require('./internal/deploy-template.js')
@@ -31,14 +32,14 @@ require('./internal/execute-tasks.js')({
3132
const fse = require('fs-extra')
3233

3334
for (const { target, resolved } of packageList) {
34-
console.log(green('Deleting ') + blue(target))
35-
await fse.remove(resolved)
35+
console.log(green('Deleting ') + (target ? blue(target) : green('root')))
36+
await fse.remove(path.join(resolved, 'node_modules'))
3637
}
3738

3839
console.log(green('Preparing dependencies...'))
3940
// We have the package and distribution bundles here in source control, and these should only
4041
// be updated with that dependency. (Removing them causes build issues.)
41-
await run('git', ['checkout', '--', 'dev-portal/node_modules'])
42+
await exec('git', ['checkout', '--', 'dev-portal/node_modules'])
4243

4344
await this.install()
4445
},
@@ -89,5 +90,58 @@ require('./internal/execute-tasks.js')({
8990

9091
async 'cfn-lint' () {
9192
await exec('cfn-lint')
93+
},
94+
95+
async 'version' ({ inc: increment, type }) {
96+
if (increment == null) {
97+
throw new TypeError('Increment is required')
98+
}
99+
100+
if (!/^(?:patch|minor|major|prepatch|preminor|premajor|prerelease)$/.test(increment)) {
101+
throw new TypeError('Increment is invalid')
102+
}
103+
104+
// Note: this might not necessarily be installed yet, so it can't be loaded globally.
105+
const fs = require('fs-extra')
106+
const inc = require('semver/functions/inc')
107+
108+
await this.build()
109+
110+
console.log(green('Updating "version" field in ') + blue('package.json'))
111+
const rootPkg = JSON.parse(await fs.readFile(p('package.json'), 'utf-8'))
112+
const newVersion = inc(rootPkg.version, increment, type)
113+
114+
async function updatePackage (resolved, target) {
115+
console.log(green('Updating "version" field in ') + blue(target))
116+
const pkg = JSON.parse(await fs.readFile(resolved, 'utf-8'))
117+
pkg.version = newVersion
118+
fs.writeFile(resolved, JSON.stringify(pkg, null, 2) + '\n', 'utf-8')
119+
}
120+
121+
rootPkg.version = newVersion
122+
123+
await Promise.all([
124+
fs.writeFile(p('package.json'), JSON.stringify(rootPkg, null, 2) + '\n', 'utf-8'),
125+
updatePackage(p('package-lock.json'), 'package-lock.json'),
126+
...packageList
127+
.filter(p => p.target !== '')
128+
.map(async ({ target, resolved }) => Promise.all([
129+
updatePackage(path.join(resolved, 'package.json'), path.join(target, 'package.json')),
130+
updatePackage(path.join(resolved, 'package-lock.json'), path.join(target, 'package-lock.json'))
131+
]))
132+
])
133+
134+
console.log(green('Committing updated packages'))
135+
136+
await exec('git', [
137+
'commit', '--message', `v${newVersion}`,
138+
'lambdas/static-asset-uploader/build',
139+
...packageList.map(p => path.join(p.target, 'package.json')),
140+
...packageList.map(p => path.join(p.target, 'package-lock.json'))
141+
])
142+
143+
await exec('git', ['tag', `v${newVersion}`])
144+
145+
console.log(green('Release tag created: ') + blue(`v${newVersion}`))
92146
}
93147
})

0 commit comments

Comments
 (0)