@squiz/dxp-cli-next 5.14.0 → 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 (75) 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 +8 -0
  12. package/lib/porter/constants.js +11 -0
  13. package/lib/porter/index.d.ts +3 -0
  14. package/lib/porter/index.js +14 -0
  15. package/lib/porter/port/abort/abort.d.ts +3 -0
  16. package/lib/porter/port/abort/abort.js +77 -0
  17. package/lib/porter/port/abort/abort.spec.d.ts +1 -0
  18. package/lib/porter/port/abort/abort.spec.js +182 -0
  19. package/lib/porter/port/get/get.d.ts +3 -0
  20. package/lib/porter/port/get/get.js +94 -0
  21. package/lib/porter/port/get/get.spec.d.ts +1 -0
  22. package/lib/porter/port/get/get.spec.js +255 -0
  23. package/lib/porter/port/portCommand.d.ts +3 -0
  24. package/lib/porter/port/portCommand.js +19 -0
  25. package/lib/porter/port/start/start.d.ts +3 -0
  26. package/lib/porter/port/start/start.js +79 -0
  27. package/lib/porter/port/start/start.spec.d.ts +1 -0
  28. package/lib/porter/port/start/start.spec.js +198 -0
  29. package/lib/porter/project/add/add.d.ts +3 -0
  30. package/lib/porter/project/add/add.js +125 -0
  31. package/lib/porter/project/add/add.spec.d.ts +1 -0
  32. package/lib/porter/project/add/add.spec.js +270 -0
  33. package/lib/porter/project/get/get.d.ts +3 -0
  34. package/lib/porter/project/get/get.js +62 -0
  35. package/lib/porter/project/get/get.spec.d.ts +1 -0
  36. package/lib/porter/project/get/get.spec.js +185 -0
  37. package/lib/porter/project/projectCommand.d.ts +3 -0
  38. package/lib/porter/project/projectCommand.js +19 -0
  39. package/lib/porter/project/remove/remove.d.ts +3 -0
  40. package/lib/porter/project/remove/remove.js +66 -0
  41. package/lib/porter/project/remove/remove.spec.d.ts +1 -0
  42. package/lib/porter/project/remove/remove.spec.js +201 -0
  43. package/lib/porter/types.d.ts +5 -0
  44. package/lib/porter/types.js +2 -0
  45. package/lib/porter/utils/AuthUtils/CheckAuthorisation.d.ts +1 -0
  46. package/lib/porter/utils/AuthUtils/CheckAuthorisation.js +32 -0
  47. package/lib/porter/utils/AuthUtils/CheckAuthorisation.spec.d.ts +1 -0
  48. package/lib/porter/utils/AuthUtils/CheckAuthorisation.spec.js +77 -0
  49. package/lib/porter/utils/BuildPorterUrl/BuildPorterUrl.d.ts +1 -0
  50. package/lib/porter/utils/BuildPorterUrl/BuildPorterUrl.js +27 -0
  51. package/lib/porter/utils/BuildPorterUrl/BuildPorterUrl.spec.d.ts +1 -0
  52. package/lib/porter/utils/BuildPorterUrl/BuildPorterUrl.spec.js +59 -0
  53. package/lib/porter/utils/CoreUtils/CoreUtils.d.ts +2 -0
  54. package/lib/porter/utils/CoreUtils/CoreUtils.js +13 -0
  55. package/lib/porter/utils/CoreUtils/CoreUtils.spec.d.ts +1 -0
  56. package/lib/porter/utils/CoreUtils/CoreUtils.spec.js +36 -0
  57. package/lib/porter/utils/DoesPathExist/DoesPathExist.d.ts +1 -0
  58. package/lib/porter/utils/DoesPathExist/DoesPathExist.js +27 -0
  59. package/lib/porter/utils/DoesPathExist/DoesPathExist.spec.d.ts +1 -0
  60. package/lib/porter/utils/DoesPathExist/DoesPathExist.spec.js +40 -0
  61. package/lib/porter/utils/ErrorUtils/ErrorUtils.d.ts +4 -0
  62. package/lib/porter/utils/ErrorUtils/ErrorUtils.js +71 -0
  63. package/lib/porter/utils/ErrorUtils/ErrorUtils.spec.d.ts +1 -0
  64. package/lib/porter/utils/ErrorUtils/ErrorUtils.spec.js +190 -0
  65. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.d.ts +2 -0
  66. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.js +33 -0
  67. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.spec.d.ts +1 -0
  68. package/lib/porter/utils/GetProjectConfig/GetProjectConfig.spec.js +50 -0
  69. package/lib/porter/utils/GetProjectName/GetProjectName.d.ts +4 -0
  70. package/lib/porter/utils/GetProjectName/GetProjectName.js +30 -0
  71. package/lib/porter/utils/GetProjectName/GetProjectName.spec.d.ts +1 -0
  72. package/lib/porter/utils/GetProjectName/GetProjectName.spec.js +91 -0
  73. package/lib/porter/utils/index.d.ts +7 -0
  74. package/lib/porter/utils/index.js +23 -0
  75. package/package.json +6 -3
