-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.ts
More file actions
98 lines (84 loc) · 3.45 KB
/
base.ts
File metadata and controls
98 lines (84 loc) · 3.45 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
import { resolve as pResolve } from 'node:path';
import { writeFile } from 'node:fs/promises';
import { FsUtility, log, CLIProgressManager, configHandler } from '@contentstack/cli-utilities';
import type { AssetManagementAPIConfig } from '../types/asset-management-api';
import type { ExportContext } from '../types/export-types';
import { AssetManagementAdapter } from '../utils/asset-management-api-adapter';
import { AM_MAIN_PROCESS_NAME, FALLBACK_AM_CHUNK_FILE_SIZE_MB } from '../constants/index';
export type { ExportContext };
/**
* Base class for export modules. Extends the API adapter and adds export context,
* internal progress management, and shared write helpers.
*/
export class AssetManagementExportAdapter extends AssetManagementAdapter {
protected readonly apiConfig: AssetManagementAPIConfig;
protected readonly exportContext: ExportContext;
protected progressManager: CLIProgressManager | null = null;
protected parentProgressManager: CLIProgressManager | null = null;
protected readonly processName: string = AM_MAIN_PROCESS_NAME;
constructor(apiConfig: AssetManagementAPIConfig, exportContext: ExportContext) {
super(apiConfig);
this.apiConfig = apiConfig;
this.exportContext = exportContext;
}
public setParentProgressManager(parent: CLIProgressManager): void {
this.parentProgressManager = parent;
}
protected get progressOrParent(): CLIProgressManager | null {
return this.parentProgressManager ?? this.progressManager;
}
protected createNestedProgress(moduleName: string): CLIProgressManager {
if (this.parentProgressManager) {
this.progressManager = this.parentProgressManager;
return this.parentProgressManager;
}
const logConfig = configHandler.get('log') || {};
const showConsoleLogs = logConfig.showConsoleLogs ?? false;
this.progressManager = CLIProgressManager.createNested(moduleName, showConsoleLogs);
return this.progressManager;
}
protected tick(success: boolean, itemName: string, error: string | null, processName?: string): void {
this.progressOrParent?.tick?.(success, itemName, error, processName ?? this.processName);
}
protected updateStatus(message: string, processName?: string): void {
this.progressOrParent?.updateStatus?.(message, processName ?? this.processName);
}
protected completeProcess(processName: string, success: boolean): void {
if (!this.parentProgressManager) {
this.progressManager?.completeProcess?.(processName, success);
}
}
protected get spacesRootPath(): string {
return this.exportContext.spacesRootPath;
}
protected getAssetTypesDir(): string {
return pResolve(this.exportContext.spacesRootPath, 'asset_types');
}
protected getFieldsDir(): string {
return pResolve(this.exportContext.spacesRootPath, 'fields');
}
protected async writeItemsToChunkedJson(
dir: string,
indexFileName: string,
moduleName: string,
metaPickKeys: string[],
items: unknown[],
): Promise<void> {
if (items.length === 0) {
await writeFile(pResolve(dir, indexFileName), '{}');
return;
}
const chunkMb = this.exportContext.chunkFileSizeMb ?? FALLBACK_AM_CHUNK_FILE_SIZE_MB;
const fs = new FsUtility({
basePath: dir,
indexFileName,
chunkFileSize: chunkMb,
moduleName,
fileExt: 'json',
metaPickKeys,
keepMetadata: true,
});
fs.writeIntoFile(items as Record<string, string>[], { mapKeyVal: true });
fs.completeFile(true);
}
}