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