UNPKG

@vortex.so/cli

Version:

CLI to interact with Vortex.

130 lines (127 loc) 3.88 kB
import { defineCommand } from 'citty'; import fs from 'fs-extra'; import yaml from 'js-yaml'; import prompts from 'prompts'; import { capitalize } from 'string-ts'; import 'node:process'; import c from 'chalk'; import 'figures'; import 'jiti'; import { Log } from '../../../utils/log/index.mjs'; import { CONTEXTS } from './init.constants.mjs'; import { ShipManifestSchema } from '../../../plugins/ship/ship.types.mjs'; const log = new Log("Init"); const initCommand = defineCommand({ meta: { name: "init", description: c.dim("Initialize workspace.") }, args: {}, async run() { let manifest; try { manifest = yaml.load( fs.readFileSync("vortex.yaml", "utf-8") ); log.info( "There is existing Vortex manifest file, using it as initial values." ); prompts.override(manifest); } catch (error) { log.warn(`There isn't any Vortex manifest file, let's create one.`); } await new Promise((resolve) => setTimeout(resolve, 300)); const answers = await prompts([ { type: "select", name: "kind", message: "What do you want to initialize?", choices: [ { title: "Component", value: "component" }, { title: "Repository", value: "repository" } ], initial: 0 }, { type: "select", name: "context", message: "Choose context of the project.", choices: [ { title: "Vortex", value: "vrt" }, { title: "Neon", value: "neo" }, { title: "Neutron", value: "neu" } ] }, { type: "text", name: "handle", message: `What's the project handle?`, validate: (v) => { if (!v) return "Project handle is required."; if (v !== v.toLowerCase()) return "Project handle must be in lowercase."; else return true; } }, { type: "text", name: "description", message: `Describe the project.` }, { type: (_, answers2) => { if (answers2.kind === "component") return "multiselect"; else return null; }, name: "type", message: "What type of component is this?", choices: [ { title: "Back-end", value: "be" }, { title: "Front-end", value: "fe" }, { title: "Mobile", value: "me" }, { title: "Package", value: "pkg" }, { title: "Interface", value: "ui" } ], max: 2, hint: "- Space to select. Return to submit" } ]); if (answers.kind === "component" && typeof answers.type === "string") { answers.type = [answers.type]; } try { const context = CONTEXTS[answers.context]; manifest = ShipManifestSchema.parse({ name: `${context.namespace}/${answers.handle}`, org: answers.context, namespace: context.namespace, handle: answers.handle, kind: answers.kind, type: answers.type, description: answers.description, version: manifest?.version || "0.1.0", build: manifest?.build || 0, repo: manifest?.repo || { name: `${context.repo.namespace}/__repo___`, url: `gitlab.com/${context.repo.namespace}/__repo___`, host: context.repo.host } }); const yamlData = yaml.dump(manifest); await fs.writeFile("vortex.yaml", yamlData, { encoding: "utf-8" }); log.ok(`${c.bold("vortex.yaml")} has been updated.`); } catch (error) { const fmt = error?.flatten(); for (const key in fmt?.fieldErrors) { const messages = fmt.fieldErrors[key]; for (const msg of messages) { log.fail(msg, { title: capitalize(key) }); } } log.abort(); } } }); export { initCommand };