1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs");
|
4 | const request = require("request");
|
5 | const retry = require("retry");
|
6 | function fetchSitemap(options) {
|
7 | return loadDataFromMagnolia({
|
8 | authHeader: options.magnolia.auth.header,
|
9 | url: options.magnolia.url + options.magnolia.sitemapEndpoint,
|
10 | errorMessage: "Attempt to get the sitemap failed, will retry in some time...",
|
11 | resolverHandler: body => body,
|
12 | retryOptions: { forever: true }
|
13 | });
|
14 | }
|
15 | exports.fetchSitemap = fetchSitemap;
|
16 | function fetchWorkspace(workspace, options) {
|
17 | return loadDataFromMagnolia({
|
18 | authHeader: options.magnolia.auth.header,
|
19 | url: options.magnolia.url + "/.rest/delivery/" + workspace + "/v1",
|
20 | errorMessage: `Attempt to get workspace ${workspace} failed, will retry in some time...`,
|
21 | resolverHandler: body => body.results
|
22 | });
|
23 | }
|
24 | exports.fetchWorkspace = fetchWorkspace;
|
25 | function fetchPages(options) {
|
26 | return loadDataFromMagnolia({
|
27 | authHeader: options.magnolia.auth.header,
|
28 | url: options.magnolia.url + options.magnolia.pagesEndpoint,
|
29 | errorMessage: `Attempt to get pages failed, will retry in some time...`,
|
30 | resolverHandler: body => body.results
|
31 | });
|
32 | }
|
33 | exports.fetchPages = fetchPages;
|
34 | function loadDataFromMagnolia(params) {
|
35 | const operation = retry.operation(params.retryOptions);
|
36 | return new Promise(resolve => {
|
37 | operation.attempt(() => {
|
38 | request.get(params.url, {
|
39 | json: true,
|
40 | headers: {
|
41 | Authorization: params.authHeader,
|
42 | "User-Agent": "Paperboy"
|
43 | },
|
44 | timeout: 60 * 1000
|
45 | }, (error, response, body) => {
|
46 | const operationError = error ||
|
47 | (response && response.statusCode !== 200
|
48 | ? new Error(`Return code was: ${response.statusCode}`)
|
49 | : undefined);
|
50 | if (operation.retry(operationError)) {
|
51 | console.error(params.errorMessage);
|
52 | if (error) {
|
53 | console.error(error);
|
54 | }
|
55 | return;
|
56 | }
|
57 | resolve(params.resolverHandler(body));
|
58 | });
|
59 | });
|
60 | });
|
61 | }
|
62 | function writePagesFile(pages, options) {
|
63 | return new Promise((resolve, reject) => {
|
64 | if (!fs.existsSync(options.output.json)) {
|
65 | fs.mkdirSync(options.output.json);
|
66 | }
|
67 | fs.writeFile(options.output.json + "/pages.json", JSON.stringify(pages), err => {
|
68 | if (err) {
|
69 | reject(err);
|
70 | }
|
71 | resolve();
|
72 | });
|
73 | });
|
74 | }
|
75 | exports.writePagesFile = writePagesFile;
|
76 | function writeWorkspaceFile(workspace, workspaceData, options) {
|
77 | return new Promise((resolve, reject) => {
|
78 | if (!fs.existsSync(options.output.json)) {
|
79 | fs.mkdirSync(options.output.json);
|
80 | }
|
81 | fs.writeFile(options.output.json + "/" + workspace + ".json", JSON.stringify(workspaceData), err => {
|
82 | if (err) {
|
83 | reject(err);
|
84 | }
|
85 | resolve();
|
86 | });
|
87 | });
|
88 | }
|
89 | exports.writeWorkspaceFile = writeWorkspaceFile;
|
90 | function sanitizeJson(json, damAssets, pages, sourceOptions, workspaces = {}) {
|
91 | const sanitized = {};
|
92 | if (json) {
|
93 | Object.keys(json).forEach(key => {
|
94 | const isKeyExcluded = sourceOptions &&
|
95 | sourceOptions.output.excludedProperties &&
|
96 | sourceOptions.output.excludedProperties.findIndex(prop => prop === key) > -1;
|
97 | if (!isKeyExcluded && key === "@nodes") {
|
98 | const contentOrder = json[key];
|
99 | if (contentOrder.length > 0) {
|
100 | sanitized[key.substr(1)] = contentOrder.map(contentKeyIndex => sanitizeJson(json[contentKeyIndex], damAssets, pages, sourceOptions, workspaces));
|
101 | }
|
102 | }
|
103 | else if (!isKeyExcluded && key !== "content" && !key.match(/^\d+$/)) {
|
104 | const originalKey = key;
|
105 | const sanitizedKey = key
|
106 | .replace(/^@/, "")
|
107 | .replace(/^mgnl:/, "")
|
108 | .replace(/^jcr:uuid/, "id");
|
109 | if (!sanitizedKey.match(/^jcr:/)) {
|
110 | if (typeof json[key] === "object" && !Array.isArray(json[key])) {
|
111 | sanitized[sanitizedKey] = sanitizeJson(json[key], damAssets, pages, sourceOptions, workspaces);
|
112 | }
|
113 | else {
|
114 | let items = Array.isArray(json[key]) ? json[key] : [json[key]];
|
115 | items = items.map((item) => {
|
116 | if (item.match(/^jcr:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)) {
|
117 | const uuid = item.replace("jcr:", "");
|
118 | return damAssets.find(damAsset => damAsset && damAsset.id === uuid);
|
119 | }
|
120 | else if (!originalKey.match(/^@/) &&
|
121 | !originalKey.match(/^jcr:uuid/) &&
|
122 | item.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)) {
|
123 | const node = getPopulatedNode(item, pages);
|
124 | let value;
|
125 | if (node) {
|
126 | value = Object.assign(node || {}, {
|
127 | workspace: "website"
|
128 | });
|
129 | }
|
130 | else {
|
131 | const asset = getPopulatedNode(item, damAssets);
|
132 | if (asset) {
|
133 | value = Object.assign(asset || {}, {
|
134 | workspace: "dam"
|
135 | });
|
136 | }
|
137 | else {
|
138 | value = {};
|
139 | Object.keys(workspaces)
|
140 | .forEach(key => {
|
141 | const foundItem = getPopulatedNode(item, workspaces[key]);
|
142 | if (foundItem) {
|
143 | value = Object.assign({}, foundItem, { workspace: key });
|
144 | }
|
145 | });
|
146 | }
|
147 | }
|
148 | return value;
|
149 | }
|
150 | else {
|
151 | return item;
|
152 | }
|
153 | });
|
154 | sanitized[sanitizedKey] = Array.isArray(json[key])
|
155 | ? items
|
156 | : items[0];
|
157 | }
|
158 | }
|
159 | }
|
160 | });
|
161 | }
|
162 | return sanitized;
|
163 | }
|
164 | exports.sanitizeJson = sanitizeJson;
|
165 | function getPopulatedNode(id, source, populatedNode) {
|
166 | if (populatedNode || !source) {
|
167 | return populatedNode;
|
168 | }
|
169 | else {
|
170 | if (source["jcr:uuid"] === id || source.id === id) {
|
171 | populatedNode = {
|
172 | id,
|
173 | path: source["@path"] || source.path
|
174 | };
|
175 | }
|
176 | else {
|
177 | Object.keys(source).forEach(key => {
|
178 | if (source[key] && typeof source[key] === "object") {
|
179 | populatedNode = getPopulatedNode(id, source[key], populatedNode);
|
180 | }
|
181 | });
|
182 | }
|
183 | return populatedNode;
|
184 | }
|
185 | }
|
186 | exports.getPopulatedNode = getPopulatedNode;
|
187 |
|
\ | No newline at end of file |