1
- import async from 'async ' ;
1
+ import pLimit from 'p-limit ' ;
2
2
import _ from 'lodash' ;
3
3
import * as eventsHandler from './events-handler' ;
4
4
import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp' ;
@@ -11,25 +11,21 @@ import {
11
11
import { StorageAdapter } from 'oc-storage-adapters-utils' ;
12
12
13
13
export default function componentsDetails ( conf : Config , cdn : StorageAdapter ) {
14
- const returnError = (
15
- code : string ,
16
- message : string | Error ,
17
- callback : ( code : string ) => void
18
- ) => {
19
- eventsHandler . fire ( 'error' , { code, message } ) ;
20
- return callback ( code ) ;
14
+ const returnError = ( code : string , message : any ) => {
15
+ eventsHandler . fire ( 'error' , { code, message : message ?. message ?? message } ) ;
16
+ throw code ;
21
17
} ;
22
18
23
19
const filePath = ( ) : string =>
24
20
`${ conf . storage . options . componentsDir } /components-details.json` ;
25
21
26
- const getFromJson = ( callback : Callback < ComponentsDetails , string > ) =>
27
- cdn . getJson < ComponentsDetails > ( filePath ( ) , true , callback ) ;
22
+ const getFromJson = ( ) : Promise < ComponentsDetails > =>
23
+ cdn . getJson ( filePath ( ) , true ) ;
28
24
29
- const getFromDirectories = (
30
- options : { componentsList : ComponentsList ; details : ComponentsDetails } ,
31
- callback : Callback < ComponentsDetails , Error | undefined >
32
- ) => {
25
+ const getFromDirectories = async ( options : {
26
+ componentsList : ComponentsList ;
27
+ details ?: ComponentsDetails ;
28
+ } ) : Promise < ComponentsDetails > => {
33
29
const details = Object . assign ( { } , _ . cloneDeep ( options . details ) ) ;
34
30
details . components = details . components || { } ;
35
31
@@ -43,70 +39,51 @@ export default function componentsDetails(conf: Config, cdn: StorageAdapter) {
43
39
} ) ;
44
40
} ) ;
45
41
46
- async . eachLimit (
47
- missing ,
48
- cdn . maxConcurrentRequests ,
49
- ( { name, version } , next ) => {
50
- cdn . getJson < Component > (
51
- `${ conf . storage . options . componentsDir } /${ name } /${ version } /package.json` ,
52
- true ,
53
- ( err , content ) => {
54
- if ( err ) {
55
- return next ( err as any ) ;
56
- }
57
- details . components [ name ] [ version ] = {
58
- publishDate : content . oc . date || 0
59
- } ;
60
- next ( ) ;
61
- }
62
- ) ;
63
- } ,
64
- err =>
65
- callback ( err , {
66
- lastEdit : getUnixUTCTimestamp ( ) ,
67
- components : details . components
42
+ const limit = pLimit ( cdn . maxConcurrentRequests ) ;
43
+
44
+ await Promise . all (
45
+ missing . map ( ( { name, version } ) =>
46
+ limit ( async ( ) => {
47
+ const content : Component = await cdn . getJson (
48
+ `${ conf . storage . options . componentsDir } /${ name } /${ version } /package.json` ,
49
+ true
50
+ ) ;
51
+ details . components [ name ] [ version ] = {
52
+ publishDate : content . oc . date || 0
53
+ } ;
68
54
} )
55
+ )
69
56
) ;
57
+
58
+ return {
59
+ lastEdit : getUnixUTCTimestamp ( ) ,
60
+ components : details . components
61
+ } ;
70
62
} ;
71
63
72
- const save = ( data : ComponentsDetails , callback : Callback < unknown , string > ) =>
73
- cdn . putFileContent ( JSON . stringify ( data ) , filePath ( ) , true , callback ) ;
64
+ const save = ( data : ComponentsDetails ) : Promise < unknown > =>
65
+ cdn . putFileContent ( JSON . stringify ( data ) , filePath ( ) , true ) ;
74
66
75
- const refresh = (
76
- componentsList : ComponentsList ,
77
- callback : Callback < ComponentsDetails >
78
- ) => {
79
- getFromJson ( ( jsonErr , details : ComponentsDetails ) => {
80
- getFromDirectories (
81
- { componentsList, details } ,
82
- ( dirErr , dirDetails : ComponentsDetails ) => {
83
- if ( dirErr ) {
84
- return returnError (
85
- 'components_details_get' ,
86
- dirErr ,
87
- callback as any
88
- ) ;
89
- } else if (
90
- jsonErr ||
91
- ! _ . isEqual ( dirDetails . components , details . components )
92
- ) {
93
- save ( dirDetails , saveErr => {
94
- if ( saveErr ) {
95
- return returnError (
96
- 'components_details_save' ,
97
- saveErr ,
98
- callback as any
99
- ) ;
100
- }
67
+ const refresh = async (
68
+ componentsList : ComponentsList
69
+ ) : Promise < ComponentsDetails > => {
70
+ const jsonDetails = await getFromJson ( ) . catch ( ( ) => undefined ) ;
71
+ const dirDetails = await getFromDirectories ( {
72
+ componentsList,
73
+ details : jsonDetails
74
+ } ) . catch ( err => returnError ( 'components_details_get' , err ) ) ;
101
75
102
- callback ( null , dirDetails ) ;
103
- } ) ;
104
- } else {
105
- callback ( null , details ) ;
106
- }
107
- }
76
+ if (
77
+ ! jsonDetails ||
78
+ ! _ . isEqual ( dirDetails . components , jsonDetails . components )
79
+ ) {
80
+ await save ( dirDetails ) . catch ( err =>
81
+ returnError ( 'components_details_save' , err )
108
82
) ;
109
- } ) ;
83
+ return dirDetails ;
84
+ }
85
+
86
+ return jsonDetails ;
110
87
} ;
111
88
112
89
return {
0 commit comments