-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathassets.ts
More file actions
362 lines (325 loc) · 13.2 KB
/
assets.ts
File metadata and controls
362 lines (325 loc) · 13.2 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import map from 'lodash/map';
import values from 'lodash/values';
import filter from 'lodash/filter';
import unionBy from 'lodash/unionBy';
import orderBy from 'lodash/orderBy';
import isEmpty from 'lodash/isEmpty';
import uniq from 'lodash/uniq';
import { existsSync } from 'node:fs';
import includes from 'lodash/includes';
import { v4 as uuid } from 'uuid';
import { resolve as pResolve, join } from 'node:path';
import { FsUtility } from '@contentstack/cli-utilities';
import config from '../../config';
import { log, formatError, formatDate } from '../../utils';
import BaseClass, { ApiOptions } from './base-class';
import { ModuleClassParams } from '../../types';
export default class ImportAssets extends BaseClass {
private fs: FsUtility;
private assetsPath: string;
private mapperDirPath: string;
private assetsRootPath: string;
private assetUidMapperPath: string;
private assetUrlMapperPath: string;
private assetFolderUidMapperPath: string;
public assetConfig = config.modules.assets;
private environments: Record<string, any> = {};
private assetsUidMap: Record<string, unknown> = {};
private assetsUrlMap: Record<string, unknown> = {};
private assetsFolderMap: Record<string, unknown> = {};
private rootFolder: { uid: string; name: string; parent_uid: string; created_at: string };
constructor({ importConfig, stackAPIClient }: ModuleClassParams) {
super({ importConfig, stackAPIClient });
this.assetsPath = join(this.importConfig.backupDir, 'assets');
this.mapperDirPath = join(this.importConfig.backupDir, 'mapper', 'assets');
this.assetUidMapperPath = join(this.mapperDirPath, 'uid-mapping.json');
this.assetUrlMapperPath = join(this.mapperDirPath, 'url-mapping.json');
this.assetFolderUidMapperPath = join(this.mapperDirPath, 'folder-mapping.json');
this.assetsRootPath = join(this.importConfig.backupDir, this.assetConfig.dirName);
this.fs = new FsUtility({ basePath: this.mapperDirPath });
this.environments = this.fs.readFile(
join(this.importConfig.backupDir, 'environments', 'environments.json'),
true,
) as Record<string, unknown>;
}
/**
* @method start
* @returns {Promise<void>} Promise<any>
*/
async start(): Promise<void> {
// NOTE Step 1: Import folders and create uid mapping file
await this.importFolders();
// NOTE Step 2: Import versioned assets and create it mapping files (uid, url)
if (this.assetConfig.includeVersionedAssets) {
if (existsSync(`${this.assetsPath}/versions`)) await this.importAssets(true);
else log(this.importConfig, 'No Versioned assets found to import', 'info');
}
// NOTE Step 3: Import Assets and create it mapping files (uid, url)
await this.importAssets();
// NOTE Step 4: Publish assets
if (!this.importConfig.skipAssetsPublish) await this.publish();
}
/**
* @method importFolders
* @returns {Promise<any>} Promise<any>
*/
async importFolders(): Promise<any> {
const folders = this.fs.readFile(pResolve(this.assetsRootPath, 'folders.json'));
if (isEmpty(folders)) {
log(this.importConfig, 'No folders found to import', 'info');
return;
}
const batches = this.constructFolderImportOrder(folders);
const onSuccess = ({ response, apiData: { uid, name } = { uid: null, name: '' } }: any) => {
this.assetsFolderMap[uid] = response.uid;
log(this.importConfig, `Created folder: '${name}'`, 'success');
};
const onReject = ({ error, apiData: { name } = { name: '' } }: any) => {
log(this.importConfig, `${name} folder creation failed.!`, 'error');
log(this.importConfig, formatError(error), 'error');
};
const serializeData = (apiOptions: ApiOptions) => {
if (apiOptions.apiData.parent_uid) {
apiOptions.apiData.parent_uid = this.assetsFolderMap[apiOptions.apiData.parent_uid];
}
return apiOptions;
};
const batch = map(unionBy(batches, 'parent_uid'), 'parent_uid');
for (const parent_uid of batch) {
// NOTE create parent folders
/* eslint-disable no-await-in-loop */
await this.makeConcurrentCall(
{
apiContent: orderBy(filter(batches, { parent_uid }), 'created_at'),
processName: 'import assets folders',
apiParams: {
serializeData,
reject: onReject,
resolve: onSuccess,
entity: 'create-assets-folder',
includeParamOnCompletion: true,
},
concurrencyLimit: this.assetConfig.importFoldersConcurrency,
},
undefined,
false,
);
}
if (!isEmpty(this.assetsFolderMap)) {
this.fs.writeFile(this.assetFolderUidMapperPath, this.assetsFolderMap);
}
}
/**
* @method importAssets
* @param {boolean} isVersion boolean
* @returns {Promise<void>} Promise<void>
*/
async importAssets(isVersion = false): Promise<void> {
const processName = isVersion ? 'import versioned assets' : 'import assets';
const indexFileName = isVersion ? 'versioned-assets.json' : 'assets.json';
const basePath = isVersion ? join(this.assetsPath, 'versions') : this.assetsPath;
const fs = new FsUtility({ basePath, indexFileName });
const indexer = fs.indexFileContent;
const indexerCount = values(indexer).length;
const onSuccess = ({ response = {}, apiData: { uid, url, title } = undefined }: any) => {
this.assetsUidMap[uid] = response.uid;
this.assetsUrlMap[url] = response.url;
log(this.importConfig, `Created asset: '${title}'`, 'info');
};
const onReject = ({ error, apiData: { title } = undefined }: any) => {
log(this.importConfig, `${title} asset upload failed.!`, 'error');
log(this.importConfig, formatError(error), 'error');
};
/* eslint-disable @typescript-eslint/no-unused-vars, guard-for-in */
for (const index in indexer) {
const chunk = await fs.readChunkFiles.next().catch((error) => {
log(this.importConfig, error, 'error');
});
if (chunk) {
let apiContent = orderBy(values(chunk as Record<string, any>[]), '_version');
if (isVersion && this.assetConfig.importSameStructure) {
// NOTE to create same structure it must have seed assets/version 1 asset to be created first
await this.makeConcurrentCall({
processName,
indexerCount,
currentIndexer: +index,
apiContent: filter(apiContent, ({ _version }) => _version === 1),
apiParams: {
reject: onReject,
resolve: onSuccess,
entity: 'create-assets',
includeParamOnCompletion: true,
serializeData: this.serializeAssets.bind(this),
},
concurrencyLimit: this.assetConfig.uploadAssetsConcurrency,
});
apiContent = filter(apiContent, ({ _version }) => _version > 1);
}
await this.makeConcurrentCall(
{
apiContent,
processName,
indexerCount,
currentIndexer: +index,
apiParams: {
reject: onReject,
resolve: onSuccess,
entity: 'create-assets',
includeParamOnCompletion: true,
serializeData: this.serializeAssets.bind(this),
},
concurrencyLimit: this.assetConfig.uploadAssetsConcurrency,
},
undefined,
!isVersion,
);
}
}
if (!isVersion && (!isEmpty(this.assetsUidMap) || !isEmpty(this.assetsUrlMap))) {
this.fs.writeFile(this.assetUidMapperPath, this.assetsUidMap);
this.fs.writeFile(this.assetUrlMapperPath, this.assetsUrlMap);
}
}
/**
* @method serializeAssets
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeAssets(apiOptions: ApiOptions): ApiOptions {
const { apiData: asset } = apiOptions;
if (
!this.assetConfig.importSameStructure &&
!this.assetConfig.includeVersionedAssets &&
/* eslint-disable @typescript-eslint/no-unused-vars, no-prototype-builtins */
this.assetsUidMap.hasOwnProperty(asset.uid)
) {
log(
this.importConfig,
`Skipping upload of asset: ${asset.uid}. Its mapped to: ${this.assetsUidMap[asset.uid]}`,
'success',
);
apiOptions.entity = undefined;
return apiOptions;
}
asset.upload = join(this.assetsPath, 'files', asset.uid, asset.filename);
if (asset.parent_uid) {
asset.parent_uid = this.assetsFolderMap[asset.parent_uid];
} else if (this.importConfig.replaceExisting) {
// adds the root folder as parent for all assets in the root level
asset.parent_uid = this.assetsFolderMap[this.rootFolder.uid];
}
apiOptions.apiData = asset;
if (this.assetsUidMap[asset.uid] && this.assetConfig.importSameStructure) {
apiOptions.entity = 'replace-assets';
apiOptions.uid = this.assetsUidMap[asset.uid] as string;
}
return apiOptions;
}
/**
* @method publish
* @returns {Promise<void>} Promise<void>
*/
async publish() {
const fs = new FsUtility({ basePath: this.assetsPath, indexFileName: 'assets.json' });
if (isEmpty(this.assetsUidMap)) {
this.assetsUidMap = fs.readFile(this.assetUidMapperPath, true) as any;
}
const indexer = fs.indexFileContent;
const indexerCount = values(indexer).length;
const onSuccess = ({ apiData: { uid, title } = undefined }: any) => {
log(this.importConfig, `Asset '${uid}: ${title}' published successfully`, 'success');
};
const onReject = ({ error, apiData: { uid, title } = undefined }: any) => {
log(this.importConfig, `Asset '${uid}: ${title}' not published`, 'error');
log(this.importConfig, formatError(error), 'error');
};
const serializeData = (apiOptions: ApiOptions) => {
const { apiData: asset } = apiOptions;
const publishDetails = filter(asset.publish_details, ({ environment }) => {
return this.environments?.hasOwnProperty(environment);
});
if (publishDetails.length) {
const environments = uniq(map(publishDetails, ({ environment }) => this.environments[environment].name));
const locales = uniq(map(publishDetails, 'locale'));
if (environments.length === 0 || locales.length === 0) {
apiOptions.entity = undefined
return apiOptions;
}
asset.locales = locales;
asset.environments = environments;
apiOptions.apiData.publishDetails = { locales, environments };
}
apiOptions.uid = this.assetsUidMap[asset.uid] as string;
if (!apiOptions.uid) apiOptions.entity = undefined;
return apiOptions;
};
/* eslint-disable @typescript-eslint/no-unused-vars */
for (const index in indexer) {
const apiContent = filter(
values(await fs.readChunkFiles.next()),
({ publish_details }) => !isEmpty(publish_details),
);
await this.makeConcurrentCall({
apiContent,
indexerCount,
currentIndexer: +index,
processName: 'assets publish',
apiParams: {
serializeData,
reject: onReject,
resolve: onSuccess,
entity: 'publish-assets',
includeParamOnCompletion: true,
},
concurrencyLimit: this.assetConfig.uploadAssetsConcurrency,
});
}
}
/**
* @method constructFolderImportOrder
* @param {Record<string, any>[]} folders object
* @returns {Array<Record<string, any>>} Array<Record<string, any>>
*/
constructFolderImportOrder(folders: any): Array<Record<string, any>> {
let parentUIds: unknown[] = [];
// NOTE: Read root folder
const importOrder = filter(folders, { parent_uid: null }).map(({ uid, name, parent_uid, created_at }) => {
parentUIds.push(uid);
return { uid, name, parent_uid, created_at };
});
while (!isEmpty(parentUIds)) {
// NOTE: Read nested folders every iteration until we find empty folders
parentUIds = filter(folders, ({ parent_uid }) => includes(parentUIds, parent_uid)).map(
({ uid, name, parent_uid, created_at }) => {
importOrder.push({ uid, name, parent_uid, created_at });
return uid;
},
);
}
if (this.importConfig.replaceExisting) {
// Note: adds a root folder to distinguish latest asset uploads
// Todo: This temporary approach should be updated with asset and folder overwrite strategy, which follows
// folder overwrite
// 1. Create folder trees, 2. Export all target stack folders, 3.Match the source to target folders and create a list of existing folders
// 4. Replace existing folders
// Asset overwrite
// 1. Search asset with title + filename + type
// 2. if there are multiple assets fetched with same query, then check the parent uid against mapper created while importing folders
// 3. Replace matched assets
this.rootFolder = {
uid: uuid(),
name: `Import-${formatDate()}`,
parent_uid: null,
created_at: null,
};
filter(importOrder, (folder, index) => {
if (!folder.parent_uid) {
importOrder.splice(index, 1, { ...folder, parent_uid: this.rootFolder.uid });
}
});
// NOTE: Adds root folder
importOrder.unshift(this.rootFolder);
}
return importOrder;
}
}