oc 0.49.7 → 0.49.8

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 (163) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +2 -15
  3. package/dist/cli/domain/clean.d.ts +2 -2
  4. package/dist/cli/domain/clean.js +10 -13
  5. package/dist/cli/domain/get-components-by-dir.d.ts +1 -1
  6. package/dist/cli/domain/get-components-by-dir.js +8 -15
  7. package/dist/cli/domain/get-mocked-plugins.js +8 -6
  8. package/dist/cli/domain/handle-dependencies/ensure-compiler-is-declared-as-devDependency.d.ts +1 -1
  9. package/dist/cli/domain/handle-dependencies/ensure-compiler-is-declared-as-devDependency.js +4 -5
  10. package/dist/cli/domain/handle-dependencies/get-compiler.d.ts +1 -1
  11. package/dist/cli/domain/handle-dependencies/get-compiler.js +3 -3
  12. package/dist/cli/domain/handle-dependencies/index.d.ts +4 -3
  13. package/dist/cli/domain/handle-dependencies/index.js +40 -34
  14. package/dist/cli/domain/handle-dependencies/install-compiler.d.ts +1 -1
  15. package/dist/cli/domain/handle-dependencies/install-compiler.js +14 -7
  16. package/dist/cli/domain/handle-dependencies/install-missing-dependencies.d.ts +1 -1
  17. package/dist/cli/domain/handle-dependencies/install-missing-dependencies.js +13 -9
  18. package/dist/cli/domain/handle-dependencies/link-missing-dependencies.d.ts +1 -1
  19. package/dist/cli/domain/handle-dependencies/link-missing-dependencies.js +4 -8
  20. package/dist/cli/domain/init-template/index.d.ts +2 -2
  21. package/dist/cli/domain/init-template/index.js +6 -8
  22. package/dist/cli/domain/init-template/install-template.d.ts +2 -2
  23. package/dist/cli/domain/init-template/install-template.js +10 -9
  24. package/dist/cli/domain/init-template/scaffold.d.ts +2 -2
  25. package/dist/cli/domain/init-template/scaffold.js +7 -7
  26. package/dist/cli/domain/local.js +11 -10
  27. package/dist/cli/domain/mock.d.ts +1 -1
  28. package/dist/cli/domain/mock.js +21 -21
  29. package/dist/cli/domain/package-components.d.ts +1 -1
  30. package/dist/cli/domain/package-components.js +14 -17
  31. package/dist/cli/domain/registry.js +68 -81
  32. package/dist/cli/domain/watch.js +1 -1
  33. package/dist/cli/facade/clean.d.ts +10 -4
  34. package/dist/cli/facade/clean.js +38 -28
  35. package/dist/cli/facade/dev.d.ts +108 -15
  36. package/dist/cli/facade/dev.js +85 -99
  37. package/dist/cli/facade/init.d.ts +12 -5
  38. package/dist/cli/facade/init.js +22 -20
  39. package/dist/cli/facade/mock.d.ts +12 -5
  40. package/dist/cli/facade/mock.js +5 -6
  41. package/dist/cli/facade/package.d.ts +12 -5
  42. package/dist/cli/facade/package.js +30 -32
  43. package/dist/cli/facade/preview.d.ts +8 -3
  44. package/dist/cli/facade/preview.js +12 -10
  45. package/dist/cli/facade/publish.d.ts +16 -7
  46. package/dist/cli/facade/publish.js +94 -114
  47. package/dist/cli/facade/registry-add.d.ts +8 -3
  48. package/dist/cli/facade/registry-add.js +10 -9
  49. package/dist/cli/facade/registry-ls.d.ts +4 -1
  50. package/dist/cli/facade/registry-ls.js +19 -17
  51. package/dist/cli/facade/registry-remove.d.ts +8 -3
  52. package/dist/cli/facade/registry-remove.js +10 -9
  53. package/dist/cli/facade/registry.d.ts +4 -1
  54. package/dist/cli/facade/registry.js +6 -3
  55. package/dist/cli/index.js +3 -5
  56. package/dist/cli/programmatic-api.d.ts +1 -1
  57. package/dist/components/oc-client/_package/package.json +1 -1
  58. package/dist/components/oc-client/package.json +1 -1
  59. package/dist/registry/app-start.d.ts +2 -2
  60. package/dist/registry/app-start.js +20 -20
  61. package/dist/registry/domain/components-cache/components-list.d.ts +7 -6
  62. package/dist/registry/domain/components-cache/components-list.js +31 -45
  63. package/dist/registry/domain/components-cache/index.d.ts +6 -5
  64. package/dist/registry/domain/components-cache/index.js +47 -50
  65. package/dist/registry/domain/components-details.d.ts +5 -4
  66. package/dist/registry/domain/components-details.js +27 -38
  67. package/dist/registry/domain/extract-package.d.ts +2 -2
  68. package/dist/registry/domain/extract-package.js +9 -12
  69. package/dist/registry/domain/get-package-json-from-temp-dir.d.ts +1 -1
  70. package/dist/registry/domain/get-package-json-from-temp-dir.js +2 -2
  71. package/dist/registry/domain/nested-renderer.d.ts +7 -6
  72. package/dist/registry/domain/nested-renderer.js +36 -72
  73. package/dist/registry/domain/options-sanitiser.js +5 -3
  74. package/dist/registry/domain/plugins-initialiser.d.ts +3 -1
  75. package/dist/registry/domain/plugins-initialiser.js +29 -33
  76. package/dist/registry/domain/repository.js +78 -100
  77. package/dist/registry/domain/require-wrapper.js +1 -2
  78. package/dist/registry/domain/storage-adapter.d.ts +10 -0
  79. package/dist/registry/domain/storage-adapter.js +55 -0
  80. package/dist/registry/index.d.ts +2 -2
  81. package/dist/registry/index.js +15 -28
  82. package/dist/registry/middleware/index.d.ts +6 -2
  83. package/dist/registry/middleware/index.js +5 -1
  84. package/dist/registry/router.d.ts +2 -2
  85. package/dist/registry/router.js +0 -2
  86. package/dist/registry/routes/component-info.js +3 -2
  87. package/dist/registry/routes/component-preview.js +2 -1
  88. package/dist/registry/routes/components.js +1 -1
  89. package/dist/registry/routes/helpers/get-component-fallback.d.ts +2 -7
  90. package/dist/registry/routes/helpers/get-component.d.ts +5 -5
  91. package/dist/registry/routes/helpers/get-component.js +6 -5
  92. package/dist/registry/routes/helpers/is-url-discoverable.d.ts +2 -2
  93. package/dist/registry/routes/helpers/is-url-discoverable.js +15 -9
  94. package/dist/registry/routes/index.js +4 -3
  95. package/dist/registry/routes/publish.d.ts +1 -1
  96. package/dist/registry/routes/publish.js +32 -31
  97. package/dist/resources/settings.js +1 -1
  98. package/dist/types.d.ts +35 -43
  99. package/dist/utils/npm-utils.d.ts +6 -8
  100. package/dist/utils/npm-utils.js +20 -9
  101. package/dist/utils/put.d.ts +2 -1
  102. package/dist/utils/put.js +10 -29
  103. package/logintervals.md +1 -1
  104. package/package.json +6 -1
  105. package/src/cli/domain/clean.ts +10 -20
  106. package/src/cli/domain/get-components-by-dir.ts +12 -26
  107. package/src/cli/domain/get-mocked-plugins.ts +32 -26
  108. package/src/cli/domain/handle-dependencies/ensure-compiler-is-declared-as-devDependency.ts +8 -12
  109. package/src/cli/domain/handle-dependencies/get-compiler.ts +8 -11
  110. package/src/cli/domain/handle-dependencies/index.ts +55 -109
  111. package/src/cli/domain/handle-dependencies/install-compiler.ts +22 -15
  112. package/src/cli/domain/handle-dependencies/install-missing-dependencies.ts +19 -13
  113. package/src/cli/domain/handle-dependencies/link-missing-dependencies.ts +8 -16
  114. package/src/cli/domain/init-template/index.ts +13 -20
  115. package/src/cli/domain/init-template/install-template.ts +11 -12
  116. package/src/cli/domain/init-template/scaffold.ts +9 -12
  117. package/src/cli/domain/local.ts +19 -22
  118. package/src/cli/domain/mock.ts +32 -36
  119. package/src/cli/domain/package-components.ts +14 -25
  120. package/src/cli/domain/registry.ts +80 -116
  121. package/src/cli/domain/watch.ts +1 -1
  122. package/src/cli/facade/clean.ts +37 -37
  123. package/src/cli/facade/dev.ts +159 -182
  124. package/src/cli/facade/init.ts +36 -38
  125. package/src/cli/facade/mock.ts +11 -10
  126. package/src/cli/facade/package.ts +44 -49
  127. package/src/cli/facade/preview.ts +20 -18
  128. package/src/cli/facade/publish.ts +134 -172
  129. package/src/cli/facade/registry-add.ts +16 -15
  130. package/src/cli/facade/registry-ls.ts +27 -22
  131. package/src/cli/facade/registry-remove.ts +16 -15
  132. package/src/cli/facade/registry.ts +7 -5
  133. package/src/cli/index.ts +3 -5
  134. package/src/components/oc-client/_package/package.json +1 -1
  135. package/src/components/oc-client/package.json +1 -1
  136. package/src/globals.d.ts +38 -0
  137. package/src/registry/app-start.ts +27 -31
  138. package/src/registry/domain/components-cache/components-list.ts +45 -67
  139. package/src/registry/domain/components-cache/index.ts +55 -60
  140. package/src/registry/domain/components-details.ts +49 -75
  141. package/src/registry/domain/extract-package.ts +19 -27
  142. package/src/registry/domain/get-package-json-from-temp-dir.ts +3 -4
  143. package/src/registry/domain/nested-renderer.ts +64 -122
  144. package/src/registry/domain/options-sanitiser.ts +6 -3
  145. package/src/registry/domain/plugins-initialiser.ts +45 -49
  146. package/src/registry/domain/repository.ts +129 -235
  147. package/src/registry/domain/require-wrapper.ts +1 -2
  148. package/src/registry/domain/storage-adapter.ts +85 -0
  149. package/src/registry/index.ts +48 -79
  150. package/src/registry/middleware/index.ts +10 -2
  151. package/src/registry/router.ts +2 -10
  152. package/src/registry/routes/component-info.ts +4 -3
  153. package/src/registry/routes/component-preview.ts +3 -2
  154. package/src/registry/routes/components.ts +1 -1
  155. package/src/registry/routes/helpers/get-component-fallback.ts +3 -9
  156. package/src/registry/routes/helpers/get-component.ts +11 -9
  157. package/src/registry/routes/helpers/is-url-discoverable.ts +14 -15
  158. package/src/registry/routes/index.ts +18 -13
  159. package/src/registry/routes/publish.ts +31 -34
  160. package/src/resources/settings.ts +1 -1
  161. package/src/types.ts +63 -136
  162. package/src/utils/npm-utils.ts +38 -37
  163. package/src/utils/put.ts +15 -37
