@squiz/component-cli-lib 1.3.4-alpha.2 → 1.5.1-alpha.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/component-cli-lib",
3
- "version": "1.3.4-alpha.2",
3
+ "version": "1.5.1-alpha.1",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -13,12 +13,12 @@
13
13
  "author": "",
14
14
  "license": "ISC",
15
15
  "devDependencies": {
16
- "@squiz/component-lib": "1.3.4-alpha.2",
17
- "@squiz/component-web-api-lib": "1.3.4-alpha.2",
18
- "@squiz/dx-common-lib": "1.3.4-alpha.2",
19
- "@squiz/dx-json-schema-lib": "1.3.4-alpha.2",
20
- "@squiz/dx-logger-lib": "1.3.4-alpha.2",
21
- "@squiz/virus-scanner-lib": "1.3.4-alpha.2",
16
+ "@squiz/component-lib": "1.5.1-alpha.1",
17
+ "@squiz/component-web-api-lib": "1.5.1-alpha.1",
18
+ "@squiz/dx-common-lib": "1.5.1-alpha.1",
19
+ "@squiz/dx-json-schema-lib": "1.5.1-alpha.1",
20
+ "@squiz/dx-logger-lib": "1.5.1-alpha.1",
21
+ "@squiz/virus-scanner-lib": "1.5.1-alpha.1",
22
22
  "@types/cli-color": "2.0.2",
23
23
  "@types/express": "4.17.14",
24
24
  "@types/jest": "28.1.8",
@@ -31,10 +31,10 @@
31
31
  "ts-jest": "28.0.8",
32
32
  "ts-loader": "9.3.1",
33
33
  "ts-node": "10.9.1",
34
- "typescript": "4.9.3"
34
+ "typescript": "4.9.4"
35
35
  },
36
36
  "dependencies": {
37
- "@squiz/render-runtime-lib": "1.3.4-alpha.2",
37
+ "@squiz/render-runtime-lib": "1.5.1-alpha.1",
38
38
  "archiver": "5.3.1",
39
39
  "axios": "0.27.2",
40
40
  "cli-color": "^2.0.2",
@@ -42,5 +42,5 @@
42
42
  "open": "^8.4.0",
43
43
  "supertest": "^6.2.3"
44
44
  },
45
- "gitHead": "4f9e34a5a5c1bd225abc38a430fb8483acb594c6"
45
+ "gitHead": "c13280d14eb309774d081880dcd4d062e39d7c40"
46
46
  }
@@ -12,7 +12,7 @@ import configObj, {
12
12
  managementServiceRoot,
13
13
  contentService,
14
14
  } from './helper';
15
- import color from 'cli-color';
15
+
16
16
  import path from 'path';
17
17
  import supertest from 'supertest';
18
18
  import { logger } from '../upload-component-folder';
@@ -20,10 +20,6 @@ import { ComponentSetWebModelForCreate } from '@squiz/component-lib';
20
20
  import fsp from 'fs/promises';
21
21
  import { randomUUID } from 'crypto';
22
22
 
23
- const mockConsoleError = jest.fn();
24
- const mockConsoleLog = jest.fn();
25
-
26
- const orgConsoleError = console.error;
27
23
  const webPath = 'set-' + randomUUID();
28
24
  const contentItemId = randomUUID();
29
25
  const testFilesDir = path.resolve(__dirname, 'test-files');
