UNPKG

@vortex.so/cli

Version:

CLI to interact with Vortex.

139 lines (133 loc) 4.41 kB
'use strict'; const citty = require('citty'); const fs = require('fs-extra'); const yaml = require('js-yaml'); const prompts = require('prompts'); const stringTs = require('string-ts'); require('node:process'); const c = require('chalk'); require('figures'); require('jiti'); const index = require('../../../utils/log/index.cjs'); const init_constants = require('./init.constants.cjs'); const ship_types = require('../../../plugins/ship/ship.types.cjs'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const fs__default = /*#__PURE__*/_interopDefaultCompat(fs); const yaml__default = /*#__PURE__*/_interopDefaultCompat(yaml); const prompts__default = /*#__PURE__*/_interopDefaultCompat(prompts); const c__default = /*#__PURE__*/_interopDefaultCompat(c); const log = new index.Log("Init"); const initCommand = citty.defineCommand({ meta: { name: "init", description: c__default.dim("Initialize workspace.") }, args: {}, async run() { let manifest; try { manifest = yaml__default.load( fs__default.readFileSync("vortex.yaml", "utf-8") ); log.info( "There is existing Vortex manifest file, using it as initial values." ); prompts__default.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__default([ { 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 = init_constants.CONTEXTS[answers.context]; manifest = ship_types.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__default.dump(manifest); await fs__default.writeFile("vortex.yaml", yamlData, { encoding: "utf-8" }); log.ok(`${c__default.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: stringTs.capitalize(key) }); } } log.abort(); } } }); exports.initCommand = initCommand;