UNPKG

@mondaycom/apps-cli

Version:

A cli tool to manage apps (and monday-code projects) in monday.com

103 lines (102 loc) 4.3 kB
import { Flags } from '@oclif/core'; import { Listr } from 'listr2'; import { AuthenticatedCommand } from '../../commands-base/authenticated-command.js'; import { DynamicChoicesService } from '../../services/dynamic-choices-service.js'; import * as importService from '../../services/import-manifest-service.js'; import { PromptService } from '../../services/prompt-service.js'; import logger from '../../utils/logger.js'; const MESSAGES = { path: 'Path to your app manifest file on your machine', appId: 'App id (will create a new draft version)', appVersionId: 'App version id to override', newApp: 'Create new app', allowMissingVariables: 'Allow missing variables', }; export default class ManifestImport extends AuthenticatedCommand { static description = 'Import manifest with optional template variables.'; static withPrintCommand = false; static examples = [ '<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> -p ./manifest.json', '<%= config.bin %> <%= command.id %> --manifestPath ./manifest.json', ]; static flags = ManifestImport.serializeFlags({ manifestPath: Flags.string({ char: 'p', description: MESSAGES.path, }), appId: Flags.string({ char: 'a', description: MESSAGES.appId, }), appVersionId: Flags.integer({ char: 'i', aliases: ['v'], description: MESSAGES.appVersionId, }), newApp: Flags.boolean({ description: MESSAGES.newApp, char: 'n', aliases: ['new'], default: false, }), allowMissingVariables: Flags.boolean({ description: MESSAGES.allowMissingVariables, char: 'm', aliases: ['allow-missing-variables'], default: false, }), }); DEBUG_TAG = 'manifest_import'; async getAppVersionId(appVersionId, appId) { if (appVersionId) return appVersionId; const latestDraftVersion = await DynamicChoicesService.chooseAppAndAppVersion(false, false, { appId: Number(appId), autoSelectVersion: false, }); return latestDraftVersion.appVersionId; } async run() { try { const { flags } = await this.parse(ManifestImport); const { manifestPath: initialManifestPath } = flags; const { appId: appIdAsString, appVersionId: appVersionIdAsString, newApp, allowMissingVariables } = flags; let manifestPath = initialManifestPath; let appId = appIdAsString ? Number(appIdAsString) : undefined; let appVersionId = appVersionIdAsString ? Number(appVersionIdAsString) : undefined; if (!manifestPath) { manifestPath = await PromptService.promptFile(MESSAGES.path, ['json', 'yaml']); } const shouldCreateNewApp = await importService.shouldCreateNewApp({ appId, appVersionId, newApp }); if (!shouldCreateNewApp) { appId = await DynamicChoicesService.chooseApp(); } const shouldCreateNewAppVersion = await importService.shouldCreateNewAppVersion({ appId, appVersionId, newApp: shouldCreateNewApp, }); if (!shouldCreateNewAppVersion && !shouldCreateNewApp) { appVersionId = await this.getAppVersionId(appVersionId, appId); } if (appVersionId && !appId) { logger.error('App id is required when app version id is provided', this.DEBUG_TAG); process.exit(1); } this.preparePrintCommand(this, { appVersionId, manifestPath, appId, newApp: shouldCreateNewApp }); const ctx = { appVersionId, appId, manifestFilePath: manifestPath, allowMissingVariables, }; const tasks = new Listr([{ title: 'Importing app manifest', task: importService.uploadManifestTsk }], { ctx }); await tasks.run(); } catch (error) { logger.debug(error, this.DEBUG_TAG); process.exit(1); } } }