UNPKG

7.47 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs");
4const request = require("request");
5const retry = require("retry");
6function 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}
32exports.fetchSitemap = fetchSitemap;
33function 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}
58exports.fetchWorkspace = fetchWorkspace;
59function 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}
84exports.fetchPages = fetchPages;
85function 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}
98exports.writePagesFile = writePagesFile;
99function 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}
112exports.writeWorkspaceFile = writeWorkspaceFile;
113function sanitizeJson(json, damAssets, pages, sourceOptions) {
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));
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') {
134 sanitized[sanitizedKey] = sanitizeJson(json[key], damAssets, pages, sourceOptions);
135 }
136 else {
137 if (json[key].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}$/)) {
138 const uuid = json[key].replace('jcr:', '');
139 sanitized[sanitizedKey] = damAssets.find(damAsset => damAsset && damAsset.id === uuid);
140 }
141 else if (!originalKey.match(/^@/) &&
142 !originalKey.match(/^jcr:uuid/) &&
143 json[key].match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)) {
144 const node = getPopulatedNode(json[key], pages);
145 let value;
146 if (node) {
147 value = Object.assign(getPopulatedNode(json[key], pages) || {}, {
148 workspace: 'website'
149 });
150 }
151 else {
152 value = Object.assign(getPopulatedNode(json[key], damAssets) || {}, {
153 workspace: 'dam'
154 });
155 }
156 sanitized[sanitizedKey] = value;
157 }
158 else {
159 sanitized[sanitizedKey] = json[key];
160 }
161 }
162 }
163 }
164 });
165 }
166 return sanitized;
167}
168exports.sanitizeJson = sanitizeJson;
169function getPopulatedNode(id, source, populatedNode) {
170 if (populatedNode || !source) {
171 return populatedNode;
172 }
173 else {
174 if (source['jcr:uuid'] === id || source.id === id) {
175 populatedNode = {
176 id,
177 path: source['@path'] || source.path
178 };
179 }
180 else {
181 Object.keys(source).forEach(key => {
182 if (source[key] && typeof source[key] === 'object') {
183 populatedNode = getPopulatedNode(id, source[key], populatedNode);
184 }
185 });
186 }
187 return populatedNode;
188 }
189}
190exports.getPopulatedNode = getPopulatedNode;
191//# sourceMappingURL=pages.util.js.map
\No newline at end of file