Skip to content

Commit 629f866

Browse files
committed
chore: update version script
1 parent 6d8aaa1 commit 629f866

File tree

1 file changed

+62
-22
lines changed

1 file changed

+62
-22
lines changed

scripts/bump-package-versions.mjs

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import path from 'path';
2424
import { readFileSync } from 'fs';
2525
import { globSync } from 'glob';
2626

27+
// -- Utility functions --
2728
// TODO: move this into a common file
2829
const readJson = (filePath) => {
2930
return JSON.parse(readFileSync(filePath));
@@ -41,6 +42,18 @@ const getPackages = () => {
4142
return pkgInfo;
4243
});
4344
}
45+
46+
const getCommitsFrom = (commitOrTag) => {
47+
return execSync(`git log ${commitOrTag}..HEAD --oneline`, { encoding: 'utf-8' }).split('\n');
48+
}
49+
50+
const getScopedCommitsFrom = (scope, commitOrTag) => {
51+
const commits = execSync(`git log ${commitOrTag}..HEAD --oneline`, { encoding: 'utf-8' }).split('\n');
52+
53+
return commits.filter((c) => c.indexOf(`(${scope})`) !== -1);
54+
}
55+
56+
// -- Main line
4457
const publicPkgList = getPackages().filter(pkg => !pkg.private);
4558
const repoTags = execSync('git tag', { encoding: 'utf-8' }).split('\n');
4659

@@ -61,38 +74,65 @@ repoTags.forEach((tag) => {
6174
// Check for commits on each package
6275
// Assumption: current version is latest on npm (so no checking it)
6376
publicPkgList.forEach((pkgInfo) => {
77+
const pkgScope = pkgInfo.name.replace('@opentelemetry/', '');
78+
6479
if (pkgInfo.tag) {
65-
const commitList = execSync(`git log ${pkgInfo.tag}..HEAD --oneline`, { encoding: 'utf-8' }).split('\n');
66-
const pkgScope = pkgInfo.name.replace('@opentelemetry/', '');
67-
const commitsForPackage = commitList
68-
// Get only the ones with the scope
69-
.filter((c) => c.indexOf(`(${pkgScope})`) !== -1);
80+
const scopedCommits = getScopedCommitsFrom(pkgScope, pkgInfo.tag);
7081

71-
if (commitsForPackage.length === 0) {
82+
if (scopedCommits.length === 0) {
7283
return;
7384
}
74-
console.log(pkgInfo.tag)
75-
console.log(commitsForPackage)
85+
7686
const isExperimental = pkgInfo.version.startsWith('0.');
77-
const bumpMinor = commitsForPackage.some((cmt) => {
87+
const bumpMinor = scopedCommits.some((cmt) => {
7888
const pattern = isExperimental ? `(${pkgScope})!:` : `feat(${pkgScope}):`
7989
return cmt.includes(pattern);
8090
});
81-
const bumpMajor = !isExperimental && commitsForPackage.some((cmt) => cmt.includes(`(${pkgScope})!:`));
91+
const bumpMajor = !isExperimental && scopedCommits.some((cmt) => cmt.includes(`(${pkgScope})!:`));
92+
const bumpType = bumpMajor ? 'major' : (bumpMinor ? 'minor' : 'patch');
8293

83-
let command;
84-
if (bumpMajor) {
85-
command = 'npm version major';
86-
} else if (bumpMinor) {
87-
command = 'npm version minor';
88-
} else {
89-
command = 'npm version patch';
90-
}
91-
console.log(`executing ${command}`)
92-
execSync(`${command} --git-tag-version=false`, { cwd: pkgInfo.location });
94+
console.log(`Bumping ${bumpType} version in ${pkgInfo.name}`);
95+
execSync(`npm version ${bumpType} --git-tag-version=false`, { cwd: pkgInfo.location });
9396

9497
} else {
95-
// is the 1st time so no new version needed
96-
console.log(pkgInfo.name, 'has no tag')
98+
// NOTE: this could be one of two scenairios
99+
// - new package
100+
// - package being moved here like @opentelemetry/propagator-aws-xray-lambda
101+
console.log(pkgInfo.name, 'has no tag');
102+
let isNewPkg = false;
103+
let versions;
104+
try {
105+
versions = JSON.parse(
106+
execSync(`npm info ${pkgInfo.name} --json time`, { encoding: 'utf-8' })
107+
);
108+
} catch (err) {
109+
console.log(`*********\n${err.message}\n********`)
110+
isNewPkg = err.message.includes('npm ERR! 404 Not Found - GET');
111+
}
112+
113+
114+
if (isNewPkg) {
115+
console.log(pkgInfo.name, 'is not in the registry. No bump needed');
116+
} else {
117+
// - assume version is the last in npm
118+
// - find the commit where it was added
119+
// - check for commits since then, and do the calculation
120+
const addCommit = execSync(`git log --diff-filter=A -- ${pkgInfo.location}/package.json`, { encoding: 'utf-8' });
121+
const commitSha = addCommit.substring(7, 14);
122+
const scopedCommits = getScopedCommitsFrom(pkgScope, commitSha);
123+
124+
console.log(`Package ${pkgInfo.name} was added in ${commitSha}`);
125+
126+
const isExperimental = pkgInfo.version.startsWith('0.');
127+
const bumpMinor = scopedCommits.some((cmt) => {
128+
const pattern = isExperimental ? `(${pkgScope})!:` : `feat(${pkgScope}):`
129+
return cmt.includes(pattern);
130+
});
131+
const bumpMajor = !isExperimental && scopedCommits.some((cmt) => cmt.includes(`(${pkgScope})!:`));
132+
const bumpType = bumpMajor ? 'major' : (bumpMinor ? 'minor' : 'patch');
133+
134+
console.log(`Bumping ${bumpType} version in ${pkgInfo.name}`);
135+
execSync(`npm version ${bumpType} --git-tag-version=false`, { cwd: pkgInfo.location });
136+
}
97137
}
98138
})

0 commit comments

Comments
 (0)