UNPKG

@vortex.so/cli

Version:

CLI to interact with Vortex.

89 lines (86 loc) 2.63 kB
import process from 'node:process'; import { $ } from 'execa'; import { resolve } from 'pathe'; import prompts from 'prompts'; import { valid } from 'semver'; import c from 'chalk'; import 'figures'; import 'jiti'; import { Log } from '../../utils/log/index.mjs'; import { getPackageManager } from '../../utils/package/index.mjs'; import { getInput } from './ship.input.mjs'; import { Operation } from './ship.operation.mjs'; const log = new Log("Ship"); async function publish() { let operation = await Operation.start({ release: { type: "prompt", preid: "beta" }, push: false, files: ["vortex.yaml"], cwd: process.cwd(), ignoreScripts: false, interface: { input: process.stdin, output: process.stdout } }); operation = await getInput(operation); const { name, type, version } = operation.state; if (!type?.includes("pkg")) { throw new Error("This component is not package, can't publish it."); } const answers = await prompts([ { type: "confirm", name: "isConfirmed", message: `Are you sure you want to publish ${c.bold.green(name)}?`, initial: false } ]); if (!answers.isConfirmed) throw new Error(`Cancelled.`); const loader = log.wait(); try { const rootDir = resolve("."); if (!version) throw new Error("Missing version."); if (!valid(version)) { throw new Error("Version is invalid."); } const commands = { yarn: { build: () => $`yarn build`, publish: ["yarn", "npm", "publish"] }, pnpm: { build: () => $`pnpm build`, publish: ["pnpm", "publish"] } }; const manager = getPackageManager(rootDir); if (!manager) throw new Error("Unknown package manager."); if (manager === "npm") throw new Error("NPM is not supported."); const cmd = commands[manager]; loader.text(`Building ${c.bold.green(name)} v${version}...`); const { stdout: r1 } = await cmd.build(); if (r1) loader.persist(c.gray(r1)); const command = cmd.publish; const isBeta = version.includes("beta"); if (isBeta) command.push("--tag", "beta"); if (manager === "pnpm") { command.push("--no-git-checks"); } loader.text(`Publishing ${c.bold.green(name)} v${version} to NPM...`); const { stdout: r2 } = await $`${command}`; if (r2) loader.persist(c.gray(r2)); loader.ok(`${c.bold.green(name)} v${version} has been published to NPM!`); } catch (error) { console.error(error); loader.fail(c.gray(error?.message || "Something is failed.")); } } export { publish };