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

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 (35) hide show
  1. package/lib/porter/constants.d.ts +2 -0
  2. package/lib/porter/constants.js +2 -0
  3. package/lib/porter/index.js +3 -1
  4. package/lib/porter/port/abort/abort.d.ts +3 -0
  5. package/lib/porter/port/abort/abort.js +77 -0
  6. package/lib/porter/port/abort/abort.spec.d.ts +1 -0
  7. package/lib/porter/port/abort/abort.spec.js +182 -0
  8. package/lib/porter/port/get/get.d.ts +3 -0
  9. package/lib/porter/port/get/get.js +93 -0
  10. package/lib/porter/port/get/get.spec.d.ts +1 -0
  11. package/lib/porter/port/get/get.spec.js +273 -0
  12. package/lib/porter/port/portCommand.d.ts +3 -0
  13. package/lib/porter/port/portCommand.js +19 -0
  14. package/lib/porter/port/start/start.d.ts +3 -0
  15. package/lib/porter/port/start/start.js +79 -0
  16. package/lib/porter/port/start/start.spec.d.ts +1 -0
  17. package/lib/porter/port/start/start.spec.js +198 -0
  18. package/lib/porter/project/add/add.js +6 -1
  19. package/lib/porter/project/add/add.spec.js +52 -99
  20. package/lib/porter/project/get/get.js +4 -9
  21. package/lib/porter/project/get/get.spec.js +47 -83
  22. package/lib/porter/project/projectCommand.js +1 -1
  23. package/lib/porter/project/remove/remove.js +1 -6
  24. package/lib/porter/project/remove/remove.spec.js +42 -77
  25. package/lib/porter/utils/ErrorUtils/ErrorUtils.js +4 -1
  26. package/lib/porter/utils/ErrorUtils/ErrorUtils.spec.js +17 -2
  27. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.js +2 -1
  28. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.spec.js +4 -3
  29. package/lib/porter/utils/GetProjectName/GetProjectName.d.ts +4 -0
  30. package/lib/porter/utils/GetProjectName/GetProjectName.js +30 -0
  31. package/lib/porter/utils/GetProjectName/GetProjectName.spec.d.ts +1 -0
  32. package/lib/porter/utils/GetProjectName/GetProjectName.spec.js +91 -0
  33. package/lib/porter/utils/index.d.ts +1 -0
  34. package/lib/porter/utils/index.js +1 -0
  35. package/package.json +2 -2
@@ -31,27 +31,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
32
  });
33
33
  };
34
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
34
  Object.defineProperty(exports, "__esModule", { value: true });
38
- const nock_1 = __importDefault(require("nock"));
39
- const remove_1 = __importDefault(require("./remove"));
40
35
  const axios_1 = require("axios");
41
- const ApiService = __importStar(require("../../../ApiService"));
42
36
  const Inquirer = __importStar(require("inquirer"));
43
- const BuildPorterUrl = __importStar(require("../../utils/BuildPorterUrl/BuildPorterUrl"));
44
- const ErrorUtils = __importStar(require("../../utils/ErrorUtils/ErrorUtils"));
45
- const GetProjectConfig = __importStar(require("../../utils/GetProjectConfig/GetProjectConfig"));
37
+ const ApiService = __importStar(require("../../../ApiService"));
38
+ const PorterUtils = __importStar(require("../../utils"));
46
39
  jest.mock('inquirer', () => {
47
40
  return {
48
41
  prompt: jest.fn(),
49
42
  };
50
43
  });
