mlld
Version:
mlld: llm scripting language
226 lines (224 loc) • 7.61 kB
JavaScript
import { __name } from './chunk-NJQWMXLH.mjs';
// core/registry/utils/ModuleNeeds.ts
function normalizeModuleNeeds(rawNeeds, legacyDetails) {
const normalized = {
runtimes: [],
tools: [],
packages: {}
};
const addRuntime = /* @__PURE__ */ __name((candidate) => {
if (!candidate.name) {
return;
}
if (!normalized.runtimes.find((existing2) => existing2.name === candidate.name)) {
normalized.runtimes.push(candidate);
return;
}
const existing = normalized.runtimes.find((existing2) => existing2.name === candidate.name);
if (existing && !existing.specifier && candidate.specifier) {
existing.specifier = candidate.specifier;
existing.raw = candidate.raw;
}
}, "addRuntime");
const addTool = /* @__PURE__ */ __name((candidate) => {
if (!candidate.name) {
return;
}
if (!normalized.tools.find((existing2) => existing2.name === candidate.name)) {
normalized.tools.push(candidate);
return;
}
const existing = normalized.tools.find((existing2) => existing2.name === candidate.name);
if (existing && !existing.specifier && candidate.specifier) {
existing.specifier = candidate.specifier;
existing.raw = candidate.raw;
}
}, "addTool");
const addPackages = /* @__PURE__ */ __name((ecosystem, packages) => {
if (!ecosystem) {
return;
}
const bucket = normalized.packages[ecosystem] || [];
for (const pkg of packages) {
if (!pkg.name) {
continue;
}
if (!bucket.find((existing) => existing.name === pkg.name && existing.specifier === pkg.specifier)) {
bucket.push(pkg);
}
}
normalized.packages[ecosystem] = bucket;
}, "addPackages");
const handleRuntimeEntry = /* @__PURE__ */ __name((entry) => {
if (typeof entry === "string") {
addRuntime(parseVersionSpecifier(entry));
} else if (entry && typeof entry === "object") {
const name = String(entry.name || "");
const specifier = entry.specifier ?? entry.version;
if (name) {
addRuntime({
name,
specifier: typeof specifier === "string" && specifier.length > 0 ? specifier : void 0,
raw: formatVersionSpecifier(name, specifier)
});
}
}
}, "handleRuntimeEntry");
const handleToolEntry = /* @__PURE__ */ __name((entry) => {
if (typeof entry === "string") {
addTool(parseVersionSpecifier(entry));
} else if (entry && typeof entry === "object") {
const name = String(entry.name || "");
const specifier = entry.specifier ?? entry.version;
if (name) {
addTool({
name,
specifier: typeof specifier === "string" && specifier.length > 0 ? specifier : void 0,
raw: formatVersionSpecifier(name, specifier)
});
}
}
}, "handleToolEntry");
const handlePackageList = /* @__PURE__ */ __name((ecosystem, value) => {
if (Array.isArray(value)) {
const packages = value.map((item) => parseVersionSpecifier(String(item)));
addPackages(ecosystem, packages);
return;
}
if (value && typeof value === "object") {
const packages = Object.values(value).map((item) => parseVersionSpecifier(String(item)));
addPackages(ecosystem, packages);
}
}, "handlePackageList");
if (Array.isArray(rawNeeds)) {
for (const entry of rawNeeds) {
handleRuntimeEntry(entry);
}
} else if (typeof rawNeeds === "string") {
handleRuntimeEntry(rawNeeds);
} else if (rawNeeds && typeof rawNeeds === "object") {
const structuredNeeds = rawNeeds;
if (structuredNeeds.runtimes) {
for (const runtime of structuredNeeds.runtimes) {
handleRuntimeEntry(runtime);
}
}
if (structuredNeeds.tools) {
for (const tool of structuredNeeds.tools) {
handleToolEntry(tool);
}
}
if (structuredNeeds.packages) {
for (const [ecosystem, packageList] of Object.entries(structuredNeeds.packages)) {
handlePackageList(ecosystem, packageList);
}
}
}
if (legacyDetails) {
if (legacyDetails.js) {
inheritLegacyRuntimeDetail("js", legacyDetails.js, addRuntime, addPackages, addTool);
}
if (legacyDetails.node) {
inheritLegacyRuntimeDetail("node", legacyDetails.node, addRuntime, addPackages, addTool);
}
if (legacyDetails.py) {
inheritLegacyRuntimeDetail("python", legacyDetails.py, addRuntime, addPackages, addTool);
}
if (legacyDetails.sh) {
inheritLegacyRuntimeDetail("sh", legacyDetails.sh, addRuntime, addPackages, addTool);
}
}
return normalized;
}
__name(normalizeModuleNeeds, "normalizeModuleNeeds");
function parseVersionSpecifier(raw) {
const trimmed = raw.trim();
if (!trimmed) {
return { name: "", raw: "" };
}
if (trimmed.startsWith("@")) {
const parts = trimmed.split("@");
if (parts.length > 2) {
const spec = parts.pop();
const scopedName = parts.join("@");
if (spec) {
return {
name: scopedName,
specifier: spec,
raw: trimmed
};
}
}
}
const atIndex = trimmed.lastIndexOf("@");
if (atIndex > 0 && atIndex < trimmed.length - 1) {
const name = trimmed.slice(0, atIndex);
const specifier = trimmed.slice(atIndex + 1);
return { name, specifier, raw: trimmed };
}
return { name: trimmed, raw: trimmed };
}
__name(parseVersionSpecifier, "parseVersionSpecifier");
function formatVersionSpecifier(name, specifier) {
if (!name) {
return "";
}
if (typeof specifier === "string" && specifier.length > 0) {
return `${name}@${specifier}`;
}
return name;
}
__name(formatVersionSpecifier, "formatVersionSpecifier");
function moduleNeedsToSerializable(needs) {
const result = {};
if (needs.runtimes.length > 0) {
result.runtimes = needs.runtimes.map((runtime) => ({
name: runtime.name,
specifier: runtime.specifier,
raw: runtime.raw
}));
}
if (needs.tools.length > 0) {
result.tools = needs.tools.map((tool) => ({
name: tool.name,
specifier: tool.specifier,
raw: tool.raw
}));
}
const packageEntries = Object.entries(needs.packages);
if (packageEntries.length > 0) {
result.packages = packageEntries.reduce((acc, [ecosystem, packages]) => {
acc[ecosystem] = packages.map((pkg) => ({
name: pkg.name,
specifier: pkg.specifier,
raw: pkg.raw
}));
return acc;
}, {});
}
return result;
}
__name(moduleNeedsToSerializable, "moduleNeedsToSerializable");
function inheritLegacyRuntimeDetail(name, detail, addRuntime, addPackages, addTool) {
if (detail.node && (name === "node" || name === "js")) {
addRuntime({ name: "node", specifier: detail.node, raw: formatVersionSpecifier("node", detail.node) });
}
if (detail.python && name === "python") {
addRuntime({ name: "python", specifier: detail.python, raw: formatVersionSpecifier("python", detail.python) });
}
if (detail.shell && name === "sh") {
addTool({ name: detail.shell, raw: detail.shell });
}
if (detail.packages && detail.packages.length > 0) {
addPackages(name, detail.packages.map((pkg) => parseVersionSpecifier(pkg)));
}
if (detail.commands && detail.commands.length > 0) {
for (const command of detail.commands) {
addTool(parseVersionSpecifier(command));
}
}
}
__name(inheritLegacyRuntimeDetail, "inheritLegacyRuntimeDetail");
export { moduleNeedsToSerializable, normalizeModuleNeeds };
//# sourceMappingURL=chunk-R42WGS45.mjs.map
//# sourceMappingURL=chunk-R42WGS45.mjs.map