studiocms
Version:
Astro Native CMS for AstroDB. Built from the ground up by the Astro community.
85 lines (84 loc) • 3.19 kB
JavaScript
import { StudioCMSColorwayBg } from "@withstudiocms/cli-kit/colors";
import { label } from "@withstudiocms/cli-kit/messages";
import { intro, log, multiselect, tasks } from "@withstudiocms/effect/clack";
import { Cli, Effect, genLogger } from "../../effect.js";
import { CliContext, genContext, parseDebug } from "../utils/context.js";
import { intro as SCMS_Intro } from "../utils/intro.js";
import { buildDebugLogger } from "../utils/logger.js";
import { appendOptionsToSteps } from "../utils/types.js";
import { env } from "./steps/env.js";
import { next } from "./steps/next.js";
const debug = Cli.Options.boolean("debug").pipe(
Cli.Options.optional,
Cli.Options.withDefault(false),
Cli.Options.withDescription("Enable debug mode")
);
const dryRun = Cli.Options.boolean("dry-run").pipe(
Cli.Options.optional,
Cli.Options.withAlias("d"),
Cli.Options.withDescription("Dry run mode")
);
const exitIfEmpty = Effect.fn(function* (context, items, itemType) {
if (items.length === 0) {
yield* log.error(`No ${itemType} selected, exiting...`);
yield* context.exit(0);
}
});
const OptionToStepMap = {
env
};
const initCMD = Cli.Command.make(
"init",
{ debug, dryRun },
({ debug: rawDebug, dryRun: rawDryRun }) => genLogger("studiocms/cli/init")(function* () {
const [dry, context, debug2] = yield* Effect.all([
rawDryRun,
genContext,
parseDebug(rawDebug)
]);
const debugLogger = yield* buildDebugLogger(debug2);
const cliContext = CliContext.makeProvide(context);
yield* Effect.all([
debugLogger("Starting interactive CLI..."),
debugLogger(`Options: ${JSON.stringify({ debug: debug2, dry }, null, 2)}`),
debugLogger(`Context: ${JSON.stringify(context, null, 2)}`),
intro(
`${label("StudioCMS", StudioCMSColorwayBg, context.chalk.black)} Interactive CLI - initializing...`
)
]);
yield* SCMS_Intro(debug2).pipe(cliContext);
const steps = [];
const [_dropOptionLog, options] = yield* Effect.all([
debugLogger("Running Option selection..."),
multiselect({
message: "What would you like to do? (Select all that apply)",
options: [{ value: "env", label: "Setup Environment File", hint: "Create a .env file" }]
})
]);
if (typeof options === "symbol") {
return yield* context.pCancel(options);
}
appendOptionsToSteps(options, steps, OptionToStepMap);
yield* exitIfEmpty(context, steps, "steps");
yield* Effect.all([
debugLogger("Running steps..."),
Effect.forEach(steps, (step) => step(context, debug2, dry))
]);
yield* exitIfEmpty(context, context.tasks, "tasks");
yield* Effect.all([
debugLogger(`Tasks to run: ${context.tasks.length}`),
debugLogger("Running tasks..."),
tasks(context.tasks)
]);
yield* Effect.all([
debugLogger("Tasks complete, running next steps..."),
next(debug2).pipe(cliContext)
]);
yield* Effect.all([debugLogger("Interactive CLI completed, exiting..."), context.exit(0)]);
})
).pipe(Cli.Command.withDescription("Initialize the StudioCMS project after new installation."));
export {
debug,
dryRun,
initCMD
};