@anmiles/google-api-wrapper 7.0.6 → 7.0.7

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 (55) hide show
  1. package/.eslintrc.js +1 -86
  2. package/.gitlab-ci.yml +0 -1
  3. package/.vscode/settings.json +0 -4
  4. package/CHANGELOG.md +7 -0
  5. package/README.md +5 -2
  6. package/dist/index.d.ts +5 -5
  7. package/dist/index.js +15 -15
  8. package/dist/lib/api/calendar.d.ts +3 -3
  9. package/dist/lib/api/calendar.js +13 -13
  10. package/dist/lib/api/shared.d.ts +23 -23
  11. package/dist/lib/api/shared.js +23 -23
  12. package/dist/lib/api/shared.js.map +1 -1
  13. package/dist/lib/api/youtube.d.ts +3 -3
  14. package/dist/lib/api/youtube.js +13 -13
  15. package/dist/lib/auth.d.ts +10 -10
  16. package/dist/lib/auth.js +34 -34
  17. package/dist/lib/auth.js.map +1 -1
  18. package/dist/lib/jsonLib.d.ts +14 -14
  19. package/dist/lib/jsonLib.js +54 -55
  20. package/dist/lib/jsonLib.js.map +1 -1
  21. package/dist/lib/paths.d.ts +16 -16
  22. package/dist/lib/paths.js +45 -45
  23. package/dist/lib/profiles.d.ts +10 -10
  24. package/dist/lib/profiles.js +32 -33
  25. package/dist/lib/profiles.js.map +1 -1
  26. package/dist/lib/secrets.d.ts +22 -22
  27. package/dist/lib/secrets.js +150 -146
  28. package/dist/lib/secrets.js.map +1 -1
  29. package/dist/lib/sleep.d.ts +6 -6
  30. package/dist/lib/sleep.js +10 -10
  31. package/dist/types/common.d.ts +3 -3
  32. package/dist/types/common.js +2 -2
  33. package/dist/types/index.d.ts +2 -2
  34. package/dist/types/index.js +18 -18
  35. package/dist/types/secrets.d.ts +15 -15
  36. package/dist/types/secrets.js +2 -2
  37. package/package.json +16 -15
  38. package/src/lib/__tests__/auth.test.ts +23 -23
  39. package/src/lib/__tests__/jsonLib.test.ts +33 -41
  40. package/src/lib/__tests__/paths.test.ts +5 -5
  41. package/src/lib/__tests__/profiles.test.ts +10 -13
  42. package/src/lib/__tests__/secrets.test.ts +40 -48
  43. package/src/lib/api/__tests__/calendar.test.ts +3 -3
  44. package/src/lib/api/__tests__/shared.test.ts +10 -10
  45. package/src/lib/api/__tests__/youtube.test.ts +3 -3
  46. package/src/lib/api/shared.ts +1 -1
  47. package/src/lib/auth.ts +1 -1
  48. package/src/lib/jsonLib.ts +2 -2
  49. package/src/lib/profiles.ts +1 -2
  50. package/src/lib/secrets.ts +8 -4
  51. package/dist/lib/logger.d.ts +0 -12
  52. package/dist/lib/logger.js +0 -46
  53. package/dist/lib/logger.js.map +0 -1
  54. package/src/lib/__tests__/logger.test.ts +0 -57
  55. package/src/lib/logger.ts +0 -21
@@ -1,10 +1,9 @@
1
1
  import http from 'http';
2
2
  import path from 'path';
3
- import * as colorette from 'colorette';
4
3
  import open from 'open';
5
4
  import type GoogleApis from 'googleapis';
5
+ import logger from '@anmiles/logger';
6
6
  import jsonLib from '../jsonLib';
7
- import logger from '../logger';
8
7
  import paths from '../paths';
9
8
  import type { Secrets } from '../../types';
10
9
 
@@ -38,10 +37,6 @@ jest.mock<Partial<typeof path>>('path', () => ({
38
37
  join : jest.fn().mockImplementation((...args) => args.join('/')),
39
38
  }));
