@supernovaio/cli
Version:
Supernova.io Command Line Interface
107 lines (105 loc) • 4.41 kB
JavaScript
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="405e0a9a-82b3-5830-b645-242d4cc5799f")}catch(e){}}();
import { Command } from "@oclif/core";
import inquirer from "inquirer";
import { getApiClient } from "../utils/api-client.js";
import { SupernovaConfigService } from "../utils/config.service.js";
import { getTargetEnv } from "./environment.js";
const hasAccess = (role) => role && ["Admin", "Contributor", "Creator", "Owner"].includes(role);
export class BaseCommand extends Command {
env = getTargetEnv();
configService = SupernovaConfigService.getInstance();
_apiClient;
async apiClient() {
if (this._apiClient)
return this._apiClient;
const apiClient = await getApiClient(this.env);
this._apiClient = apiClient;
return apiClient;
}
async init() {
await super.init();
this.log(`Using ${this.env} environment`);
}
async promptBrandId(designSystemId, versionId) {
const client = await this.apiClient();
const brandsEndpoint = client.designSystems.versions.brands;
const { brands } = await brandsEndpoint.list(designSystemId, versionId ?? "head");
const options = brands.map(b => ({ name: b.meta.name, value: b.persistentId }));
if (options.length === 1)
return options[0].value;
return this.prompt("Select a brand:", options);
}
async promptDesignSystemId() {
const { designSystems: client } = await this.apiClient();
const { designSystems, workspaces } = await client.listUserDesignSystems();
const workspaceById = new Map(workspaces.map(ws => [ws.id, ws]));
const dsNameCount = designSystems.reduce((acc, { meta: { name }, role }) => {
if (hasAccess(role))
acc.set(name, (acc.get(name) ?? 0) + 1);
return acc;
}, new Map());
if (designSystems.length === 0) {
this.error("You don't have any design system.");
}
const dsIds = designSystems
.sort(({ meta: { name: a } }, { meta: { name: b } }) => a.localeCompare(b))
.map(({ id, meta: { name }, role, workspaceId }) => ({
id,
name: (dsNameCount.get(name) ?? 0) > 1 ? `${name} (${workspaceById.get(workspaceId)?.profile?.name})` : name,
role,
}))
.filter(({ role }) => hasAccess(role));
if (dsIds.length === 0) {
this.error("You don't have access to any design system.");
}
if (dsIds.length === 1) {
return dsIds[0].id;
}
return this.prompt("Select a design system:", dsIds.map(({ id, name }) => ({
name,
value: id,
})));
}
async getVersionId(designSystemId, versionId) {
if (versionId)
return versionId;
const { versions } = (await this.apiClient()).designSystems;
const verIds = (await versions.list(designSystemId)).designSystemVersions.map(({ id, meta: { name } }) => ({
id,
name,
}));
if (verIds.length === 1) {
return verIds[0].id;
}
return this.prompt("Select a version:", verIds.map(wrk => ({
name: wrk.name,
value: wrk.id,
})));
}
async getWorkspaceId(workspaceId) {
if (workspaceId)
return workspaceId;
const { workspaces } = await this.apiClient();
const wrks = (await workspaces.list()).membership.map(({ workspace: { id, profile: { name }, }, }) => ({ id, name }));
if (wrks.length === 1) {
return wrks[0].id;
}
return this.prompt("Select a workspace:", wrks.map(wrk => ({
name: wrk.name,
value: wrk.id,
})));
}
async prompt(message, choices) {
const choice = await inquirer.prompt([
{
choices: [...choices, { name: "Exit", value: "exit" }],
message,
name: "selected",
type: "list",
},
]);
return choice.selected;
}
}
//# sourceMappingURL=base-command.js.map
//# debugId=405e0a9a-82b3-5830-b645-242d4cc5799f