@anmiles/google-api-wrapper 7.0.7 → 8.0.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.
@@ -1,11 +1,12 @@
1
+ import fs from 'fs';
1
2
  import http from 'http';
2
3
  import path from 'path';
3
4
  import open from 'open';
4
5
  import type GoogleApis from 'googleapis';
5
6
  import logger from '@anmiles/logger';
6
- import jsonLib from '../jsonLib';
7
7
  import paths from '../paths';
8
8
  import type { Secrets } from '../../types';
9
+ import '@anmiles/prototypes';
9
10
 
10
11
  import secrets from '../secrets';
11
12
  const original = jest.requireActual('../secrets').default as typeof secrets;
@@ -33,6 +34,10 @@ jest.mock<Partial<typeof http>>('http', () => ({
33
34
  }),
34
35
  }));
35
36
 
37
+ jest.mock<Partial<typeof fs>>('fs', () => ({
38
+ existsSync : jest.fn().mockImplementation(() => exists),
39
+ }));
40
+
36
41
  jest.mock<Partial<typeof path>>('path', () => ({
37
42
  join : jest.fn().mockImplementation((...args) => args.join('/')),
38
43
  }));
@@ -41,12 +46,6 @@ jest.mock('open', () => jest.fn().mockImplementation((url: string) => {
41
46
  willOpen(url.replace('http://localhost:6006', ''));
42
47
  }));
43
48
 
44
- jest.mock<Partial<typeof jsonLib>>('../jsonLib', () => ({
45
- getJSON : jest.fn().mockImplementation(() => json),
46
- getJSONAsync : jest.fn().mockImplementation(async () => json),
47
- readJSON : jest.fn().mockImplementation(async () => json),
48
- }));
49
-
50
49
  jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
51
50
  warn : jest.fn(),
52
51
  }));
@@ -55,7 +54,6 @@ jest.mock<Partial<typeof paths>>('../paths', () => ({
55
54
  getScopesFile : jest.fn().mockImplementation(() => scopesFile),
56
55
  getSecretsFile : jest.fn().mockImplementation(() => secretsFile),
57
56
  getCredentialsFile : jest.fn().mockImplementation(() => credentialsFile),
58
- ensureFile : jest.fn().mockImplementation(() => fileExists),
59
57
  }));
60
58
 
61
59
  const profile = 'username1';
@@ -140,75 +138,91 @@ const connections = [
140
138
  { remoteAddress : 'server', remotePort : '1003', on : jest.fn(), destroy : jest.fn() },
141
139
  ];
142
140
 
143
- let fileExists: boolean;
141
+ let exists: boolean;
142
+
143
+ let getJSONSpy: jest.SpyInstance;
144
+ let getJSONAsyncSpy: jest.SpyInstance;
145
+ let readJSONSpy: jest.SpyInstance;
146
+
147
+ beforeAll(() => {
148
+ getJSONSpy = jest.spyOn(fs, 'getJSON');
149
+ getJSONAsyncSpy = jest.spyOn(fs, 'getJSONAsync');
150
+ readJSONSpy = jest.spyOn(fs, 'readJSON');
151
+ });
152
+
153
+ beforeEach(() => {
154
+ getJSONSpy.mockImplementation(() => json);
155
+ getJSONAsyncSpy.mockImplementation(() => json);
156
+ readJSONSpy.mockImplementation(() => json);
157
+ });
158
+
159
+ afterAll(() => {
160
+ getJSONSpy.mockRestore();
161
+ getJSONAsyncSpy.mockRestore();
162
+ readJSONSpy.mockRestore();
163
+ });
144
164
 
