studiocms
Version:
Astro Native CMS for AstroDB. Built from the ground up by the Astro community.
55 lines (54 loc) • 1.78 kB
JavaScript
import { detectPackageManager } from "@withstudiocms/cli-kit/context";
import { cancelMessage, getName } from "@withstudiocms/cli-kit/messages";
import { cancel, isCancel } from "@withstudiocms/effect/clack";
import chalk from "chalk";
import { Context, Effect, genLogger, Layer, Option } from "../../effect.js";
class CliContext extends Context.Tag("CliContext")() {
static makeLayer = (context) => Layer.succeed(this, this.of(context));
static makeProvide = (context) => Effect.provide(this.makeLayer(context));
}
const genContext = genLogger("studiocms/cli/utils/context.genContext")(function* () {
const packageManager = yield* Effect.orElse(
Effect.try(() => {
const manager = detectPackageManager();
if (manager) return manager;
throw new Error("Failed to detect package manager, falling back to npm");
}),
() => Effect.succeed("npm")
);
const cwd = process.cwd();
const username = yield* Effect.tryPromise(() => getName());
const exit = (code) => Effect.try(() => process.exit(code)).pipe(Effect.catchAll(() => Effect.succeed(void 0)));
const context = {
chalk,
cwd,
packageManager,
username,
tasks: [],
pCancel: Effect.fn(function* (val) {
const shouldCancel = yield* isCancel(val);
if (shouldCancel) {
yield* cancel(cancelMessage);
return yield* exit(0);
}
}),
pOnCancel: Effect.fn(function* () {
yield* cancel(cancelMessage);
return yield* exit(0);
}),
exit
};
return context;
});
const parseDebug = Effect.fn(function* (debugOpt) {
if (typeof debugOpt === "boolean") return debugOpt;
return Option.match(debugOpt, {
onNone: () => false,
onSome: (v) => v
});
});
export {
CliContext,
genContext,
parseDebug
};