azion
Version:
Azion Packages for Edge Computing.
236 lines (235 loc) • 5.88 kB
JavaScript
import { existsSync as C, mkdirSync as _, readdirSync as A, lstatSync as T, copyFileSync as $, promises as O, readFileSync as B } from "fs";
import R, { resolve as w, join as b } from "path";
import { spawn as F, exec as j } from "child_process";
import h from "signale";
function x(e, n, s) {
const i = w(e), t = w(n);
if (i === t)
return;
C(t) || _(t, { recursive: !0 });
const r = A(i);
for (const o of r) {
const f = b(i, o), g = b(t, o);
s && s.length > 0 && s.includes(o) || g.startsWith(f) || (T(f).isDirectory() ? x(f, g) : $(f, g));
}
}
async function D(e, { scope: n = "Azion", verbose: s = !1, interactive: i = !1 } = {}) {
const t = new h.Signale({
interactive: !0,
scope: n
});
return new Promise((r, o) => {
const f = e.split(" "), g = f.shift();
let m = "", v = "";
const y = F(g, f, {
shell: !0,
stdio: i ? "inherit" : "pipe"
});
i || (y.stdout?.on("data", (a) => {
const p = a.toString();
m += p, s && t.info(p);
}), y.stderr?.on("data", (a) => {
const p = a.toString();
v += p, s && (p.toLowerCase().includes("error") ? t.error(p) : t.info(p));
}), y.on("error", (a) => {
o(a);
})), y.on("close", (a) => {
a === 0 ? r({ stdout: m, stderr: v }) : o(new Error(`Command '${e}' failed with code ${a}`));
}), y.on("exit", (a) => {
a === 0 ? r({ stdout: m, stderr: v }) : o(new Error(`Command '${e}' failed with code ${a}`));
});
});
}
const E = process.env.CLEAN_OUTPUT_MODE === "true", L = {
displayScope: !1,
displayBadge: !1,
displayDate: !1,
displayFilename: !1,
displayLabel: !1,
displayTimestamp: !1,
underlineLabel: !1,
underlineMessage: !1,
underlinePrefix: !1,
underlineSuffix: !1,
uppercaseLabel: !1
}, u = (e = {}) => {
const n = new h.Signale({ ...e });
return E && n.config(L), Object.assign(n, {
breakInteractiveChain: () => console.log()
});
}, c = {
deployed: {
badge: "🚀",
color: "green",
label: "forged",
logLevel: "info"
},
option: {
badge: "📦",
color: "green",
label: "",
logLevel: "info"
}
}, l = (e = "Azion") => new h.Signale({
interactive: !1,
scope: e,
types: c
});
E && l().config(L);
const S = (e = "Azion") => ({
...l(e),
interactive: {
...u({
interactive: !0,
scope: [e],
types: c
})
},
server: {
...l(e).scope(e, "Server"),
interactive: u({
interactive: !0,
scope: [e, "Server"],
types: c
})
},
runtime: {
...l(e).scope(e, "Runtime"),
interactive: u({
interactive: !0,
scope: [e, "Runtime"],
types: c
})
},
prebuild: {
...l(e).scope(e, "Pre-Build"),
interactive: u({
interactive: !0,
scope: [e, "Pre-Build"],
types: c
})
},
build: {
...l(e).scope(e, "Build"),
interactive: u({
interactive: !0,
scope: [e, "Build"],
types: c
})
},
postbuild: {
...l(e).scope(e, "Post-Build"),
interactive: u({
interactive: !0,
scope: [e, "Post-build"],
types: c
})
},
storage: {
...l(e).scope(e, "Storage"),
interactive: u({
interactive: !0,
scope: [e, "Storage"],
types: c
})
},
bindings: {
...l(e).scope(e, "Bindings"),
interactive: u({
interactive: !0,
scope: [e, "Bindings"],
types: c
})
},
manifest: {
...l(e).scope(e, "IaC"),
interactive: u({
interactive: !0,
scope: [e, "IaC"],
types: c
})
}
}), I = {
...S(),
globalScope: (e) => ({ ...S(e) })
}, k = process.platform === "win32";
function M(e = import.meta.url, n = "bundler") {
const s = new RegExp(`(.*${n})(.*)`), i = e.match(s);
let t = i ? i[1] : "";
if (e.includes("edge-functions") && n === "bundler") {
const r = new RegExp("(.*edge-functions)(.*)"), o = e.match(r);
t = o ? o[1] : t;
}
return k && (t = new URL(t).pathname, t.startsWith("/") && (t = t.slice(1))), k || (t = t.replace("file://", "")), t;
}
const U = {
yarn: {
fileName: "yarn.lock",
pmType: "yarn"
},
npm: {
fileName: "package-lock.json",
pmType: "npm"
},
pnpm: {
fileName: "pnpm-lock.yaml",
pmType: "pnpm"
}
}, z = "npx", J = async (e) => {
try {
return await O.access(e), !0;
} catch {
return !1;
}
}, d = /* @__PURE__ */ new Map(), P = async (e) => {
const n = `has_global_${e}`;
return d.has(n) ? d.get(n) : new Promise((s) => {
j(`${e} --version`, (i, t) => {
const r = !i && /^\d+\.\d+\.\d+$/.test(t.trim());
d.set(n, r), s(r);
});
});
}, G = async (e = ".") => {
const n = `lockfile_${e}`;
if (d.has(n))
return d.get(n);
const i = (await Promise.all(
Object.values(U).map(async ({ fileName: r, pmType: o }) => ({
exists: await J(R.resolve(e, r)),
pmType: o
}))
)).find(({ exists: r }) => r), t = i ? i.pmType : null;
return d.set(n, t), t;
}, W = async ({ cwd: e } = {}) => {
const n = await G(e);
if (n)
return n;
const [s, i] = await Promise.all([P("yarn"), P("pnpm")]);
return s ? "yarn" : i ? "pnpm" : z;
};
function K(e) {
if (!e || e === "" || e === " ")
throw new Error("Invalid package name!");
const n = b(process.cwd(), "package.json"), s = B(n, "utf8"), i = JSON.parse(s);
if (!i.dependencies || !i.dependencies[e])
throw new Error(`'${e}' not detected in project dependencies!`);
return i.dependencies[e];
}
const H = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
copyDirectory: x,
exec: D,
feedback: I,
getAbsoluteDirPath: M,
getPackageManager: W,
getPackageVersion: K
}, Symbol.toStringTag, { value: "Module" }));
export {
x as copyDirectory,
D as exec,
I as feedback,
M as getAbsoluteDirPath,
W as getPackageManager,
K as getPackageVersion,
H as n
};