Better version metadata string
This commit is contained in:
1 parent
e6c8d4f3f2
commit
f8e6e93602
1 file changed
+27
-8
+27
-8
@@ -2,6 +2,7 @@ import path from "node:path";
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import { createHash } from "node:crypto";
|
import { createHash } from "node:crypto";
|
||||||
import { promisify } from "node:util";
|
import { promisify } from "node:util";
|
||||||
|
import { exec } from "node:child_process";
|
||||||
import * as esbuild from "esbuild";
|
import * as esbuild from "esbuild";
|
||||||
import { optimize } from "svgo";
|
import { optimize } from "svgo";
|
||||||
import * as xmlParser from "../lib/xml-parser.ts";
|
import * as xmlParser from "../lib/xml-parser.ts";
|
||||||
@@ -20,12 +21,9 @@ const fsAsync = {
|
|||||||
mkdir: promisify(fs.mkdir),
|
mkdir: promisify(fs.mkdir),
|
||||||
};
|
};
|
||||||
|
|
||||||
const MODE = process.env["NODE_ENV"] || "production";
|
const execAsync = promisify(exec);
|
||||||
|
|
||||||
const BUILD_METADATA = new Date()
|
const MODE = process.env["NODE_ENV"] || "production";
|
||||||
.toISOString()
|
|
||||||
.split(".")[0]
|
|
||||||
.replace(/[^0-9]/g, "");
|
|
||||||
|
|
||||||
const INPUT_DIR = process.cwd();
|
const INPUT_DIR = process.cwd();
|
||||||
const OUTPUT_DIR = path.join(INPUT_DIR, "dist");
|
const OUTPUT_DIR = path.join(INPUT_DIR, "dist");
|
||||||
@@ -140,17 +138,38 @@ function generateSymbol(id: string, svgStr: string): string {
|
|||||||
return `<symbol id="icon-${id}" ${viewBox}>${symbolBody}</symbol>`;
|
return `<symbol id="icon-${id}" ${viewBox}>${symbolBody}</symbol>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getBuildMetadata(): Promise<string> {
|
||||||
|
const date = new Date().toISOString().slice(2, 10).replaceAll("-", "");
|
||||||
|
|
||||||
|
const [commit, diff, newFiles] = await Promise.all([
|
||||||
|
execAsync("git rev-parse HEAD"),
|
||||||
|
execAsync("git diff HEAD"),
|
||||||
|
execAsync("git ls-files --others --exclude-standard"),
|
||||||
|
]).then((res) => res.map((r) => r.stdout.trim()));
|
||||||
|
|
||||||
|
if (!diff && !newFiles) return date + commit.slice(0, 4);
|
||||||
|
|
||||||
|
const hash = createHash("md5");
|
||||||
|
hash.update(commit).update(diff).update(newFiles);
|
||||||
|
for (const file of newFiles.split("\n").filter((f) => f))
|
||||||
|
hash.update(await fsAsync.readFile(file));
|
||||||
|
return date + hash.digest("hex").slice(0, 4);
|
||||||
|
}
|
||||||
|
|
||||||
async function init(): Promise<void> {
|
async function init(): Promise<void> {
|
||||||
const [packageJsonFile, npmShrinkwrapFile] = await Promise.all([
|
const [buildMetadata, packageJsonFile, npmShrinkwrapFile] = await Promise.all(
|
||||||
|
[
|
||||||
|
getBuildMetadata(),
|
||||||
fsAsync.readFile(path.join(INPUT_DIR, "package.json")),
|
fsAsync.readFile(path.join(INPUT_DIR, "package.json")),
|
||||||
fsAsync.readFile(path.join(INPUT_DIR, "npm-shrinkwrap.json")),
|
fsAsync.readFile(path.join(INPUT_DIR, "npm-shrinkwrap.json")),
|
||||||
]);
|
],
|
||||||
|
);
|
||||||
|
|
||||||
const packageJson = JSON.parse(packageJsonFile.toString());
|
const packageJson = JSON.parse(packageJsonFile.toString());
|
||||||
delete packageJson["devDependencies"];
|
delete packageJson["devDependencies"];
|
||||||
delete packageJson["private"];
|
delete packageJson["private"];
|
||||||
delete packageJson["scripts"];
|
delete packageJson["scripts"];
|
||||||
packageJson["version"] = `${packageJson["version"]}+${BUILD_METADATA}`;
|
packageJson["version"] = `${packageJson["version"]}+${buildMetadata}`;
|
||||||
|
|
||||||
const npmShrinkwrap = JSON.parse(npmShrinkwrapFile.toString());
|
const npmShrinkwrap = JSON.parse(npmShrinkwrapFile.toString());
|
||||||
npmShrinkwrap["version"] = packageJson["version"];
|
npmShrinkwrap["version"] = packageJson["version"];
|
||||||
|
|||||||
Reference in new issue
Block a user