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