@squiz/dxp-cli-next 5.14.0 → 5.15.0-develop.1

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.
Files changed (57) hide show
  1. package/README.md +1 -0
  2. package/lib/__tests__/integration/main.spec.js +3 -2
  3. package/lib/cdp/index.js +3 -1
  4. package/lib/cmp/deploy.js +49 -2
  5. package/lib/cmp/edge-components/compiler.d.ts +13 -0
  6. package/lib/cmp/edge-components/compiler.js +55 -0
  7. package/lib/cmp/utils/definitions.d.ts +12 -0
  8. package/lib/cmp/utils/definitions.js +76 -0
  9. package/lib/datastore/index.js +1 -1
  10. package/lib/dxp.js +2 -0
  11. package/lib/porter/constants.d.ts +6 -0
  12. package/lib/porter/constants.js +9 -0
  13. package/lib/porter/index.d.ts +3 -0
  14. package/lib/porter/index.js +12 -0
  15. package/lib/porter/project/add/add.d.ts +3 -0
  16. package/lib/porter/project/add/add.js +125 -0
  17. package/lib/porter/project/add/add.spec.d.ts +1 -0
  18. package/lib/porter/project/add/add.spec.js +323 -0
  19. package/lib/porter/project/get/get.d.ts +3 -0
  20. package/lib/porter/project/get/get.js +67 -0
  21. package/lib/porter/project/get/get.spec.d.ts +1 -0
  22. package/lib/porter/project/get/get.spec.js +221 -0
  23. package/lib/porter/project/projectCommand.d.ts +3 -0
  24. package/lib/porter/project/projectCommand.js +19 -0
  25. package/lib/porter/project/remove/remove.d.ts +3 -0
  26. package/lib/porter/project/remove/remove.js +71 -0
  27. package/lib/porter/project/remove/remove.spec.d.ts +1 -0
  28. package/lib/porter/project/remove/remove.spec.js +236 -0
  29. package/lib/porter/types.d.ts +5 -0
  30. package/lib/porter/types.js +2 -0
  31. package/lib/porter/utils/AuthUtils/CheckAuthorisation.d.ts +1 -0
  32. package/lib/porter/utils/AuthUtils/CheckAuthorisation.js +32 -0
  33. package/lib/porter/utils/AuthUtils/CheckAuthorisation.spec.d.ts +1 -0
  34. package/lib/porter/utils/AuthUtils/CheckAuthorisation.spec.js +77 -0
  35. package/lib/porter/utils/BuildPorterUrl/BuildPorterUrl.d.ts +1 -0
  36. package/lib/porter/utils/BuildPorterUrl/BuildPorterUrl.js +27 -0
  37. package/lib/porter/utils/BuildPorterUrl/BuildPorterUrl.spec.d.ts +1 -0
  38. package/lib/porter/utils/BuildPorterUrl/BuildPorterUrl.spec.js +59 -0
  39. package/lib/porter/utils/CoreUtils/CoreUtils.d.ts +2 -0
  40. package/lib/porter/utils/CoreUtils/CoreUtils.js +13 -0
  41. package/lib/porter/utils/CoreUtils/CoreUtils.spec.d.ts +1 -0
  42. package/lib/porter/utils/CoreUtils/CoreUtils.spec.js +36 -0
  43. package/lib/porter/utils/DoesPathExist/DoesPathExist.d.ts +1 -0
  44. package/lib/porter/utils/DoesPathExist/DoesPathExist.js +27 -0
  45. package/lib/porter/utils/DoesPathExist/DoesPathExist.spec.d.ts +1 -0
  46. package/lib/porter/utils/DoesPathExist/DoesPathExist.spec.js +40 -0
  47. package/lib/porter/utils/ErrorUtils/ErrorUtils.d.ts +4 -0
  48. package/lib/porter/utils/ErrorUtils/ErrorUtils.js +68 -0
  49. package/lib/porter/utils/ErrorUtils/ErrorUtils.spec.d.ts +1 -0
  50. package/lib/porter/utils/ErrorUtils/ErrorUtils.spec.js +175 -0
  51. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.d.ts +2 -0
  52. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.js +32 -0
  53. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.spec.d.ts +1 -0
  54. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.spec.js +49 -0
  55. package/lib/porter/utils/index.d.ts +6 -0
  56. package/lib/porter/utils/index.js +22 -0
  57. package/package.json +6 -3
