1
- import async from 'async' ;
2
1
import colors from 'colors/safe' ;
3
2
import express from 'express' ;
4
3
import http from 'http' ;
@@ -12,17 +11,15 @@ import Repository from './domain/repository';
12
11
import { create as createRouter } from './router' ;
13
12
import sanitiseOptions from './domain/options-sanitiser' ;
14
13
import * as validator from './domain/validators' ;
15
- import { ComponentsList , Config , Plugin } from '../types' ;
16
- import { fromPromise } from 'universalify' ;
14
+ import { Config , Plugin } from '../types' ;
17
15
18
16
interface Input extends Partial < Omit < Config , 'beforePublish' > > {
19
17
baseUrl : string ;
20
18
}
21
19
22
20
export default function registry ( inputOptions : Input ) {
23
- const validationResult = validator . validateRegistryConfiguration (
24
- inputOptions
25
- ) ;
21
+ const validationResult =
22
+ validator . validateRegistryConfiguration ( inputOptions ) ;
26
23
if ( ! validationResult . isValid ) {
27
24
throw validationResult . message ;
28
25
}
@@ -47,87 +44,64 @@ export default function registry(inputOptions: Input) {
47
44
plugins . push ( Object . assign ( plugin , { callback } ) ) ;
48
45
} ;
49
46
50
- const start = (
51
- callback : Callback < { app : express . Express ; server : http . Server } >
47
+ const start = async (
48
+ callback : (
49
+ err : unknown ,
50
+ data : { app : express . Express ; server : http . Server }
51
+ ) => void
52
52
) => {
53
53
// eslint-disable-next-line no-console
54
54
const ok = ( msg : string ) => console . log ( colors . green ( msg ) ) ;
55
- if ( typeof callback !== 'function' ) {
56
- callback = _ . noop ;
57
- }
58
55
createRouter ( router , options , repository ) ;
59
- async . waterfall (
60
- [
61
- ( cb : Callback < Dictionary < ( ...args : unknown [ ] ) => unknown > , unknown > ) =>
62
- fromPromise ( pluginsInitialiser . init ) ( plugins , cb ) ,
63
-
64
- (
65
- plugins : Dictionary < ( ...args : unknown [ ] ) => void > ,
66
- cb : Callback < ComponentsList | string , unknown >
67
- ) => {
68
- options . plugins = plugins ;
69
- fromPromise ( repository . init ) ( cb ) ;
70
- } ,
71
-
72
- (
73
- componentsInfo : ComponentsList ,
74
- cb : Callback < ComponentsList , string >
75
- ) => {
76
- fromPromise ( appStart ) ( repository , options , ( err : any ) =>
77
- cb ( err ? err . msg : null , componentsInfo )
78
- ) ;
79
- }
80
- ] ,
81
- ( err , componentsInfo ) => {
56
+
57
+ try {
58
+ options . plugins = await pluginsInitialiser . init ( plugins ) ;
59
+ const componentsInfo = await repository . init ( ) ;
60
+ await appStart ( repository , options ) ;
61
+
62
+ server = http . createServer ( app ) ;
63
+ server . timeout = options . timeout ;
64
+ if ( options . keepAliveTimeout ) {
65
+ server . keepAliveTimeout = options . keepAliveTimeout ;
66
+ }
67
+
68
+ // @ts -ignore Type not taking error on callback (this can error, though)
69
+ server . listen ( options . port , ( err : any ) => {
82
70
if ( err ) {
83
71
return callback ( err , undefined as any ) ;
84
72
}
85
-
86
- server = http . createServer ( app ) ;
87
- server . timeout = options . timeout ;
88
- if ( options . keepAliveTimeout ) {
89
- server . keepAliveTimeout = options . keepAliveTimeout ;
90
- }
91
-
92
- // @ts -ignore Type not taking error on callback (this can error, though)
93
- server . listen ( options . port , ( err : any ) => {
94
- if ( err ) {
95
- return callback ( err , undefined as any ) ;
96
- }
97
- eventsHandler . fire ( 'start' , { } ) ;
98
-
99
- if ( options . verbosity ) {
100
- ok ( `Registry started at port ${ app . get ( 'port' ) } ` ) ;
101
-
102
- if ( _ . isObject ( componentsInfo ) ) {
103
- const componentsNumber = Object . keys (
104
- // @ts -ignore
105
- componentsInfo . components
106
- ) . length ;
107
- const componentsReleases = _ . reduce (
108
- // @ts -ignore
109
- componentsInfo . components ,
110
- ( memo , component ) => parseInt ( memo , 10 ) + component . length
111
- ) ;
112
-
113
- ok (
114
- `Registry serving ${ componentsNumber } components for a total of ${ componentsReleases } releases.`
115
- ) ;
116
- }
73
+ eventsHandler . fire ( 'start' , { } ) ;
74
+
75
+ if ( options . verbosity ) {
76
+ ok ( `Registry started at port ${ app . get ( 'port' ) } ` ) ;
77
+
78
+ if ( _ . isObject ( componentsInfo ) ) {
79
+ const componentsNumber = Object . keys (
80
+ componentsInfo . components
81
+ ) . length ;
82
+ const componentsReleases = Object . values (
83
+ componentsInfo . components
84
+ ) . reduce ( ( acc , component ) => acc + component . length , 0 ) ;
85
+
86
+ ok (
87
+ `Registry serving ${ componentsNumber } components for a total of ${ componentsReleases } releases.`
88
+ ) ;
117
89
}
90
+ }
118
91
119
- callback ( null , { app, server } ) ;
120
- } ) ;
92
+ callback ( null , { app, server } ) ;
93
+ } ) ;
121
94
122
- server . on ( 'error' , error => {
123
- eventsHandler . fire ( 'error' , {
124
- code : 'EXPRESS_ERROR' ,
125
- message : error ?. message ?? String ( error )
126
- } ) ;
127
- callback ( error , undefined as any ) ;
95
+ server . on ( 'error' , error => {
96
+ eventsHandler . fire ( 'error' , {
97
+ code : 'EXPRESS_ERROR' ,
98
+ message : error ?. message ?? String ( error )
128
99
} ) ;
129
- }
130
- ) ;
100
+ callback ( error , undefined as any ) ;
101
+ } ) ;
102
+ } catch ( err ) {
103
+ callback ( ( err as any ) ?. msg || err , undefined as any ) ;
104
+ }
131
105
} ;
132
106
133
107
return {
0 commit comments