"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createEnsureApiDeployedPlugins = void 0;
var _utils = require("@webiny/cli-plugin-deploy-pulumi/utils");
var _plugins = require("@webiny/cli-plugin-deploy-pulumi/plugins");
const NO_DEPLOYMENT_CHECKS_FLAG_NAME = "--no-deployment-checks";
const createPluginCallable = (command, appName) => {
  return (params, ctx) => {
    const {
      env,
      inputs
    } = params;
    // Just in case, we want to allow users to skip the system requirements check.
    if (inputs.deploymentChecks === false) {
      return;
    }
    const output = (0, _utils.getStackOutput)({
      folder: "apps/api",
      env
    });
    const apiDeployed = output && Object.keys(output).length > 0;
    if (apiDeployed) {
      return;
    }
    const apiAppName = ctx.error.hl("API");
    const adminAppName = ctx.error.hl(appName);
    const cmd = ctx.error.hl(`yarn webiny deploy api --env ${env}`);
    ctx.error(`Cannot ${command} ${adminAppName} project application before deploying ${apiAppName}.`);
    const message = [`Before ${command}ing ${adminAppName} project application, please`, `deploy ${apiAppName} first by running: ${cmd}.`, `If you think this is a mistake, you can also try skipping`, `the deployment checks by appending the ${ctx.error.hl(NO_DEPLOYMENT_CHECKS_FLAG_NAME)} flag.`, `Learn more: https://webiny.link/deploy-api-first`];
    throw new _utils.GracefulError(message.join(" "));
  };
};
const createEnsureApiDeployedPlugins = appName => {
  const ensureApiDeployedBeforeBuild = (0, _plugins.createBeforeBuildPlugin)(createPluginCallable("build", appName));
  ensureApiDeployedBeforeBuild.name = `${appName}.before-deploy.ensure-api-deployed`;
  const ensureApiDeployedBeforeWatch = (0, _plugins.createBeforeWatchPlugin)(createPluginCallable("watch", appName));
  ensureApiDeployedBeforeWatch.name = `${appName}.before-watch.ensure-api-deployed`;
  return [ensureApiDeployedBeforeBuild, ensureApiDeployedBeforeWatch];
};
exports.createEnsureApiDeployedPlugins = createEnsureApiDeployedPlugins;

//# sourceMappingURL=ensureApiDeployed.js.map