-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate-changed-package-versions.js
More file actions
122 lines (100 loc) · 4.49 KB
/
Copy pathvalidate-changed-package-versions.js
File metadata and controls
122 lines (100 loc) · 4.49 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
const path = require('path');
const fs = require('fs');
const cp = require('child_process');
const semver = require('semver');
function main() {
const changedFiles = readChangedFilesFile(process.argv[2]);
if (changedFiles.length === 0) {
console.log('No changed files; no verification needed');
process.exit(0);
}
displayList('THE FOLLOWING FILES WERE CHANGED:', changedFiles);
const changedPackages = identifyMeaningfullyChangedPackages(changedFiles);
if (changedPackages.length === 0) {
console.log('No changed packages; no verification needed');
process.exit(0);
}
displayList('THE FOLLOWING PACKAGES HAVE CHANGED (NON-TEST) FILES:', changedPackages);
const incorrectlyVersionedPackages = identifyIncorrectlyVersionedPackages(changedPackages);
if (incorrectlyVersionedPackages.length > 0) {
displayList('PROBLEM: SOME PACKAGES ARE INCORRECTLY VERSIONED:', incorrectlyVersionedPackages);
process.exit(1);
} else {
console.log('ALL CHANGED PACKAGES ARE APPROPRIATELY VERSIONED');
process.exit(0);
}
}
function displayList(header, list) {
console.log(header);
for (const listItem of list) {
console.log(`* ${listItem}`);
}
console.log('');
}
function readChangedFilesFile(changedFilesFileName) {
return fs.readFileSync(changedFilesFileName, 'utf-8').split('\n').map(s => s.trim());
}
function identifyMeaningfullyChangedPackages(changedFiles) {
const changedPackages = new Set();
for (const changedFile of changedFiles) {
const changedPackage = convertFileNameToPackageNameIfPossible(changedFile);
if (changedPackage && !isFileInTestFolder(changedFile)) {
changedPackages.add(changedPackage);
}
}
return [...changedPackages.keys()];
}
function convertFileNameToPackageNameIfPossible(changedFile) {
const changedFilePathSegments = changedFile.split('/');
if (changedFilePathSegments.length < 2 || changedFilePathSegments[0] !== 'packages') {
return null;
} else {
return path.join(changedFilePathSegments[0], changedFilePathSegments[1]);
}
}
function isFileInTestFolder(changedFile) {
const changedFilePathSegments = path.dirname(changedFile).split('/');
return changedFilePathSegments.length >= 3
&& changedFilePathSegments[0] === 'packages'
&& changedFilePathSegments[2] === 'test';
}
function identifyIncorrectlyVersionedPackages(changedPackages) {
const incorrectlyVersionedPackages = [];
for (const changedPackage of changedPackages) {
if (isPackageThatHasNotPublished(changedPackage)) {
continue; // We can't check previous versions of this package because it hasn't published yet, so the version must be correct
}
const packageJsonPath = path.join(changedPackage, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
continue; // This means the package was deleted, so we ignore this package
}
const packageVersion = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')).version;
if (!packageVersion.endsWith('-SNAPSHOT')) {
incorrectlyVersionedPackages.push(`${changedPackage} (currently versioned as ${packageVersion}) lacks a trailing "-SNAPSHOT"`);
continue;
}
const releasedPackageVersion = getLatestReleasedVersion(changedPackage);
if (semver.lte(semver.parse(packageVersion.slice(0, packageVersion.length - 9)), semver.parse(releasedPackageVersion))) {
incorrectlyVersionedPackages.push(`${changedPackage} (currently versioned as ${packageVersion}) is not semantically ahead of latest published release ${releasedPackageVersion}`);
}
}
return incorrectlyVersionedPackages;
}
function getLatestReleasedVersion(changedPackage) {
const publishedPackageName = JSON.parse(fs.readFileSync(path.join(changedPackage, 'package.json'), 'utf-8')).name;
try {
return cp.execSync(`npm view ${publishedPackageName} version`, {
encoding: 'utf-8'
});
} catch (e) {
console.log(`NOTE: Could not fetch latest release version of ${publishedPackageName} (located in ${changedPackage}). Is that an error?`);
return undefined;
}
}
function isPackageThatHasNotPublished(changedPackage) {
return [
"packages/ENGINE-TEMPLATE",
"packages/code-analyzer-apexguru-engine" // remove ths exception once PR merges and is published
].includes(changedPackage.replace("\\","/"));
}
main();