@mondaycom/apps-cli
Version:
A cli tool to manage apps (and monday-code projects) in monday.com
102 lines (101 loc) • 5.54 kB
JavaScript
import { APP_TEMPLATES_CONFIG } from '../consts/app-templates-config.js';
import { APP_VERSION_STATUS } from '../consts/app-versions.js';
import { listAppBuilds } from './app-builds-service.js';
import { listAppFeaturesByAppVersionId } from './app-features-service.js';
import { defaultVersionByAppId, listAppVersionsByAppId } from './app-versions-service.js';
import { listApps } from './apps-service.js';
import { PromptService } from './prompt-service.js';
import { LIVE_VERSION_ERROR_LOG } from '../consts/messages.js';
import { AppFeatureType } from '../types/services/app-features-service.js';
import { SchedulerService } from './scheduler-service.js';
export const DynamicChoicesService = {
async chooseApp() {
const apps = await listApps();
const appChoicesMap = {};
for (const app of apps) {
appChoicesMap[`${app.id} | ${app.name}`] = app.id;
}
const selectedAppKey = await PromptService.promptSelectionWithAutoComplete('Select an app', Object.keys(appChoicesMap));
const selectedAppId = appChoicesMap[selectedAppKey];
return selectedAppId;
},
async chooseAppVersion(appId, filterByStatus) {
let appVersions = await listAppVersionsByAppId(appId);
if (filterByStatus) {
appVersions = appVersions.filter(appVersion => filterByStatus.includes(appVersion.status));
}
const appVersionChoicesMap = {};
for (const appVersion of appVersions) {
appVersionChoicesMap[`${appVersion.id} | ${appVersion.versionNumber} | ${appVersion.name} | ${appVersion.status}`] = appVersion.id;
}
const selectedAppVersionKey = await PromptService.promptSelectionWithAutoComplete('Select an app version', Object.keys(appVersionChoicesMap));
const selectedAppVersionId = appVersionChoicesMap[selectedAppVersionKey];
return selectedAppVersionId;
},
async chooseAppAndAppVersion(useDeprecatedVersion, useLiveVersion, options) {
const { appId, autoSelectVersion = false } = options || {};
const filterByStatus = [APP_VERSION_STATUS.DRAFT];
if (useDeprecatedVersion)
filterByStatus.push(APP_VERSION_STATUS.DEPRECATED);
if (useLiveVersion)
filterByStatus.push(APP_VERSION_STATUS.LIVE);
if (appId && autoSelectVersion) {
const defaultVersion = await defaultVersionByAppId(appId, {
customLogMessage: LIVE_VERSION_ERROR_LOG,
useLiveVersion,
});
if (!defaultVersion)
throw new Error(`No default version found for app id - ${appId}`);
return { appId, appVersionId: defaultVersion.id };
}
const chosenAppId = appId || (await this.chooseApp());
const appVersionId = await this.chooseAppVersion(chosenAppId, filterByStatus);
return { appId: chosenAppId, appVersionId };
},
async chooseAppFeatureType(excludeTypes) {
const featureTypes = Object.values(AppFeatureType);
const featureTypeChoicesMap = {};
for (const featureType of featureTypes) {
if (excludeTypes?.includes(featureType))
continue;
featureTypeChoicesMap[featureType] = featureType;
}
const selectedFeatureTypeKey = await PromptService.promptSelectionWithAutoComplete('Select a feature type', Object.keys(featureTypeChoicesMap));
const selectedFeatureType = featureTypeChoicesMap[selectedFeatureTypeKey];
return selectedFeatureType;
},
async chooseBuild(appVersionId) {
const appReleases = await listAppBuilds(appVersionId);
const appReleaseChoicesMap = {};
for (const appRelease of appReleases) {
appReleaseChoicesMap[`${appRelease.id} | ${appRelease.category} | | ${appRelease.data?.url || ' '}`] =
appRelease.id;
}
const selectedAppReleaseKey = await PromptService.promptSelectionWithAutoComplete('Select a build', Object.keys(appReleaseChoicesMap));
const selectedAppReleaseId = appReleaseChoicesMap[selectedAppReleaseKey];
return selectedAppReleaseId;
},
async chooseAppFeature(appVersionId, options) {
const appFeatures = await listAppFeaturesByAppVersionId(appVersionId, options);
const appFeatureChoicesMap = {};
for (const appFeature of appFeatures) {
appFeatureChoicesMap[`${appFeature.id} | ${appFeature.name} | ${appFeature.type}`] = appFeature;
}
const selectedAppFeatureKey = await PromptService.promptSelectionWithAutoComplete('Select an app feature', Object.keys(appFeatureChoicesMap));
return appFeatureChoicesMap[selectedAppFeatureKey];
},
async chooseAppTemplate() {
const selectedTemplateName = await PromptService.promptSelectionWithAutoComplete('Select a template to start with', APP_TEMPLATES_CONFIG.map(template => template.name));
return APP_TEMPLATES_CONFIG.find(template => template.name === selectedTemplateName);
},
async chooseSchedulerJob(appId, region) {
const jobs = await SchedulerService.listJobs(appId, region);
const jobChoicesMap = {};
for (const job of jobs) {
jobChoicesMap[`${job.name} (${job.targetUrl})`] = job.name;
}
const selectedJobKey = await PromptService.promptSelectionWithAutoComplete('Select a job', Object.keys(jobChoicesMap));
const selectedJobName = jobChoicesMap[selectedJobKey];
return selectedJobName;
},
};