40
39
 
41
- jest.mock<Partial<typeof colorette>>('colorette', () => ({
42
- yellow : jest.fn().mockImplementation((text) => `yellow:${text}`),
43
- }));
44
-
45
40
  jest.mock('open', () => jest.fn().mockImplementation((url: string) => {
46
41
  willOpen(url.replace('http://localhost:6006', ''));
47
42
  }));
@@ -52,11 +47,8 @@ jest.mock<Partial<typeof jsonLib>>('../jsonLib', () => ({
52
47
  readJSON : jest.fn().mockImplementation(async () => json),
53
48
  }));
54
49
 
55
- jest.mock<Partial<typeof logger>>('../logger', () => ({
56
- warn : jest.fn(),
57
- error : jest.fn().mockImplementation((error) => {
58
- throw error;
59
- }) as jest.Mock<never, any>,
50
+ jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
51
+ warn : jest.fn(),
60
52
  }));
61
53
 
62
54
  jest.mock<Partial<typeof paths>>('../paths', () => ({
@@ -161,14 +153,14 @@ describe('src/lib/secrets', () => {
161
153
  it('should get json from scopes file', async () => {
162
154
  await original.getScopes();
163
155
 
164
- expect(getJSONSpy).toBeCalled();
156
+ expect(getJSONSpy).toHaveBeenCalled();
165
157
  expect(getJSONSpy.mock.calls[0][0]).toEqual(scopesFile);
166
158
  });
167
159
 
168
160
  it('should fallback to error', async () => {
169
161
  await original.getScopes();
170
162
 
171
- expect(getJSONSpy.mock.calls[0][1]).toThrowError(scopesError);
163
+ expect(getJSONSpy.mock.calls[0][1]).toThrow(scopesError);
172
164
  });
173
165
 
174
166
  it('should return scopes', async () => {
@@ -188,20 +180,20 @@ describe('src/lib/secrets', () => {
188
180
  it('should get json from secrets file', async () => {
189
181
  await original.getSecrets(profile);
190
182
 
191
- expect(getJSONSpy).toBeCalled();
183
+ expect(getJSONSpy).toHaveBeenCalled();
192
184
  expect(getJSONSpy.mock.calls[0][0]).toEqual(secretsFile);
193
185
  });
194
186
 
195
187
  it('should fallback to error', async () => {
196
188
  await original.getSecrets(profile);
197
189
 
198
- expect(getJSONSpy.mock.calls[0][1]).toThrowError(secretsError);
190
+ expect(getJSONSpy.mock.calls[0][1]).toThrow(secretsError);
199
191
  });
200
192
 
201
193
  it('should check secrets', async () => {
202
194
  await original.getSecrets(profile);
203
195
 
204
- expect(secrets.checkSecrets).toBeCalledWith(profile, json, secretsFile);
196
+ expect(secrets.checkSecrets).toHaveBeenCalledWith(profile, json, secretsFile);
205
197
  });
206
198
 
207
199
  it('should return secrets', async () => {
@@ -222,21 +214,21 @@ describe('src/lib/secrets', () => {
222
214
  it('should get json from credentials file by default', async () => {
223
215
  await original.getCredentials(profile, auth);
224
216
 
225
- expect(getJSONAsyncSpy).toBeCalled();
217
+ expect(getJSONAsyncSpy).toHaveBeenCalled();
226
218
  expect(getJSONAsyncSpy.mock.calls[0][0]).toEqual(credentialsFile);
227
219
  });
228
220
 
229
221
  it('should get json from credentials file if temporariness not set', async () => {
230
222
  await original.getCredentials(profile, auth, { temporary : false });
231
223
 
232
- expect(getJSONAsyncSpy).toBeCalled();
224
+ expect(getJSONAsyncSpy).toHaveBeenCalled();
233
225
  expect(getJSONAsyncSpy.mock.calls[0][0]).toEqual(credentialsFile);
234
226
  });
235
227
 
236
228
  it('should not get json from credentials file if temporariness set', async () => {
237
229
  await original.getCredentials(profile, auth, { temporary : true });
238
230
 
239
- expect(getJSONAsyncSpy).not.toBeCalled();
231
+ expect(getJSONAsyncSpy).not.toHaveBeenCalled();
240
232
  });
241
233
 
242
234
  it('should call createCredentials with consent in fallback if no existing credentials', async () => {
@@ -244,13 +236,13 @@ describe('src/lib/secrets', () => {
244
236
 
245
237
  await original.getCredentials(profile, auth);
246
238
 
247
- expect(secrets.createCredentials).not.toBeCalled();
239
+ expect(secrets.createCredentials).not.toHaveBeenCalled();
248
240
 
249
241
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
250
242
  const result = await fallback();
251
243
 
252
- expect(jsonLib.readJSON).not.toBeCalled();
253
- expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined, 'consent');
244
+ expect(jsonLib.readJSON).not.toHaveBeenCalled();
245
+ expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, undefined, 'consent');
254
246
  expect(result).toEqual(credentialsJSON);
255
247
  });
256
248
 
@@ -259,13 +251,13 @@ describe('src/lib/secrets', () => {
259
251
 
260
252
  await original.getCredentials(profile, auth, { temporary : false });
261
253
 
262
- expect(secrets.createCredentials).not.toBeCalled();
254
+ expect(secrets.createCredentials).not.toHaveBeenCalled();
263
255
 
264
256
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
265
257
  const result = await fallback();
266
258
 
267
- expect(jsonLib.readJSON).not.toBeCalled();
268
- expect(secrets.createCredentials).toBeCalledWith(profile, auth, { temporary : false }, 'consent');
259
+ expect(jsonLib.readJSON).not.toHaveBeenCalled();
260
+ expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, { temporary : false }, 'consent');
269
261
  expect(result).toEqual(credentialsJSON);
270
262
  });
271
263
 
@@ -274,13 +266,13 @@ describe('src/lib/secrets', () => {
274
266
 
275
267
  await original.getCredentials(profile, auth);
276
268
 
277
- expect(secrets.createCredentials).not.toBeCalled();
269
+ expect(secrets.createCredentials).not.toHaveBeenCalled();
278
270
 
279
271
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
280
272
  const result = await fallback();
281
273
 
282
- expect(jsonLib.readJSON).toBeCalledWith(credentialsFile);
283
- expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined, 'consent');
274
+ expect(jsonLib.readJSON).toHaveBeenCalledWith(credentialsFile);
275
+ expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, undefined, 'consent');
284
276
  expect(result).toEqual(credentialsJSON);
285
277
  });
286
278
 
@@ -291,13 +283,13 @@ describe('src/lib/secrets', () => {
291
283
 
292
284
  await original.getCredentials(profile, auth);
293
285
 
294
- expect(secrets.createCredentials).not.toBeCalled();
286
+ expect(secrets.createCredentials).not.toHaveBeenCalled();
295
287
 
296
288
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
297
289
  const result = await fallback();
298
290
 
299
- expect(jsonLib.readJSON).toBeCalledWith(credentialsFile);
300
- expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined, undefined);
291
+ expect(jsonLib.readJSON).toHaveBeenCalledWith(credentialsFile);
292
+ expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, undefined, undefined);
301
293
  // eslint-disable-next-line camelcase
302
294
  expect(result).toEqual({ ... credentialsJSON, refresh_token : 'refresh_token' });
303
295
  });
@@ -314,8 +306,8 @@ describe('src/lib/secrets', () => {
314
306
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
315
307
  const result = await fallback();
316
308
 
317
- expect(jsonLib.readJSON).toBeCalledWith(credentialsFile);
318
- expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined, undefined);
309
+ expect(jsonLib.readJSON).toHaveBeenCalledWith(credentialsFile);
310
+ expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, undefined, undefined);
319
311
  // eslint-disable-next-line camelcase
320
312
  expect(result).toEqual({ ...credentialsJSON, refresh_token : 'refresh_token_exists' });
321
313
  });
@@ -359,7 +351,7 @@ describe('src/lib/secrets', () => {
359
351
 
360
352
  await original.createCredentials(profile, auth);
361
353
 
362
- expect(auth.generateAuthUrl).toBeCalledWith({
354
+ expect(auth.generateAuthUrl).toHaveBeenCalledWith({
363
355
  // eslint-disable-next-line camelcase
364
356
  access_type : 'offline',
365
357
  prompt : undefined,
@@ -375,7 +367,7 @@ describe('src/lib/secrets', () => {
375
367
 
376
368
  await original.createCredentials(profile, auth, { temporary : true }, 'consent');
377
369
 
378
- expect(auth.generateAuthUrl).toBeCalledWith({
370
+ expect(auth.generateAuthUrl).toHaveBeenCalledWith({
379
371
  // eslint-disable-next-line camelcase
380
372
  access_type : 'offline',
381
373
  prompt : 'consent',
@@ -391,7 +383,7 @@ describe('src/lib/secrets', () => {
391
383
 
392
384
  await original.createCredentials(profile, auth, { scopes : [ 'scope1', 'scope2' ] });
393
385
 
394
- expect(auth.generateAuthUrl).toBeCalledWith({
386
+ expect(auth.generateAuthUrl).toHaveBeenCalledWith({
395
387
  // eslint-disable-next-line camelcase
396
388
  access_type : 'offline',
397
389
  prompt : undefined,
@@ -404,8 +396,8 @@ describe('src/lib/secrets', () => {
404
396
 
405
397
  await original.createCredentials(profile, auth);
406
398
 
407
- expect(http.createServer).toBeCalled();
408
- expect(listen).toBeCalledWith(6006);
399
+ expect(http.createServer).toHaveBeenCalled();
400
+ expect(listen).toHaveBeenCalledWith(6006);
409
401
  });
410
402
 
411
403
  it('should open browser page and warn about it', async () => {
@@ -413,8 +405,8 @@ describe('src/lib/secrets', () => {
413
405
 
414
406
  await original.createCredentials(profile, auth);
415
407
 
416
- expect(open).toBeCalledWith('http://localhost:6006/');
417
- expect(logger.warn).toBeCalledWith('Please check your browser for further actions');
408
+ expect(open).toHaveBeenCalledWith('http://localhost:6006/');
409
+ expect(logger.warn).toHaveBeenCalledWith('Please check your browser for further actions');
418
410
  });
419
411
 
420
412
  it('should show nothing on the browser page if request.url is empty', async () => {
@@ -423,7 +415,7 @@ describe('src/lib/secrets', () => {
423
415
 
424
416
  await original.createCredentials(profile, auth);
425
417
 
426
- expect(response.end).toBeCalledWith('');
418
+ expect(response.end).toHaveBeenCalledWith('');
427
419
  });
428
420
 
429
421
  it('should show opening instructions if opened the home page', async () => {
@@ -432,7 +424,7 @@ describe('src/lib/secrets', () => {
432
424
 
433
425
  await original.createCredentials(profile, auth);
434
426
 
435
- expect(response.end).toBeCalledWith(`\
427
+ expect(response.end).toHaveBeenCalledWith(`\
436
428
  <div style="width: 100%;height: 100%;display: flex;align-items: start;justify-content: center">\n\
437
429
  <div style="padding: 0 1em;border: 1px solid black;font-family: Arial, sans-serif;margin: 1em;">\n\
438
430
  <p>Please open <a href="${authUrl}">auth page</a> using <strong>${profile}</strong> google profile</p>\n\
@@ -447,7 +439,7 @@ describe('src/lib/secrets', () => {
447
439
 
448
440
  await original.createCredentials(profile, auth);
449
441
 
450
- expect(response.end).toBeCalledWith('\
442
+ expect(response.end).toHaveBeenCalledWith('\
451
443
  <div style="width: 100%;height: 100%;display: flex;align-items: start;justify-content: center">\n\
452
444
  <div style="padding: 0 1em;border: 1px solid black;font-family: Arial, sans-serif;margin: 1em;">\n\
453
445
  <p>Please close this page and return to application</p>\n\
@@ -460,9 +452,9 @@ describe('src/lib/secrets', () => {
460
452
 
461
453
  await original.createCredentials(profile, auth);
462
454
 
463
- expect(close).toBeCalled();
455
+ expect(close).toHaveBeenCalled();
464
456
 
465
- connections.forEach((connection) => expect(connection.destroy).toBeCalled());
457
+ connections.forEach((connection) => expect(connection.destroy).toHaveBeenCalled());
466
458
  });
467
459
 
468
460
  it('should only resolve when request.url is truthy', async () => {
@@ -476,7 +468,7 @@ describe('src/lib/secrets', () => {
476
468
  const result = await original.createCredentials(profile, auth);
477
469
  const after = new Date().getTime();
478
470
 
479
- expect(close).toBeCalledTimes(1);
471
+ expect(close).toHaveBeenCalledTimes(1);
480
472
  expect(closedTime - before).toBeGreaterThanOrEqual(requestTime - 1);
481
473
  expect(after - before).toBeGreaterThanOrEqual(requestTime - 1);
482
474
  expect(result).toEqual(credentialsJSON);
@@ -493,7 +485,7 @@ describe('src/lib/secrets', () => {
493
485
  const result = await original.createCredentials(profile, auth);
494
486
  const after = new Date().getTime();
495
487
 
496
- expect(close).toBeCalledTimes(1);
488
+ expect(close).toHaveBeenCalledTimes(1);
497
489
  expect(closedTime - before).toBeGreaterThanOrEqual(requestTime - 1);
498
490
  expect(after - before).toBeGreaterThanOrEqual(requestTime - 1);
499
491
  expect(result).toEqual(credentialsJSON);
@@ -520,7 +512,7 @@ describe('src/lib/secrets', () => {
520
512
  wrongSecretsJSON.web.redirect_uris[0] = wrongRedirectURI;
521
513
  const func = () => original.checkSecrets(profile, wrongSecretsJSON, secretsFile);
522
514
 
523
- expect(func).toThrowError('Error in credentials file: redirect URI should be http://localhost:6006/oauthcallback.\nsecretsError');
515
+ expect(func).toThrow('Error in credentials file: redirect URI should be http://localhost:6006/oauthcallback.\nsecretsError');
524
516
  });
525
517
  });
526
518
 
@@ -21,19 +21,19 @@ describe('src/lib/api/calendar', () => {
21
21
  it('should call getAuth', async () => {
22
22
  await getAPI(profile);
23
23
 
24
- expect(auth.getAuth).toBeCalledWith(profile, undefined);
24
+ expect(auth.getAuth).toHaveBeenCalledWith(profile, undefined);
25
25
  });
26
26
 
27
27
  it('should pass temporariness', async () => {
28
28
  await getAPI(profile, { temporary : true });
29
29
 
30
- expect(auth.getAuth).toBeCalledWith(profile, { temporary : true });
30
+ expect(auth.getAuth).toHaveBeenCalledWith(profile, { temporary : true });
31
31
  });
32
32
 
33
33
  it('should get calendar api', async () => {
34
34
  await getAPI(profile);
35
35
 
36
- expect(google.calendar).toBeCalledWith({ version : 'v3', auth : googleAuth });
36
+ expect(google.calendar).toHaveBeenCalledWith({ version : 'v3', auth : googleAuth });
37
37
  });
38
38
 
39
39
  it('should return calendar api', async () => {
@@ -1,4 +1,4 @@
1
- import logger from '../../logger';
1
+ import logger from '@anmiles/logger';
2
2
  import sleep from '../../sleep';
3
3
  import shared from '../shared';
4
4
 
@@ -7,7 +7,7 @@ jest.mock<Partial<typeof shared>>('../shared', () => ({
7
7
  getItems : jest.fn().mockImplementation(async () => items),
8
8
  }));
9
9
 
10
- jest.mock<Partial<typeof logger>>('../../logger', () => ({
10
+ jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
11
11
  log : jest.fn(),
12
12
  }));
13
13
 
@@ -60,30 +60,30 @@ describe('src/lib/api/shared', () => {
60
60
  await original.getItems(api, args);
61
61
 
62
62
  pageTokens.forEach((pageToken) => {
63
- expect(api.list).toBeCalledWith({ ...args, pageToken });
63
+ expect(api.list).toHaveBeenCalledWith({ ...args, pageToken });
64
64
  });
65
65
  });
66
66
 
67
67
  it('should output progress by default', async () => {
68
68
  await original.getItems(api, args);
69
69
 
70
- expect(logger.log).toBeCalledTimes(response.length);
71
- expect(logger.log).toBeCalledWith('Getting items (2 of 4)...');
72
- expect(logger.log).toBeCalledWith('Getting items (2 of many)...');
73
- expect(logger.log).toBeCalledWith('Getting items (4 of 4)...');
70
+ expect(logger.log).toHaveBeenCalledTimes(response.length);
71
+ expect(logger.log).toHaveBeenCalledWith('Getting items (2 of 4)...');
72
+ expect(logger.log).toHaveBeenCalledWith('Getting items (2 of many)...');
73
+ expect(logger.log).toHaveBeenCalledWith('Getting items (4 of 4)...');
74
74
  });
75
75
 
76
76
  it('should not output progress if hidden', async () => {
77
77
  await original.getItems(api, args, { hideProgress : true });
78
78
 
79
- expect(logger.log).not.toBeCalled();
79
+ expect(logger.log).not.toHaveBeenCalled();
80
80
  });
81
81
 
82
82
  it('should sleep after reach request', async () => {
83
83
  await original.getItems(api, args);
84
84
 
85
- expect(sleep.sleep).toBeCalledTimes(response.length);
86
- expect(sleep.sleep).toBeCalledWith(300);
85
+ expect(sleep.sleep).toHaveBeenCalledTimes(response.length);
86
+ expect(sleep.sleep).toHaveBeenCalledWith(300);
87
87
  });
88
88
 
89
89
  it('should return items data', async () => {
@@ -21,19 +21,19 @@ describe('src/lib/api/youtube', () => {
21
21
  it('should call getAuth', async () => {
22
22
  await getAPI(profile);
23
23
 
24
- expect(auth.getAuth).toBeCalledWith(profile, undefined);
24
+ expect(auth.getAuth).toHaveBeenCalledWith(profile, undefined);
25
25
  });
26
26
 
27
27
  it('should pass temporariness', async () => {
28
28
  await getAPI(profile, { temporary : true });
29
29
 
30
- expect(auth.getAuth).toBeCalledWith(profile, { temporary : true });
30
+ expect(auth.getAuth).toHaveBeenCalledWith(profile, { temporary : true });
31
31
  });
32
32
 
33
33
  it('should get youtube api', async () => {
34
34
  await getAPI(profile);
35
35
 
36
- expect(google.youtube).toBeCalledWith({ version : 'v3', auth : googleAuth });
36
+ expect(google.youtube).toHaveBeenCalledWith({ version : 'v3', auth : googleAuth });
37
37
  });
38
38
 
39
39
  it('should return youtube api', async () => {
@@ -1,6 +1,6 @@
1
1
  import type GoogleApis from 'googleapis';
2
+ import { log } from '@anmiles/logger';
2
3
  import type { CommonOptions } from '../../types';
3
- import { log } from '../logger';
4
4
  import { sleep } from '../sleep';
5
5
 
6
6
  export { getItems };
package/src/lib/auth.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { google } from 'googleapis';
2
2
  import type GoogleApis from 'googleapis';
3
+ import { info, warn } from '@anmiles/logger';
3
4
  import type { CommonOptions, AuthOptions } from '../types';
4
- import { info, warn } from './logger';
5
5
  import { getProfiles } from './profiles';
6
6
  import { getCredentials, getSecrets } from './secrets';
7
7
 
@@ -1,5 +1,4 @@
1
1
  import fs from 'fs';
2
- import { error } from './logger';
3
2
  import { ensureFile } from './paths';
4
3
 
5
4
  import jsonLib from './jsonLib';
@@ -53,5 +52,6 @@ function checkJSON<T>(filename: string, json: T): void {
53
52
  if (json) {
54
53
  return;
55
54
  }
56
- error(`File ${filename} doesn't exist and should be created with initial data, but function createCallback returned nothing`);
55
+
56
+ throw `File ${filename} doesn't exist and should be created with initial data, but function createCallback returned nothing`;
57
57
  }
@@ -1,5 +1,4 @@
1
1
  import { getJSON, writeJSON } from './jsonLib';
2
- import { error } from './logger';
3
2
  import { getProfilesFile } from './paths';
4
3
 
5
4
  import profiles from './profiles';
@@ -19,7 +18,7 @@ function setProfiles(profiles: string[]): void {
19
18
 
20
19
  function createProfile(profile: string): void {
21
20
  if (!profile) {
22
- error('Usage: `npm run create <profile>` where `profile` - is any profile name you want');
21
+ throw 'Usage: `npm run create <profile>` where `profile` - is any profile name you want';
23
22
  }
24
23
 
25
24
  const existingProfiles = profiles.getProfiles();
@@ -2,9 +2,9 @@ import http from 'http';
2
2
  import enableDestroy from 'server-destroy';
3
3
  import open from 'open';
4
4
  import type GoogleApis from 'googleapis';
5
+ import { warn } from '@anmiles/logger';
5
6
  import type { Secrets, AuthOptions } from '../types';
6
7
  import { getJSON, getJSONAsync, readJSON } from './jsonLib';
7
- import { warn, error } from './logger';
8
8
  import { getScopesFile, getSecretsFile, getCredentialsFile, ensureFile } from './paths';
9
9
 
10
10
  import secrets from './secrets';
@@ -19,13 +19,17 @@ const tokenExpiration = 7 * 24 * 60 * 60 * 1000;
19
19
 
20
20
  function getScopes(): string[] {
21
21
  const scopesFile = getScopesFile();
22
- const scopes = getJSON<string[]>(scopesFile, () => error(secrets.getScopesError(scopesFile)) as never);
22
+ const scopes = getJSON<string[]>(scopesFile, () => {
23
+ throw secrets.getScopesError(scopesFile);
24
+ });
23
25
  return scopes;
24
26
  }
25
27
 
26
28
  function getSecrets(profile: string): Secrets {
27
29
  const secretsFile = getSecretsFile(profile);
28
- const secretsObject = getJSON<Secrets>(secretsFile, () => error(secrets.getSecretsError(profile, secretsFile)) as never);
30
+ const secretsObject = getJSON<Secrets>(secretsFile, () => {
31
+ throw secrets.getSecretsError(profile, secretsFile);
32
+ });
29
33
  secrets.checkSecrets(profile, secretsObject, secretsFile);
30
34
  return secretsObject;
31
35
  }
@@ -116,7 +120,7 @@ function checkSecrets(profile: string, secretsObject: Secrets, secretsFile: stri
116
120
  if (secretsObject.web.redirect_uris[0] === callbackURI) {
117
121
  return true;
118
122
  }
119
- error(`Error in credentials file: redirect URI should be ${callbackURI}.\n${secrets.getSecretsError(profile, secretsFile)}`);
123
+ throw `Error in credentials file: redirect URI should be ${callbackURI}.\n${secrets.getSecretsError(profile, secretsFile)}`;
120
124
  }
121
125
 
122
126
  function getScopesError(scopesFile: string) {
@@ -1,12 +0,0 @@
1
- export { log, info, warn, error };
2
- declare const _default: {
3
- log: typeof log;
4
- info: typeof info;
5
- warn: typeof warn;
6
- error: typeof error;
7
- };
8
- export default _default;
9
- declare function log(message: string): void;
10
- declare function info(message: string): void;
11
- declare function warn(message: string): void;
12
- declare function error(message: string): never;
@@ -1,46 +0,0 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.error = exports.warn = exports.info = exports.log = void 0;
27
- const colorette = __importStar(require("colorette"));
28
- exports.default = { log, info, warn, error };
29
- function log(message) {
30
- console.log(message);
31
- }
32
- exports.log = log;
33
- function info(message) {
34
- console.log(colorette.greenBright(message));
35
- }
36
- exports.info = info;
37
- function warn(message) {
38
- console.warn(colorette.yellowBright(message));
39
- }
40
- exports.warn = warn;
41
- function error(message) {
42
- console.error(`${colorette.redBright(message)}\n`);
43
- process.exit(1);
44
- }
45
- exports.error = error;
46
- //# sourceMappingURL=logger.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/lib/logger.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAuC;AAGvC,kBAAe,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAE1C,SAAS,GAAG,CAAC,OAAe;IAC3B,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACtB,CAAC;AALQ,kBAAG;AAOZ,SAAS,IAAI,CAAC,OAAe;IAC5B,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC;AAC7C,CAAC;AATa,oBAAI;AAWlB,SAAS,IAAI,CAAC,OAAe;IAC5B,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;AAC/C,CAAC;AAbmB,oBAAI;AAexB,SAAS,KAAK,CAAC,OAAe;IAC7B,OAAO,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACjB,CAAC;AAlByB,sBAAK"}
@@ -1,57 +0,0 @@
1
- import * as colorette from 'colorette';
2
-
3
- import logger from '../logger';
4
-
5
- const text = 'text';
6
-
7
- const originalConsole = global.console;
8
- global.console.log = jest.fn();
9
- global.console.info = jest.fn();
10
- global.console.warn = jest.fn();
11
- global.console.error = jest.fn();
12
-
13
- const exit = jest.spyOn(process, 'exit').mockImplementation();
14
-
15
- afterAll(() => {
16
- global.console = originalConsole;
17
- });
18
-
19
- describe('src/lib/logger', () => {
20
- describe('log', () => {
21
- it('should call console.log with original text', () => {
22
- logger.log(text);
23
-
24
- expect(global.console.log).toBeCalledWith(text);
25
- });
26
- });
27
-
28
- describe('info', () => {
29
- it('should call console.info with bright green text', () => {
30
- logger.info(text);
31
-
32
- expect(global.console.log).toBeCalledWith(colorette.greenBright(text));
33
- });
34
- });
35
-
36
- describe('warn', () => {
37
- it('should call console.warn with bright yellow text', () => {
38
- logger.warn(text);
39
-
40
- expect(global.console.warn).toBeCalledWith(colorette.yellowBright(text));
41
- });
42
- });
43
-
44
- describe('error', () => {
45
- it('should call console.error with bright red text and newline', () => {
46
- logger.error(text);
47
-
48
- expect(global.console.error).toBeCalledWith(`${colorette.redBright(text)}\n`);
49
- });
50
-
51
- it('should exit the process with code 1', () => {
52
- logger.error(text);
53
-
54
- expect(exit).toBeCalledWith(1);
55
- });
56
- });
57
- });
package/src/lib/logger.ts DELETED
@@ -1,21 +0,0 @@
1
- import * as colorette from 'colorette';
2
-
3
- export { log, info, warn, error };
4
- export default { log, info, warn, error };
5
-
6
- function log(message: string): void {
7
- console.log(message);
8
- }
9
-
10
- function info(message: string): void {
11
- console.log(colorette.greenBright(message));
12
- }
13
-
14
- function warn(message: string): void {
15
- console.warn(colorette.yellowBright(message));
16
- }
17
-
18
- function error(message: string): never {
19
- console.error(`${colorette.redBright(message)}\n`);
20
- process.exit(1);
21
- }