145
165
  describe('src/lib/secrets', () => {
146
166
  describe('getScopes', () => {
147
- const getJSONSpy = jest.spyOn(jsonLib, 'getJSON');
148
-
149
167
  beforeEach(() => {
150
168
  json = scopesJSON;
151
169
  });
152
170
 
153
- it('should get json from scopes file', async () => {
154
- await original.getScopes();
171
+ it('should get json from scopes file', () => {
172
+ original.getScopes();
155
173
 
156
174
  expect(getJSONSpy).toHaveBeenCalled();
157
175
  expect(getJSONSpy.mock.calls[0][0]).toEqual(scopesFile);
158
176
  });
159
177
 
160
- it('should fallback to error', async () => {
161
- await original.getScopes();
178
+ it('should fallback to error', () => {
179
+ original.getScopes();
162
180
 
163
181
  expect(getJSONSpy.mock.calls[0][1]).toThrow(scopesError);
164
182
  });
165
183
 
166
- it('should return scopes', async () => {
167
- const result = await original.getScopes();
184
+ it('should return scopes', () => {
185
+ const result = original.getScopes();
168
186
 
169
187
  expect(result).toEqual(scopesJSON);
170
188
  });
171
189
  });
172
190
 
173
191
  describe('getSecrets', () => {
174
- const getJSONSpy = jest.spyOn(jsonLib, 'getJSON');
175
-
176
192
  beforeEach(() => {
177
193
  json = secretsJSON;
178
194
  });
179
195
 
180
- it('should get json from secrets file', async () => {
181
- await original.getSecrets(profile);
196
+ it('should get json from secrets file', () => {
197
+ original.getSecrets(profile);
182
198
 
183
199
  expect(getJSONSpy).toHaveBeenCalled();
184
200
  expect(getJSONSpy.mock.calls[0][0]).toEqual(secretsFile);
185
201
  });
186
202
 
187
- it('should fallback to error', async () => {
188
- await original.getSecrets(profile);
203
+ it('should fallback to error', () => {
204
+ original.getSecrets(profile);
189
205
 
190
206
  expect(getJSONSpy.mock.calls[0][1]).toThrow(secretsError);
191
207
  });
192
208
 
193
- it('should check secrets', async () => {
194
- await original.getSecrets(profile);
209
+ it('should check secrets', () => {
210
+ original.getSecrets(profile);
195
211
 
196
212
  expect(secrets.checkSecrets).toHaveBeenCalledWith(profile, json, secretsFile);
197
213
  });
198
214
 
199
- it('should return secrets', async () => {
200
- const result = await original.getSecrets(profile);
215
+ it('should return secrets', () => {
216
+ const result = original.getSecrets(profile);
201
217
 
202
218
  expect(result).toEqual(secretsJSON);
203
219
  });
204
220
  });
205
221
 
206
222
  describe('getCredentials', () => {
207
- const getJSONAsyncSpy = jest.spyOn(jsonLib, 'getJSONAsync');
208
-
209
223
  beforeEach(() => {
210
- json = credentialsJSON;
211
- fileExists = false;
224
+ json = credentialsJSON;
225
+ exists = false;
212
226
  });
213
227
 
214
228
  it('should get json from credentials file by default', async () => {
@@ -232,7 +246,7 @@ describe('src/lib/secrets', () => {
232
246
  });
233
247
 
234
248
  it('should call createCredentials with consent in fallback if no existing credentials', async () => {
235
- fileExists = false;
249
+ exists = false;
236
250
 
237
251
  await original.getCredentials(profile, auth);
238
252
 
@@ -241,13 +255,13 @@ describe('src/lib/secrets', () => {
241
255
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
242
256
  const result = await fallback();
243
257
 
244
- expect(jsonLib.readJSON).not.toHaveBeenCalled();
258
+ expect(readJSONSpy).not.toHaveBeenCalled();
245
259
  expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, undefined, 'consent');
246
260
  expect(result).toEqual(credentialsJSON);
247
261
  });
248
262
 
249
263
  it('should call createCredentials with consent in fallback if no existing credentials and pass temporariness', async () => {
250
- fileExists = false;
264
+ exists = false;
251
265
 
252
266
  await original.getCredentials(profile, auth, { temporary : false });
253
267
 
@@ -256,13 +270,13 @@ describe('src/lib/secrets', () => {
256
270
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
257
271
  const result = await fallback();
258
272
 
259
- expect(jsonLib.readJSON).not.toHaveBeenCalled();
273
+ expect(readJSONSpy).not.toHaveBeenCalled();
260
274
  expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, { temporary : false }, 'consent');
261
275
  expect(result).toEqual(credentialsJSON);
262
276
  });
263
277
 
264
278
  it('should call createCredentials with consent in fallback if existing credentials do not have refresh token', async () => {
265
- fileExists = true;
279
+ exists = true;
266
280
 
267
281
  await original.getCredentials(profile, auth);
268
282
 
@@ -271,15 +285,15 @@ describe('src/lib/secrets', () => {
271
285
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
272
286
  const result = await fallback();
273
287
 
274
- expect(jsonLib.readJSON).toHaveBeenCalledWith(credentialsFile);
288
+ expect(readJSONSpy).toHaveBeenCalledWith(credentialsFile);
275
289
  expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, undefined, 'consent');
276
290
  expect(result).toEqual(credentialsJSON);
277
291
  });
278
292
 
279
293
  it('should call createCredentials without consent in fallback and replace refresh_token if existing credentials have refresh token', async () => {
280
- fileExists = true;
294
+ exists = true;
281
295
  // eslint-disable-next-line camelcase
282
- jest.spyOn(jsonLib, 'readJSON').mockReturnValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token' });
296
+ readJSONSpy.mockReturnValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token' });
283
297
 
284
298
  await original.getCredentials(profile, auth);
285
299
 
@@ -288,16 +302,16 @@ describe('src/lib/secrets', () => {
288
302
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
289
303
  const result = await fallback();
290
304
 
291
- expect(jsonLib.readJSON).toHaveBeenCalledWith(credentialsFile);
305
+ expect(readJSONSpy).toHaveBeenCalledWith(credentialsFile);
292
306
  expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, undefined, undefined);
293
307
  // eslint-disable-next-line camelcase
294
308
  expect(result).toEqual({ ... credentialsJSON, refresh_token : 'refresh_token' });
295
309
  });
296
310
 
297
311
  it('should call createCredentials without consent in fallback and leave refresh token if existing credentials have refresh token', async () => {
298
- fileExists = true;
312
+ exists = true;
299
313
  // eslint-disable-next-line camelcase
300
- jest.spyOn(jsonLib, 'readJSON').mockReturnValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token' });
314
+ readJSONSpy.mockReturnValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token' });
301
315
  // eslint-disable-next-line camelcase
302
316
  jest.spyOn(secrets, 'createCredentials').mockResolvedValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token_exists' });
303
317
 
@@ -306,7 +320,7 @@ describe('src/lib/secrets', () => {
306
320
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
307
321
  const result = await fallback();
308
322
 
309
- expect(jsonLib.readJSON).toHaveBeenCalledWith(credentialsFile);
323
+ expect(readJSONSpy).toHaveBeenCalledWith(credentialsFile);
310
324
  expect(secrets.createCredentials).toHaveBeenCalledWith(profile, auth, undefined, undefined);
311
325
  // eslint-disable-next-line camelcase
312
326
  expect(result).toEqual({ ...credentialsJSON, refresh_token : 'refresh_token_exists' });
@@ -315,31 +329,31 @@ describe('src/lib/secrets', () => {
315
329
 
316
330
  describe('validateCredentials', () => {
317
331
  it('should return false if no access token', async () => {
318
- expect(await original.validateCredentials({})).toEqual(false);
332
+ expect(await original.validateCredentials({})).toEqual({ isValid : false, validationError : 'Credentials does not have access_token' });
319
333
  });
320
334
 
321
- it('should return true if no refresh token', async () => {
335
+ it('should return false if no refresh token', async () => {
322
336
  // eslint-disable-next-line camelcase
323
- expect(await original.validateCredentials({ access_token : 'token' })).toEqual(false);
337
+ expect(await original.validateCredentials({ access_token : 'token' })).toEqual({ isValid : false, validationError : 'Credentials does not have refresh_token' });
324
338
  });
325
339
 
326
- it('should return true if no expiration', async () => {
340
+ it('should return false if no expiration date', async () => {
327
341
  // eslint-disable-next-line camelcase
328
- expect(await original.validateCredentials({ access_token : 'token', refresh_token : 'token' })).toEqual(true);
342
+ expect(await original.validateCredentials({ access_token : 'token', refresh_token : 'token' })).toEqual({ isValid : false, validationError : 'Credentials does not have expiry_date' });
329
343
  });
330
344
 
331
345
  it('should return true if credentials are not more than 1 week ago', async () => {
332
346
  const expiryDate = new Date();
333
347
  expiryDate.setDate(expiryDate.getDate() - 6);
334
348
  // eslint-disable-next-line camelcase
335
- expect(await original.validateCredentials({ access_token : 'token', refresh_token : 'token', expiry_date : expiryDate.getTime() })).toEqual(true);
349
+ expect(await original.validateCredentials({ access_token : 'token', refresh_token : 'token', expiry_date : expiryDate.getTime() })).toEqual({ isValid : true });
336
350
  });
337
351
 
338
- it('should return true if credentials are more than 1 week ago', async () => {
352
+ it('should return false if credentials are more than 1 week ago', async () => {
339
353
  const expiryDate = new Date();
340
354
  expiryDate.setDate(expiryDate.getDate() - 8);
341
355
  // eslint-disable-next-line camelcase
342
- expect(await original.validateCredentials({ access_token : 'token', refresh_token : 'token', expiry_date : expiryDate.getTime() })).toEqual(false);
356
+ expect(await original.validateCredentials({ access_token : 'token', refresh_token : 'token', expiry_date : expiryDate.getTime() })).toEqual({ isValid : false, validationError : 'Credentials expired' });
343
357
  });
344
358
  });
345
359
 
@@ -1,5 +1,5 @@
1
1
  import logger from '@anmiles/logger';
2
- import sleep from '../../sleep';
2
+ import sleep from '@anmiles/sleep';
3
3
  import shared from '../shared';
4
4
 
5
5
  const original = jest.requireActual('../shared').default as typeof shared;
@@ -11,9 +11,7 @@ jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
11
11
  log : jest.fn(),
12
12
  }));
13
13
 
14
- jest.mock<Partial<typeof sleep>>('../../sleep', () => ({
15
- sleep : jest.fn(),
16
- }));
14
+ jest.mock<Partial<typeof sleep>>('@anmiles/sleep', () => jest.fn());
17
15
 
18
16
  const items: Array<{ data: string}> = [
19
17
  { data : 'first' },
@@ -82,8 +80,8 @@ describe('src/lib/api/shared', () => {
82
80
  it('should sleep after reach request', async () => {
83
81
  await original.getItems(api, args);
84
82
 
85
- expect(sleep.sleep).toHaveBeenCalledTimes(response.length);
86
- expect(sleep.sleep).toHaveBeenCalledWith(300);
83
+ expect(sleep).toHaveBeenCalledTimes(response.length);
84
+ expect(sleep).toHaveBeenCalledWith(300);
87
85
  });
88
86
 
89
87
  it('should return items data', async () => {
@@ -1,7 +1,7 @@
1
1
  import type GoogleApis from 'googleapis';
2
2
  import { log } from '@anmiles/logger';
3
+ import sleep from '@anmiles/sleep';
3
4
  import type { CommonOptions } from '../../types';
4
- import { sleep } from '../sleep';
5
5
 
6
6
  export { getItems };
7
7
  export default { getItems };
package/src/lib/paths.ts CHANGED
@@ -1,31 +1,13 @@
1
- import fs from 'fs';
2
1
  import path from 'path';
3
- import paths from './paths';
4
2
 
5
- export { ensureDir, ensureFile, getProfilesFile, getScopesFile, getSecretsFile, getCredentialsFile };
6
- export default { ensureDir, ensureFile, getProfilesFile, getScopesFile, getSecretsFile, getCredentialsFile };
3
+ export { getProfilesFile, getScopesFile, getSecretsFile, getCredentialsFile };
4
+ export default { getProfilesFile, getScopesFile, getSecretsFile, getCredentialsFile };
7
5
 
8
6
  const dirPaths = {
9
7
  input : 'input',
10
8
  secrets : 'secrets',
11
9
  };
12
10
 
13
- function ensureDir(dirPath: string) {
14
- if (!fs.existsSync(dirPath)) {
15
- fs.mkdirSync(dirPath, { recursive : true });
16
- }
17
- return dirPath;
18
- }
19
-
20
- function ensureFile(filePath: string) {
21
- paths.ensureDir(path.dirname(filePath));
22
-
23
- if (!fs.existsSync(filePath)) {
24
- fs.writeFileSync(filePath, '');
25
- }
26
- return filePath;
27
- }
28
-
29
11
  function getProfilesFile() {
30
12
  return path.join(dirPaths.input, 'profiles.json');
31
13
  }
@@ -1,4 +1,5 @@
1
- import { getJSON, writeJSON } from './jsonLib';
1
+ import fs from 'fs';
2
+ import '@anmiles/prototypes';
2
3
  import { getProfilesFile } from './paths';
3
4
 
4
5
  import profiles from './profiles';
@@ -8,12 +9,12 @@ export default { getProfiles, setProfiles, createProfile };
8
9
 
9
10
  function getProfiles(): string[] {
10
11
  const profilesFile = getProfilesFile();
11
- return getJSON(profilesFile, () => []);
12
+ return fs.getJSON(profilesFile, () => []);
12
13
  }
13
14
 
14
15
  function setProfiles(profiles: string[]): void {
15
16
  const profilesFile = getProfilesFile();
16
- writeJSON(profilesFile, profiles);
17
+ fs.writeJSON(profilesFile, profiles);
17
18
  }
18
19
 
19
20
  function createProfile(profile: string): void {
@@ -1,11 +1,12 @@
1
+ import fs from 'fs';
1
2
  import http from 'http';
2
3
  import enableDestroy from 'server-destroy';
3
4
  import open from 'open';
4
5
  import type GoogleApis from 'googleapis';
5
6
  import { warn } from '@anmiles/logger';
6
7
  import type { Secrets, AuthOptions } from '../types';
7
- import { getJSON, getJSONAsync, readJSON } from './jsonLib';
8
- import { getScopesFile, getSecretsFile, getCredentialsFile, ensureFile } from './paths';
8
+ import '@anmiles/prototypes';
9
+ import { getScopesFile, getSecretsFile, getCredentialsFile } from './paths';
9
10
 
10
11
  import secrets from './secrets';
11
12
 
@@ -19,7 +20,7 @@ const tokenExpiration = 7 * 24 * 60 * 60 * 1000;
19
20
 
20
21
  function getScopes(): string[] {
21
22
  const scopesFile = getScopesFile();
22
- const scopes = getJSON<string[]>(scopesFile, () => {
23
+ const scopes = fs.getJSON<string[]>(scopesFile, () => {
23
24
  throw secrets.getScopesError(scopesFile);
24
25
  });
25
26
  return scopes;
@@ -27,7 +28,7 @@ function getScopes(): string[] {
27
28
 
28
29
  function getSecrets(profile: string): Secrets {
29
30
  const secretsFile = getSecretsFile(profile);
30
- const secretsObject = getJSON<Secrets>(secretsFile, () => {
31
+ const secretsObject = fs.getJSON<Secrets>(secretsFile, () => {
31
32
  throw secrets.getSecretsError(profile, secretsFile);
32
33
  });
33
34
  secrets.checkSecrets(profile, secretsObject, secretsFile);
@@ -41,30 +42,32 @@ async function getCredentials(profile: string, auth: GoogleApis.Common.OAuth2Cli
41
42
  return secrets.createCredentials(profile, auth, options);
42
43
  }
43
44
 
44
- return getJSONAsync(credentialsFile, async () => {
45
+ return fs.getJSONAsync(credentialsFile, async () => {
46
+ const refreshToken = fs.existsSync(credentialsFile) ? fs.readJSON<GoogleApis.Auth.Credentials>(credentialsFile).refresh_token : undefined;
47
+ const credentials = await secrets.createCredentials(profile, auth, options, refreshToken ? undefined : 'consent');
45
48
  // eslint-disable-next-line camelcase
46
- const refresh_token = ensureFile(credentialsFile) ? readJSON<GoogleApis.Auth.Credentials>(credentialsFile).refresh_token : undefined;
47
- // eslint-disable-next-line camelcase
48
- const credentials = await secrets.createCredentials(profile, auth, options, refresh_token ? undefined : 'consent');
49
- // eslint-disable-next-line camelcase
50
- return { refresh_token, ...credentials };
49
+ return { refresh_token : refreshToken, ...credentials };
51
50
  }, secrets.validateCredentials);
52
51
  }
53
52
 
54
- async function validateCredentials(credentials: GoogleApis.Auth.Credentials): Promise<boolean> {
53
+ async function validateCredentials(credentials: GoogleApis.Auth.Credentials): Promise<{ isValid: boolean, validationError?: string}> {
55
54
  if (!credentials.access_token) {
56
- return false;
55
+ return { isValid : false, validationError : 'Credentials does not have access_token' };
57
56
  }
58
57
 
59
58
  if (!credentials.refresh_token) {
60
- return false;
59
+ return { isValid : false, validationError : 'Credentials does not have refresh_token' };
61
60
  }
62
61
 
63
62
  if (!credentials.expiry_date) {
64
- return true;
63
+ return { isValid : false, validationError : 'Credentials does not have expiry_date' };
64
+ }
65
+
66
+ if (new Date().getTime() - credentials.expiry_date >= tokenExpiration) {
67
+ return { isValid : false, validationError : 'Credentials expired' };
65
68
  }
66
69
 
67
- return new Date().getTime() - credentials.expiry_date < tokenExpiration;
70
+ return { isValid : true };
68
71
  }
69
72
 
70
73
  async function createCredentials(profile: string, auth: GoogleApis.Auth.OAuth2Client, options?: AuthOptions, prompt?: GoogleApis.Auth.GenerateAuthUrlOpts['prompt']): Promise<GoogleApis.Auth.Credentials> {
@@ -1,14 +0,0 @@
1
- export { getJSON, getJSONAsync, writeJSON, readJSON };
2
- declare const _default: {
3
- getJSON: typeof getJSON;
4
- getJSONAsync: typeof getJSONAsync;
5
- writeJSON: typeof writeJSON;
6
- readJSON: typeof readJSON;
7
- checkJSON: typeof checkJSON;
8
- };
9
- export default _default;
10
- declare function getJSON<T>(filename: string, createCallback: () => Exclude<T, Promise<any>>, validateJSON?: (json: T) => boolean): T;
11
- declare function getJSONAsync<T>(filename: string, createCallbackAsync: () => Promise<T>, validateJSONAsync?: (json: T) => Promise<boolean>): Promise<T>;
12
- declare function writeJSON<T>(filename: string, json: T): void;
13
- declare function readJSON<T>(filename: string): T;
14
- declare function checkJSON<T>(filename: string, json: T): void;
@@ -1,55 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.readJSON = exports.writeJSON = exports.getJSONAsync = exports.getJSON = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const paths_1 = require("./paths");
9
- const jsonLib_1 = __importDefault(require("./jsonLib"));
10
- exports.default = { getJSON, getJSONAsync, writeJSON, readJSON, checkJSON };
11
- function getJSON(filename, createCallback, validateJSON) {
12
- if (fs_1.default.existsSync(filename)) {
13
- const json = jsonLib_1.default.readJSON(filename);
14
- if (!validateJSON || validateJSON(json)) {
15
- return json;
16
- }
17
- }
18
- const json = createCallback();
19
- jsonLib_1.default.checkJSON(filename, json);
20
- (0, paths_1.ensureFile)(filename);
21
- jsonLib_1.default.writeJSON(filename, json);
22
- return json;
23
- }
24
- exports.getJSON = getJSON;
25
- async function getJSONAsync(filename, createCallbackAsync, validateJSONAsync) {
26
- if (fs_1.default.existsSync(filename)) {
27
- const json = jsonLib_1.default.readJSON(filename);
28
- if (!validateJSONAsync || await validateJSONAsync(json)) {
29
- return json;
30
- }
31
- }
32
- const json = await createCallbackAsync();
33
- jsonLib_1.default.checkJSON(filename, json);
34
- (0, paths_1.ensureFile)(filename);
35
- jsonLib_1.default.writeJSON(filename, json);
36
- return json;
37
- }
38
- exports.getJSONAsync = getJSONAsync;
39
- function writeJSON(filename, json) {
40
- const jsonString = JSON.stringify(json, null, ' ');
41
- fs_1.default.writeFileSync(filename, jsonString);
42
- }
43
- exports.writeJSON = writeJSON;
44
- function readJSON(filename) {
45
- const jsonString = fs_1.default.readFileSync(filename).toString();
46
- return JSON.parse(jsonString);
47
- }
48
- exports.readJSON = readJSON;
49
- function checkJSON(filename, json) {
50
- if (json) {
51
- return;
52
- }
53
- throw `File ${filename} doesn't exist and should be created with initial data, but function createCallback returned nothing`;
54
- }
55
- //# sourceMappingURL=jsonLib.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"jsonLib.js","sourceRoot":"","sources":["../../src/lib/jsonLib.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,mCAAqC;AAErC,wDAAgC;AAGhC,kBAAe,EAAE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;AAEzE,SAAS,OAAO,CAAI,QAAgB,EAAE,cAA8C,EAAE,YAAmC;IACxH,IAAI,YAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;QAC5B,MAAM,IAAI,GAAG,iBAAO,CAAC,QAAQ,CAAI,QAAQ,CAAC,CAAC;QAE3C,IAAI,CAAC,YAAY,IAAI,YAAY,CAAC,IAAI,CAAC,EAAE;YACxC,OAAO,IAAI,CAAC;SACZ;KACD;IAED,MAAM,IAAI,GAAG,cAAc,EAAE,CAAC;IAC9B,iBAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAClC,IAAA,kBAAU,EAAC,QAAQ,CAAC,CAAC;IACrB,iBAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAClC,OAAO,IAAI,CAAC;AACb,CAAC;AAjBQ,0BAAO;AAmBhB,KAAK,UAAU,YAAY,CAAI,QAAgB,EAAE,mBAAqC,EAAE,iBAAiD;IACxI,IAAI,YAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;QAC5B,MAAM,IAAI,GAAG,iBAAO,CAAC,QAAQ,CAAI,QAAQ,CAAC,CAAC;QAE3C,IAAI,CAAC,iBAAiB,IAAI,MAAM,iBAAiB,CAAC,IAAI,CAAC,EAAE;YACxD,OAAO,IAAI,CAAC;SACZ;KACD;IAED,MAAM,IAAI,GAAG,MAAM,mBAAmB,EAAE,CAAC;IACzC,iBAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAClC,IAAA,kBAAU,EAAC,QAAQ,CAAC,CAAC;IACrB,iBAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAClC,OAAO,IAAI,CAAC;AACb,CAAC;AAjCiB,oCAAY;AAmC9B,SAAS,SAAS,CAAI,QAAgB,EAAE,IAAO;IAC9C,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IACtD,YAAE,CAAC,aAAa,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;AACxC,CAAC;AAtC+B,8BAAS;AAwCzC,SAAS,QAAQ,CAAI,QAAgB;IACpC,MAAM,UAAU,GAAG,YAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC;IACxD,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,CAAM,CAAC;AACpC,CAAC;AA3C0C,4BAAQ;AA6CnD,SAAS,SAAS,CAAI,QAAgB,EAAE,IAAO;IAC9C,IAAI,IAAI,EAAE;QACT,OAAO;KACP;IAED,MAAM,QAAQ,QAAQ,sGAAsG,CAAC;AAC9H,CAAC"}
@@ -1,6 +0,0 @@
1
- export { sleep };
2
- declare const _default: {
3
- sleep: typeof sleep;
4
- };
5
- export default _default;
6
- declare function sleep(milliSeconds: number): Promise<unknown>;
package/dist/lib/sleep.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sleep = void 0;
4
- exports.default = { sleep };
5
- async function sleep(milliSeconds) {
6
- return new Promise((resolve) => {
7
- setTimeout(resolve, milliSeconds);
8
- });
9
- }
10
- exports.sleep = sleep;
11
- //# sourceMappingURL=sleep.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sleep.js","sourceRoot":"","sources":["../../src/lib/sleep.ts"],"names":[],"mappings":";;;AACA,kBAAe,EAAE,KAAK,EAAE,CAAC;AAEzB,KAAK,UAAU,KAAK,CAAC,YAAoB;IACxC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC9B,UAAU,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;IACnC,CAAC,CAAC,CAAC;AACJ,CAAC;AAPQ,sBAAK"}