Skip to content

Commit

Permalink
feat: add hook-stacks-info plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Aug 7, 2020
1 parent 77de092 commit ff788b1
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 27 deletions.
87 changes: 74 additions & 13 deletions packages/cwp-template-cms/hooks/api.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,81 @@
const { green, blue } = require("chalk");
const { green, blue, cyan, gray } = require("chalk");
const fs = require("fs");
const path = require("path");
const readJson = require("load-json-file");
const indentString = require("indent-string");

module.exports = (opts = {}) => ({
type: "hook-stack-after-deploy",
hook(params) {
const stackName = opts.stackName || "api";
const getTitle = (environmentsCount = 0) => {
let title = "Stack: api";
if (environmentsCount) {
title += ` (${environmentsCount} environment`;
title += environmentsCount > 1 ? `s)` : `)`;
}
return cyan(title);
};

module.exports = (opts = {}) => [
{
type: "hook-stack-after-deploy",
hook(params) {
const stackName = opts.stackName || "api";

if (params.stack !== stackName) {
return;
if (params.stack !== stackName) {
return;
}

if (params.isFirstDeploy) {
printFirstDeploySummary(params);
} else {
printDeploySummary(params);
}
}
},
{
name: "hook-stacks-info-api",
type: "hook-stacks-info",
async hook({ last }) {
const stackName = opts.stackName || "api";
const stackFolder = `./.webiny/state/${stackName}`;

try {
if (!fs.existsSync(stackFolder)) {
console.log(getTitle());
console.log("Nothing to show (stack not deployed).");
return;
}

const stackEnvs = fs.readdirSync(stackFolder);
if (!stackEnvs.length) {
console.log(getTitle());
console.log("Nothing to show (stack not deployed).");
return;
}

console.log(getTitle(stackEnvs.length));
for (let i = 0; i < stackEnvs.length; i++) {
const stackEnv = stackEnvs[i];

const webinyJson = await readJson(
path.join(stackFolder, stackEnv, "Webiny.json")
);

if (webinyJson.outputs) {
console.log(gray(`${stackEnv}`));
printDeploySummary({ state: webinyJson.outputs, indent: 2 });
}

if (params.isFirstDeploy) {
printFirstDeploySummary(params);
} else {
printDeploySummary(params);
const last = i === stackEnvs.length - 1;
if (!last) {
console.log();
}
}
} finally {
// Add space between this plugin and the next one that's about to be called.
!last && console.log();
}
}
}
});
];

function printFirstDeploySummary({ state }) {
const hasGraphQL = state.apolloGateway;
Expand Down Expand Up @@ -53,7 +113,7 @@ function printFirstDeploySummary({ state }) {
}
}

function printDeploySummary({ state }) {
function printDeploySummary({ state, indent = 0 }) {
const hasGraphQL = state.apolloGateway;
const hasCMS = state.cmsContent;
if (state.cdn && state.apolloGateway) {
Expand All @@ -67,6 +127,7 @@ function printDeploySummary({ state }) {
` - Content Preview API: ${green(state.cdn.url + "/cms/preview/production")}`
]
.filter(l => l !== false)
.map(item => indentString(item, indent))
.join("\n")
);
}
Expand Down
88 changes: 74 additions & 14 deletions packages/cwp-template-cms/hooks/apps.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,81 @@
const { green } = require("chalk");
const { green, cyan, gray } = require("chalk");
const fs = require("fs");
const path = require("path");
const readJson = require("load-json-file");
const indentString = require("indent-string");

module.exports = (opts = {}) => ({
type: "hook-stack-after-deploy",
hook(params) {
const stackName = opts.stackName || "apps";
const getTitle = (environmentsCount = 0) => {
let title = "Stack: apps";
if (environmentsCount) {
title += ` (${environmentsCount} environment`;
title += environmentsCount > 1 ? `s)` : `)`;
}
return cyan(title);
};

module.exports = (opts = {}) => [
{
type: "hook-stack-after-deploy",
hook(params) {
const stackName = opts.stackName || "apps";

if (params.stack !== stackName) {
return;
if (params.stack !== stackName) {
return;
}

if (params.isFirstDeploy) {
printFirstDeploySummary(params);
} else {
printDeploySummary(params);
}
}
},
{
name: "hook-stacks-info-apps",
type: "hook-stacks-info",
async hook({ last }) {
const stackName = opts.stackName || "apps";
const stackFolder = `./.webiny/state/${stackName}`;

try {
if (!fs.existsSync(stackFolder)) {
console.log(getTitle());
console.log("Nothing to show (stack not deployed).");
return;
}

const stackEnvs = fs.readdirSync(stackFolder);
if (!stackEnvs.length) {
console.log(getTitle());
console.log("Nothing to show (stack not deployed).");
return;
}

console.log(getTitle(stackEnvs.length));
for (let i = 0; i < stackEnvs.length; i++) {
const stackEnv = stackEnvs[i];

const webinyJson = await readJson(
path.join(stackFolder, stackEnv, "Webiny.json")
);

if (webinyJson.outputs) {
console.log(gray(`${stackEnv}`));
printDeploySummary({ state: webinyJson.outputs, indent: 2 });
}

if (params.isFirstDeploy) {
printFirstDeploySummary(params);
} else {
printDeploySummary(params);
const last = i === stackEnvs.length - 1;
if (!last) {
console.log();
}
}
} finally {
// Add space between this plugin and the next one that's about to be called.
!last && console.log();
}
}
}
});
];

function printFirstDeploySummary({ state }) {
if (!state.cdn) {
Expand All @@ -38,12 +98,12 @@ function printFirstDeploySummary({ state }) {
);
}

function printDeploySummary({ state }) {
function printDeploySummary({ state, indent = 0 }) {
if (!state.cdn) {
return;
}

const adminUrl = state.cdn.url + "/admin";

console.log(`🔗 Access your ${green("admin")} app at ${green(adminUrl)}`);
console.log(indentString(`🔗 Access your ${green("admin")} app at ${green(adminUrl)}`, indent));
}

0 comments on commit ff788b1

Please sign in to comment.