@@ -57,49 +53,48 @@ describe('uploading a component', () => {
57
53
 
58
54
  describe('Test isolated test cases', () => {
59
55
  beforeEach(() => {
60
- jest.spyOn(logger, 'error').mockImplementation(mockConsoleError);
61
- jest.spyOn(logger, 'info').mockImplementation(mockConsoleLog);
56
+ // jest.spyOn(logger, 'error').mockImplementation(mockConsoleError);
57
+ // jest.spyOn(logger, 'info').mockImplementation(mockConsoleLog);
62
58
  });
63
59
 
64
60
  it('Should fail uploading a component without manifest.json', async () => {
65
- mockConsoleError.mockClear();
66
61
  const componentPath = path.join(__dirname, '/__components__/invalid-upload');
67
- await uploadComponentFolder(managementServiceRoot, configObj.managementServiceUrl, componentPath, testFilesDir);
68
- expect(mockConsoleError.mock.calls[0][0]).toEqual(color.red('manifest could not be found'));
62
+ await expect(
63
+ uploadComponentFolder(managementServiceRoot, configObj.managementServiceUrl, componentPath, testFilesDir),
64
+ ).rejects.toThrowError(new Error('manifest could not be found'));
69
65
  });
70
66
 
71
67
  it('Should fail uploading a component that has invalid manifest.json', async () => {
72
- mockConsoleError.mockClear();
73
68
  const componentPath = path.join(__dirname, '/__components__/invalid-manifest');
74
- await uploadComponentFolder(
75
- managementServiceRoot,
76
- configObj.managementServiceUrl,
77
- componentPath,
69
+ await expect(
70
+ uploadComponentFolder(
71
+ managementServiceRoot,
72
+ configObj.managementServiceUrl,
73
+ componentPath,
78
74
 
79
- testFilesDir,
80
- );
81
- expect(mockConsoleError.mock.calls[0][0]).toEqual(
82
- color.red(
75
+ testFilesDir,
76
+ ),
77
+ ).rejects.toThrowError(
78
+ new Error(
83
79
  'failed validation: Value in `#/name` should match `^[a-zA-Z0-9_\\-]+$`, but received `invalid-manifes@t`',
84
80
  ),
85
81
  );
86
82
  });
87
83
 
88
84
  it('Should fail uploading the component that has size more than 100MB', async () => {
89
- mockConsoleError.mockClear();
90
85
  const componentPath = path.join(__dirname, '/__components__/big-package');
91
86
  const filePath = `${componentPath}/105mb-file`;
92
87
  await createFile(filePath, 105); // Higher limit has been used because compression reduces the size if you use closer to 100MB
93
- await uploadComponentFolder(
94
- managementServiceRoot,
95
- configObj.managementServiceUrl,
96
- componentPath,
88
+ await expect(
89
+ uploadComponentFolder(
90
+ managementServiceRoot,
91
+ configObj.managementServiceUrl,
92
+ componentPath,
93
+
94
+ testFilesDir,
95
+ ),
96
+ ).rejects.toThrowError(new Error('File size exceeds the maximum limit of 100MB'));
97
97
 
98
- testFilesDir,
99
- );
100
- expect(mockConsoleError.mock.calls[0][0]).toEqual(
101
- color.red(['File size exceeds the maximum limit of 100MB'].join('')),
102
- );
103
98
  removeFile(filePath);
104
99
  });
105
100
  });
@@ -120,15 +115,6 @@ describe('uploading a component', () => {
120
115
  }
121
116
  });
122
117
 
123
- beforeEach(() => {
124
- console.error = mockConsoleError;
125
- console.log = mockConsoleLog;
126
- });
127
-
128
- afterEach(() => {
129
- console.error = orgConsoleError;
130
- });
131
-
132
118
  it('Should upload the component and return a valid url to preview', async () => {
133
119
  const componentPath = path.join(__dirname, '/__components__/cmp-format-string');
134
120
  await uploadComponentFolder(managementServiceRoot, configObj.managementServiceUrl, componentPath, testFilesDir);
@@ -186,15 +172,6 @@ describe('uploading a component', () => {
186
172
  }
187
173
  });
188
174
 
189
- beforeEach(() => {
190
- console.error = mockConsoleError;
191
- console.log = mockConsoleLog;
192
- });
193
-
194
- afterEach(() => {
195
- console.error = orgConsoleError;
196
- });
197
-
198
175
  it('Should upload the component and return a valid url to preview', async () => {
199
176
  await uploadComponentFolder(
200
177
  managementServiceRoot,
@@ -211,16 +188,16 @@ describe('uploading a component', () => {
211
188
  });
212
189
 
213
190
  it('Should fail upload the component with same version', async () => {
214
- mockConsoleError.mockClear();
215
- await uploadComponentFolder(
216
- managementServiceRoot,
217
- configObj.managementServiceUrl,
218
- componentPath,
191
+ await expect(
192
+ uploadComponentFolder(
193
+ managementServiceRoot,
194
+ configObj.managementServiceUrl,
195
+ componentPath,
219
196
 
220
- testFilesDir,
221
- );
222
- expect(mockConsoleError.mock.calls[0][0]).toEqual(
223
- color.red('Cannot upload component version, smoke-test-components/cmp-static-file-test 1.0.0 already exists'),
197
+ testFilesDir,
198
+ ),
199
+ ).rejects.toThrowError(
200
+ new Error('Cannot upload component version, smoke-test-components/cmp-static-file-test 1.0.0 already exists'),
224
201
  );
225
202
  });
226
203
 
@@ -49,11 +49,7 @@ export async function uploadComponentFolder(
49
49
  } catch (e) {
50
50
  await fsp.rm(tmpDir, { force: true, recursive: true });
51
51
 
52
- if (e instanceof Error) {
53
- logger.error(color.red(e.message));
54
- } else {
55
- throw e;
56
- }
52
+ throw e;
57
53
  }
58
54
  }
59
55
 
@@ -70,16 +66,30 @@ async function preUploadChecks(apiClient: AxiosInstance, managementURL: string,
70
66
  }
71
67
 
72
68
  async function checkIfVersionExists(apiClient: AxiosInstance, managementURL: string, inputManifest: Manifest) {
73
- const response = await apiClient.get(`/v1/component/${inputManifest.getName()}/${inputManifest.getVersion()}`, {
74
- validateStatus: null,
75
- baseURL: managementURL,
76
- });
77
- if (response.status === 200) {
78
- return true;
79
- } else if (response.status === 404) {
80
- return false;
69
+ try {
70
+ const response = await apiClient.get(`/v1/component/${inputManifest.getName()}/${inputManifest.getVersion()}`, {
71
+ validateStatus: null,
72
+ baseURL: managementURL,
73
+ });
74
+
75
+ if (response.status === 200) {
76
+ return true;
77
+ } else if (response.status === 404) {
78
+ return false;
79
+ }
80
+
81
+ const error = new AxiosError(
82
+ response?.data?.message || response.statusText,
83
+ response.status.toString(),
84
+ undefined,
85
+ undefined,
86
+ response,
87
+ );
88
+
89
+ throw handleError(error);
90
+ } catch (error) {
91
+ throw handleError(error);
81
92
  }
82
- throw new Error(`Unexpected response code ${response.status}`);
83
93
  }
84
94
 
85
95
  async function watchAndWaitForUploadAndScanComplete(
@@ -121,15 +131,20 @@ async function handleResponse<T>(axiosInstance: Promise<AxiosResponse<T>>): Prom
121
131
 
122
132
  function handleError(error: any): Error {
123
133
  const { response } = error;
124
- if (!response || !isAxiosError(error)) {
125
- return error;
126
- }
127
- const message = (response.data as any).message;
128
- if (message) {
129
- return new Error(`Unexpected response code ${response.status}. message: ${message}`);
130
- } else {
131
- return new Error(`Unexpected response code ${response.status}.`);
134
+ const errorMessage = error.message;
135
+ const newError = new Error(errorMessage || 'An error has occurred');
136
+
137
+ if (isAxiosError(error)) {
138
+ if (response?.data?.message) {
139
+ newError.message = `${errorMessage} (code: ${response.status})`;
140
+ } else {
141
+ newError.message = `Unexpected response code ${response.status}. ${errorMessage}`.trim();
142
+ }
132
143
  }
144
+
145
+ newError.stack = error.stack;
146
+
147
+ return newError;
133
148
  }
134
149
 
135
150
  function isAxiosError(error: any): error is AxiosError {