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

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 (33) 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 +94 -0
  10. package/lib/porter/port/get/get.spec.d.ts +1 -0
  11. package/lib/porter/port/get/get.spec.js +255 -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.spec.js +39 -92
  19. package/lib/porter/project/get/get.js +4 -9
  20. package/lib/porter/project/get/get.spec.js +47 -83
  21. package/lib/porter/project/remove/remove.js +1 -6
  22. package/lib/porter/project/remove/remove.spec.js +42 -77
  23. package/lib/porter/utils/ErrorUtils/ErrorUtils.js +4 -1
  24. package/lib/porter/utils/ErrorUtils/ErrorUtils.spec.js +17 -2
  25. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.js +2 -1
  26. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.spec.js +4 -3
  27. package/lib/porter/utils/GetProjectName/GetProjectName.d.ts +4 -0
  28. package/lib/porter/utils/GetProjectName/GetProjectName.js +30 -0
  29. package/lib/porter/utils/GetProjectName/GetProjectName.spec.d.ts +1 -0
  30. package/lib/porter/utils/GetProjectName/GetProjectName.spec.js +91 -0
  31. package/lib/porter/utils/index.d.ts +1 -0
  32. package/lib/porter/utils/index.js +1 -0
  33. package/package.json +2 -2
@@ -31,22 +31,15 @@ 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 get_1 = __importDefault(require("./get"));
40
35
  const axios_1 = require("axios");
41
36
  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"));
37
+ const PorterUtils = __importStar(require("../../utils"));
45
38
  const logSpy = jest.spyOn(global.console, 'log');
