-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpostversion.js
More file actions
68 lines (62 loc) · 1.99 KB
/
postversion.js
File metadata and controls
68 lines (62 loc) · 1.99 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
import { glob } from 'node:fs/promises';
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { major, parse, satisfies } from 'semver';
/**
* This assumes that we currently either have a fixed version or only a major version.
* @param currentVersionOrRange Either a fixed version (e.g. `1.2.3`) or a major version (e.g. `2`)
* @param newVersion
* @return {string} The new version or range
*/
function getNewVersionOrRange(currentVersionOrRange, newVersion) {
const parsed = parse(currentVersionOrRange);
if (parsed) {
return newVersion;
} else {
if (!satisfies(newVersion, currentVersionOrRange)) {
// set the new version
return `${major(newVersion)}`;
}
}
}
async function updatePeerDependencies() {
const rootDir = new URL('.', import.meta.url).pathname;
const versions = new Map();
for await (const file of glob([
join(rootDir, 'package.json'),
join(rootDir, 'projects/**/package.json')
])) {
const content = JSON.parse(await readFile(file, { encoding: 'utf8' }));
versions.set(content.name, content.version);
}
for await (const file of glob([
join(rootDir, 'package.json'),
join(rootDir, 'projects/**/package.json'),
join(rootDir, 'dist/**/package.json')
])) {
let updated = false;
const content = JSON.parse(await readFile(file, { encoding: 'utf8' }));
if (content.peerDependencies) {
for (const dependencyType of [
'dependencies',
'devDependencies',
'peerDependencies',
'optionalDependencies'
]) {
for (const name of Object.keys(content[dependencyType] ?? [])) {
if (versions.has(name)) {
content[dependencyType][name] = getNewVersionOrRange(
content[dependencyType][name],
versions.get(name)
);
updated = true;
}
}
}
}
if (updated) {
await writeFile(file, JSON.stringify(content, null, 2));
}
}
}
updatePeerDependencies();