44
+ jest.mock('../../utils');
51
45
  describe('removeProject', () => {
52
46
  let baseUrl;
53
47
  let projectName;
54
- let projectConfig;
55
48
  let mockHandleCommandError;
56
49
  let mockHandleError;
57
50
  let mockBuildPorterUrl;
@@ -60,14 +53,8 @@ describe('removeProject', () => {
60
53
  beforeEach(() => {
61
54
  jest.resetAllMocks();
62
55
  jest.clearAllMocks();
63
- nock_1.default.restore();
64
56
  projectName = 'porter-project';
65
57
  baseUrl = 'mock-url';
66
- projectConfig = {
67
- service: 'dxp-porter',
68
- projectName,
69
- version: '0.1.0',
70
- };
71
58
  mockHandleCommandError = jest.fn(() => Promise.resolve());
72
59
  mockHandleError = jest.fn();
73
60
  mockBuildPorterUrl = jest.fn(() => Promise.resolve(baseUrl));
@@ -78,14 +65,18 @@ describe('removeProject', () => {
78
65
  mockClient = {
79
66
  delete: jest.fn().mockImplementationOnce(() => mockResponse),
80
67
  };
68
+ jest.spyOn(PorterUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
81
69
  jest
82
- .spyOn(BuildPorterUrl, 'buildPorterUrl')
70
+ .spyOn(PorterUtils, 'getProjectName')
71
+ .mockImplementationOnce(() => Promise.resolve(projectName));
72
+ jest
73
+ .spyOn(PorterUtils, 'buildPorterUrl')
83
74
  .mockImplementationOnce(mockBuildPorterUrl);
84
75
  jest
85
- .spyOn(ErrorUtils, 'handleCommandError')
76
+ .spyOn(PorterUtils, 'handleCommandError')
86
77
  .mockImplementationOnce(mockHandleCommandError);
87
78
  jest
88
- .spyOn(ErrorUtils, 'handleError')
79
+ .spyOn(PorterUtils, 'handleError')
89
80
  .mockImplementationOnce(mockHandleError);
90
81
  jest.spyOn(process, 'exit').mockImplementation();
91
82
  jest.spyOn(ApiService, 'ApiService').mockImplementationOnce(() => {
@@ -97,57 +88,32 @@ describe('removeProject', () => {
97
88
  it('should throw an error if not logged in', () => __awaiter(void 0, void 0, void 0, function* () {
98
89
  const mockError = new Error('Error not logged in');
99
90
  jest
100
- .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
91
+ .spyOn(PorterUtils, 'throwErrorIfNotLoggedIn')
101
92
  .mockImplementationOnce(() => {
102
93
  throw mockError;
103
94
  });
104
- (0, nock_1.default)('http://localhost:9999')
105
- .get('/__dxp/au/porter/SquizRoot-0000/health')
106
- .reply(401);
107
- const program = (0, remove_1.default)();
95
+ const { default: createRemoveCommand } = require('./remove');
96
+ const program = createRemoveCommand();
108
97
  yield expect(() => program.parseAsync(['dxp-next', 'porter', 'project', 'removeProject'])).rejects.toThrow(mockError);
109
98
  const opts = program.opts();
110
99
  expect(opts).toStrictEqual({});
100
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
101
+ expect(PorterUtils.buildPorterUrl).not.toHaveBeenCalled();
111
102
  }));
112
- it('should throw an error if not in a project directory', () => __awaiter(void 0, void 0, void 0, function* () {
113
- jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
114
- jest
115
- .spyOn(GetProjectConfig, 'getProjectConfig')
116
- .mockImplementationOnce(() => {
117
- throw new Error('You must be within a porter project directory to use this command');
103
+ it('should throw an error if project name could not be found', () => __awaiter(void 0, void 0, void 0, function* () {
104
+ jest.spyOn(PorterUtils, 'getProjectName').mockImplementationOnce(() => {
105
+ throw new Error();
118
106
  });
119
107
  const { default: createRemoveCommand } = require('./remove');
120
108
  const program = createRemoveCommand();
121
- yield program.parseAsync([
122
- 'dxp-next',
123
- 'porter',
124
- 'project',
125
- 'removeProject',
126
- ]);
127
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
128
- expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
129
- }));
130
- it('should throw an error if project name is missing in the config.json file', () => __awaiter(void 0, void 0, void 0, function* () {
131
- jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
132
- jest
133
- .spyOn(GetProjectConfig, 'getProjectConfig')
134
- .mockImplementationOnce(() => Promise.resolve(Object.assign(Object.assign({}, projectConfig), { projectName: '' })));
135
- const { default: createRemoveCommand } = require('./remove');
136
- const program = createRemoveCommand();
137
- yield program.parseAsync([
138
- 'dxp-next',
139
- 'porter',
140
- 'project',
141
- 'removeProject',
142
- ]);
143
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
144
- expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
109
+ yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
110
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
111
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
112
+ expect(PorterUtils.getProjectName).toHaveBeenCalled();
113
+ expect(mockClient.delete).not.toHaveBeenCalled();
114
+ expect(PorterUtils.handleCommandError).toHaveBeenCalled();
145
115
  }));
146
116
  it('should end the program if the prompt was input with anything other than "y"', () => __awaiter(void 0, void 0, void 0, function* () {
147
- jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
148
- jest
149
- .spyOn(GetProjectConfig, 'getProjectConfig')
150
- .mockImplementationOnce(() => Promise.resolve(projectConfig));
151
117
  jest.spyOn(Inquirer, 'prompt').mockImplementationOnce(() => Promise.resolve({
152
118
  answer: 'not-y',
153
119
  }));
@@ -159,15 +125,13 @@ describe('removeProject', () => {
159
125
  'project',
160
126
  'removeProject',
161
127
  ]);
162
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
128
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
129
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
130
+ expect(PorterUtils.getProjectName).toHaveBeenCalled();
163
131
  expect(mockClient.delete).not.toHaveBeenCalled();
164
- expect(ErrorUtils.handleCommandError).not.toHaveBeenCalled();
132
+ expect(PorterUtils.handleCommandError).not.toHaveBeenCalled();
165
133
  }));
166
134
  it('should throw an error if the project does not exist in the cloud', () => __awaiter(void 0, void 0, void 0, function* () {
167
- jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
168
- jest
169
- .spyOn(GetProjectConfig, 'getProjectConfig')
170
- .mockImplementationOnce(() => Promise.resolve(projectConfig));
171
135
  jest.spyOn(Inquirer, 'prompt').mockImplementationOnce(() => Promise.resolve({
172
136
  answer: 'y',
173
137
  }));
@@ -184,15 +148,16 @@ describe('removeProject', () => {
184
148
  'project',
185
149
  'removeProject',
186
150
  ]);
187
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
151
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
152
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
153
+ expect(PorterUtils.getProjectName).toHaveBeenCalled();
188
154
  expect(mockClient.delete).toHaveBeenCalled();
189
- expect(ErrorUtils.handleError).toHaveBeenCalled();
155
+ expect(PorterUtils.handleError).toHaveBeenCalled();
190
156
  }));
191
157
  it('should successfully delete the project from the cloud', () => __awaiter(void 0, void 0, void 0, function* () {
192
- jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
193
158
  jest
194
- .spyOn(GetProjectConfig, 'getProjectConfig')
195
- .mockImplementationOnce(() => Promise.resolve(projectConfig));
159
+ .spyOn(PorterUtils, 'getProjectName')
160
+ .mockImplementationOnce(() => Promise.resolve(projectName));
196
161
  jest.spyOn(Inquirer, 'prompt').mockImplementationOnce(() => Promise.resolve({
197
162
  answer: 'y',
198
163
  }));
@@ -204,15 +169,13 @@ describe('removeProject', () => {
204
169
  'project',
205
170
  'removeProject',
206
171
  ]);
207
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
172
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
173
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
174
+ expect(PorterUtils.getProjectName).toHaveBeenCalled();
208
175
  expect(mockClient.delete).toHaveBeenCalled();
209
- expect(ErrorUtils.handleError).not.toHaveBeenCalled();
176
+ expect(PorterUtils.handleError).not.toHaveBeenCalled();
210
177
  }));
211
178
  it('should handle unexpected errors', () => __awaiter(void 0, void 0, void 0, function* () {
212
- jest.spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
213
- jest
214
- .spyOn(GetProjectConfig, 'getProjectConfig')
215
- .mockImplementationOnce(() => Promise.resolve(projectConfig));
216
179
  jest.spyOn(Inquirer, 'prompt').mockImplementationOnce(() => Promise.resolve({
217
180
  answer: 'y',
218
181
  }));
@@ -229,8 +192,10 @@ describe('removeProject', () => {
229
192
  'project',
230
193
  'removeProject',
231
194
  ]);
232
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
195
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
196
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
197
+ expect(PorterUtils.getProjectName).toHaveBeenCalled();
233
198
  expect(mockClient.delete).toHaveBeenCalled();
234
- expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
199
+ expect(PorterUtils.handleCommandError).toHaveBeenCalled();
235
200
  }));
236
201
  });
@@ -41,7 +41,10 @@ function handleCommandError(command, error) {
41
41
  if (error.response.data) {
42
42
  let errorMessage;
43
43
  if (error.response.data['invalid-params']) {
44
- errorMessage = JSON.stringify(error.response.data['invalid-params']);
44
+ errorMessage = error.response.data['invalid-params']
45
+ .map((item) => `${item.name} - ${item.reason}`)
46
+ .join('\n');
47
+ return command.error(chalk_1.default.red(errorMessage));
45
48
  }
46
49
  else {
47
50
  errorMessage = error.response.data.title;
@@ -97,7 +97,7 @@ describe('handleCommandError', () => {
97
97
  // read the mock call strings directly otherwise colours cause test failures with .toEqual()
98
98
  expect(stderrSpy.mock.calls[0][0]).toContain('You must be logged in as a DXP super admin or Squiz support staff to use this service');
99
99
  }));
100
- it('correctly displays the console logs for axios errors from the porter service', () => __awaiter(void 0, void 0, void 0, function* () {
100
+ it('correctly displays the console logs for invalid-params errors from the porter service', () => __awaiter(void 0, void 0, void 0, function* () {
101
101
  const command = new commander_1.Command();
102
102
  const errorStatus = 400;
103
103
  const errorTitle = 'Bad request';
@@ -115,7 +115,22 @@ describe('handleCommandError', () => {
115
115
  (0, ErrorUtils_1.handleCommandError)(command, new axios_1.AxiosError(errorTitle, `${errorStatus}`, undefined, undefined, errorResponse));
116
116
  expect(stderrSpy).toHaveBeenCalledTimes(1);
117
117
  // read the mock call strings directly otherwise colours cause test failures with .toEqual()
118
- expect(stderrSpy.mock.calls[0][0]).toContain(JSON.stringify(errorResponse.data['invalid-params']));
118
+ expect(stderrSpy.mock.calls[0][0]).toContain(`${errorResponse.data['invalid-params'][0].name} - ${errorResponse.data['invalid-params'][0].reason}`);
119
+ }));
120
+ it('correctly displays the console logs for errors (using the title) from the porter service', () => __awaiter(void 0, void 0, void 0, function* () {
121
+ const errorStatus = 400;
122
+ const errorTitle = 'Bad request';
123
+ const command = new commander_1.Command();
124
+ const errorResponse = {
125
+ data: {
126
+ title: errorTitle,
127
+ status: errorStatus,
128
+ },
129
+ };
130
+ (0, ErrorUtils_1.handleCommandError)(command, new axios_1.AxiosError(errorTitle, `${errorStatus}`, undefined, undefined, errorResponse));
131
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
132
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
133
+ expect(stderrSpy.mock.calls[0][0]).toContain(errorTitle);
119
134
  }));
120
135
  });
121
136
  describe('handleError', () => {
@@ -16,6 +16,7 @@ exports.getProjectConfig = void 0;
16
16
  const path_1 = __importDefault(require("path"));
17
17
  const promises_1 = __importDefault(require("fs/promises"));
18
18
  const CoreUtils_1 = require("../CoreUtils/CoreUtils");
19
+ const constants_1 = require("../../constants");
19
20
  function getProjectConfig() {
20
21
  return __awaiter(this, void 0, void 0, function* () {
21
22
  try {
@@ -25,7 +26,7 @@ function getProjectConfig() {
25
26
  }
26
27
  catch (err) {
27
28
  (0, CoreUtils_1.logDebug)(err);
28
- throw new Error('You must be within a porter project directory to use this command');
29
+ throw new Error(constants_1.PORTER_ERRORS.projectDirectoryInvalid);
29
30
  }
30
31
  });
31
32
  }
@@ -14,24 +14,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const GetProjectConfig_1 = require("./GetProjectConfig");
16
16
  const promises_1 = __importDefault(require("fs/promises"));
17
+ const constants_1 = require("../../constants");
17
18
  jest.mock('fs/promises', () => {
18
19
  return {
19
20
  readFile: jest.fn(),
20
21
  };
21
22
  });
22
23
  describe('getProjectConfig', () => {
23
- it('should throw an error if cannot config file does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
24
+ it('should throw an error if config file does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
24
25
  jest.spyOn(promises_1.default, 'readFile').mockImplementationOnce(() => {
25
26
  throw new Error('ENOENT');
26
27
  });
27
- yield expect((0, GetProjectConfig_1.getProjectConfig)()).rejects.toThrowError('You must be within a porter project directory to use this command');
28
+ yield expect((0, GetProjectConfig_1.getProjectConfig)()).rejects.toThrowError(constants_1.PORTER_ERRORS.projectDirectoryInvalid);
28
29
  expect(promises_1.default.readFile).toHaveBeenCalled();
29
30
  }));
30
31
  it('should throw an error if cannot parse config file', () => __awaiter(void 0, void 0, void 0, function* () {
31
32
  jest
32
33
  .spyOn(promises_1.default, 'readFile')
33
34
  .mockImplementationOnce(() => Promise.resolve('invalid-json'));
34
- yield expect((0, GetProjectConfig_1.getProjectConfig)()).rejects.toThrowError('You must be within a porter project directory to use this command');
35
+ yield expect((0, GetProjectConfig_1.getProjectConfig)()).rejects.toThrowError(constants_1.PORTER_ERRORS.projectDirectoryInvalid);
35
36
  expect(promises_1.default.readFile).toHaveBeenCalled();
36
37
  }));
37
38
  it('should return the project config json', () => __awaiter(void 0, void 0, void 0, function* () {
@@ -0,0 +1,4 @@
1
+ /**
2
+ * The project name set in the project config file.
3
+ */
4
+ export declare function getProjectName(): Promise<string>;
@@ -0,0 +1,30 @@
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
+ exports.getProjectName = void 0;
13
+ const GetProjectConfig_1 = require("../GetProjectConfig/GetProjectConfig");
14
+ const constants_1 = require("../../constants");
15
+ const CoreUtils_1 = require("../CoreUtils/CoreUtils");
16
+ /**
17
+ * The project name set in the project config file.
18
+ */
19
+ function getProjectName() {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const config = yield (0, GetProjectConfig_1.getProjectConfig)();
22
+ const { projectName } = config;
23
+ if (!projectName) {
24
+ (0, CoreUtils_1.logDebug)(`Missing project name. Config: ${JSON.stringify(config)}`);
25
+ throw new Error(constants_1.PORTER_ERRORS.projectNameMissing);
26
+ }
27
+ return projectName;
28
+ });
29
+ }
30
+ exports.getProjectName = getProjectName;
@@ -0,0 +1,91 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ const constants_1 = require("../../constants");
36
+ const GetProjectConfig = __importStar(require("../GetProjectConfig/GetProjectConfig"));
37
+ const GetProjectName_1 = require("./GetProjectName");
38
+ describe('getProjectName', () => {
39
+ beforeEach(() => {
40
+ jest.mock('fs/promises', () => {
41
+ return {
42
+ readFile: jest.fn(),
43
+ };
44
+ });
45
+ jest.mock('../GetProjectConfig/GetProjectConfig', () => {
46
+ return {
47
+ getProjectConfig: jest.fn(),
48
+ };
49
+ });
50
+ });
51
+ it('should throw an error if could not retrieve project config', () => __awaiter(void 0, void 0, void 0, function* () {
52
+ const mockErrorMessage = 'missing config';
53
+ jest
54
+ .spyOn(GetProjectConfig, 'getProjectConfig')
55
+ .mockImplementationOnce(() => {
56
+ throw new Error(mockErrorMessage);
57
+ });
58
+ yield expect((0, GetProjectName_1.getProjectName)()).rejects.toThrowError(mockErrorMessage);
59
+ }));
60
+ it('should throw an error if project name is missing from config file', () => __awaiter(void 0, void 0, void 0, function* () {
61
+ const projectConfigBlank = {
62
+ projectName: '',
63
+ service: 'dxp-porter',
64
+ version: '0.1.0',
65
+ };
66
+ const projectConfigMissing = {
67
+ service: 'dxp-porter',
68
+ version: '0.1.0',
69
+ };
70
+ jest
71
+ .spyOn(GetProjectConfig, 'getProjectConfig')
72
+ .mockImplementationOnce(() => Promise.resolve(projectConfigBlank))
73
+ .mockImplementationOnce(() => Promise.resolve(projectConfigMissing));
74
+ // Blank project name
75
+ yield expect((0, GetProjectName_1.getProjectName)()).rejects.toThrowError(constants_1.PORTER_ERRORS.projectNameMissing);
76
+ // Missing project name
77
+ yield expect((0, GetProjectName_1.getProjectName)()).rejects.toThrowError(constants_1.PORTER_ERRORS.projectNameMissing);
78
+ }));
79
+ it('should return the project name from the project config', () => __awaiter(void 0, void 0, void 0, function* () {
80
+ const projectConfig = {
81
+ service: 'dxp-porter',
82
+ projectName: 'mock-name',
83
+ version: '0.1.0',
84
+ };
85
+ jest
86
+ .spyOn(GetProjectConfig, 'getProjectConfig')
87
+ .mockImplementationOnce(() => Promise.resolve(projectConfig));
88
+ const projectName = yield (0, GetProjectName_1.getProjectName)();
89
+ expect(projectName).toStrictEqual('mock-name');
90
+ }));
91
+ });
@@ -4,3 +4,4 @@ export * from './CoreUtils/CoreUtils';
4
4
  export * from './DoesPathExist/DoesPathExist';
5
5
  export * from './ErrorUtils/ErrorUtils';
6
6
  export * from './GetProjectConfig/GetProjectConfig';
7
+ export * from './GetProjectName/GetProjectName';
@@ -20,3 +20,4 @@ __exportStar(require("./CoreUtils/CoreUtils"), exports);
20
20
  __exportStar(require("./DoesPathExist/DoesPathExist"), exports);
21
21
  __exportStar(require("./ErrorUtils/ErrorUtils"), exports);
22
22
  __exportStar(require("./GetProjectConfig/GetProjectConfig"), exports);
23
+ __exportStar(require("./GetProjectName/GetProjectName"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/dxp-cli-next",
3
- "version": "5.15.0-develop.1",
3
+ "version": "5.15.0-develop.3",
4
4
  "repository": {
5
5
  "url": "https://gitlab.squiz.net/dxp/dxp-cli-next"
6
6
  },
@@ -41,7 +41,7 @@
41
41
  "dependencies": {
42
42
  "@apidevtools/swagger-parser": "10.1.0",
43
43
  "@squiz/component-cli-lib": "1.65.1",
44
- "@squiz/dxp-porter-shared": "0.2.7",
44
+ "@squiz/dxp-porter-shared": "0.3.0",
45
45
  "axios": "1.1.3",
46
46
  "cli-color": "2.0.3",
47
47
  "commander": "9.4.0",