UNPKG

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