@@ -0,0 +1,323 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const nock_1 = __importDefault(require("nock"));
39
+ const commander_1 = require("commander");
40
+ const promises_1 = __importDefault(require("fs/promises"));
41
+ const axios_1 = require("axios");
42
+ const ApiService = __importStar(require("../../../ApiService"));
43
+ const BuildPorterUrl = __importStar(require("../../utils/BuildPorterUrl/BuildPorterUrl"));
44
+ const ErrorUtils = __importStar(require("../../utils/ErrorUtils/ErrorUtils"));
45
+ const DoesPathExist = __importStar(require("../../utils/DoesPathExist/DoesPathExist"));
46
+ const mockCliVersion = '1.2.3';
47
+ jest.mock('../../../../package.json', () => {
48
+ return {
49
+ version: mockCliVersion,
50
+ };
51
+ });
52
+ jest.mock('fs/promises', () => {
53
+ return {
54
+ access: jest.fn(),
55
+ mkdir: jest.fn(),
56
+ rm: jest.fn(),
57
+ writeFile: jest.fn(),
58
+ };
59
+ });
60
+ describe('addProject', () => {
61
+ let projectName;
62
+ let projectPath;
63
+ let baseUrl;
64
+ let mockHandleCommandError;
65
+ let mockHandleError;
66
+ let mockBuildPorterUrl;
67
+ let mockResponse;
68
+ let mockClient;
69
+ beforeEach(() => {
70
+ jest.resetAllMocks();
71
+ jest.clearAllMocks();
72
+ nock_1.default.restore();
73
+ projectName = 'porter-project';
74
+ projectPath = 'tmp/project-dir';
75
+ baseUrl = 'mock-url';
76
+ mockHandleCommandError = jest.fn(() => Promise.resolve());
77
+ mockHandleError = jest.fn();
78
+ mockBuildPorterUrl = jest.fn(() => Promise.resolve(baseUrl));
79
+ mockResponse = {
80
+ data: {},
81
+ status: 200,
82
+ };
83
+ mockClient = {
84
+ put: jest.fn().mockImplementationOnce(() => mockResponse),
85
+ };
86
+ jest
87
+ .spyOn(BuildPorterUrl, 'buildPorterUrl')
88
+ .mockImplementationOnce(mockBuildPorterUrl);
89
+ jest
90
+ .spyOn(ErrorUtils, 'handleCommandError')
91
+ .mockImplementationOnce(mockHandleCommandError);
92
+ jest
93
+ .spyOn(ErrorUtils, 'handleError')
94
+ .mockImplementationOnce(mockHandleError);
95
+ jest.spyOn(process, 'exit').mockImplementation();
96
+ jest.spyOn(ApiService, 'ApiService').mockImplementationOnce(() => {
97
+ return {
98
+ client: mockClient,
99
+ };
100
+ });
101
+ });
102
+ it('should throw an error if project name is invalid', () => __awaiter(void 0, void 0, void 0, function* () {
103
+ // Project name can only contain letters, numbers, hyphens and underscores'
104
+ const projectNameInvalid = '!@#$';
105
+ const { default: createAddCommand } = require('./add');
106
+ const program = createAddCommand();
107
+ yield expect(() => program.parseAsync([
108
+ 'dxp-next',
109
+ 'porter',
110
+ 'project',
111
+ 'addProject',
112
+ '--project-name',
113
+ projectNameInvalid,
114
+ '--path',
115
+ projectPath,
116
+ ])).rejects.toThrow(commander_1.InvalidArgumentError);
117
+ expect(process.exit).toHaveBeenCalled();
118
+ }));
119
+ it('should throw an error if not logged in', () => __awaiter(void 0, void 0, void 0, function* () {
120
+ const mockError = new Error('Error not logged in');
121
+ jest
122
+ .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
123
+ .mockImplementationOnce(() => {
124
+ throw mockError;
125
+ });
126
+ (0, nock_1.default)('http://localhost:9999')
127
+ .get('/__dxp/au/porter/SquizRoot-0000/health')
128
+ .reply(401);
129
+ const { default: createAddCommand } = require('./add');
130
+ const program = createAddCommand();
131
+ yield expect(() => program.parseAsync([
132
+ 'dxp-next',
133
+ 'porter',
134
+ 'project',
135
+ 'addProject',
136
+ '--project-name',
137
+ projectName,
138
+ '--path',
139
+ projectPath,
140
+ ])).rejects.toThrow(mockError);
141
+ const opts = program.opts();
142
+ expect(opts.projectName).toEqual(projectName);
143
+ expect(opts.path).toEqual(projectPath);
144
+ expect(BuildPorterUrl.buildPorterUrl).not.toHaveBeenCalled();
145
+ }));
146
+ it('should throw an error if project directory already exists', () => __awaiter(void 0, void 0, void 0, function* () {
147
+ jest
148
+ .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
149
+ .mockImplementationOnce(() => Promise.resolve(undefined));
150
+ jest
151
+ .spyOn(DoesPathExist, 'doesPathExist')
152
+ .mockImplementationOnce(() => Promise.resolve(true));
153
+ (0, nock_1.default)('http://localhost:9999')
154
+ .get('/__dxp/au/porter/SquizRoot-0000/health')
155
+ .reply(200);
156
+ const { default: createAddCommand } = require('./add');
157
+ const program = createAddCommand();
158
+ yield program.parseAsync([
159
+ 'dxp-next',
160
+ 'porter',
161
+ 'project',
162
+ 'addProject',
163
+ '--project-name',
164
+ projectName,
165
+ '--path',
166
+ projectPath,
167
+ ]);
168
+ const opts = program.opts();
169
+ expect(opts.projectName).toEqual(projectName);
170
+ expect(opts.path).toEqual(projectPath);
171
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
172
+ expect(DoesPathExist.doesPathExist).toHaveBeenCalled();
173
+ expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
174
+ }));
175
+ it('should throw an error if parent directory does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
176
+ jest
177
+ .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
178
+ .mockImplementationOnce(() => Promise.resolve(undefined));
179
+ jest
180
+ .spyOn(DoesPathExist, 'doesPathExist')
181
+ .mockImplementationOnce(() => Promise.resolve(false));
182
+ jest.spyOn(promises_1.default, 'access').mockImplementationOnce(() => {
183
+ throw new Error();
184
+ });
185
+ (0, nock_1.default)('http://localhost:9999')
186
+ .get('/__dxp/au/porter/SquizRoot-0000/health')
187
+ .reply(200);
188
+ const { default: createAddCommand } = require('./add');
189
+ const program = createAddCommand();
190
+ yield program.parseAsync([
191
+ 'dxp-next',
192
+ 'porter',
193
+ 'project',
194
+ 'addProject',
195
+ '--project-name',
196
+ projectName,
197
+ '--path',
198
+ projectPath,
199
+ ]);
200
+ const opts = program.opts();
201
+ expect(opts.projectName).toEqual(projectName);
202
+ expect(opts.path).toEqual(projectPath);
203
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
204
+ expect(DoesPathExist.doesPathExist).toHaveBeenCalled();
205
+ expect(promises_1.default.access).toHaveBeenCalled();
206
+ expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
207
+ }));
208
+ it('should clean up the directory if project already exists', () => __awaiter(void 0, void 0, void 0, function* () {
209
+ jest
210
+ .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
211
+ .mockImplementationOnce(() => Promise.resolve(undefined));
212
+ jest
213
+ .spyOn(DoesPathExist, 'doesPathExist')
214
+ .mockImplementationOnce(() => Promise.resolve(false));
215
+ mockClient.put = jest.fn(() => {
216
+ throw new axios_1.AxiosError('Project already exists', '409', undefined, undefined, {
217
+ data: { status: 409 },
218
+ });
219
+ });
220
+ (0, nock_1.default)('http://localhost:9999')
221
+ .get('/__dxp/au/porter/SquizRoot-0000/health')
222
+ .reply(200);
223
+ const { default: createAddCommand } = require('./add');
224
+ const program = createAddCommand();
225
+ yield program.parseAsync([
226
+ 'dxp-next',
227
+ 'porter',
228
+ 'project',
229
+ 'addProject',
230
+ '--project-name',
231
+ projectName,
232
+ '--path',
233
+ projectPath,
234
+ ]);
235
+ const opts = program.opts();
236
+ expect(opts.projectName).toEqual(projectName);
237
+ expect(opts.path).toEqual(projectPath);
238
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
239
+ expect(DoesPathExist.doesPathExist).toHaveBeenCalled();
240
+ expect(promises_1.default.access).toHaveBeenCalled();
241
+ // Create project dir, create "stage-configs" directory, create directories for (crawl, index, template-dedupe, validation, report)
242
+ expect(promises_1.default.mkdir).toHaveBeenCalledTimes(7);
243
+ // Create project config.json as well as 5 example config.json (for each stage)
244
+ expect(promises_1.default.writeFile).toHaveBeenCalledTimes(6);
245
+ expect(promises_1.default.writeFile).toHaveBeenNthCalledWith(1, expect.any(String), expect.stringContaining(mockCliVersion));
246
+ expect(mockClient.put).toHaveBeenCalled();
247
+ expect(promises_1.default.rm).toHaveBeenCalled();
248
+ expect(ErrorUtils.handleError).toHaveBeenCalled();
249
+ expect(ErrorUtils.handleCommandError).not.toHaveBeenCalled();
250
+ }));
251
+ it('should handle unexpected errors', () => __awaiter(void 0, void 0, void 0, function* () {
252
+ jest
253
+ .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
254
+ .mockImplementationOnce(() => Promise.resolve(undefined));
255
+ jest
256
+ .spyOn(DoesPathExist, 'doesPathExist')
257
+ .mockImplementationOnce(() => Promise.resolve(false));
258
+ (0, nock_1.default)('http://localhost:9999')
259
+ .get('/__dxp/au/porter/SquizRoot-0000/health')
260
+ .reply(200);
261
+ const { default: createAddCommand } = require('./add');
262
+ const program = createAddCommand();
263
+ yield program.parseAsync([
264
+ 'dxp-next',
265
+ 'porter',
266
+ 'project',
267
+ 'addProject',
268
+ '--project-name',
269
+ projectName,
270
+ '--path',
271
+ projectPath,
272
+ ]);
273
+ const opts = program.opts();
274
+ expect(opts.projectName).toEqual(projectName);
275
+ expect(opts.path).toEqual(projectPath);
276
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
277
+ expect(DoesPathExist.doesPathExist).toHaveBeenCalled();
278
+ expect(promises_1.default.access).toHaveBeenCalled();
279
+ // Create project dir, create "stage-configs" directory, create directories for (crawl, index, template-dedupe, validation, report)
280
+ expect(promises_1.default.mkdir).toHaveBeenCalledTimes(7);
281
+ // Create project config.json as well as 5 example config.json (for each stage)
282
+ expect(promises_1.default.writeFile).toHaveBeenCalledTimes(6);
283
+ expect(promises_1.default.writeFile).toHaveBeenNthCalledWith(1, expect.any(String), expect.stringContaining(mockCliVersion));
284
+ expect(mockClient.put).toHaveBeenCalled();
285
+ expect(ErrorUtils.handleCommandError).not.toHaveBeenCalled();
286
+ }));
287
+ it('should successfully initialise the project directory and the porter project', () => __awaiter(void 0, void 0, void 0, function* () {
288
+ jest
289
+ .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
290
+ .mockImplementationOnce(() => Promise.resolve(undefined));
291
+ jest
292
+ .spyOn(DoesPathExist, 'doesPathExist')
293
+ .mockImplementationOnce(() => Promise.resolve(false));
294
+ jest.spyOn(promises_1.default, 'mkdir').mockImplementationOnce(() => {
295
+ throw new Error('mock error');
296
+ });
297
+ (0, nock_1.default)('http://localhost:9999')
298
+ .get('/__dxp/au/porter/SquizRoot-0000/health')
299
+ .reply(200);
300
+ const { default: createAddCommand } = require('./add');
301
+ const program = createAddCommand();
302
+ yield program.parseAsync([
303
+ 'dxp-next',
304
+ 'porter',
305
+ 'project',
306
+ 'addProject',
307
+ '--project-name',
308
+ projectName,
309
+ '--path',
310
+ projectPath,
311
+ ]);
312
+ const opts = program.opts();
313
+ expect(opts.projectName).toEqual(projectName);
314
+ expect(opts.path).toEqual(projectPath);
315
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
316
+ expect(DoesPathExist.doesPathExist).toHaveBeenCalled();
317
+ expect(promises_1.default.access).toHaveBeenCalled();
318
+ expect(promises_1.default.mkdir).toHaveBeenCalledTimes(1);
319
+ expect(promises_1.default.writeFile).not.toHaveBeenCalled();
320
+ expect(mockClient.put).not.toHaveBeenCalled();
321
+ expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
322
+ }));
323
+ });
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ declare const createGetProjectCommand: () => Command;
3
+ export default createGetProjectCommand;
@@ -0,0 +1,67 @@
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
+ const commander_1 = require("commander");
16
+ const chalk_1 = __importDefault(require("chalk"));
17
+ const ora_1 = __importDefault(require("ora"));
18
+ const ApiService_1 = require("../../../ApiService");
19
+ const utils_1 = require("../../utils");
20
+ const constants_1 = require("../../constants");
21
+ const createGetProjectCommand = () => {
22
+ const getCommand = new commander_1.Command('getProject')
23
+ .description('Retrieves the project')
24
+ .configureOutput({
25
+ outputError(str, write) {
26
+ write(chalk_1.default.red(str));
27
+ },
28
+ })
29
+ .action((options) => __awaiter(void 0, void 0, void 0, function* () {
30
+ var _a, _b;
31
+ yield (0, utils_1.throwErrorIfNotLoggedIn)(getCommand);
32
+ const baseUrl = yield (0, utils_1.buildPorterUrl)();
33
+ let projectName = '';
34
+ const spinner = (0, ora_1.default)();
35
+ try {
36
+ const projectConfig = yield (0, utils_1.getProjectConfig)();
37
+ projectName = projectConfig.projectName;
38
+ if (!projectName) {
39
+ throw new Error(constants_1.PORTER_ERRORS.projectNameMissing);
40
+ }
41
+ spinner.start('Retrieving project..');
42
+ const apiService = new ApiService_1.ApiService(utils_1.validateAxiosStatus, baseUrl);
43
+ const response = yield apiService.client.get(`/projects/${projectName}`);
44
+ const jsonResponse = response.data;
45
+ (0, utils_1.logDebug)(jsonResponse);
46
+ spinner.succeed();
47
+ console.log('');
48
+ console.log(`Project name: ${projectName}`);
49
+ console.log(`Download last port report: ${jsonResponse['report-link'] || 'Not generated'}`);
50
+ console.log(`Download last port artifacts: ${jsonResponse['artifacts-link'] || 'Not generated'}`);
51
+ }
52
+ catch (error) {
53
+ (0, utils_1.logDebug)(`ERROR: ${error instanceof Error ? `${error.message}` : JSON.stringify(error)}`);
54
+ if (spinner.isSpinning) {
55
+ spinner.fail();
56
+ }
57
+ if (((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.status) === 404) {
58
+ (0, utils_1.logDebug)(error.response.data);
59
+ const errorMessage = `Project "${projectName}" not found in DXP porter service`;
60
+ return (0, utils_1.handleError)(getCommand, errorMessage);
61
+ }
62
+ (0, utils_1.handleCommandError)(getCommand, error);
63
+ }
64
+ }));
65
+ return getCommand;
66
+ };
67
+ exports.default = createGetProjectCommand;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const nock_1 = __importDefault(require("nock"));
39
+ const get_1 = __importDefault(require("./get"));
40
+ const axios_1 = require("axios");
41
+ const ApiService = __importStar(require("../../../ApiService"));
42
+ const BuildPorterUrl = __importStar(require("../../utils/BuildPorterUrl/BuildPorterUrl"));
43
+ const ErrorUtils = __importStar(require("../../utils/ErrorUtils/ErrorUtils"));
44
+ const GetProjectConfig = __importStar(require("../../utils/GetProjectConfig/GetProjectConfig"));
45
+ const logSpy = jest.spyOn(global.console, 'log');
46
+ describe('getProject', () => {
47
+ let baseUrl;
48
+ let projectName;
49
+ let projectConfig;
50
+ let mockHandleCommandError;
51
+ let mockHandleError;
52
+ let mockBuildPorterUrl;
53
+ let mockResponse;
54
+ let mockClient;
55
+ beforeEach(() => {
56
+ jest.resetAllMocks();
57
+ jest.clearAllMocks();
58
+ nock_1.default.restore();
59
+ projectName = 'porter-project';
60
+ baseUrl = 'mock-url';
61
+ projectConfig = {
62
+ service: 'dxp-porter',
63
+ projectName,
64
+ version: '0.1.0',
65
+ };
66
+ mockHandleCommandError = jest.fn(() => Promise.resolve());
67
+ mockHandleError = jest.fn();
68
+ mockBuildPorterUrl = jest.fn(() => Promise.resolve(baseUrl));
69
+ mockResponse = {
70
+ data: {},
71
+ status: 200,
72
+ };
73
+ mockClient = {
74
+ get: jest.fn().mockImplementationOnce(() => mockResponse),
75
+ };
76
+ jest
77
+ .spyOn(BuildPorterUrl, 'buildPorterUrl')
78
+ .mockImplementationOnce(mockBuildPorterUrl);
79
+ jest
80
+ .spyOn(ErrorUtils, 'handleCommandError')
81
+ .mockImplementationOnce(mockHandleCommandError);
82
+ jest
83
+ .spyOn(ErrorUtils, 'handleError')
84
+ .mockImplementationOnce(mockHandleError);
85
+ jest.spyOn(process, 'exit').mockImplementation();
86
+ jest.spyOn(ApiService, 'ApiService').mockImplementationOnce(() => {
87
+ return {
88
+ client: mockClient,
89
+ };
90
+ });
91
+ });
92
+ it('should throw an error if not logged in', () => __awaiter(void 0, void 0, void 0, function* () {
93
+ const mockError = new Error('Error not logged in');
94
+ jest
95
+ .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
96
+ .mockImplementationOnce(() => {
97
+ throw mockError;
98
+ });
99
+ (0, nock_1.default)('http://localhost:9999')
100
+ .get('/__dxp/au/porter/SquizRoot-0000/health')
101
+ .reply(401);
102
+ const program = (0, get_1.default)();
103
+ yield expect(() => program.parseAsync(['dxp-next', 'porter', 'project', 'getProject'])).rejects.toThrow(mockError);
104
+ const opts = program.opts();
105
+ expect(opts).toStrictEqual({});
106
+ }));
107
+ it('should throw an error if not in a project directory', () => __awaiter(void 0, void 0, void 0, function* () {
108
+ jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
109
+ jest
110
+ .spyOn(GetProjectConfig, 'getProjectConfig')
111
+ .mockImplementationOnce(() => {
112
+ throw new Error('You must be within a porter project directory to use this command');
113
+ });
114
+ const { default: createGetCommand } = require('./get');
115
+ const program = createGetCommand();
116
+ yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
117
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
118
+ expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
119
+ }));
120
+ it('should throw an error if project name is missing in the config.json file', () => __awaiter(void 0, void 0, void 0, function* () {
121
+ jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
122
+ jest
123
+ .spyOn(GetProjectConfig, 'getProjectConfig')
124
+ .mockImplementationOnce(() => Promise.resolve(Object.assign(Object.assign({}, projectConfig), { projectName: '' })));
125
+ const { default: createGetCommand } = require('./get');
126
+ const program = createGetCommand();
127
+ yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
128
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
129
+ expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
130
+ }));
131
+ it('should throw an error if the project does not exist in the cloud', () => __awaiter(void 0, void 0, void 0, function* () {
132
+ jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
133
+ jest
134
+ .spyOn(GetProjectConfig, 'getProjectConfig')
135
+ .mockImplementationOnce(() => Promise.resolve(projectConfig));
136
+ mockClient.get = jest.fn(() => {
137
+ throw new axios_1.AxiosError('Project does not exist', '404', undefined, undefined, {
138
+ data: { status: 404 },
139
+ });
140
+ });
141
+ const { default: createGetCommand } = require('./get');
142
+ const program = createGetCommand();
143
+ yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
144
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
145
+ expect(mockClient.get).toHaveBeenCalled();
146
+ expect(ErrorUtils.handleError).toHaveBeenCalled();
147
+ }));
148
+ it('should handle unexpected errors', () => __awaiter(void 0, void 0, void 0, function* () {
149
+ jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
150
+ jest
151
+ .spyOn(GetProjectConfig, 'getProjectConfig')
152
+ .mockImplementationOnce(() => Promise.resolve(projectConfig));
153
+ mockClient.get = jest.fn(() => {
154
+ throw new axios_1.AxiosError('Internal server error', '500', undefined, undefined, {
155
+ data: { status: 500 },
156
+ });
157
+ });
158
+ const { default: createGetCommand } = require('./get');
159
+ const program = createGetCommand();
160
+ yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
161
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
162
+ expect(mockClient.get).toHaveBeenCalled();
163
+ expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
164
+ }));
165
+ it('should successfully retrieve the project details (without generated links) and log this to the console', () => __awaiter(void 0, void 0, void 0, function* () {
166
+ jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
167
+ jest
168
+ .spyOn(GetProjectConfig, 'getProjectConfig')
169
+ .mockImplementationOnce(() => Promise.resolve(projectConfig));
170
+ const mockGetResponse = {
171
+ portid: '1234-5678-abcd-efgh',
172
+ stages: {},
173
+ status: {},
174
+ 'artifacts-link': null,
175
+ 'report-link': null,
176
+ 'stage-settings': {},
177
+ };
178
+ mockResponse = {
179
+ data: mockGetResponse,
180
+ status: 200,
181
+ };
182
+ const { default: createGetCommand } = require('./get');
183
+ const program = createGetCommand();
184
+ yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
185
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
186
+ expect(mockClient.get).toHaveBeenCalled();
187
+ expect(ErrorUtils.handleCommandError).not.toHaveBeenCalled();
188
+ expect(logSpy).toHaveBeenNthCalledWith(2, `Project name: ${projectName}`);
189
+ expect(logSpy).toHaveBeenNthCalledWith(3, 'Download last port report: Not generated');
190
+ expect(logSpy).toHaveBeenNthCalledWith(4, 'Download last port artifacts: Not generated');
191
+ }));
192
+ it('should successfully retrieve the project details and log this to the console', () => __awaiter(void 0, void 0, void 0, function* () {
193
+ jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
194
+ jest
195
+ .spyOn(GetProjectConfig, 'getProjectConfig')
196
+ .mockImplementationOnce(() => Promise.resolve(projectConfig));
197
+ const mockReportLink = 'mock-report-link';
198
+ const mockArtifactsLink = 'mock-artifacts-link';
199
+ const mockGetResponse = {
200
+ portid: '1234-5678-abcd-efgh',
201
+ stages: {},
202
+ status: {},
203
+ 'artifacts-link': mockArtifactsLink,
204
+ 'report-link': mockReportLink,
205
+ 'stage-settings': {},
206
+ };
207
+ mockResponse = {
208
+ data: mockGetResponse,
209
+ status: 200,
210
+ };
211
+ const { default: createGetCommand } = require('./get');
212
+ const program = createGetCommand();
213
+ yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
214
+ expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
215
+ expect(mockClient.get).toHaveBeenCalled();
216
+ expect(ErrorUtils.handleCommandError).not.toHaveBeenCalled();
217
+ expect(logSpy).toHaveBeenNthCalledWith(2, `Project name: ${projectName}`);
218
+ expect(logSpy).toHaveBeenNthCalledWith(3, `Download last port report: ${mockReportLink}`);
219
+ expect(logSpy).toHaveBeenNthCalledWith(4, `Download last port artifacts: ${mockArtifactsLink}`);
220
+ }));
221
+ });
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ declare const createProjectCommand: () => Command;
3
+ export default createProjectCommand;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const commander_1 = require("commander");
7
+ const get_1 = __importDefault(require("./get/get"));
8
+ const add_1 = __importDefault(require("./add/add"));
9
+ const remove_1 = __importDefault(require("./remove/remove"));
10
+ const createProjectCommand = () => {
11
+ const projectCommand = new commander_1.Command('project');
12
+ projectCommand
13
+ .description('Porter project commands')
14
+ .addCommand((0, get_1.default)())
15
+ .addCommand((0, add_1.default)())
16
+ .addCommand((0, remove_1.default)());
17
+ return projectCommand;
18
+ };
19
+ exports.default = createProjectCommand;
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ declare const createRemoveProjectCommand: () => Command;
3
+ export default createRemoveProjectCommand;