39
+ jest.mock('../../utils');
46
40
  describe('getProject', () => {
47
41
  let baseUrl;
48
42
  let projectName;
49
- let projectConfig;
50
43
  let mockHandleCommandError;
51
44
  let mockHandleError;
52
45
  let mockBuildPorterUrl;
@@ -55,14 +48,8 @@ describe('getProject', () => {
55
48
  beforeEach(() => {
56
49
  jest.resetAllMocks();
57
50
  jest.clearAllMocks();
58
- nock_1.default.restore();
59
51
  projectName = 'porter-project';
60
52
  baseUrl = 'mock-url';
61
- projectConfig = {
62
- service: 'dxp-porter',
63
- projectName,
64
- version: '0.1.0',
65
- };
66
53
  mockHandleCommandError = jest.fn(() => Promise.resolve());
67
54
  mockHandleError = jest.fn();
68
55
  mockBuildPorterUrl = jest.fn(() => Promise.resolve(baseUrl));
@@ -73,14 +60,18 @@ describe('getProject', () => {
73
60
  mockClient = {
74
61
  get: jest.fn().mockImplementationOnce(() => mockResponse),
75
62
  };
63
+ jest.spyOn(PorterUtils, 'throwErrorIfNotLoggedIn').mockImplementation();
64
+ jest
65
+ .spyOn(PorterUtils, 'getProjectName')
66
+ .mockImplementationOnce(() => Promise.resolve(projectName));
76
67
  jest
77
- .spyOn(BuildPorterUrl, 'buildPorterUrl')
68
+ .spyOn(PorterUtils, 'buildPorterUrl')
78
69
  .mockImplementationOnce(mockBuildPorterUrl);
79
70
  jest
80
- .spyOn(ErrorUtils, 'handleCommandError')
71
+ .spyOn(PorterUtils, 'handleCommandError')
81
72
  .mockImplementationOnce(mockHandleCommandError);
82
73
  jest
83
- .spyOn(ErrorUtils, 'handleError')
74
+ .spyOn(PorterUtils, 'handleError')
84
75
  .mockImplementationOnce(mockHandleError);
85
76
  jest.spyOn(process, 'exit').mockImplementation();
86
77
  jest.spyOn(ApiService, 'ApiService').mockImplementationOnce(() => {
@@ -92,47 +83,34 @@ describe('getProject', () => {
92
83
  it('should throw an error if not logged in', () => __awaiter(void 0, void 0, void 0, function* () {
93
84
  const mockError = new Error('Error not logged in');
94
85
  jest
95
- .spyOn(ErrorUtils, 'throwErrorIfNotLoggedIn')
96
- .mockImplementationOnce(() => {
86
+ .spyOn(PorterUtils, 'throwErrorIfNotLoggedIn')
87
+ .mockImplementation(() => {
97
88
  throw mockError;
98
89
  });
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);
90
+ const { default: createGetCommand } = require('./get');
91
+ const program = createGetCommand();
92
+ yield expect(() => program.parseAsync(['project', 'getProject'])).rejects.toThrow(mockError);
104
93
  const opts = program.opts();
105
94
  expect(opts).toStrictEqual({});
95
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
96
+ expect(PorterUtils.buildPorterUrl).not.toHaveBeenCalled();
106
97
  }));
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();
98
+ it('should throw an error if project name could not be found', () => __awaiter(void 0, void 0, void 0, function* () {
109
99
  jest
110
- .spyOn(GetProjectConfig, 'getProjectConfig')
111
- .mockImplementationOnce(() => {
112
- throw new Error('You must be within a porter project directory to use this command');
100
+ .spyOn(PorterUtils, 'getProjectName')
101
+ .mockReset()
102
+ .mockImplementation(() => {
103
+ throw new Error();
113
104
  });
114
105
  const { default: createGetCommand } = require('./get');
115
106
  const program = createGetCommand();
116
107
  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();
108
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
109
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
110
+ expect(mockClient.get).not.toHaveBeenCalled();
111
+ expect(PorterUtils.handleCommandError).toHaveBeenCalled();
130
112
  }));
131
113
  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
114
  mockClient.get = jest.fn(() => {
137
115
  throw new axios_1.AxiosError('Project does not exist', '404', undefined, undefined, {
138
116
  data: { status: 404 },
@@ -140,16 +118,13 @@ describe('getProject', () => {
140
118
  });
141
119
  const { default: createGetCommand } = require('./get');
142
120
  const program = createGetCommand();
143
- yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
144
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
121
+ yield program.parseAsync(['project', 'getProject']);
122
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
123
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
145
124
  expect(mockClient.get).toHaveBeenCalled();
146
- expect(ErrorUtils.handleError).toHaveBeenCalled();
125
+ expect(PorterUtils.handleError).toHaveBeenCalled();
147
126
  }));
148
127
  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
128
  mockClient.get = jest.fn(() => {
154
129
  throw new axios_1.AxiosError('Internal server error', '500', undefined, undefined, {
155
130
  data: { status: 500 },
@@ -157,23 +132,17 @@ describe('getProject', () => {
157
132
  });
158
133
  const { default: createGetCommand } = require('./get');
159
134
  const program = createGetCommand();
160
- yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
161
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
135
+ yield program.parseAsync(['project', 'getProject']);
136
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
137
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
162
138
  expect(mockClient.get).toHaveBeenCalled();
163
- expect(ErrorUtils.handleCommandError).toHaveBeenCalled();
139
+ expect(PorterUtils.handleCommandError).toHaveBeenCalled();
164
140
  }));
165
141
  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
142
  const mockGetResponse = {
171
- portid: '1234-5678-abcd-efgh',
172
- stages: {},
173
- status: {},
174
- 'artifacts-link': null,
175
- 'report-link': null,
176
- 'stage-settings': {},
143
+ projectid: projectName,
144
+ 'last-artifacts-link': null,
145
+ 'last-report-link': null,
177
146
  };
178
147
  mockResponse = {
179
148
  data: mockGetResponse,
@@ -181,28 +150,22 @@ describe('getProject', () => {
181
150
  };
182
151
  const { default: createGetCommand } = require('./get');
183
152
  const program = createGetCommand();
184
- yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
185
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
153
+ yield program.parseAsync(['project', 'getProject']);
154
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
155
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
186
156
  expect(mockClient.get).toHaveBeenCalled();
187
- expect(ErrorUtils.handleCommandError).not.toHaveBeenCalled();
157
+ expect(PorterUtils.handleCommandError).not.toHaveBeenCalled();
188
158
  expect(logSpy).toHaveBeenNthCalledWith(2, `Project name: ${projectName}`);
189
159
  expect(logSpy).toHaveBeenNthCalledWith(3, 'Download last port report: Not generated');
190
160
  expect(logSpy).toHaveBeenNthCalledWith(4, 'Download last port artifacts: Not generated');
191
161
  }));
192
162
  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
163
  const mockReportLink = 'mock-report-link';
198
164
  const mockArtifactsLink = 'mock-artifacts-link';
199
165
  const mockGetResponse = {
200
- portid: '1234-5678-abcd-efgh',
201
- stages: {},
202
- status: {},
203
- 'artifacts-link': mockArtifactsLink,
204
- 'report-link': mockReportLink,
205
- 'stage-settings': {},
166
+ projectid: '1234-5678-abcd-efgh',
167
+ 'last-artifacts-link': mockArtifactsLink,
168
+ 'last-report-link': mockReportLink,
206
169
  };
207
170
  mockResponse = {
208
171
  data: mockGetResponse,
@@ -210,10 +173,11 @@ describe('getProject', () => {
210
173
  };
211
174
  const { default: createGetCommand } = require('./get');
212
175
  const program = createGetCommand();
213
- yield program.parseAsync(['dxp-next', 'porter', 'project', 'getProject']);
214
- expect(BuildPorterUrl.buildPorterUrl).toHaveBeenCalled();
176
+ yield program.parseAsync(['project', 'getProject']);
177
+ expect(PorterUtils.throwErrorIfNotLoggedIn).toHaveBeenCalled();
178
+ expect(PorterUtils.buildPorterUrl).toHaveBeenCalled();
215
179
  expect(mockClient.get).toHaveBeenCalled();
216
- expect(ErrorUtils.handleCommandError).not.toHaveBeenCalled();
180
+ expect(PorterUtils.handleCommandError).not.toHaveBeenCalled();
217
181
  expect(logSpy).toHaveBeenNthCalledWith(2, `Project name: ${projectName}`);
218
182
  expect(logSpy).toHaveBeenNthCalledWith(3, `Download last port report: ${mockReportLink}`);
219
183
  expect(logSpy).toHaveBeenNthCalledWith(4, `Download last port artifacts: ${mockArtifactsLink}`);
@@ -18,7 +18,6 @@ const inquirer_1 = require("inquirer");
18
18
  const ora_1 = __importDefault(require("ora"));
19
19
  const ApiService_1 = require("../../../ApiService");
20
20
  const utils_1 = require("../../utils");
21
- const constants_1 = require("../../constants");
22
21
  const createRemoveProjectCommand = () => {
23
22
  const removeCommand = new commander_1.Command('removeProject')
24
23
  .description('Deletes the project')
@@ -34,11 +33,7 @@ const createRemoveProjectCommand = () => {
34
33
  let projectName = '';
35
34
  const spinner = (0, ora_1.default)();
36
35
  try {
37
- const projectConfig = yield (0, utils_1.getProjectConfig)();
38
- projectName = projectConfig.projectName;
39
- if (!projectName) {
40
- throw new Error(constants_1.PORTER_ERRORS.projectNameMissing);
41
- }
36
+ projectName = yield (0, utils_1.getProjectName)();
42
37
  const confirmation = yield (0, inquirer_1.prompt)({
43
38
  message: `Are you sure you wish to remove all data about the project "${projectName}" from the cloud permanently? [y/N]`,
44
39
  name: 'answer',
@@ -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;