vlt
Version:
The vlt CLI
196 lines (191 loc) • 5.51 kB
JavaScript
var global = globalThis;
import {Buffer} from "node:buffer";
import {setTimeout,clearTimeout,setImmediate,clearImmediate,setInterval,clearInterval} from "node:timers";
import {createRequire as _vlt_createRequire} from "node:module";
var require = _vlt_createRequire(import.meta.filename);
import {
is,
isClean,
spawn
} from "./chunk-LEKM5RQR.js";
import {
commandUsage
} from "./chunk-2Y5QRO5N.js";
import "./chunk-3HSZY4YW.js";
import "./chunk-TJHWNOOA.js";
import "./chunk-264UXZEG.js";
import {
asError,
inc,
parse,
versionIncrements
} from "./chunk-X4RDKJKD.js";
import "./chunk-BNCOU5ZT.js";
import {
error
} from "./chunk-RV3EHS4P.js";
import "./chunk-AECDW3EJ.js";
// ../../src/cli-sdk/src/commands/version.ts
import { dirname } from "node:path";
import assert from "node:assert";
var isValidVersionIncrement = (value) => versionIncrements.includes(value);
var version = async (conf, increment, cwd, {
// Hardcode happy path options for now.
// TODO: make these config definitions
prereleaseId = "pre",
commit = true,
tag = true,
message = "v%s",
tagMessage = "v%s"
} = {}) => {
assert(
increment,
error("Version increment argument is required", {
code: "EUSAGE",
validOptions: versionIncrements
})
);
const manifestPath = conf.options.packageJson.find(cwd);
assert(
manifestPath,
error("No package.json found", {
code: "ENOENT",
path: cwd
})
);
const spawn2 = (args, opts) => spawn(args, { cwd: manifestDir, ...opts });
const manifestDir = dirname(manifestPath);
const manifest = conf.options.packageJson.read(manifestDir);
assert(
manifest.version,
error("No version field found in package.json", {
path: manifestPath
})
);
const oldVersion = manifest.version;
let newVersion;
const parsedIncrement = parse(increment);
if (parsedIncrement) {
newVersion = parsedIncrement.toString();
} else if (isValidVersionIncrement(increment)) {
const incrementType = increment;
try {
const result2 = inc(oldVersion, incrementType, prereleaseId);
newVersion = result2.toString();
} catch (err) {
throw error(
`Failed to increment version from ${oldVersion} with ${increment}`,
{ version: oldVersion, wanted: increment, cause: err }
);
}
} else {
throw error(
`Invalid version increment: ${increment}. Must be a valid semver version or one of: major, minor, patch, premajor, preminor, prepatch, prerelease`,
{
found: increment,
validOptions: versionIncrements
}
);
}
manifest.version = newVersion;
conf.options.packageJson.write(manifestDir, manifest);
const result = {
oldVersion,
newVersion,
dir: manifestDir
};
if (
/* c8 ignore next -- commit and tag are always true for now */
(commit || tag) && await is({ cwd: conf.options.projectRoot })
) {
if (!await isClean({ cwd: conf.options.projectRoot })) {
try {
const gitResult = await spawn2(["diff", "--name-only", "HEAD"]);
const changedFiles = gitResult.stdout.trim().split("\n").filter(Boolean);
const nonPackageJsonChanges = changedFiles.filter(
(file) => file !== "package.json"
);
assert(
nonPackageJsonChanges.length === 0,
error(
"Git working directory not clean. Please commit or stash your changes first.",
{ found: nonPackageJsonChanges }
)
);
} catch (err) {
throw error(
"Git working directory not clean. Please commit or stash your changes first.",
asError(err)
);
}
}
if (commit) {
try {
const files = ["package.json"];
await spawn2(["add", ...files]);
await spawn2([
"commit",
"-m",
message.replace("%s", newVersion)
]);
result.committed = files;
} catch (err) {
throw error("Failed to commit version changes", {
version: newVersion,
cause: err
});
}
}
if (tag) {
try {
const tagName = `v${newVersion}`;
await spawn2([
"tag",
tagName,
"-m",
tagMessage.replace("%s", newVersion)
]);
result.tag = tagName;
} catch (err) {
throw error("Failed to create git tag", {
version: newVersion,
cause: err
});
}
}
}
return result;
};
var usage = () => {
return commandUsage({
command: "version",
usage: "[<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease]",
description: `Bump a package's version.
Run in a package directory to bump the version and write the new data back to package.json.
The \`<newversion>\` argument should be a valid semver string or a valid increment type (one of patch, minor, major, prepatch, preminor, premajor, prerelease).
If run in a git repository, it will also create a version commit and tag.`
});
};
var views = {
json: (result) => result,
human: (result) => {
let output = `v${result.newVersion}`;
if (result.committed) {
output += ` +commit`;
}
if (result.tag) {
output += ` +tag`;
}
return output;
}
};
var command = async (conf) => {
const { positionals } = conf;
return version(conf, positionals[0], process.cwd());
};
export {
command,
usage,
views
};
//# sourceMappingURL=version-DTMNJJPD.js.map