-
Notifications
You must be signed in to change notification settings - Fork 290
/
publish-version.mjs
156 lines (129 loc) · 4.51 KB
/
publish-version.mjs
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
import { execa } from 'execa';
import fs from 'fs/promises';
import glob from 'glob';
import path from 'path';
import os from 'os';
async function run() {
const { stdout: branchName } = await execa('git', [
'rev-parse',
'--abbrev-ref',
'HEAD',
]);
console.log('Current branch:', branchName);
const lernaJson = JSON.parse(await fs.readFile('lerna.json', 'utf-8'));
// read the current version from ./version.txt
const nextVersion = await fs.readFile('./version.txt', 'utf-8');
const packages = lernaJson.packages;
if (!packages) {
throw new Error('Could not find packages in lerna.json');
}
// Get the npm package names for the packages we are publishing
const npmPackageNames = [];
for (const packagePathPattern of packages) {
// Use glob to find all matching directories
const matchingDirectories = glob.sync(packagePathPattern);
for (const packageDirectory of matchingDirectories) {
const packageJsonPath = path.join(packageDirectory, 'package.json');
npmPackageNames.push(
JSON.parse(await fs.readFile(packageJsonPath, 'utf-8')).name
);
}
}
// add packages/docs so that we can update the peer dependencies
packages.push('packages/docs');
// for each package's package.json file, see if there is a peerdependency,
// and for each peer dependency see if it includes a package that
// starts with @ohif/, if so update the version to the
// next version since lerna will not handle this for us
// Iterate over each package path pattern
for (const packagePathPattern of packages) {
// Use glob to find all matching directories
const matchingDirectories = glob.sync(packagePathPattern);
for (const packageDirectory of matchingDirectories) {
const packageJsonPath = path.join(packageDirectory, 'package.json');
try {
const packageJson = JSON.parse(
await fs.readFile(packageJsonPath, 'utf-8')
);
// Iterate over peerDependencies, dependencies, and devDependencies
for (const dependencyType of [
'peerDependencies',
'dependencies',
'devDependencies',
]) {
const dependencies = packageJson[dependencyType];
if (!dependencies) {
continue;
}
for (const dependency of Object.keys(dependencies)) {
if (
dependency.startsWith('@cornerstonejs/') &&
npmPackageNames.includes(dependency)
) {
dependencies[dependency] = `^${nextVersion}`;
console.log(
`updating ${dependencyType} of ${dependency} to `,
dependencies[dependency]
);
}
}
}
await fs.writeFile(
packageJsonPath,
JSON.stringify(packageJson, null, 2) + '\n'
);
// run prettier on the package.json file
await execa('npx', ['prettier', '--write', packageJsonPath]);
console.log(`Updated ${packageJsonPath}`);
} catch (err) {
// This could be a directory without a package.json file. Ignore and continue.
continue;
}
}
}
// remove the .npmrc to not accidentally publish to npm
const localNpmrc = '.npmrc';
const repoNpmrc = path.join(os.homedir(), 'repo/.npmrc');
await unlinkFile(localNpmrc);
await unlinkFile(repoNpmrc);
// Todo: Do we really need to run the build command here?
// Maybe we need to hook the netlify deploy preview
// await execa('yarn', ['run', 'build']);
console.log('Committing and pushing changes...');
await execa('git', ['add', '-A']);
await execa('git', [
'commit',
'-m',
'chore(version): version.json [skip ci]',
]);
await execa('git', ['push', 'origin', branchName]);
console.log('Setting the version using lerna...');
// add a message to the commit to indicate that the version was set using lerna
await execa('npx', [
'lerna',
'version',
nextVersion,
'--yes',
'--exact',
'--force-publish',
'--message',
'chore(version): Update package versions [skip ci]',
'--conventional-commits',
'--create-release',
'github',
]);
console.log('Version set using lerna');
}
async function unlinkFile(filePath) {
try {
await fs.access(filePath);
await fs.unlink(filePath);
console.log(`${filePath} has been deleted`);
} catch (error) {
console.log(`${filePath} does not exist or an error occurred:`, error);
}
}
run().catch((err) => {
console.error('Error encountered during version bump:', err);
process.exit(1);
});