Skip to content

Commit

Permalink
fix: prettify environments output
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Aug 7, 2020
1 parent af783cf commit 994147a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 58 deletions.
75 changes: 46 additions & 29 deletions packages/cwp-template-full/hooks/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
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");

const getTitle = (environmentsCount = 0) => {
let title = "Stack: apps";
if (environmentsCount) {
title += ` (${environmentsCount} environment`;
title += environmentsCount > 1 ? `s)` : `)`;
}
return cyan(title);
};

module.exports = (opts = {}) => (
{
Expand All @@ -23,39 +33,45 @@ module.exports = (opts = {}) => (
{
name: "hook-stacks-info-api",
type: "hook-stacks-info",
async hook() {
async hook({ last }) {
const stackName = opts.stackName || "api";
const stackFolder = `./.webiny/state/${stackName}`;

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

const info = [];
const stackEnvs = fs.readdirSync(stackFolder);
for (const stackEnv of stackEnvs) {
const webinyJson = await readJson(path.join(stackFolder, stackEnv, "Webiny.json"));
if (webinyJson.outputs) {
const url = webinyJson.outputs.cdn.url;
info.push({ stack: stackName, env: stackEnv, url });
const stackEnvs = fs.readdirSync(stackFolder);
if (!stackEnvs.length) {
console.log(getTitle());
console.log("Nothing to show (stack not deployed).");
return;
}
}

if (info.length) {
console.log(`List of URLs for stack "${stackName}"`);
const prettyInfo =
info
.map(
stackInfo =>
` Environment "${stackInfo.env}"\n` +
` ${stackInfo.url}/graphql\n` +
` ${stackInfo.url}/cms/read/production\n` +
` ${stackInfo.url}/cms/preview/production`
)
.join("\n\n") + "\n\n";
console.log(prettyInfo);
} else {
console.log(`There are no available URLs for stack ${stackName} yet.`);
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 });
}

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();
}
}
}
Expand Down Expand Up @@ -97,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 @@ -111,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
77 changes: 48 additions & 29 deletions packages/cwp-template-full/hooks/apps.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
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");

const getTitle = (environmentsCount = 0) => {
let title = "Stack: apps";
if (environmentsCount) {
title += ` (${environmentsCount} environment`;
title += environmentsCount > 1 ? `s)` : `)`;
}
return cyan(title);
};

module.exports = (opts = {}) => (
{
Expand All @@ -23,38 +33,45 @@ module.exports = (opts = {}) => (
{
name: "hook-stacks-info-apps",
type: "hook-stacks-info",
async hook() {
async hook({ last }) {
const stackName = opts.stackName || "apps";
const stackFolder = `./.webiny/state/${stackName}`;

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

const info = [];
const stackEnvs = fs.readdirSync(stackFolder);
for (const stackEnv of stackEnvs) {
const webinyJson = await readJson(path.join(stackFolder, stackEnv, "Webiny.json"));
if (webinyJson.outputs) {
const url = webinyJson.outputs.cdn.url;
info.push({ stack: stackName, env: stackEnv, url });
const stackEnvs = fs.readdirSync(stackFolder);
if (!stackEnvs.length) {
console.log(getTitle());
console.log("Nothing to show (stack not deployed).");
return;
}
}

if (info.length) {
console.log(`List of URLs for stack "${stackName}"`);
const prettyInfo =
info
.map(
stackInfo =>
` Environment "${stackInfo.env}"\n` +
` ${stackInfo.url}/ (site app)\n` +
` ${stackInfo.url}/admin`
)
.join("\n\n") + "\n\n";
console.log(prettyInfo);
} else {
console.log(`There are no available URLs for stack ${stackName} yet.`);
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 });
}

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();
}
}
}
Expand Down Expand Up @@ -85,7 +102,7 @@ function printFirstDeploySummary({ state }) {
);
}

function printDeploySummary({ state }) {
function printDeploySummary({ state, indent = 0 }) {
if (!state.cdn) {
return;
}
Expand All @@ -97,6 +114,8 @@ function printDeploySummary({ state }) {
`🔗 Access your apps at:`,
` - ${green("site")} app: ${green(state.cdn.url)}`,
` - ${green("admin")} app: ${green(adminUrl)}`
].join("\n")
]
.map(item => indentString(item, indent))
.join("\n")
);
}

0 comments on commit 994147a

Please sign in to comment.