@@ -1,8 +1,7 @@
1
1
  import colors from 'colors/safe';
2
2
  import path from 'path';
3
- import _ from 'lodash';
4
3
  import fs from 'fs-extra';
5
- import { ComponentsDetails, Config, Repository } from '../types';
4
+ import { Config, Repository } from '../types';
6
5
 
7
6
  const packageInfo = fs.readJsonSync(
8
7
  path.join(
@@ -15,16 +14,16 @@ const packageInfo = fs.readJsonSync(
15
14
  )
16
15
  );
17
16
 
18
- export default function appStart(
17
+ export default async function appStart(
19
18
  repository: Repository,
20
- options: Config,
21
- callback: (err: any | null, data: ComponentsDetails | string) => void
22
- ): void {
19
+ options: Config
20
+ ): Promise<void> {
23
21
  if (options.local) {
24
- return callback(null, 'ok');
22
+ return;
25
23
  }
26
24
 
27
- const logger = options.verbosity ? console : { log: _.noop };
25
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
26
+ const logger = options.verbosity ? console : { log: () => {} };
28
27
 
29
28
  logger.log(
30
29
  colors.yellow(
@@ -32,10 +31,8 @@ export default function appStart(
32
31
  )
33
32
  );
34
33
 
35
- repository.getComponentVersions('oc-client', (err, componentInfo) => {
36
- if (err) {
37
- return logger.log(colors.red(err));
38
- }
34
+ try {
35
+ const componentInfo = await repository.getComponentVersions('oc-client');
39
36
 
40
37
  logger.log(
41
38
  colors.yellow(
@@ -43,7 +40,7 @@ export default function appStart(
43
40
  )
44
41
  );
45
42
 
46
- if (!_.includes(componentInfo, packageInfo.version)) {
43
+ if (!componentInfo.includes(packageInfo.version)) {
47
44
  logger.log(colors.yellow('Component not found. Publishing it...'));
48
45
 
49
46
  const pkgInfo = {
@@ -54,25 +51,24 @@ export default function appStart(
54
51
  packageJson: packageInfo
55
52
  };
56
53
 
57
- repository.publishComponent(
58
- pkgInfo,
59
- 'oc-client',
60
- packageInfo.version,
61
- (err, res) => {
62
- if (!err) {
63
- logger.log(colors.green('Component published.'));
64
- } else {
65
- logger.log(
66
- colors.red(`Component not published: ${(err as any).message}`)
67
- );
68
- }
69
-
70
- callback(err, res);
71
- }
72
- );
54
+ try {
55
+ await repository.publishComponent(
56
+ pkgInfo,
57
+ 'oc-client',
58
+ packageInfo.version
59
+ );
60
+ logger.log(colors.green('Component published.'));
61
+ } catch (err) {
62
+ logger.log(
63
+ colors.red(`Component not published: ${(err as any).message}`)
64
+ );
65
+ throw err;
66
+ }
73
67
  } else {
74
68
  logger.log(colors.green('Component is available on library.'));
75
- callback(null, 'ok');
76
69
  }
77
- });
70
+ } catch (err) {
71
+ logger.log(colors.red(String(err)));
72
+ throw err;
73
+ }
78
74
  }
@@ -1,91 +1,69 @@
1
- import async from 'async';
2
1
  import semver from 'semver';
2
+ import pLimit from 'p-limit';
3
3
  import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
4
- import { Cdn, ComponentsList, Config } from '../../../types';
4
+ import { ComponentsList, Config } from '../../../types';
5
+ import { StorageAdapter } from 'oc-storage-adapters-utils';
5
6
 
6
- export default function componentsList(conf: Config, cdn: Cdn) {
7
+ export default function componentsList(conf: Config, cdn: StorageAdapter) {
7
8
  const filePath = (): string =>
8
9
  `${conf.storage.options.componentsDir}/components.json`;
9
10
 
10
11
  const componentsList = {
11
- getFromJson: (callback: (err: string | null, data: any) => void) =>
12
- cdn.getJson(filePath(), true, callback),
12
+ getFromJson: (): Promise<ComponentsList> => cdn.getJson(filePath(), true),
13
13
 
14
- getFromDirectories: (
15
- callback: (err: string | null, data: ComponentsList) => void
16
- ) => {
14
+ getFromDirectories: async (): Promise<ComponentsList> => {
17
15
  const componentsInfo: Record<string, string[]> = {};
18
16
 
19
- const getVersionsForComponent = (
20
- componentName: string,
21
- cb: (err: Error | null, data: string[]) => void
22
- ) => {
23
- cdn.listSubDirectories(
24
- `${conf.storage.options.componentsDir}/${componentName}`,
25
- (err, versions) => {
26
- if (err) {
27
- return cb(err, undefined as any);
28
- }
29
- cb(null, versions.sort(semver.compare));
30
- }
17
+ const getVersionsForComponent = async (
18
+ componentName: string
19
+ ): Promise<string[]> => {
20
+ const versions = await cdn.listSubDirectories(
21
+ `${conf.storage.options.componentsDir}/${componentName}`
31
22
  );
32
- };
33
23
 
34
- cdn.listSubDirectories(
35
- conf.storage.options.componentsDir,
36
- (err, components) => {
37
- if (err) {
38
- if (err.code === 'dir_not_found') {
39
- return callback(null, {
40
- lastEdit: getUnixUTCTimestamp(),
41
- components: [] as any
42
- });
43
- }
24
+ return versions.sort(semver.compare);
25
+ };
44
26
 
45
- return callback(err as any, undefined as any);
46
- }
27
+ try {
28
+ const components = await cdn.listSubDirectories(
29
+ conf.storage.options.componentsDir
30
+ );
31
+ const limit = pLimit(cdn.maxConcurrentRequests);
47
32
 
48
- async.mapLimit(
49
- components,
50
- cdn.maxConcurrentRequests,
51
- getVersionsForComponent,
52
- (errors, versions) => {
53
- if (errors) {
54
- return callback(errors as any, undefined as any);
55
- }
33
+ const versions = await Promise.all(
34
+ components.map(component =>
35
+ limit(() => getVersionsForComponent(component))
36
+ )
37
+ );
56
38
 
57
- components.forEach((component, i) => {
58
- componentsInfo[component] = (versions as any)[i];
59
- });
39
+ components.forEach((component, i) => {
40
+ componentsInfo[component] = versions[i];
41
+ });
60
42
 
61
- callback(null, {
62
- lastEdit: getUnixUTCTimestamp(),
63
- components: componentsInfo
64
- });
65
- }
66
- );
43
+ return {
44
+ lastEdit: getUnixUTCTimestamp(),
45
+ components: componentsInfo
46
+ };
47
+ } catch (err: any) {
48
+ if (err.code === 'dir_not_found') {
49
+ throw {
50
+ lastEdit: getUnixUTCTimestamp(),
51
+ components: [] as any
52
+ };
67
53
  }
68
- );
54
+ throw err;
55
+ }
69
56
  },
70
57
 
71
- refresh(callback: (err: string | null, data: ComponentsList) => void) {
72
- componentsList.getFromDirectories((err, components) => {
73
- if (err) {
74
- return callback(err, undefined as any);
75
- }
76
- componentsList.save(components, err => {
77
- if (err) {
78
- return callback(err, undefined as any);
79
- }
80
- callback(err, components);
81
- });
82
- });
58
+ async refresh(): Promise<ComponentsList> {
59
+ const components = await componentsList.getFromDirectories();
60
+ await componentsList.save(components);
61
+
62
+ return components;
83
63
  },
84
64
 
85
- save: (
86
- data: ComponentsList,
87
- callback: (err: string | null, data: unknown) => void
88
- ) => cdn.putFileContent(JSON.stringify(data), filePath(), true, callback)
65
+ save: (data: ComponentsList): Promise<unknown> =>
66
+ cdn.putFileContent(JSON.stringify(data), filePath(), true)
89
67
  };
90
68
 
91
69
  return componentsList;
@@ -2,94 +2,89 @@ import _ from 'lodash';
2
2
  import getComponentsList from './components-list';
3
3
  import eventsHandler from '../events-handler';
4
4
  import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
5
- import { Cdn, ComponentsList, Config } from '../../../types';
5
+ import { ComponentsList, Config } from '../../../types';
6
+ import { StorageAdapter } from 'oc-storage-adapters-utils';
6
7
 
7
- export default function componentsCache(conf: Config, cdn: Cdn) {
8
+ export default function componentsCache(conf: Config, cdn: StorageAdapter) {
8
9
  let cachedComponentsList: ComponentsList;
9
10
  let refreshLoop: NodeJS.Timeout;
10
11
 
11
12
  const componentsList = getComponentsList(conf, cdn);
12
13
 
13
14
  const poll = () =>
14
- setTimeout(() => {
15
- componentsList.getFromJson((err, data) => {
16
- if (err) {
17
- eventsHandler.fire('error', {
18
- code: 'components_list_get',
19
- message: err
20
- });
21
- } else {
22
- eventsHandler.fire('cache-poll', getUnixUTCTimestamp());
15
+ setTimeout(async () => {
16
+ try {
17
+ const data = await componentsList.getFromJson();
23
18
 
24
- if (data.lastEdit > cachedComponentsList.lastEdit) {
25
- cachedComponentsList = data;
26
- }
19
+ eventsHandler.fire('cache-poll', getUnixUTCTimestamp());
20
+
21
+ if (data.lastEdit > cachedComponentsList.lastEdit) {
22
+ cachedComponentsList = data;
27
23
  }
28
- refreshLoop = poll();
29
- });
24
+ } catch (err: any) {
25
+ eventsHandler.fire('error', {
26
+ code: 'components_list_get',
27
+ message: err?.message || String(err)
28
+ });
29
+ }
30
+ refreshLoop = poll();
30
31
  }, conf.pollingInterval * 1000);
31
32
 
32
- const cacheDataAndStartPolling = (
33
- data: ComponentsList,
34
- callback: (err: null, data: ComponentsList) => void
35
- ) => {
33
+ const cacheDataAndStartPolling = (data: ComponentsList) => {
36
34
  cachedComponentsList = data;
37
35
  refreshLoop = poll();
38
- callback(null, data);
36
+
37
+ return data;
39
38
  };
40
39
 
41
- const returnError = (
42
- code: string,
43
- message: string,
44
- callback: (err: any | null, data: any) => void
45
- ) => {
46
- eventsHandler.fire('error', { code, message });
47
- return callback(code, undefined as any);
40
+ const throwError = (code: string, message: any) => {
41
+ eventsHandler.fire('error', { code, message: message?.message ?? message });
42
+ throw code;
48
43
  };
49
44
 
50
45
  return {
51
- get(callback: (err: Error | null, data: ComponentsList) => void) {
46
+ get(): ComponentsList {
52
47
  if (!cachedComponentsList) {
53
- return returnError(
48
+ return throwError(
54
49
  'components_cache_empty',
55
- `The component's cache was empty`,
56
- callback
50
+ `The component's cache was empty`
57
51
  );
58
52
  }
59
53
 
60
- callback(null, cachedComponentsList);
54
+ return cachedComponentsList;
61
55
  },
62
56
 
63
- load(callback: (err: Error | null, data: ComponentsList) => void) {
64
- componentsList.getFromJson((jsonErr, jsonComponents) => {
65
- componentsList.getFromDirectories((dirErr, dirComponents) => {
66
- if (dirErr) {
67
- return returnError('components_list_get', dirErr, callback);
68
- } else if (
69
- jsonErr ||
70
- !_.isEqual(dirComponents.components, jsonComponents.components)
71
- ) {
72
- componentsList.save(dirComponents, saveErr => {
73
- if (saveErr) {
74
- return returnError('components_list_save', saveErr, callback);
75
- }
76
- cacheDataAndStartPolling(dirComponents, callback);
77
- });
78
- } else {
79
- cacheDataAndStartPolling(jsonComponents, callback);
80
- }
81
- });
82
- });
57
+ async load(): Promise<ComponentsList> {
58
+ const dirComponents = await componentsList
59
+ .getFromDirectories()
60
+ .catch(err => throwError('components_list_get', err));
61
+ const jsonComponents = await componentsList
62
+ .getFromJson()
63
+ .catch(() => null);
64
+
65
+ if (
66
+ !jsonComponents ||
67
+ !_.isEqual(dirComponents.components, jsonComponents.components)
68
+ ) {
69
+ await componentsList
70
+ .save(dirComponents)
71
+ .catch(err => throwError('components_list_save', err));
72
+ }
73
+ cacheDataAndStartPolling(dirComponents);
74
+
75
+ return dirComponents;
83
76
  },
84
- refresh(callback: (err: Error | null, data: ComponentsList) => void) {
77
+
78
+ async refresh(): Promise<ComponentsList> {
85
79
  clearTimeout(refreshLoop);
86
- componentsList.refresh((err, components) => {
87
- if (err) {
88
- return returnError('components_cache_refresh', err, callback);
89
- }
80
+ try {
81
+ const components = await componentsList.refresh();
82
+ cacheDataAndStartPolling(components);
90
83
 
91
- cacheDataAndStartPolling(components, callback);
92
- });
84
+ return components;
85
+ } catch (err) {
86
+ return throwError('components_cache_refresh', err);
87
+ }
93
88
  }
94
89
  };
95
90
  }
@@ -1,39 +1,34 @@
1
- import async from 'async';
1
+ import pLimit from 'p-limit';
2
2
  import _ from 'lodash';
3
3
  import eventsHandler from './events-handler';
4
4
  import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
5
5
  import {
6
- Cdn,
7
6
  Component,
8
7
  ComponentsDetails,
9
8
  ComponentsList,
10
9
  Config
11
10
  } from '../../types';
11
+ import { StorageAdapter } from 'oc-storage-adapters-utils';
12
12
 
13
- export default function componentsDetails(conf: Config, cdn: Cdn) {
14
- const returnError = (
15
- code: string,
16
- message: string | Error,
17
- callback: (code: string) => void
18
- ) => {
13
+ export default function componentsDetails(conf: Config, cdn: StorageAdapter) {
14
+ const returnError = (code: string, message: string | Error) => {
19
15
  eventsHandler.fire('error', {
20
16
  code,
21
17
  message: (message as Error)?.message ?? message
22
18
  });
23
- return callback(code);
19
+ throw code;
24
20
  };
25
21
 
26
22
  const filePath = (): string =>
27
23
  `${conf.storage.options.componentsDir}/components-details.json`;
28
24
 
29
- const getFromJson = (
30
- callback: (err: string | null, data: ComponentsDetails) => void
31
- ) => cdn.getJson<ComponentsDetails>(filePath(), true, callback);
25
+ const getFromJson = (): Promise<ComponentsDetails> =>
26
+ cdn.getJson(filePath(), true);
32
27
 
33
- const getFromDirectories = (
34
- options: { componentsList: ComponentsList; details: ComponentsDetails },
35
- callback: (err: Error | undefined | null, data: ComponentsDetails) => void
36
- ) => {
28
+ const getFromDirectories = async (options: {
29
+ componentsList: ComponentsList;
30
+ details?: ComponentsDetails;
31
+ }): Promise<ComponentsDetails> => {
37
32
  const details = Object.assign({}, _.cloneDeep(options.details));
38
33
  details.components = details.components || {};
39
34
 
@@ -47,72 +42,51 @@ export default function componentsDetails(conf: Config, cdn: Cdn) {
47
42
  });
48
43
  });
49
44
 
50
- async.eachLimit(
51
- missing,
52
- cdn.maxConcurrentRequests,
53
- ({ name, version }, next) => {
54
- cdn.getJson<Component>(
55
- `${conf.storage.options.componentsDir}/${name}/${version}/package.json`,
56
- true,
57
- (err, content) => {
58
- if (err) {
59
- return next(err as any);
60
- }
61
- details.components[name][version] = {
62
- publishDate: content.oc.date || 0
63
- };
64
- next();
65
- }
66
- );
67
- },
68
- err =>
69
- callback(err, {
70
- lastEdit: getUnixUTCTimestamp(),
71
- components: details.components
45
+ const limit = pLimit(cdn.maxConcurrentRequests);
46
+
47
+ await Promise.all(
48
+ missing.map(({ name, version }) =>
49
+ limit(async () => {
50
+ const content: Component = await cdn.getJson(
51
+ `${conf.storage.options.componentsDir}/${name}/${version}/package.json`,
52
+ true
53
+ );
54
+ details.components[name][version] = {
55
+ publishDate: content.oc.date || 0
56
+ };
72
57
  })
58
+ )
73
59
  );
60
+
61
+ return {
62
+ lastEdit: getUnixUTCTimestamp(),
63
+ components: details.components
64
+ };
74
65
  };
75
66
 
76
- const save = (
77
- data: ComponentsDetails,
78
- callback: (err: string | null, data: unknown) => void
79
- ) => cdn.putFileContent(JSON.stringify(data), filePath(), true, callback);
67
+ const save = (data: ComponentsDetails): Promise<unknown> =>
68
+ cdn.putFileContent(JSON.stringify(data), filePath(), true);
80
69
 
81
- const refresh = (
82
- componentsList: ComponentsList,
83
- callback: (err: Error | null, data: ComponentsDetails) => void
84
- ) => {
85
- getFromJson((jsonErr, details: ComponentsDetails) => {
86
- getFromDirectories(
87
- { componentsList, details },
88
- (dirErr, dirDetails: ComponentsDetails) => {
89
- if (dirErr) {
90
- return returnError(
91
- 'components_details_get',
92
- dirErr,
93
- callback as any
94
- );
95
- } else if (
96
- jsonErr ||
97
- !_.isEqual(dirDetails.components, details.components)
98
- ) {
99
- save(dirDetails, saveErr => {
100
- if (saveErr) {
101
- return returnError(
102
- 'components_details_save',
103
- saveErr,
104
- callback as any
105
- );
106
- }
70
+ const refresh = async (
71
+ componentsList: ComponentsList
72
+ ): Promise<ComponentsDetails> => {
73
+ const jsonDetails = await getFromJson().catch(() => undefined);
74
+ const dirDetails = await getFromDirectories({
75
+ componentsList,
76
+ details: jsonDetails
77
+ }).catch(err => returnError('components_details_get', err));
107
78
 
108
- callback(null, dirDetails);
109
- });
110
- } else {
111
- callback(null, details);
112
- }
113
- }
79
+ if (
80
+ !jsonDetails ||
81
+ !_.isEqual(dirDetails.components, jsonDetails.components)
82
+ ) {
83
+ await save(dirDetails).catch(err =>
84
+ returnError('components_details_save', err)
114
85
  );
115
- });
86
+ return dirDetails;
87
+ }
88
+
89
+ return jsonDetails;
116
90
  };
117
91
 
118
92
  return {
@@ -1,23 +1,20 @@
1
1
  import path from 'path';
2
2
  import targz from 'targz';
3
+ import { promisify } from 'util';
3
4
  import { PackageJson } from 'type-fest';
4
5
 
5
6
  import getPackageJsonFromTempDir from './get-package-json-from-temp-dir';
6
7
 
7
- export default function extractPackage(
8
+ export default async function extractPackage(
8
9
  files:
9
10
  | Express.Multer.File[]
10
11
  | {
11
12
  [fieldname: string]: Express.Multer.File[];
12
- },
13
- callback: (
14
- err: Error | null,
15
- data: {
16
- outputFolder: string;
17
- packageJson: PackageJson;
18
- }
19
- ) => void
20
- ): void {
13
+ }
14
+ ): Promise<{
15
+ outputFolder: string;
16
+ packageJson: PackageJson;
17
+ }> {
21
18
  const packageFile: Express.Multer.File = (files as any)[0];
22
19
  const packagePath = path.resolve(packageFile.path);
23
20
  const packageUntarOutput = path.resolve(
@@ -27,22 +24,17 @@ export default function extractPackage(
27
24
  );
28
25
  const packageOutput = path.resolve(packageUntarOutput, '_package');
29
26
 
30
- targz.decompress(
31
- {
32
- src: packagePath,
33
- dest: packageUntarOutput
34
- },
35
- err => {
36
- if (err) {
37
- return (callback as any)(err);
38
- }
27
+ const decompress = promisify(targz.decompress);
39
28
 
40
- getPackageJsonFromTempDir(packageOutput, (err, packageJson) => {
41
- callback(err, {
42
- outputFolder: packageOutput,
43
- packageJson: packageJson
44
- });
45
- });
46
- }
47
- );
29
+ await decompress({
30
+ src: packagePath,
31
+ dest: packageUntarOutput
32
+ });
33
+
34
+ const packageJson = await getPackageJsonFromTempDir(packageOutput);
35
+
36
+ return {
37
+ outputFolder: packageOutput,
38
+ packageJson: packageJson
39
+ };
48
40
  }
@@ -3,8 +3,7 @@ import path from 'path';
3
3
  import { PackageJson } from 'type-fest';
4
4
 
5
5
  export default function getPackageJsonFromTempDir(
6
- tempDirPath: string,
7
- callback: (err: Error | null, data: PackageJson) => void
8
- ): void {
9
- return fs.readJson(path.join(tempDirPath, 'package.json'), callback);
6
+ tempDirPath: string
7
+ ): Promise<PackageJson> {
8
+ return fs.readJson(path.join(tempDirPath, 'package.json'));
10
9
  }