@@ -0,0 +1,4 @@
1
+ import { Command } from 'commander';
2
+ export declare function handleCommandError(command: Command, error: Error): undefined;
3
+ export declare function handleError(command: Command, errorMessage: string): void;
4
+ export declare function throwErrorIfNotLoggedIn(command: Command): Promise<undefined>;
@@ -0,0 +1,71 @@
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.throwErrorIfNotLoggedIn = exports.handleError = exports.handleCommandError = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const chalk_1 = __importDefault(require("chalk"));
18
+ const ApplicationStore_1 = require("../../../ApplicationStore");
19
+ const CheckAuthorisation_1 = require("../AuthUtils/CheckAuthorisation");
20
+ function handleCommandError(command, error) {
21
+ if (!axios_1.default.isAxiosError(error)) {
22
+ if (!!process.env.DEBUG && error.stack) {
23
+ return command.error(error.stack);
24
+ }
25
+ if (error.message) {
26
+ return command.error(chalk_1.default.red(error.message));
27
+ }
28
+ return command.error(chalk_1.default.red('An unknown error occurred'));
29
+ }
30
+ if (!error.response) {
31
+ return command.error(chalk_1.default.red(error.message));
32
+ }
33
+ if (error.response.status === 401 || error.response.status === 403) {
34
+ const errorMessage = 'You must be logged in as a DXP super admin or Squiz support staff to use this service';
35
+ return command.error(chalk_1.default.red(errorMessage));
36
+ }
37
+ if (error.response.status >= 500) {
38
+ const errorMessage = `Action failed. Service responded with "${error.response.status}" response code`;
39
+ return command.error(chalk_1.default.red(errorMessage));
40
+ }
41
+ if (error.response.data) {
42
+ let errorMessage;
43
+ if (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));
48
+ }
49
+ else {
50
+ errorMessage = error.response.data.title;
51
+ }
52
+ return command.error(chalk_1.default.red(errorMessage));
53
+ }
54
+ }
55
+ exports.handleCommandError = handleCommandError;
56
+ function handleError(command, errorMessage) {
57
+ command.error(chalk_1.default.red(errorMessage));
58
+ }
59
+ exports.handleError = handleError;
60
+ function throwErrorIfNotLoggedIn(command) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ if (!(yield (0, ApplicationStore_1.getApplicationFile)(ApplicationStore_1.STORE_FILES.sessionCookie))) {
63
+ return command.error(chalk_1.default.red('You must login to interact with the porter service. See `dxp-next auth login`'));
64
+ }
65
+ const isAuthorised = yield (0, CheckAuthorisation_1.checkAuthorisation)();
66
+ if (!isAuthorised) {
67
+ command.error(chalk_1.default.red('You must login to interact with the porter service. See `dxp-next auth login`'));
68
+ }
69
+ });
70
+ }
71
+ exports.throwErrorIfNotLoggedIn = throwErrorIfNotLoggedIn;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,190 @@
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 axios_1 = require("axios");
36
+ const commander_1 = require("commander");
37
+ const GetApplicationFile = __importStar(require("../../../ApplicationStore"));
38
+ const CheckAuthorisation = __importStar(require("../AuthUtils/CheckAuthorisation"));
39
+ const ErrorUtils_1 = require("./ErrorUtils");
40
+ describe('handleCommandError', () => {
41
+ let stderrSpy;
42
+ beforeEach(() => {
43
+ jest.resetAllMocks();
44
+ stderrSpy = jest.spyOn(process.stderr, 'write');
45
+ jest.spyOn(process, 'exit').mockImplementation();
46
+ });
47
+ it('correctly displays the console logs for command error', () => __awaiter(void 0, void 0, void 0, function* () {
48
+ process.env.DEBUG = 'true';
49
+ const command = new commander_1.Command();
50
+ const message = 'Something bad happened';
51
+ (0, ErrorUtils_1.handleCommandError)(command, new Error(message));
52
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
53
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
54
+ expect(stderrSpy.mock.calls[0][0]).toContain(message);
55
+ }));
56
+ it('correctly displays the console logs for command error', () => __awaiter(void 0, void 0, void 0, function* () {
57
+ delete process.env.DEBUG;
58
+ const command = new commander_1.Command();
59
+ const message = 'Something bad happened';
60
+ (0, ErrorUtils_1.handleCommandError)(command, new Error(message));
61
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
62
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
63
+ expect(stderrSpy.mock.calls[0][0]).toContain(message);
64
+ }));
65
+ it('correctly displays the console logs for error without a message', () => __awaiter(void 0, void 0, void 0, function* () {
66
+ const command = new commander_1.Command();
67
+ (0, ErrorUtils_1.handleCommandError)(command, new Error());
68
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
69
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
70
+ expect(stderrSpy.mock.calls[0][0]).toContain('An unknown error occurred');
71
+ }));
72
+ it('correctly displays the console logs for axios error without a proper response', () => __awaiter(void 0, void 0, void 0, function* () {
73
+ const command = new commander_1.Command();
74
+ const message = 'Something bad happened';
75
+ (0, ErrorUtils_1.handleCommandError)(command, new axios_1.AxiosError(message, '500', undefined, undefined, undefined));
76
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
77
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
78
+ expect(stderrSpy.mock.calls[0][0]).toContain(message);
79
+ }));
80
+ it('correctly displays the console logs for axios 500 error from the porter service', () => __awaiter(void 0, void 0, void 0, function* () {
81
+ const command = new commander_1.Command();
82
+ const message = 'Internal server error';
83
+ (0, ErrorUtils_1.handleCommandError)(command, new axios_1.AxiosError(message, '500', undefined, undefined, {
84
+ status: 500,
85
+ }));
86
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
87
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
88
+ expect(stderrSpy.mock.calls[0][0]).toContain('Action failed. Service responded with "500" response code');
89
+ }));
90
+ it.each([401, 403])('correctly displays the console logs for axios %s error from the porter service', (status) => __awaiter(void 0, void 0, void 0, function* () {
91
+ const command = new commander_1.Command();
92
+ const message = 'Unauthorized';
93
+ (0, ErrorUtils_1.handleCommandError)(command, new axios_1.AxiosError(message, `${status}`, undefined, undefined, {
94
+ status,
95
+ }));
96
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
97
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
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
+ }));
100
+ it('correctly displays the console logs for invalid-params errors from the porter service', () => __awaiter(void 0, void 0, void 0, function* () {
101
+ const command = new commander_1.Command();
102
+ const errorStatus = 400;
103
+ const errorTitle = 'Bad request';
104
+ const errorResponse = {
105
+ data: {
106
+ title: errorTitle,
107
+ 'invalid-params': [
108
+ {
109
+ name: 'name',
110
+ reason: 'reason',
111
+ },
112
+ ],
113
+ },
114
+ };
115
+ (0, ErrorUtils_1.handleCommandError)(command, new axios_1.AxiosError(errorTitle, `${errorStatus}`, undefined, undefined, errorResponse));
116
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
117
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
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);
134
+ }));
135
+ });
136
+ describe('handleError', () => {
137
+ let stderrSpy;
138
+ beforeEach(() => {
139
+ jest.resetAllMocks();
140
+ stderrSpy = jest.spyOn(process.stderr, 'write');
141
+ });
142
+ it('correctly displays the console logs for command error', () => __awaiter(void 0, void 0, void 0, function* () {
143
+ const command = new commander_1.Command();
144
+ const message = 'Something bad happened';
145
+ (0, ErrorUtils_1.handleError)(command, message);
146
+ expect(stderrSpy).toHaveBeenCalledTimes(1);
147
+ // read the mock call strings directly otherwise colours cause test failures with .toEqual()
148
+ expect(stderrSpy.mock.calls[0][0]).toContain(message);
149
+ }));
150
+ });
151
+ describe('throwErrorIfNotLoggedIn', () => {
152
+ let stderrSpy;
153
+ let command;
154
+ beforeEach(() => {
155
+ jest.resetAllMocks();
156
+ command = new commander_1.Command();
157
+ stderrSpy = jest.spyOn(process.stderr, 'write');
158
+ jest.spyOn(process, 'exit').mockImplementation();
159
+ });
160
+ it('should call the command error if cookie cannot be retrieved', () => __awaiter(void 0, void 0, void 0, function* () {
161
+ jest
162
+ .spyOn(GetApplicationFile, 'getApplicationFile')
163
+ .mockImplementationOnce(() => Promise.resolve(undefined));
164
+ yield (0, ErrorUtils_1.throwErrorIfNotLoggedIn)(command);
165
+ expect(stderrSpy.mock.calls[0][0]).toContain('You must login to interact with the porter service. See `dxp-next auth login');
166
+ expect(process.exit).toHaveBeenCalled();
167
+ }));
168
+ it('should call the command error if unauthorised', () => __awaiter(void 0, void 0, void 0, function* () {
169
+ jest
170
+ .spyOn(GetApplicationFile, 'getApplicationFile')
171
+ .mockImplementationOnce(() => Promise.resolve('cookie'));
172
+ jest
173
+ .spyOn(CheckAuthorisation, 'checkAuthorisation')
174
+ .mockImplementationOnce(() => Promise.resolve(false));
175
+ yield (0, ErrorUtils_1.throwErrorIfNotLoggedIn)(command);
176
+ expect(stderrSpy.mock.calls[0][0]).toContain('You must login to interact with the porter service. See `dxp-next auth login');
177
+ expect(process.exit).toHaveBeenCalled();
178
+ }));
179
+ it('should not call the command error if logged in', () => __awaiter(void 0, void 0, void 0, function* () {
180
+ jest
181
+ .spyOn(GetApplicationFile, 'getApplicationFile')
182
+ .mockImplementationOnce(() => Promise.resolve('cookie'));
183
+ jest
184
+ .spyOn(CheckAuthorisation, 'checkAuthorisation')
185
+ .mockImplementationOnce(() => Promise.resolve(true));
186
+ yield (0, ErrorUtils_1.throwErrorIfNotLoggedIn)(command);
187
+ expect(stderrSpy).not.toHaveBeenCalled();
188
+ expect(process.exit).not.toHaveBeenCalled();
189
+ }));
190
+ });
@@ -0,0 +1,2 @@
1
+ import { ProjectConfig } from '../../types';
2
+ export declare function getProjectConfig(): Promise<ProjectConfig>;
@@ -0,0 +1,33 @@
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.getProjectConfig = void 0;
16
+ const path_1 = __importDefault(require("path"));
17
+ const promises_1 = __importDefault(require("fs/promises"));
18
+ const CoreUtils_1 = require("../CoreUtils/CoreUtils");
19
+ const constants_1 = require("../../constants");
20
+ function getProjectConfig() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ try {
23
+ const configFile = path_1.default.resolve('config.json');
24
+ const config = yield promises_1.default.readFile(configFile, { encoding: 'utf-8' });
25
+ return JSON.parse(config);
26
+ }
27
+ catch (err) {
28
+ (0, CoreUtils_1.logDebug)(err);
29
+ throw new Error(constants_1.PORTER_ERRORS.projectDirectoryInvalid);
30
+ }
31
+ });
32
+ }
33
+ exports.getProjectConfig = getProjectConfig;
@@ -0,0 +1,50 @@
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 GetProjectConfig_1 = require("./GetProjectConfig");
16
+ const promises_1 = __importDefault(require("fs/promises"));
17
+ const constants_1 = require("../../constants");
18
+ jest.mock('fs/promises', () => {
19
+ return {
20
+ readFile: jest.fn(),
21
+ };
22
+ });
23
+ describe('getProjectConfig', () => {
24
+ it('should throw an error if config file does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
25
+ jest.spyOn(promises_1.default, 'readFile').mockImplementationOnce(() => {
26
+ throw new Error('ENOENT');
27
+ });
28
+ yield expect((0, GetProjectConfig_1.getProjectConfig)()).rejects.toThrowError(constants_1.PORTER_ERRORS.projectDirectoryInvalid);
29
+ expect(promises_1.default.readFile).toHaveBeenCalled();
30
+ }));
31
+ it('should throw an error if cannot parse config file', () => __awaiter(void 0, void 0, void 0, function* () {
32
+ jest
33
+ .spyOn(promises_1.default, 'readFile')
34
+ .mockImplementationOnce(() => Promise.resolve('invalid-json'));
35
+ yield expect((0, GetProjectConfig_1.getProjectConfig)()).rejects.toThrowError(constants_1.PORTER_ERRORS.projectDirectoryInvalid);
36
+ expect(promises_1.default.readFile).toHaveBeenCalled();
37
+ }));
38
+ it('should return the project config json', () => __awaiter(void 0, void 0, void 0, function* () {
39
+ const projectConfig = {
40
+ service: 'dxp-porter',
41
+ projectName: 'name',
42
+ version: '0.1.0',
43
+ };
44
+ jest
45
+ .spyOn(promises_1.default, 'readFile')
46
+ .mockImplementationOnce(() => Promise.resolve(JSON.stringify(projectConfig)));
47
+ const parsedConfig = yield (0, GetProjectConfig_1.getProjectConfig)();
48
+ expect(parsedConfig).toStrictEqual(projectConfig);
49
+ }));
50
+ });
@@ -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
+ });
@@ -0,0 +1,7 @@
1
+ export * from './AuthUtils/CheckAuthorisation';
2
+ export * from './BuildPorterUrl/BuildPorterUrl';
3
+ export * from './CoreUtils/CoreUtils';
4
+ export * from './DoesPathExist/DoesPathExist';
5
+ export * from './ErrorUtils/ErrorUtils';
6
+ export * from './GetProjectConfig/GetProjectConfig';
7
+ export * from './GetProjectName/GetProjectName';
@@ -0,0 +1,23 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./AuthUtils/CheckAuthorisation"), exports);
18
+ __exportStar(require("./BuildPorterUrl/BuildPorterUrl"), exports);
19
+ __exportStar(require("./CoreUtils/CoreUtils"), exports);
20
+ __exportStar(require("./DoesPathExist/DoesPathExist"), exports);
21
+ __exportStar(require("./ErrorUtils/ErrorUtils"), exports);
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.14.0",
3
+ "version": "5.15.0-develop.2",
4
4
  "repository": {
5
5
  "url": "https://gitlab.squiz.net/dxp/dxp-cli-next"
6
6
  },
@@ -41,18 +41,21 @@
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.3.0",
44
45
  "axios": "1.1.3",
45
46
  "cli-color": "2.0.3",
46
47
  "commander": "9.4.0",
47
48
  "dotenv": "^16.3.1",
48
49
  "env-paths": "2.2.1",
50
+ "esbuild": "^0.21.4",
49
51
  "inquirer": "8.2.5",
50
52
  "opener": "1.5.2",
51
53
  "ora": "^5.4.1",
52
54
  "prompt": "^1.3.0",
53
55
  "tough-cookie": "4.1.2",
54
56
  "update-notifier": "5.1.0",
55
- "yaml": "^2.3.4"
57
+ "yaml": "^2.3.4",
58
+ "zod": "^3.23.8"
56
59
  },
57
60
  "devDependencies": {
58
61
  "@semantic-release/git": "10.0.1",
@@ -84,7 +87,7 @@
84
87
  "prettier": "2.7.1",
85
88
  "semantic-release": "19.0.3",
86
89
  "ts-jest": "28.0.7",
87
- "ts-node": "10.9.1",
90
+ "ts-node": "^10.9.1",
88
91
  "typescript": "4.7.4"
89
92
  },
90
93
  "config": {