Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Warning - Watching Admin/Website Without Having API Deployed #4350

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cli/commands/wcp/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const open = require("open");
const { GraphQLClient } = require("graphql-request");
const { setProjectId, setWcpPat, sleep } = require("./utils");
const { setProjectId, setWcpPat } = require("./utils");
const { sleep } = require("../../utils");
const chalk = require("chalk");
const { getWcpGqlApiUrl, getWcpAppUrl } = require("@webiny/wcp");

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/commands/wcp/project.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const open = require("open");
const inquirer = require("inquirer");
const chalk = require("chalk");
const { getUser, WCP_APP_URL, setProjectId, sleep } = require("./utils");
const { getUser, WCP_APP_URL, setProjectId } = require("./utils");
const { sleep } = require("../../utils");

module.exports.command = () => [
{
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/commands/wcp/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { setWcpPat } = require("./setWcpPat");
const { getWcpPat } = require("./getWcpPat");
const { getWcpProjectId } = require("./getWcpProjectId");
const { getWcpOrgProjectId } = require("./getWcpOrgProjectId");
const { sleep } = require("./sleep");

module.exports = {
getUser,
Expand All @@ -16,6 +15,5 @@ module.exports = {
setWcpPat,
getWcpPat,
getWcpProjectId,
getWcpOrgProjectId,
sleep
getWcpOrgProjectId
};
1 change: 0 additions & 1 deletion packages/cli/commands/wcp/utils/sleep.js

This file was deleted.

6 changes: 5 additions & 1 deletion packages/cli/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const localStorage = require("./localStorage");
const log = require("./log");
const sendEvent = require("./sendEvent");
const PluginsContainer = require("./PluginsContainer");
const sleep = require("./sleep");
const sleepSync = require("./sleepSync");

const noop = () => {
// Do nothing.
Expand All @@ -20,5 +22,7 @@ module.exports = {
log,
noop,
sendEvent,
PluginsContainer
PluginsContainer,
sleep,
sleepSync
};
3 changes: 3 additions & 0 deletions packages/cli/utils/sleep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const sleep = (ms = 1500) => new Promise(resolve => setTimeout(resolve, ms));

module.exports = sleep;
8 changes: 8 additions & 0 deletions packages/cli/utils/sleepSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const sleepSync = (ms = 1500) => {
const start = Date.now();
while (Date.now() < start + ms) {
// Do nothing.
}
};

module.exports = sleepSync;
50 changes: 19 additions & 31 deletions packages/serverless-cms-aws/src/admin/plugins/ensureApiDeployed.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
import { getStackOutput } from "@webiny/cli-plugin-deploy-pulumi/utils";
import {
createBeforeBuildPlugin,
createBeforeWatchPlugin
} from "@webiny/cli-plugin-deploy-pulumi/plugins";
import { createBeforeBuildPlugin } from "@webiny/cli-plugin-deploy-pulumi/plugins";
import { GracefulError } from "@webiny/cli-plugin-deploy-pulumi/utils";
import type { Callable } from "@webiny/cli-plugin-deploy-pulumi/plugins/PulumiCommandLifecycleEventHookPlugin";

const createPluginCallable: (command: "build" | "watch") => Callable =
command =>
({ env }, ctx) => {
const output = getStackOutput({ folder: "apps/api", env });
const apiDeployed = output && Object.keys(output).length > 0;
if (apiDeployed) {
return;
}
export const ensureApiDeployedBeforeBuild = createBeforeBuildPlugin(({ env }, ctx) => {
const output = 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("Admin");
const cmd = ctx.error.hl(`yarn webiny deploy api --env ${env}`);
ctx.error(
`Cannot ${command} ${adminAppName} project application before deploying ${apiAppName}.`
);
const apiAppName = ctx.error.hl("API");
const adminAppName = ctx.error.hl("Admin");
const cmd = ctx.error.hl(`yarn webiny deploy api --env ${env}`);
ctx.error(`Cannot build ${adminAppName} project application before deploying ${apiAppName}.`);

throw new GracefulError(
[
`Before ${command}ing ${adminAppName} project application, please`,
`deploy ${apiAppName} first by running: ${cmd}.`
].join(" ")
);
};
throw new GracefulError(
[
`Before building ${adminAppName} project application, please`,
`deploy ${apiAppName} first by running: ${cmd}.`
].join(" ")
);
});

export const ensureApiDeployedBeforeBuild = createBeforeBuildPlugin(createPluginCallable("build"));
ensureApiDeployedBeforeBuild.name = "admin.before-deploy.ensure-api-deployed";

export const ensureApiDeployedBeforeWatch = createBeforeWatchPlugin(createPluginCallable("watch"));
ensureApiDeployedBeforeWatch.name = "admin.before-watch.ensure-api-deployed";
ensureApiDeployedBeforeBuild.name = "admin.before-build.ensure-api-deployed";
8 changes: 2 additions & 6 deletions packages/serverless-cms-aws/src/createAdminApp.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { createAdminPulumiApp, CreateAdminPulumiAppParams } from "@webiny/pulumi-aws";
import { uploadAppToS3 } from "./react/plugins";
import { ensureApiDeployedBeforeBuild, ensureApiDeployedBeforeWatch } from "./admin/plugins";
import { ensureApiDeployedBeforeBuild } from "./admin/plugins";
import { PluginCollection } from "@webiny/plugins/types";

export interface CreateAdminAppParams extends CreateAdminPulumiAppParams {
plugins?: PluginCollection;
}

export function createAdminApp(projectAppParams: CreateAdminAppParams = {}) {
const builtInPlugins = [
uploadAppToS3({ folder: "apps/admin" }),
ensureApiDeployedBeforeBuild,
ensureApiDeployedBeforeWatch
];
const builtInPlugins = [uploadAppToS3({ folder: "apps/admin" }), ensureApiDeployedBeforeBuild];

const customPlugins = projectAppParams.plugins ? [...projectAppParams.plugins] : [];

Expand Down
15 changes: 15 additions & 0 deletions packages/serverless-cms-aws/src/createAdminAppConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createReactAppConfig, ReactAppConfigModifier } from "~/createReactAppConfig";
import { ApiOutput } from "@webiny/pulumi-aws";

// @ts-expect-error Rewrite CLI into TypeScript.
import { log, sleepSync } from "@webiny/cli/utils";

export const createAdminAppConfig = (modifier?: ReactAppConfigModifier) => {
return createReactAppConfig(baseParams => {
const { config, options } = baseParams;
Expand All @@ -14,6 +17,18 @@ export const createAdminAppConfig = (modifier?: ReactAppConfigModifier) => {
}));

config.pulumiOutputToEnv<ApiOutput>("apps/api", ({ output, env }) => {
if (!output) {
log.warning(
`Could not assign required environment variables. %s project application's stack output could not be retrieved. Learn more: https://webiny.link/missing-stack-output`,
"API"
);

// We want to wait a bit because Webpack output just quickly hides the warning message.
sleepSync(5000);

return env;
}

return {
...env,
REACT_APP_USER_POOL_REGION: output.region,
Expand Down
4 changes: 1 addition & 3 deletions packages/serverless-cms-aws/src/createWebsiteApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
lambdaEdgeWarning,
renderWebsite,
telemetryNoLongerNewUser,
ensureApiDeployedBeforeWatch,
ensureApiDeployedBeforeBuild
} from "./website/plugins";
import { uploadAppToS3 } from "./react/plugins";
Expand All @@ -21,8 +20,7 @@ export function createWebsiteApp(projectAppParams: CreateWebsiteAppParams = {})
lambdaEdgeWarning,
renderWebsite,
telemetryNoLongerNewUser,
ensureApiDeployedBeforeBuild,
ensureApiDeployedBeforeWatch
ensureApiDeployedBeforeBuild
];

const customPlugins = projectAppParams.plugins ? [...projectAppParams.plugins] : [];
Expand Down
15 changes: 15 additions & 0 deletions packages/serverless-cms-aws/src/createWebsiteAppConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createReactAppConfig, ReactAppConfigModifier } from "~/createReactAppConfig";
import { ApiOutput } from "@webiny/pulumi-aws";

// @ts-expect-error Rewrite CLI into TypeScript.
import { log, sleepSync } from "@webiny/cli/utils";

export const createWebsiteAppConfig = (modifier?: ReactAppConfigModifier) => {
return createReactAppConfig(baseParams => {
const { config, options } = baseParams;
Expand All @@ -12,6 +15,18 @@ export const createWebsiteAppConfig = (modifier?: ReactAppConfigModifier) => {
}));

config.pulumiOutputToEnv<ApiOutput>("apps/api", ({ output, env }) => {
if (!output) {
log.warning(
`Could not assign required environment variables. %s project application's stack output could not be retrieved. Learn more: https://webiny.link/missing-stack-output`,
"API"
);

// We want to wait a bit because Webpack output just quickly hides the warning message.
sleepSync(5000);

return env;
}

return {
...env,
REACT_APP_GRAPHQL_API_URL: `${output.apiUrl}/graphql`,
Expand Down
8 changes: 2 additions & 6 deletions packages/serverless-cms-aws/src/enterprise/createAdminApp.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { createAdminPulumiApp, CreateAdminPulumiAppParams } from "@webiny/pulumi-aws/enterprise";
import { uploadAppToS3 } from "~/react/plugins";
import { PluginCollection } from "@webiny/plugins/types";
import { ensureApiDeployedBeforeBuild, ensureApiDeployedBeforeWatch } from "~/website/plugins";
import { ensureApiDeployedBeforeBuild } from "~/website/plugins";

export interface CreateAdminAppParams extends CreateAdminPulumiAppParams {
plugins?: PluginCollection;
}

export function createAdminApp(projectAppParams: CreateAdminAppParams = {}) {
const builtInPlugins = [
uploadAppToS3({ folder: "apps/admin" }),
ensureApiDeployedBeforeBuild,
ensureApiDeployedBeforeWatch
];
const builtInPlugins = [uploadAppToS3({ folder: "apps/admin" }), ensureApiDeployedBeforeBuild];

const customPlugins = projectAppParams.plugins ? [...projectAppParams.plugins] : [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
lambdaEdgeWarning,
renderWebsite,
telemetryNoLongerNewUser,
ensureApiDeployedBeforeBuild,
ensureApiDeployedBeforeWatch
ensureApiDeployedBeforeBuild
} from "~/website/plugins";
import { uploadAppToS3 } from "~/react/plugins";

Expand All @@ -24,8 +23,7 @@ export function createWebsiteApp(projectAppParams: CreateWebsiteAppParams = {})
lambdaEdgeWarning,
renderWebsite,
telemetryNoLongerNewUser,
ensureApiDeployedBeforeBuild,
ensureApiDeployedBeforeWatch
ensureApiDeployedBeforeBuild
];

const customPlugins = projectAppParams.plugins ? [...projectAppParams.plugins] : [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
import { getStackOutput } from "@webiny/cli-plugin-deploy-pulumi/utils";
import {
createBeforeBuildPlugin,
createBeforeWatchPlugin
} from "@webiny/cli-plugin-deploy-pulumi/plugins";
import { createBeforeBuildPlugin } from "@webiny/cli-plugin-deploy-pulumi/plugins";
import { GracefulError } from "@webiny/cli-plugin-deploy-pulumi/utils";
import type { Callable } from "@webiny/cli-plugin-deploy-pulumi/plugins/PulumiCommandLifecycleEventHookPlugin";

const createPluginCallable: (command: "build" | "watch") => Callable =
command =>
({ env }, ctx) => {
const output = getStackOutput({ folder: "apps/api", env });
const apiDeployed = output && Object.keys(output).length > 0;
if (apiDeployed) {
return;
}
export const ensureApiDeployedBeforeBuild = createBeforeBuildPlugin(({ env }, ctx) => {
const output = getStackOutput({ folder: "apps/api", env });
const apiDeployed = output && Object.keys(output).length > 0;
if (apiDeployed) {
return;
}

const apiAppName = ctx.error.hl("API");
const websiteAppName = ctx.error.hl("Website");
const cmd = ctx.error.hl(`yarn webiny deploy api --env ${env}`);
ctx.error(
`Cannot ${command} ${websiteAppName} project application before deploying ${apiAppName}.`
);
const apiAppName = ctx.error.hl("API");
const websiteAppName = ctx.error.hl("Website");
const cmd = ctx.error.hl(`yarn webiny deploy api --env ${env}`);
ctx.error(`Cannot build ${websiteAppName} project application before deploying ${apiAppName}.`);

throw new GracefulError(
[
`Before ${command}ing ${websiteAppName} project application, please`,
`deploy ${apiAppName} first by running: ${cmd}.`
].join(" ")
);
};
throw new GracefulError(
[
`Before building ${websiteAppName} project application, please`,
`deploy ${apiAppName} first by running: ${cmd}.`
].join(" ")
);
});

export const ensureApiDeployedBeforeBuild = createBeforeBuildPlugin(createPluginCallable("build"));
ensureApiDeployedBeforeBuild.name = "website.before-deploy.ensure-api-deployed";

export const ensureApiDeployedBeforeWatch = createBeforeWatchPlugin(createPluginCallable("watch"));
ensureApiDeployedBeforeWatch.name = "website.before-watch.ensure-api-deployed";
ensureApiDeployedBeforeBuild.name = "website.before-build.ensure-api-deployed";