diff --git a/packages/automatic-versioning/src/index.js b/packages/automatic-versioning/src/index.js index 19e56e2..f55bf00 100644 --- a/packages/automatic-versioning/src/index.js +++ b/packages/automatic-versioning/src/index.js @@ -20,7 +20,8 @@ program ) .option("-r, --root ", "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"), @@ -54,6 +55,7 @@ const run = async () => { opts.name, opts.skipCommit, opts.recursive, + opts.disableAutoSync, opts.prereleaseTag, opts.prereleaseBranch, opts.ignorePrefixes diff --git a/packages/automatic-versioning/src/types/default.js b/packages/automatic-versioning/src/types/default.js index 30e037c..5aa725d 100644 --- a/packages/automatic-versioning/src/types/default.js +++ b/packages/automatic-versioning/src/types/default.js @@ -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); @@ -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}`