mkver
Version:
Node.js access to your app's version and release metadata
203 lines (201 loc) • 7.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fmtYMDHMS = fmtYMDHMS;
exports.mkver = mkver;
const node_child_process_1 = require("node:child_process");
const promises_1 = require("node:fs/promises");
const node_path_1 = require("node:path");
const node_process_1 = require("node:process");
const node_util_1 = require("node:util");
const semver_1 = __importDefault(require("semver"));
const execFileP = (0, node_util_1.promisify)(node_child_process_1.execFile);
function notBlank(s) {
return s != null && String(s).trim().length > 0;
}
/**
* Recursively searches for package.json starting from the given directory,
* moving up the directory tree until found or reaching the filesystem root.
*
* @param dir - The directory to start searching from
* @returns Promise resolving to version and directory info, or undefined if not found
* @throws Error if package.json is found but has no version field
*/
async function findPackageVersion(dir) {
const path = (0, node_path_1.resolve)((0, node_path_1.join)(dir, "package.json"));
try {
const json = JSON.parse((await (0, promises_1.readFile)(path)).toString());
if (json != null) {
if (notBlank(json.version)) {
return { version: json.version, dir };
}
else {
throw new Error("No version field found in " + path);
}
}
}
catch (err) {
const parent = (0, node_path_1.resolve)((0, node_path_1.join)(dir, ".."));
if ((0, node_path_1.resolve)(dir) !== parent) {
return findPackageVersion(parent);
}
else {
throw err;
}
}
}
/**
* Retrieves the current git commit SHA from the specified directory.
*
* @param cwd - The working directory to run git command in
* @returns Promise resolving to the 40-character commit SHA
* @throws Error if git command fails or returns invalid SHA
*/
async function headSha(cwd) {
const gitSha = (await execFileP("git", ["rev-parse", "-q", "HEAD"], { cwd })).stdout
.toString()
.trim();
if (gitSha.length !== 40 || !/^[a-f0-9]{40}$/i.test(gitSha)) {
throw new Error("Invalid git SHA: " + gitSha);
}
else {
return gitSha;
}
}
/**
* Retrieves the commit date of the current git HEAD as a Date object.
*
* @param cwd - The working directory to run git command in
* @returns Promise resolving to the Date of the commit
* @throws Error if git command fails or returns invalid timestamp
*/
async function headUnixtime(cwd) {
const unixtimeStr = (await execFileP("git", ["log", "-1", "--pretty=format:%ct"], {
cwd,
})).stdout.toString();
const unixtime = parseInt(unixtimeStr);
const date = new Date(unixtime * 1000);
if (date > new Date() || date < new Date(2000, 0, 1)) {
throw new Error("Invalid commit timestamp: " + unixtime);
}
return date;
}
// NOT FOR GENERAL USE. Only works for positive values.
function pad2(i) {
const s = String(i);
return s.length >= 2 ? s : ("0" + s).slice(-2);
}
/**
* Appropriate for filenames: yMMddHHmmss
*/
function fmtYMDHMS(d) {
return (d.getFullYear() +
pad2(d.getMonth() + 1) +
pad2(d.getDate()) +
pad2(d.getHours()) +
pad2(d.getMinutes()) +
pad2(d.getSeconds()));
}
/**
* Renders version information into the appropriate format based on file extension.
* Supports TypeScript (.ts), ES modules (.mjs), and CommonJS (.js/.cjs) formats.
*
* @param o - The version information object to render
* @returns The formatted code as a string
* @throws Error if the file extension is not supported
*/
function renderVersionInfo(o) {
const msg = [];
const ext = o.path.ext.toLowerCase();
// .js maintains CommonJS for backward compatibility, .cjs is explicit CommonJS
const cjs = ext === ".js" || ext === ".cjs";
const mjs = ext === ".mjs";
const ts = ext === ".ts";
if (!cjs && !mjs && !ts) {
throw new Error(`Unsupported file extension: expected ${JSON.stringify(o.path)} to end in .ts, .js, .mjs, or .cjs`);
}
if (cjs) {
msg.push(`"use strict";`, `Object.defineProperty(exports, "__esModule", { value: true });`);
}
const parsed = semver_1.default.parse(o.version);
const fields = [];
for (const { field, value } of [
{ field: "version", value: o.version },
{ field: "versionMajor", value: parsed?.major },
{ field: "versionMinor", value: parsed?.minor },
{ field: "versionPatch", value: parsed?.patch },
{ field: "versionPrerelease", value: parsed?.prerelease },
{ field: "release", value: o.release },
{ field: "gitSha", value: o.gitSha },
{ field: "gitDate", value: o.gitDate },
]) {
if (value != null) {
fields.push(field);
const strVal = value instanceof Date
? `new Date(${value.getTime()})`
: JSON.stringify(value);
const ea = `${field} = ${strVal}`;
msg.push(cjs ? `exports.${ea};` : `export const ${ea};`);
}
}
if (ts || mjs) {
msg.push(`export default {${fields.join(",")}};`);
}
return msg.join("\n") + "\n";
}
/**
* Writes a file with version and release metadata to `output`
*
* @param output - The file to write to. Defaults to "./Version.ts". File format
* is determined by the file extension. Supported extensions are ".ts", ".js",
* ".mjs", and ".cjs".
* @returns The version and release metadata written to the file.
*/
async function mkver(output) {
if (output == null || output.trim().length === 0) {
output = "./Version.ts";
}
const file = (0, node_path_1.resolve)((0, node_path_1.normalize)(output));
const parsed = (0, node_path_1.parse)(file);
const v = await findPackageVersion(parsed.dir);
if (v == null) {
throw new Error("No package.json found in " + parsed.dir + " or parent directories");
}
const gitSha = await headSha(v.dir);
const gitDate = await headUnixtime(v.dir);
const versionInfo = {
path: parsed,
version: v.version,
release: `${v.version}+${fmtYMDHMS(gitDate)}`,
gitSha,
gitDate,
};
const buf = renderVersionInfo(versionInfo);
await (0, promises_1.mkdir)(parsed.dir, { recursive: true });
await (0, promises_1.writeFile)(file, buf);
return versionInfo;
}
async function main() {
const arg = node_process_1.argv[2] ?? "";
if (["--help", "-h"].includes(arg)) {
// Show them usage instructions:
console.log(`Usage: mkver [FILE]
Provides Node.js access to your app's version and release metadata.
With no FILE, default output is "./Version.ts".
See <https://github.com/photostructure/mkver> for more information.`);
}
else {
return mkver(arg);
}
}
// CommonJS entry point check
if (require.main === module) {
main().catch((error) => {
console.error("Failed: " + error);
(0, node_process_1.exit)(1);
});
}
//# sourceMappingURL=mkver.js.map