-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (125 loc) · 5.34 KB
/
index.js
File metadata and controls
155 lines (125 loc) · 5.34 KB
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
#!/usr/bin/env node
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/* eslint-disable @typescript-eslint/no-var-requires */
const sh = require('shelljs')
const path = require('path')
const program = require('commander')
const {saveJSONToFile, setPackageVersion, getLatestVersion} = require('../utils')
// Exit upon error
sh.set('-e')
const rootPath = path.join(__dirname, '..', '..')
const lernaConfigPath = path.join(rootPath, 'lerna.json')
const monorepoPackages = JSON.parse(sh.exec('lerna list --all --json', {silent: true}))
const monorepoPackageNames = monorepoPackages.map((pkg) => pkg.name)
const INDEPENDENT_PACKAGES = [
'@salesforce/retail-react-app',
'@salesforce/commerce-sdk-react',
'@salesforce/pwa-kit-mcp'
]
const independentPackages = INDEPENDENT_PACKAGES.map((pkgName) =>
monorepoPackages.find((pkg) => pkg.name === pkgName)
)
/**
* @param {import('commander').CommanderStatic} program
*/
const main = (program) => {
const targetVersion = program.args[0]
if (!targetVersion) {
program.help()
}
const opts = program.opts()
if (opts.package !== 'sdk') {
// Assume that we're bumping the version of package that has its own independent version
const script1 = path.join(__dirname, 'independent-pkg-version.js')
sh.exec(`node ${script1} ${targetVersion} ${opts.package}`)
const script2 = path.join(__dirname, 'pwa-kit-deps-version.js')
const updateDepsBehaviour = opts.pwaKitDeps
sh.exec(`node ${script2} ${updateDepsBehaviour} ${opts.package}`)
// After updating the dependencies, let's update the package lock files
sh.exec('npm install')
listAllVersions()
process.exit(0)
}
sh.exec(`lerna version --exact --no-push --no-git-tag-version --yes ${targetVersion}`)
// `--exact` above is for pinning the version of the pwa-kit dependencies
// https://github.com/lerna/lerna/tree/main/libs/commands/version#--exact
const lernaConfig = JSON.parse(sh.cat(lernaConfigPath))
const newMonorepoVersion = lernaConfig.version
// update versions for root package and root package lock
setPackageVersion(newMonorepoVersion, {cwd: rootPath})
const notices = []
independentPackages.forEach((pkg) => {
const {name, location, version: repoVersion} = pkg
let restoreVersion = repoVersion
if (name === '@salesforce/pwa-kit-mcp') {
restoreVersion = getLatestVersion(name)
notices.push(
`⚠️ Restoring ${name} to its latest published npm version (${restoreVersion}) instead of the repo's dev version (${repoVersion}).\n` +
` This package is released independently and excluded from the monorepo SDK version bump to avoid publishing an unreleased dev version to npm.`
)
}
// Restore to the original version
// TODO: is it possible to _not_ trigger the lifecycle scripts? See commerce-sdk-react/CHANGELOG.md
setPackageVersion(restoreVersion, {cwd: location})
})
// Now that all of the package version updates are done,
// let's make sure some dependencies' versions are updated accordingly
monorepoPackages.forEach(({location}) => {
const pathToPkgJson = path.join(location, 'package.json')
const pkgJson = JSON.parse(sh.cat(pathToPkgJson))
updatePeerDeps(pkgJson, newMonorepoVersion)
updateDeps(pkgJson)
saveJSONToFile(pkgJson, pathToPkgJson)
})
// After updating the dependencies, let's update the package lock files
sh.exec('npm install')
listAllVersions()
notices.forEach((msg) => console.log(`\n${msg}`))
}
const updatePeerDeps = (pkgJson, newMonorepoVersion) => {
const peerDependencies = pkgJson.peerDependencies
if (!peerDependencies) return
Object.keys(peerDependencies).forEach((dep) => {
if (monorepoPackageNames.includes(dep)) {
console.log(`Found lerna local package ${dep} as a peer dependency of ${pkgJson.name}.`)
peerDependencies[dep] = newMonorepoVersion
}
})
}
const updateDeps = (pkgJson) => {
independentPackages.forEach((independentPkg) => {
const newVersion = independentPkg.version
if (pkgJson.dependencies?.[independentPkg.name]) {
pkgJson.dependencies[independentPkg.name] = newVersion
} else if (pkgJson.devDependencies?.[independentPkg.name]) {
pkgJson.devDependencies[independentPkg.name] = newVersion
}
})
}
const listAllVersions = () => {
sh.echo('\nVersions of packages in the monorepo:\n')
sh.exec('lerna list --all --long')
}
program.description('Bump the version of a package in our monorepo')
program.arguments('<target-version>')
program
.option(
'-p, --package <package-name>',
'the package name or an alias to a group of packages',
'sdk'
)
.addOption(
new program.Option(
'-d, --pwa-kit-deps <update-behavior>',
'for non-sdk packages, choose how to update their pwa-kit dependencies: either sync with repo or grab @latest from npm'
)
.choices(['sync', 'latest'])
.default('sync')
)
program.parse(process.argv)
main(program)