v0.5.1
This commit is contained in:
@@ -2,6 +2,12 @@ const crypto = require("crypto");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const STATUS_OK = "ok";
|
||||
const STATUS_SUS = "sus";
|
||||
const STATUS_INHERITED_SUS = "inherited-sus";
|
||||
const VALID_STATUSES = new Set([STATUS_OK, STATUS_SUS, STATUS_INHERITED_SUS]);
|
||||
const SUS_RESULT = Symbol("optimus.sus-result");
|
||||
|
||||
function isPlainObject(value) {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
@@ -57,10 +63,42 @@ function getSourceHash(sources) {
|
||||
return sourceHash;
|
||||
}
|
||||
|
||||
function buildNode(payload, sources = {}) {
|
||||
function getSourceRef(sources) {
|
||||
const sourceRef = {};
|
||||
for (const [name, source] of Object.entries(sources)) {
|
||||
if (source && typeof source === "object" && source.artifact) sourceRef[name] = source.artifact;
|
||||
}
|
||||
return sourceRef;
|
||||
}
|
||||
|
||||
function statusOf(node) {
|
||||
if (!node || typeof node !== "object" || node.status === undefined) return STATUS_OK;
|
||||
if (!VALID_STATUSES.has(node.status)) throw new Error(`Unknown artifact status: ${JSON.stringify(node.status)}`);
|
||||
return node.status;
|
||||
}
|
||||
|
||||
function inheritedStatus(sources = {}) {
|
||||
return Object.values(sources).some(source => statusOf(source) !== STATUS_OK)
|
||||
? STATUS_INHERITED_SUS
|
||||
: STATUS_OK;
|
||||
}
|
||||
|
||||
function sus(payload) {
|
||||
return { [SUS_RESULT]: true, payload };
|
||||
}
|
||||
|
||||
function unwrapResult(result) {
|
||||
return result && typeof result === "object" && result[SUS_RESULT] === true
|
||||
? { payload: result.payload, localSus: true }
|
||||
: { payload: result, localSus: false };
|
||||
}
|
||||
|
||||
function buildNode(result, sources = {}) {
|
||||
const { payload, localSus } = unwrapResult(result);
|
||||
return {
|
||||
sourceHash: getSourceHash(sources),
|
||||
hash: hashPayload(payload),
|
||||
status: localSus ? STATUS_SUS : inheritedStatus(sources),
|
||||
payload,
|
||||
};
|
||||
}
|
||||
@@ -70,16 +108,131 @@ function buildBinaryNode(filePath, sources = {}) {
|
||||
return {
|
||||
sourceHash: getSourceHash(sources),
|
||||
hash: crypto.createHash("sha256").update(bytes).digest("hex"),
|
||||
status: inheritedStatus(sources),
|
||||
payloadBin: filePath,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function ensureDir(filename) {
|
||||
fs.mkdirSync(path.dirname(filename), { recursive: true });
|
||||
}
|
||||
|
||||
function writeJsonAtomic(filename, value) {
|
||||
ensureDir(filename);
|
||||
const tempFile = `${filename}.${process.pid}.${crypto.randomUUID()}.tmp`;
|
||||
fs.writeFileSync(tempFile, JSON.stringify(value, null, 2) + "\n", "utf8");
|
||||
try { fs.renameSync(tempFile, filename); }
|
||||
catch (error) {
|
||||
if (!fs.existsSync(filename)) throw error;
|
||||
fs.rmSync(filename, { force: true });
|
||||
fs.renameSync(tempFile, filename);
|
||||
}
|
||||
}
|
||||
|
||||
function readArtifact(filename) {
|
||||
if (!fs.existsSync(filename)) return null;
|
||||
try { return JSON.parse(fs.readFileSync(filename, "utf8")); }
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
function sameSourceHash(node, sources = {}) {
|
||||
if (!node || typeof node !== "object" || !node.hash || !node.sourceHash) return false;
|
||||
const expected = getSourceHash(sources);
|
||||
const actualKeys = Object.keys(node.sourceHash).sort();
|
||||
const expectedKeys = Object.keys(expected).sort();
|
||||
return actualKeys.length === expectedKeys.length
|
||||
&& expectedKeys.every((name, index) => name === actualKeys[index] && node.sourceHash[name] === expected[name]);
|
||||
}
|
||||
|
||||
function reconcileArtifactStatus(filename, node, sources = {}) {
|
||||
if (!node || typeof node !== "object") return node;
|
||||
const current = statusOf(node);
|
||||
const expected = current === STATUS_SUS ? STATUS_SUS : inheritedStatus(sources);
|
||||
if (current === expected) return node;
|
||||
const updated = { ...node, status: expected };
|
||||
writeJsonAtomic(filename, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
async function resolveArtifact(filename, sources, producer) {
|
||||
const existing = readArtifact(filename);
|
||||
if (existing && sameSourceHash(existing, sources)) return reconcileArtifactStatus(filename, existing, sources);
|
||||
const result = await producer();
|
||||
const node = buildNode(result, sources);
|
||||
node.artifact = path.relative(process.cwd(), filename).replace(/\\/g, "/");
|
||||
const sourceRef = getSourceRef(sources);
|
||||
if (Object.keys(sourceRef).length > 0) node.sourceRef = sourceRef;
|
||||
writeJsonAtomic(filename, node);
|
||||
return node;
|
||||
}
|
||||
|
||||
function invalidateArtifact(filename) {
|
||||
if (!fs.existsSync(filename)) return false;
|
||||
fs.rmSync(filename, { recursive: true, force: true });
|
||||
return true;
|
||||
}
|
||||
|
||||
function fileNode(filename) {
|
||||
const bytes = fs.readFileSync(filename);
|
||||
const ref = path.relative(process.cwd(), filename).replace(/\\/g, "/");
|
||||
return {
|
||||
artifact: ref,
|
||||
sourceHash: {},
|
||||
hash: crypto.createHash("sha256").update(bytes).digest("hex"),
|
||||
status: STATUS_OK,
|
||||
payload: ref,
|
||||
};
|
||||
}
|
||||
|
||||
function codeNode(label, value) {
|
||||
const code = typeof value === "function" ? value.toString() : String(value);
|
||||
return buildNode({ label, code });
|
||||
}
|
||||
|
||||
function initialize(projectRoot = process.cwd(), options = {}) {
|
||||
const settingOverlays = options.settingOverlays || [];
|
||||
if (!Array.isArray(settingOverlays)) throw new Error("options.settingOverlays must be an array");
|
||||
const settings = loadSettings(projectRoot, settingOverlays);
|
||||
const registry = new Map();
|
||||
let artifactOutcome;
|
||||
let relitigateSus = false;
|
||||
|
||||
const presentation = settings.get("presentation") || {};
|
||||
const showOnEnsure = presentation.showOnEnsure === true;
|
||||
const showOnBuild = presentation.showOnBuild !== false;
|
||||
|
||||
function displayArtifactLabel(value) {
|
||||
if (!value) return "artifact";
|
||||
if (!path.isAbsolute(value)) return String(value);
|
||||
const relative = path.relative(projectRoot, value).replace(/\\/g, "/");
|
||||
return relative || path.basename(value);
|
||||
}
|
||||
|
||||
function reportEnsure(value) {
|
||||
if (showOnEnsure) console.log(`Ensuring artifact: ${displayArtifactLabel(value)}`);
|
||||
}
|
||||
|
||||
function reportBuild(value) {
|
||||
if (showOnBuild) console.log(`Building artifact: ${displayArtifactLabel(value)}`);
|
||||
}
|
||||
|
||||
async function projectResolveArtifact(filename, sources, producer) {
|
||||
reportEnsure(filename);
|
||||
const existing = readArtifact(filename);
|
||||
if (existing && sameSourceHash(existing, sources)) {
|
||||
const reconciled = reconcileArtifactStatus(filename, existing, sources);
|
||||
if (!(relitigateSus && statusOf(reconciled) === STATUS_SUS)) return reconciled;
|
||||
}
|
||||
reportBuild(filename);
|
||||
const result = await producer();
|
||||
const node = buildNode(result, sources);
|
||||
node.artifact = path.relative(process.cwd(), filename).replace(/\\/g, "/");
|
||||
const sourceRef = getSourceRef(sources);
|
||||
if (Object.keys(sourceRef).length > 0) node.sourceRef = sourceRef;
|
||||
writeJsonAtomic(filename, node);
|
||||
return node;
|
||||
}
|
||||
|
||||
const utils = {
|
||||
loadModule(name) {
|
||||
@@ -109,7 +262,23 @@ function initialize(projectRoot = process.cwd(), options = {}) {
|
||||
hashPayload,
|
||||
buildNode,
|
||||
buildBinaryNode,
|
||||
resolveArtifact: projectResolveArtifact,
|
||||
readArtifact,
|
||||
sameSourceHash,
|
||||
statusOf,
|
||||
sus,
|
||||
reconcileArtifactStatus,
|
||||
shouldRelitigateSus(node) {
|
||||
return relitigateSus && statusOf(node) === STATUS_SUS;
|
||||
},
|
||||
invalidateArtifact,
|
||||
fileNode,
|
||||
codeNode,
|
||||
|
||||
// UI/diagnostic reporting only. These must never participate in
|
||||
// artifact identity, validity, ancestry, or dependency resolution.
|
||||
reportEnsure,
|
||||
reportBuild,
|
||||
setArtifactOutcome(value) {
|
||||
artifactOutcome = `${value ?? ""}`.trim() || undefined;
|
||||
},
|
||||
@@ -127,7 +296,8 @@ function initialize(projectRoot = process.cwd(), options = {}) {
|
||||
|
||||
for (const name of registry.keys()) utils.loadModule(name);
|
||||
|
||||
function buildArtifact(name) {
|
||||
function buildArtifact(name, options = {}) {
|
||||
if (!options || typeof options !== "object" || Array.isArray(options)) throw new Error("buildArtifact options must be an object");
|
||||
const artifacts = utils.getSettings("artifacts");
|
||||
const artifact = artifacts[name];
|
||||
if (!artifact) throw new Error(`Unknown artifact: ${name}`);
|
||||
@@ -143,36 +313,43 @@ function initialize(projectRoot = process.cwd(), options = {}) {
|
||||
const params = artifact.params === undefined ? [] : artifact.params;
|
||||
if (!Array.isArray(params)) throw new Error(`Artifact "${name}" params must be an array`);
|
||||
|
||||
const previousRelitigateSus = relitigateSus;
|
||||
relitigateSus = previousRelitigateSus || options.relitigateSus === true;
|
||||
artifactOutcome = undefined;
|
||||
console.log(`Ensuring artifact: ${name}`);
|
||||
reportEnsure(name);
|
||||
const restore = () => { relitigateSus = previousRelitigateSus; };
|
||||
try {
|
||||
const result = builder(...params);
|
||||
if (result && typeof result.then === "function") {
|
||||
return result.then(value => {
|
||||
console.log(` ${artifactOutcome ?? "completed — success"}`);
|
||||
if (showOnEnsure && artifactOutcome) console.log(` ${artifactOutcome}`);
|
||||
artifactOutcome = undefined;
|
||||
restore();
|
||||
return value;
|
||||
}, error => {
|
||||
console.error(" failed");
|
||||
console.error(`Artifact failed: ${name}`);
|
||||
artifactOutcome = undefined;
|
||||
restore();
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
console.log(` ${artifactOutcome ?? "completed — success"}`);
|
||||
if (showOnEnsure && artifactOutcome) console.log(` ${artifactOutcome}`);
|
||||
artifactOutcome = undefined;
|
||||
restore();
|
||||
return result;
|
||||
}
|
||||
catch (error) {
|
||||
console.error(" failed");
|
||||
console.error(`Artifact failed: ${name}`);
|
||||
artifactOutcome = undefined;
|
||||
restore();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function buildAll() {
|
||||
async function buildAll(options = {}) {
|
||||
const artifacts = utils.getSettings("artifacts");
|
||||
for (const name of Object.keys(artifacts)) await buildArtifact(name);
|
||||
for (const name of Object.keys(artifacts)) await buildArtifact(name, options);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -182,10 +359,33 @@ function initialize(projectRoot = process.cwd(), options = {}) {
|
||||
hashPayload,
|
||||
buildNode,
|
||||
buildBinaryNode,
|
||||
resolveArtifact: projectResolveArtifact,
|
||||
readArtifact,
|
||||
sameSourceHash,
|
||||
statusOf,
|
||||
sus,
|
||||
reconcileArtifactStatus,
|
||||
invalidateArtifact,
|
||||
fileNode,
|
||||
codeNode,
|
||||
buildArtifact,
|
||||
buildAll,
|
||||
modules: [...registry.keys()],
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { initialize, hashPayload, buildNode, buildBinaryNode };
|
||||
module.exports = {
|
||||
initialize,
|
||||
hashPayload,
|
||||
buildNode,
|
||||
buildBinaryNode,
|
||||
resolveArtifact,
|
||||
readArtifact,
|
||||
sameSourceHash,
|
||||
statusOf,
|
||||
sus,
|
||||
reconcileArtifactStatus,
|
||||
invalidateArtifact,
|
||||
fileNode,
|
||||
codeNode,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user