@squiz/dxp-cli-next 5.26.0-develop.4 → 5.26.0-develop.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/migration/create/{createMigration.js → create.js} +5 -5
- package/lib/migration/create/{createMigration.spec.js → create.spec.js} +44 -41
- package/lib/migration/get/{getMigration.js → get.js} +4 -6
- package/lib/migration/get/{getMigration.spec.js → get.spec.js} +39 -30
- package/lib/migration/index.js +9 -7
- package/lib/migration/next/next.d.ts +3 -0
- package/lib/migration/next/next.js +46 -0
- package/lib/migration/next/next.spec.js +186 -0
- package/lib/migration/revert/revert.js +5 -6
- package/lib/migration/revert/revert.spec.js +10 -7
- package/lib/migration/settings/{setMigrationSettings.js → settings.js} +7 -8
- package/lib/migration/settings/{setMigrationSettings.spec.js → settings.spec.js} +34 -26
- package/lib/migration/types/common.types.d.ts +19 -0
- package/lib/migration/types/createMigration.types.d.ts +7 -0
- package/lib/migration/types/createMigration.types.js +2 -0
- package/lib/migration/types/getMigration.types.d.ts +5 -0
- package/lib/migration/types/getMigration.types.js +2 -0
- package/lib/migration/types/index.d.ts +6 -0
- package/lib/migration/types/index.js +22 -0
- package/lib/migration/types/nextStage.types.d.ts +6 -0
- package/lib/migration/types/nextStage.types.js +2 -0
- package/lib/migration/types/revert.types.d.ts +6 -0
- package/lib/migration/types/revert.types.js +2 -0
- package/lib/migration/types/settings.types.d.ts +10 -0
- package/lib/migration/types/settings.types.js +2 -0
- package/lib/migration/utils/common.d.ts +32 -0
- package/lib/migration/utils/common.js +129 -0
- package/lib/migration/utils/common.spec.d.ts +1 -0
- package/lib/migration/utils/common.spec.js +196 -0
- package/lib/migration/utils/createMigration.d.ts +9 -0
- package/lib/migration/utils/createMigration.js +122 -0
- package/lib/migration/utils/createMigration.spec.d.ts +1 -0
- package/lib/migration/utils/createMigration.spec.js +276 -0
- package/lib/migration/utils/getMigration.d.ts +2 -0
- package/lib/migration/utils/getMigration.js +41 -0
- package/lib/migration/utils/getMigration.spec.d.ts +1 -0
- package/lib/migration/utils/getMigration.spec.js +156 -0
- package/lib/migration/utils/index.d.ts +7 -0
- package/lib/migration/utils/index.js +23 -0
- package/lib/migration/utils/nextStage.d.ts +2 -0
- package/lib/migration/utils/nextStage.js +44 -0
- package/lib/migration/utils/nextStage.spec.d.ts +1 -0
- package/lib/migration/utils/nextStage.spec.js +151 -0
- package/lib/migration/utils/options.d.ts +12 -0
- package/lib/migration/utils/options.js +82 -0
- package/lib/migration/utils/options.spec.d.ts +1 -0
- package/lib/migration/utils/options.spec.js +115 -0
- package/lib/migration/utils/revertMigration.d.ts +2 -0
- package/lib/migration/utils/revertMigration.js +38 -0
- package/lib/migration/utils/revertMigration.spec.d.ts +1 -0
- package/lib/migration/utils/revertMigration.spec.js +95 -0
- package/lib/migration/utils/setMigrationSettings.d.ts +2 -0
- package/lib/migration/utils/setMigrationSettings.js +53 -0
- package/lib/migration/utils/setMigrationSettings.spec.d.ts +1 -0
- package/lib/migration/utils/setMigrationSettings.spec.js +156 -0
- package/package.json +1 -1
- package/lib/migration/types.d.ts +0 -54
- package/lib/migration/utils.d.ts +0 -15
- package/lib/migration/utils.js +0 -298
- package/lib/migration/utils.spec.js +0 -617
- /package/lib/migration/create/{createMigration.d.ts → create.d.ts} +0 -0
- /package/lib/migration/create/{createMigration.spec.d.ts → create.spec.d.ts} +0 -0
- /package/lib/migration/get/{getMigration.d.ts → get.d.ts} +0 -0
- /package/lib/migration/get/{getMigration.spec.d.ts → get.spec.d.ts} +0 -0
- /package/lib/migration/{settings/setMigrationSettings.spec.d.ts → next/next.spec.d.ts} +0 -0
- /package/lib/migration/settings/{setMigrationSettings.d.ts → settings.d.ts} +0 -0
- /package/lib/migration/{utils.spec.d.ts → settings/settings.spec.d.ts} +0 -0
- /package/lib/migration/{types.js → types/common.types.js} +0 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const ApiService_1 = require("../../ApiService");
|
|
13
|
+
const setMigrationSettings_1 = require("./setMigrationSettings");
|
|
14
|
+
const _1 = require(".");
|
|
15
|
+
// Mock dependencies
|
|
16
|
+
jest.mock('../../ApiService');
|
|
17
|
+
jest.mock('../../ApplicationConfig');
|
|
18
|
+
jest.mock('.', () => ({
|
|
19
|
+
buildMigrationUrl: jest.fn(),
|
|
20
|
+
getMigrationHeaders: jest.fn(),
|
|
21
|
+
redactKey: jest.fn(),
|
|
22
|
+
}));
|
|
23
|
+
jest.mock('path');
|
|
24
|
+
jest.mock('child_process');
|
|
25
|
+
jest.mock('axios');
|
|
26
|
+
jest.mock('../../ApplicationConfig');
|
|
27
|
+
jest.mock('../../ApiService');
|
|
28
|
+
// Mock global fetch
|
|
29
|
+
const mockFetch = jest.fn();
|
|
30
|
+
global.fetch = mockFetch;
|
|
31
|
+
const mockApiService = ApiService_1.ApiService;
|
|
32
|
+
const mockBuildMigrationUrl = _1.buildMigrationUrl;
|
|
33
|
+
const mockGetMigrationHeaders = _1.getMigrationHeaders;
|
|
34
|
+
const mockRedactKey = _1.redactKey;
|
|
35
|
+
describe('setMigrationSettings', () => {
|
|
36
|
+
let mockApiServiceInstance;
|
|
37
|
+
let mockOptions;
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
jest.clearAllMocks();
|
|
40
|
+
mockApiServiceInstance = {
|
|
41
|
+
client: {
|
|
42
|
+
post: jest.fn(),
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
mockApiService.mockImplementation(() => mockApiServiceInstance);
|
|
46
|
+
mockOptions = {
|
|
47
|
+
tenant: 'test-tenant',
|
|
48
|
+
matrixUrl: 'https://matrix.example.com',
|
|
49
|
+
matrixIdentifier: 'test-identifier',
|
|
50
|
+
matrixKey: 'test-key-123',
|
|
51
|
+
};
|
|
52
|
+
mockBuildMigrationUrl.mockResolvedValue('https://migration.example.com');
|
|
53
|
+
mockGetMigrationHeaders.mockResolvedValue({
|
|
54
|
+
'Content-Type': 'application/json',
|
|
55
|
+
'x-dxp-tenant': 'test-tenant',
|
|
56
|
+
});
|
|
57
|
+
mockRedactKey.mockImplementation(key => `***${key.slice(-3)}`);
|
|
58
|
+
});
|
|
59
|
+
it('should set migration settings successfully', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
60
|
+
const mockResponse = {
|
|
61
|
+
status: 200,
|
|
62
|
+
data: {
|
|
63
|
+
matrixUrl: 'https://matrix.example.com',
|
|
64
|
+
matrixIdentifier: 'test-identifier',
|
|
65
|
+
matrixKey: 'test-key-123',
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
|
|
69
|
+
const result = yield (0, setMigrationSettings_1.setMigrationSettings)(mockOptions);
|
|
70
|
+
expect(result).toEqual({
|
|
71
|
+
matrixUrl: 'https://matrix.example.com',
|
|
72
|
+
matrixIdentifier: 'test-identifier',
|
|
73
|
+
matrixKey: '***123',
|
|
74
|
+
});
|
|
75
|
+
expect(mockBuildMigrationUrl).toHaveBeenCalledWith('test-tenant', undefined, 'settings');
|
|
76
|
+
expect(mockGetMigrationHeaders).toHaveBeenCalledWith('test-tenant');
|
|
77
|
+
expect(mockApiServiceInstance.client.post).toHaveBeenCalledWith('https://migration.example.com', {
|
|
78
|
+
matrixUrl: 'https://matrix.example.com',
|
|
79
|
+
matrixIdentifier: 'test-identifier',
|
|
80
|
+
matrixKey: 'test-key-123',
|
|
81
|
+
}, {
|
|
82
|
+
headers: {
|
|
83
|
+
'Content-Type': 'application/json',
|
|
84
|
+
'x-dxp-tenant': 'test-tenant',
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}));
|
|
88
|
+
it('should handle override URL when provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
|
+
const optionsWithOverride = Object.assign(Object.assign({}, mockOptions), { overrideUrl: 'https://custom.migration.url' });
|
|
90
|
+
mockBuildMigrationUrl.mockResolvedValue('https://custom.migration.url');
|
|
91
|
+
const mockResponse = {
|
|
92
|
+
status: 201,
|
|
93
|
+
data: {
|
|
94
|
+
matrixUrl: 'https://matrix.example.com',
|
|
95
|
+
matrixIdentifier: 'test-identifier',
|
|
96
|
+
matrixKey: 'test-key-123',
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
|
|
100
|
+
yield (0, setMigrationSettings_1.setMigrationSettings)(optionsWithOverride);
|
|
101
|
+
expect(mockBuildMigrationUrl).toHaveBeenCalledWith('test-tenant', 'https://custom.migration.url', 'settings');
|
|
102
|
+
}));
|
|
103
|
+
it('should handle non-success status codes', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
|
+
const mockResponse = {
|
|
105
|
+
status: 400,
|
|
106
|
+
data: {},
|
|
107
|
+
};
|
|
108
|
+
mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
|
|
109
|
+
yield expect((0, setMigrationSettings_1.setMigrationSettings)(mockOptions)).rejects.toThrow('Migration settings update failed with status: 400');
|
|
110
|
+
}));
|
|
111
|
+
it('should handle missing response data', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
112
|
+
const mockResponse = {
|
|
113
|
+
status: 200,
|
|
114
|
+
data: null,
|
|
115
|
+
};
|
|
116
|
+
mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
|
|
117
|
+
yield expect((0, setMigrationSettings_1.setMigrationSettings)(mockOptions)).rejects.toThrow('No data returned from migration service');
|
|
118
|
+
}));
|
|
119
|
+
it('should handle API service errors', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
120
|
+
const error = new Error('Network error');
|
|
121
|
+
mockApiServiceInstance.client.post.mockRejectedValue(error);
|
|
122
|
+
yield expect((0, setMigrationSettings_1.setMigrationSettings)(mockOptions)).rejects.toThrow('Network error');
|
|
123
|
+
}));
|
|
124
|
+
it('should handle unknown errors', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
125
|
+
mockApiServiceInstance.client.post.mockRejectedValue('Unknown error');
|
|
126
|
+
yield expect((0, setMigrationSettings_1.setMigrationSettings)(mockOptions)).rejects.toThrow('Failed to set migration settings: Unknown error');
|
|
127
|
+
}));
|
|
128
|
+
it('should redact matrix key in response', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
129
|
+
const mockResponse = {
|
|
130
|
+
status: 200,
|
|
131
|
+
data: {
|
|
132
|
+
matrixUrl: 'https://matrix.example.com',
|
|
133
|
+
matrixIdentifier: 'test-identifier',
|
|
134
|
+
matrixKey: 'test-key-123',
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
|
|
138
|
+
mockRedactKey.mockReturnValue('***123');
|
|
139
|
+
const result = yield (0, setMigrationSettings_1.setMigrationSettings)(mockOptions);
|
|
140
|
+
expect(result.matrixKey).toBe('***123');
|
|
141
|
+
expect(mockRedactKey).toHaveBeenCalledWith('test-key-123', 3);
|
|
142
|
+
}));
|
|
143
|
+
it('should handle missing matrix key in response', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
144
|
+
const mockResponse = {
|
|
145
|
+
status: 200,
|
|
146
|
+
data: {
|
|
147
|
+
matrixUrl: 'https://matrix.example.com',
|
|
148
|
+
matrixIdentifier: 'test-identifier',
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
mockApiServiceInstance.client.post.mockResolvedValue(mockResponse);
|
|
152
|
+
const result = yield (0, setMigrationSettings_1.setMigrationSettings)(mockOptions);
|
|
153
|
+
expect(result.matrixKey).toBe('');
|
|
154
|
+
expect(mockRedactKey).not.toHaveBeenCalled();
|
|
155
|
+
}));
|
|
156
|
+
});
|
package/package.json
CHANGED
package/lib/migration/types.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
export interface CreateMigrationOptions {
|
|
2
|
-
assetId: string;
|
|
3
|
-
previewAssetId: string;
|
|
4
|
-
matrixUrl: string;
|
|
5
|
-
tenant?: string;
|
|
6
|
-
overrideUrl?: string;
|
|
7
|
-
}
|
|
8
|
-
export interface CreateMigrationAPIResponse {
|
|
9
|
-
assetMigration: {
|
|
10
|
-
migrationId: string;
|
|
11
|
-
assetId: string;
|
|
12
|
-
xmlFilePath: string;
|
|
13
|
-
matrixUrl: string;
|
|
14
|
-
previewAssetId: string;
|
|
15
|
-
stage: string;
|
|
16
|
-
status: string;
|
|
17
|
-
created: number;
|
|
18
|
-
updated: number;
|
|
19
|
-
migrationIdAssetId: string;
|
|
20
|
-
};
|
|
21
|
-
uploadUrl: string;
|
|
22
|
-
}
|
|
23
|
-
export interface GetMigrationOptions {
|
|
24
|
-
migrationId: string;
|
|
25
|
-
assetId: string;
|
|
26
|
-
tenant?: string;
|
|
27
|
-
overrideUrl?: string;
|
|
28
|
-
}
|
|
29
|
-
export interface GetMigrationAPIResponse {
|
|
30
|
-
migrationId: string;
|
|
31
|
-
assetId: string;
|
|
32
|
-
stage: string;
|
|
33
|
-
status: string;
|
|
34
|
-
stageDetails?: string;
|
|
35
|
-
previewPageAssetId?: string;
|
|
36
|
-
componentsTarDownloadUrl?: string;
|
|
37
|
-
}
|
|
38
|
-
export interface RevertMigrationOptions extends GetMigrationOptions {
|
|
39
|
-
}
|
|
40
|
-
export interface RevertMigrationAPIResponse {
|
|
41
|
-
message: string;
|
|
42
|
-
}
|
|
43
|
-
export interface SetMigrationSettingOptions {
|
|
44
|
-
matrixUrl: string;
|
|
45
|
-
matrixIdentifier: string;
|
|
46
|
-
matrixKey: string;
|
|
47
|
-
tenant?: string;
|
|
48
|
-
overrideUrl?: string;
|
|
49
|
-
}
|
|
50
|
-
export interface SetMigrationSettingAPIResponse {
|
|
51
|
-
matrixUrl: string;
|
|
52
|
-
matrixIdentifier: string;
|
|
53
|
-
matrixKey: string;
|
|
54
|
-
}
|
package/lib/migration/utils.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Command } from 'commander';
|
|
2
|
-
import { CreateMigrationOptions, CreateMigrationAPIResponse, GetMigrationOptions, GetMigrationAPIResponse, RevertMigrationOptions, RevertMigrationAPIResponse, SetMigrationSettingOptions, SetMigrationSettingAPIResponse } from './types';
|
|
3
|
-
export declare function handleCommandError(command: Command, error: Error): void;
|
|
4
|
-
export declare function throwErrorIfNotLoggedIn(command: Command): Promise<void>;
|
|
5
|
-
export declare function buildMigrationUrl(tenantID?: string, overrideUrl?: string): Promise<string>;
|
|
6
|
-
export declare function validateAxiosStatus(status: number): boolean;
|
|
7
|
-
export declare function redactKey(key: string, visibleChars?: number): string;
|
|
8
|
-
export declare function validateExportFolder(exportPath: string): void;
|
|
9
|
-
export declare function createTarFile(exportPath: string): Promise<string>;
|
|
10
|
-
export declare function getMigrationHeaders(tenantID?: string): Promise<Record<string, string>>;
|
|
11
|
-
export declare function uploadFileToS3(uploadUrl: string, filePath: string, tenantID?: string): Promise<string>;
|
|
12
|
-
export declare function createMigration(options: CreateMigrationOptions): Promise<CreateMigrationAPIResponse>;
|
|
13
|
-
export declare function getMigration(options: GetMigrationOptions): Promise<GetMigrationAPIResponse>;
|
|
14
|
-
export declare function revertMigration(options: RevertMigrationOptions): Promise<RevertMigrationAPIResponse>;
|
|
15
|
-
export declare function setMigrationSetting(options: SetMigrationSettingOptions): Promise<SetMigrationSettingAPIResponse>;
|
package/lib/migration/utils.js
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.setMigrationSetting = exports.revertMigration = exports.getMigration = exports.createMigration = exports.uploadFileToS3 = exports.getMigrationHeaders = exports.createTarFile = exports.validateExportFolder = exports.redactKey = exports.validateAxiosStatus = exports.buildMigrationUrl = exports.throwErrorIfNotLoggedIn = exports.handleCommandError = void 0;
|
|
16
|
-
const fs_1 = __importDefault(require("fs"));
|
|
17
|
-
const path_1 = __importDefault(require("path"));
|
|
18
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
19
|
-
const ApplicationConfig_1 = require("../ApplicationConfig");
|
|
20
|
-
const axios_1 = __importDefault(require("axios"));
|
|
21
|
-
const ApplicationStore_1 = require("../ApplicationStore");
|
|
22
|
-
const child_process_1 = require("child_process");
|
|
23
|
-
const ApiService_1 = require("../ApiService");
|
|
24
|
-
function handleCommandError(command, error) {
|
|
25
|
-
var _a, _b, _c, _d;
|
|
26
|
-
if (axios_1.default.isAxiosError(error)) {
|
|
27
|
-
let message = `${error.message}`;
|
|
28
|
-
if ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) {
|
|
29
|
-
message += `: ${error.response.data.message}`;
|
|
30
|
-
}
|
|
31
|
-
if ((_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.details) {
|
|
32
|
-
message += ` - ${error.response.data.details}`;
|
|
33
|
-
}
|
|
34
|
-
command.error(chalk_1.default.red(message));
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
if (!!process.env.DEBUG && error.stack) {
|
|
38
|
-
command.error(error.stack);
|
|
39
|
-
}
|
|
40
|
-
if (error.message) {
|
|
41
|
-
command.error(chalk_1.default.red(error.message));
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
command.error(chalk_1.default.red('An unknown error occurred'));
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
exports.handleCommandError = handleCommandError;
|
|
49
|
-
function throwErrorIfNotLoggedIn(command) {
|
|
50
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
if (!(yield (0, ApplicationStore_1.getApplicationFile)(ApplicationStore_1.STORE_FILES.sessionCookie))) {
|
|
52
|
-
command.error(chalk_1.default.red('You must login to interact with the migration service. See `dxp-next auth login`'));
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
exports.throwErrorIfNotLoggedIn = throwErrorIfNotLoggedIn;
|
|
57
|
-
function buildMigrationUrl(tenantID, overrideUrl) {
|
|
58
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
if (!overrideUrl) {
|
|
60
|
-
const existingConfig = yield (0, ApplicationConfig_1.fetchApplicationConfig)(tenantID);
|
|
61
|
-
return `${existingConfig.baseUrl}/__dxp/service/aiapps/migration`;
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
return overrideUrl;
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
exports.buildMigrationUrl = buildMigrationUrl;
|
|
69
|
-
function validateAxiosStatus(status) {
|
|
70
|
-
return status < 400;
|
|
71
|
-
}
|
|
72
|
-
exports.validateAxiosStatus = validateAxiosStatus;
|
|
73
|
-
function redactKey(key, visibleChars = 3) {
|
|
74
|
-
if (!key)
|
|
75
|
-
return '';
|
|
76
|
-
const len = key.length;
|
|
77
|
-
if (len <= visibleChars)
|
|
78
|
-
return '*'.repeat(len);
|
|
79
|
-
const masked = '*'.repeat(len - visibleChars);
|
|
80
|
-
return masked + key.slice(-visibleChars);
|
|
81
|
-
}
|
|
82
|
-
exports.redactKey = redactKey;
|
|
83
|
-
function validateExportFolder(exportPath) {
|
|
84
|
-
// Check if the export folder exists
|
|
85
|
-
if (!fs_1.default.existsSync(exportPath)) {
|
|
86
|
-
throw new Error(`Export folder does not exist: ${exportPath}`);
|
|
87
|
-
}
|
|
88
|
-
// Check if it's a directory
|
|
89
|
-
if (!fs_1.default.statSync(exportPath).isDirectory()) {
|
|
90
|
-
throw new Error(`Export path is not a directory: ${exportPath}`);
|
|
91
|
-
}
|
|
92
|
-
// Check for nested export folder structure (e.g., ./export/export/...)
|
|
93
|
-
const nestedExportPath = path_1.default.join(exportPath, 'export');
|
|
94
|
-
if (!fs_1.default.existsSync(nestedExportPath)) {
|
|
95
|
-
throw new Error(`Nested export folder does not exist: ${nestedExportPath}`);
|
|
96
|
-
}
|
|
97
|
-
if (!fs_1.default.statSync(nestedExportPath).isDirectory()) {
|
|
98
|
-
throw new Error(`Nested export path is not a directory: ${nestedExportPath}`);
|
|
99
|
-
}
|
|
100
|
-
// Check for export.xml file in the nested export directory
|
|
101
|
-
const exportXmlPath = path_1.default.join(nestedExportPath, 'export.xml');
|
|
102
|
-
if (!fs_1.default.existsSync(exportXmlPath)) {
|
|
103
|
-
throw new Error(`export.xml file does not exist in: ${nestedExportPath}`);
|
|
104
|
-
}
|
|
105
|
-
if (!fs_1.default.statSync(exportXmlPath).isFile()) {
|
|
106
|
-
throw new Error(`export.xml is not a valid file: ${exportXmlPath}`);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
exports.validateExportFolder = validateExportFolder;
|
|
110
|
-
function createTarFile(exportPath) {
|
|
111
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
112
|
-
const tarFileName = `export_${Date.now()}.tar.gz`;
|
|
113
|
-
const tarFilePath = path_1.default.join(process.cwd(), tarFileName);
|
|
114
|
-
return new Promise((resolve, reject) => {
|
|
115
|
-
const tar = (0, child_process_1.spawn)('tar', [
|
|
116
|
-
'-czf',
|
|
117
|
-
tarFilePath,
|
|
118
|
-
'-C',
|
|
119
|
-
path_1.default.dirname(exportPath),
|
|
120
|
-
path_1.default.basename(exportPath),
|
|
121
|
-
]);
|
|
122
|
-
tar.on('close', code => {
|
|
123
|
-
if (code === 0) {
|
|
124
|
-
resolve(tarFilePath);
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
reject(new Error(`tar command failed with exit code ${code}`));
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
tar.on('error', error => {
|
|
131
|
-
reject(new Error(`Failed to create tar file: ${error.message}`));
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
exports.createTarFile = createTarFile;
|
|
137
|
-
function getMigrationHeaders(tenantID) {
|
|
138
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
-
const existingConfig = yield (0, ApplicationConfig_1.fetchApplicationConfig)(tenantID);
|
|
140
|
-
return {
|
|
141
|
-
'x-dxp-tenant': existingConfig.tenant,
|
|
142
|
-
};
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
exports.getMigrationHeaders = getMigrationHeaders;
|
|
146
|
-
function uploadFileToS3(uploadUrl, filePath, tenantID) {
|
|
147
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
-
const fileBuffer = fs_1.default.readFileSync(filePath);
|
|
149
|
-
const response = yield fetch(uploadUrl, {
|
|
150
|
-
method: 'PUT',
|
|
151
|
-
body: fileBuffer,
|
|
152
|
-
headers: Object.assign({}, (yield getMigrationHeaders(tenantID))),
|
|
153
|
-
});
|
|
154
|
-
return response.url;
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
exports.uploadFileToS3 = uploadFileToS3;
|
|
158
|
-
function createMigration(options) {
|
|
159
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
-
const apiService = new ApiService_1.ApiService({
|
|
161
|
-
validateStatus: validateAxiosStatus,
|
|
162
|
-
});
|
|
163
|
-
const migrationUrl = yield buildMigrationUrl(options.tenant, options.overrideUrl);
|
|
164
|
-
try {
|
|
165
|
-
const payload = {
|
|
166
|
-
assetId: options.assetId,
|
|
167
|
-
previewAssetId: options.previewAssetId,
|
|
168
|
-
matrixUrl: options.matrixUrl,
|
|
169
|
-
};
|
|
170
|
-
const response = yield apiService.client.post(`${migrationUrl}/migrations`, payload, {
|
|
171
|
-
headers: Object.assign({ 'Content-Type': 'application/json' }, (yield getMigrationHeaders(options.tenant))),
|
|
172
|
-
});
|
|
173
|
-
if (response.status !== 200 && response.status !== 201) {
|
|
174
|
-
throw new Error(`Migration creation failed with status: ${response.status}`);
|
|
175
|
-
}
|
|
176
|
-
// Validate response structure
|
|
177
|
-
const { assetMigration, uploadUrl } = response.data || {};
|
|
178
|
-
if (!(assetMigration === null || assetMigration === void 0 ? void 0 : assetMigration.migrationId) ||
|
|
179
|
-
!(assetMigration === null || assetMigration === void 0 ? void 0 : assetMigration.assetId) ||
|
|
180
|
-
!(assetMigration === null || assetMigration === void 0 ? void 0 : assetMigration.stage) ||
|
|
181
|
-
!(assetMigration === null || assetMigration === void 0 ? void 0 : assetMigration.status)) {
|
|
182
|
-
throw new Error('Invalid response format from migration service');
|
|
183
|
-
}
|
|
184
|
-
if (!uploadUrl) {
|
|
185
|
-
throw new Error('Upload URL not found in response');
|
|
186
|
-
}
|
|
187
|
-
return {
|
|
188
|
-
assetMigration,
|
|
189
|
-
uploadUrl,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
catch (error) {
|
|
193
|
-
if (error instanceof Error) {
|
|
194
|
-
throw error;
|
|
195
|
-
}
|
|
196
|
-
throw new Error(`Failed to create migration: ${error}`);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
exports.createMigration = createMigration;
|
|
201
|
-
function getMigration(options) {
|
|
202
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
-
const apiService = new ApiService_1.ApiService({
|
|
204
|
-
validateStatus: validateAxiosStatus,
|
|
205
|
-
});
|
|
206
|
-
const migrationUrl = yield buildMigrationUrl(options.tenant, options.overrideUrl);
|
|
207
|
-
try {
|
|
208
|
-
const response = yield apiService.client.get(`${migrationUrl}/migrations/${options.migrationId}/assets/${options.assetId}`, {
|
|
209
|
-
headers: Object.assign({ 'Content-Type': 'application/json' }, (yield getMigrationHeaders(options.tenant))),
|
|
210
|
-
});
|
|
211
|
-
if (response.status !== 200) {
|
|
212
|
-
throw new Error(`Failed to get migration: ${response.status}`);
|
|
213
|
-
}
|
|
214
|
-
if (!(response === null || response === void 0 ? void 0 : response.data)) {
|
|
215
|
-
throw new Error('No data returned from migration service');
|
|
216
|
-
}
|
|
217
|
-
return {
|
|
218
|
-
migrationId: response.data.migrationId,
|
|
219
|
-
assetId: response.data.assetId,
|
|
220
|
-
stage: response.data.stage,
|
|
221
|
-
status: response.data.status,
|
|
222
|
-
stageDetails: response.data.stageDetails,
|
|
223
|
-
previewPageAssetId: response.data.previewPageAssetId,
|
|
224
|
-
componentsTarDownloadUrl: response.data.componentsTarDownloadUrl,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
catch (error) {
|
|
228
|
-
if (error instanceof Error) {
|
|
229
|
-
throw error;
|
|
230
|
-
}
|
|
231
|
-
throw new Error(`Failed to get migration: ${error}`);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
exports.getMigration = getMigration;
|
|
236
|
-
function revertMigration(options) {
|
|
237
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
238
|
-
const apiService = new ApiService_1.ApiService({
|
|
239
|
-
validateStatus: validateAxiosStatus,
|
|
240
|
-
});
|
|
241
|
-
const migrationUrl = yield buildMigrationUrl(options.tenant, options.overrideUrl);
|
|
242
|
-
try {
|
|
243
|
-
const response = yield apiService.client.post(`${migrationUrl}/migrations/${options.migrationId}/assets/${options.assetId}/rollback`, {}, {
|
|
244
|
-
headers: Object.assign({ 'Content-Type': 'application/json' }, (yield getMigrationHeaders(options.tenant))),
|
|
245
|
-
});
|
|
246
|
-
if (response.status !== 200) {
|
|
247
|
-
throw new Error(`Failed to revert migration: ${response.status}`);
|
|
248
|
-
}
|
|
249
|
-
return response.data;
|
|
250
|
-
}
|
|
251
|
-
catch (error) {
|
|
252
|
-
if (error instanceof Error) {
|
|
253
|
-
throw error;
|
|
254
|
-
}
|
|
255
|
-
throw new Error(`Failed to revert migration: ${error}`);
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
exports.revertMigration = revertMigration;
|
|
260
|
-
function setMigrationSetting(options) {
|
|
261
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
262
|
-
const apiService = new ApiService_1.ApiService({
|
|
263
|
-
validateStatus: validateAxiosStatus,
|
|
264
|
-
});
|
|
265
|
-
const migrationUrl = yield buildMigrationUrl(options.tenant, options.overrideUrl);
|
|
266
|
-
try {
|
|
267
|
-
const payload = {
|
|
268
|
-
matrixUrl: options.matrixUrl,
|
|
269
|
-
matrixIdentifier: options.matrixIdentifier,
|
|
270
|
-
matrixKey: options.matrixKey,
|
|
271
|
-
};
|
|
272
|
-
const response = yield apiService.client.post(`${migrationUrl}/settings`, payload, {
|
|
273
|
-
headers: Object.assign({ 'Content-Type': 'application/json' }, (yield getMigrationHeaders(options.tenant))),
|
|
274
|
-
});
|
|
275
|
-
if (response.status !== 200 && response.status !== 201) {
|
|
276
|
-
throw new Error(`Migration settings update failed with status: ${response.status}`);
|
|
277
|
-
}
|
|
278
|
-
if (!(response === null || response === void 0 ? void 0 : response.data)) {
|
|
279
|
-
throw new Error('No data returned from migration service');
|
|
280
|
-
}
|
|
281
|
-
const responseData = {
|
|
282
|
-
matrixUrl: response.data.matrixUrl,
|
|
283
|
-
matrixIdentifier: response.data.matrixIdentifier,
|
|
284
|
-
matrixKey: response.data.matrixKey
|
|
285
|
-
? redactKey(response.data.matrixKey, 3)
|
|
286
|
-
: '',
|
|
287
|
-
};
|
|
288
|
-
return responseData;
|
|
289
|
-
}
|
|
290
|
-
catch (error) {
|
|
291
|
-
if (error instanceof Error) {
|
|
292
|
-
throw error;
|
|
293
|
-
}
|
|
294
|
-
throw new Error(`Failed to set migration settings: ${error}`);
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
exports.setMigrationSetting = setMigrationSetting;
|