Skip to content

Commit

Permalink
Merge pull request #147 from sliit-foss/feature/automatic-versioning-…
Browse files Browse the repository at this point in the history
…disable-auto-sync

Feat(automatic-versioning): added disable auto sync flag
  • Loading branch information
Akalanka47000 authored Feb 16, 2024
2 parents 2e97afc + 6c65da2 commit 5fd24f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
4 changes: 3 additions & 1 deletion packages/automatic-versioning/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ program
)
.option("-r, --root <string>", "root directory to use when executing the script")
.option("--skip-commit", "do not commit the incremented version")
.option("--tag-based", "run versioning based on git tags");
.option("--tag-based", "run versioning based on git tags")
.option("--disable-auto-sync", "disable aligning the package version with the last published version");

[
new Option("--recursive", "recursively search for a matching commit prefix"),
Expand Down Expand Up @@ -54,6 +55,7 @@ const run = async () => {
opts.name,
opts.skipCommit,
opts.recursive,
opts.disableAutoSync,
opts.prereleaseTag,
opts.prereleaseBranch,
opts.ignorePrefixes
Expand Down
52 changes: 35 additions & 17 deletions packages/automatic-versioning/src/types/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,40 @@ const getCommitPrefix = async (recursive, ignorePrefixes, n = 1) => {
return getCommitPrefix(recursive, ignorePrefixes, n + 1);
};

const runner = (name, noCommit, recursive = false, prereleaseTag, prereleaseBranch, ignorePrefixes) => {
const getCurrentVersion = async () =>
(await run("npm version"))?.split(",")?.[0]?.split(":")?.[1]?.replace(/'/g, "")?.trim();

const getPackageVersion = async (name, disableAutoSync) => {
if (!disableAutoSync) {
try {
const versions = await run(`npm view ${name} time`).then((res) =>
res
.replace(/{|}|,|'/g, "")
.trim()
.split("\n")
.filter((v) => !v.includes("modified:") && !v.includes("created:"))
);
versions.sort(
(v1, v2) => new Date(v1.trim().split(" ")[1]).getTime() - new Date(v2.trim().split(" ")[1]).getTime()
);
return versions.pop().split(":")?.[0].trim();
} catch (e) {
return getCurrentVersion();
}
} else {
return getCurrentVersion();
}
};

const runner = (
name,
noCommit,
recursive = false,
disableAutoSync,
prereleaseTag,
prereleaseBranch,
ignorePrefixes
) => {
return run("git show --first-parent ./").then(async (diff) => {
if (diff) {
console.info(`Diff found, running versioning for ${name}`.green);
Expand All @@ -42,22 +75,7 @@ const runner = (name, noCommit, recursive = false, prereleaseTag, prereleaseBran
const currentBranch = (await run("git rev-parse --abbrev-ref HEAD"))?.trim();
if (currentBranch === prereleaseBranch) {
let prerelease = false;
let currentVersion;
try {
const versions = await run(`npm view ${name} time`).then((res) =>
res
.replace(/{|}|,|'/g, "")
.trim()
.split("\n")
.filter((v) => !v.includes("modified:") && !v.includes("created:"))
);
versions.sort(
(v1, v2) => new Date(v1.trim().split(" ")[1]).getTime() - new Date(v2.trim().split(" ")[1]).getTime()
);
currentVersion = versions.pop().split(":")?.[0].trim();
} catch (e) {
currentVersion = (await run("npm version"))?.split(",")?.[0]?.split(":")?.[1]?.replace(/'/g, "")?.trim();
}
const currentVersion = await getPackageVersion(name, disableAutoSync);
if (currentVersion?.includes(prereleaseTag)) {
await run(
`npm --workspaces-update=false --no-git-tag-version version --allow-same-version ${currentVersion}`
Expand Down

0 comments on commit 5fd24f2

Please sign in to comment.