UNPKG

jovo-cli

Version:
187 lines 7.1 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const _ = __importStar(require("lodash")); const jovo_cli_core_1 = require("jovo-cli-core"); const chalk_1 = __importDefault(require("chalk")); const fs_1 = require("fs"); const _1 = require("."); const Listr = require("listr"); const Prompts_1 = require("./Prompts"); const parseJson = require('parse-json'); const project = jovo_cli_core_1.getProject(); function buildTask(ctx) { const platformsPath = project.getPlatformsPath(); if (!fs_1.existsSync(platformsPath)) { fs_1.mkdirSync(platformsPath); } const buildPlatformTasks = []; const validationTasks = []; const locales = project.getLocales(ctx.locales); let modelFileContent; for (const locale of locales) { validationTasks.push({ title: locale, async task(ctx, task) { try { modelFileContent = await project.getModelFileJsonContent(locale); } catch (err) { if (err.code === 'ENOENT') { if (fs_1.existsSync(project.getModelPath(locale, 'js'))) { return task.skip('Model file is of type .js, not .json, so check got skipped.'); } throw new jovo_cli_core_1.JovoCliError('Language model file could not be found.', 'jovo-cli', `Expected location: ${err.path}`); } throw new jovo_cli_core_1.JovoCliError(err.message, 'jovo-cli'); } try { parseJson(modelFileContent); } catch (err) { throw new jovo_cli_core_1.JovoCliError(`Model file is not valid JSON: ${err.message}`, 'jovo-cli'); } if (ctx.ignoreTasks && ctx.ignoreTasks.includes('model-validation')) { return; } try { for (const type of ctx.types) { const platform = _1.platforms.get(type, ctx.stage); project.validateModel(locale, platform.getModelValidator()); } } catch (err) { if (err instanceof jovo_cli_core_1.JovoCliError) { throw err; } throw new jovo_cli_core_1.JovoCliError(err.message, 'jovo-cli'); } }, }); } buildPlatformTasks.push({ title: 'Initializing build process...', task(ctx) { const backupLocales = []; backupLocales.push({ title: `Collecting platform configuration from project.js.\n ${chalk_1.default.grey(`Platforms: ${ctx.types.join(', ')}`)}`, task() { return; }, }, { title: `Collecting Jovo language model files from ./models folder.\n ${chalk_1.default.grey(`Locales: ${locales.join(', ')}`)}`, task() { return; }, }, { title: 'Validating model files.', task() { return new Listr(validationTasks); }, }); return new Listr(backupLocales); }, }); for (const type of ctx.types) { const platform = _1.platforms.get(type, ctx.stage); buildPlatformTasks.push(...platform.getBuildTasks(ctx)); } return buildPlatformTasks; } exports.buildTask = buildTask; function deployTask(ctx) { if (!ctx.targets || ctx.targets.length === 0) { return []; } if (ctx.targets.length === 1 && ctx.targets.includes(jovo_cli_core_1.TARGET_ZIP)) { return [project.deployTaskZipProjectSource(ctx)]; } const platformsPath = project.getPlatformsPath(); if (!fs_1.existsSync(platformsPath)) { fs_1.mkdirSync(platformsPath); } const targets = []; let targetNames = []; if (ctx.targets.includes(jovo_cli_core_1.TARGET_ALL)) { targetNames = _1.deployTargets.getAllAvailable(); } else { targetNames = ctx.targets; } const pluginDeployTargets = _1.deployTargets.getAllPluginTargets(); let preDeployTasks = []; for (const targetName of targetNames) { if (!pluginDeployTargets.includes(targetName)) { break; } const target = _1.deployTargets.get(targetName); targets.push(target); preDeployTasks = _.union(preDeployTasks, target.getPreDeployTasks()); } const deployPlatformTasks = []; if (ctx.targets.includes(jovo_cli_core_1.TARGET_ZIP) && !preDeployTasks.includes(jovo_cli_core_1.TARGET_ZIP)) { preDeployTasks.push(jovo_cli_core_1.TARGET_ZIP); } for (const target of preDeployTasks) { if (target === jovo_cli_core_1.TARGET_ZIP) { deployPlatformTasks.push(project.deployTaskZipProjectSource(ctx)); } } for (const type of ctx.types) { const platform = _1.platforms.get(type, ctx.stage); deployPlatformTasks.push(...platform.getDeployTasks(ctx, targets)); } return deployPlatformTasks; } exports.deployTask = deployTask; function buildReverseTask(ctx) { const buildReverseSubTasks = [ { title: 'Creating backups...', enabled(ctx) { return ctx.reverse === Prompts_1.ANSWER_BACKUP; }, task(ctx) { const backupLocaleTasks = []; for (const locale of ctx.locales) { backupLocaleTasks.push({ title: locale, task() { return project.backupModel(locale); }, }); } return new Listr(backupLocaleTasks); }, }, ]; for (const type of ctx.types) { const platform = _1.platforms.get(type, ctx.stage); buildReverseSubTasks.push(...platform.getBuildReverseTasks(ctx)); } return new Listr(buildReverseSubTasks); } exports.buildReverseTask = buildReverseTask; function getTask(ctx) { const platformsPath = project.getPlatformsPath(); if (!fs_1.existsSync(platformsPath)) { fs_1.mkdirSync(platformsPath); } const tasks = []; for (const type of ctx.types) { const platform = _1.platforms.get(type, ctx.stage); tasks.push(...platform.getGetTasks(ctx)); } return tasks; } exports.getTask = getTask; //# sourceMappingURL=Tasks.js.map