forked from webiny/webiny-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rerenderPages.ts
85 lines (78 loc) · 2.68 KB
/
rerenderPages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import getStackOutput from "@webiny/cli-plugin-deploy-pulumi/utils/getStackOutput";
import { GraphQLClient } from "graphql-request";
import fetch from "node-fetch";
// Or pull from env variables (.env or env.{environment-name} files).
const API_TOKEN = "xxx";
export default {
type: "cli-command",
name: "cli-command-rerender",
// Here we create the command, using "yargs" library.
create({ yargs, context }) {
yargs.example("$0 rerender-pages ");
yargs.command(
"rerender-pages",
`Re-renders all published pages created with the Page Builder application.`,
{},
// This is the function that'll be called when the command is executed.
async args => {
// Get exports from `api` stack, for `args.env` environment.
const { apiUrl } = await getStackOutput("api", args.env);
const client = new GraphQLClient(`${apiUrl}/graphql`, {
headers: {
authorization: API_TOKEN
}
});
const pages = await client
.request(LIST_PUBLISHED_PAGES)
.then(data => data.pageBuilder.listPublishedPages.data);
for (let i = 0; i < pages.length; i++) {
const page = pages[i];
const { status } = await fetch(page.url);
if (status !== 200) {
try {
await client.request(RERENDER_PAGE, page);
context.success(
`Successfully re-rendered ${context.success.hl(page.url)} page.`
);
} catch (e) {
context.error(
`Could not re-render ${context.success.hl(page.url)} page:`
);
console.log(e.message);
}
}
}
}
);
}
};
const LIST_PUBLISHED_PAGES = /* GraphQL */ `
{
pageBuilder {
listPublishedPages {
data {
id
title
url
}
}
}
}
`;
// Creates a new revision of specified pages.
const RERENDER_PAGE = /* GraphQL */ `
mutation ReRenderPage($id: ID!) {
pageBuilder {
rerenderPage(id: $id) {
data {
id
}
error {
data
code
message
}
}
}
}
`;