1- import async from 'async' ;
21import colors from 'colors/safe' ;
32import express from 'express' ;
43import http from 'http' ;
@@ -12,17 +11,15 @@ import Repository from './domain/repository';
1211import { create as createRouter } from './router' ;
1312import sanitiseOptions from './domain/options-sanitiser' ;
1413import * as validator from './domain/validators' ;
15- import { ComponentsList , Config , Plugin } from '../types' ;
16- import { fromPromise } from 'universalify' ;
14+ import { Config , Plugin } from '../types' ;
1715
1816interface Input extends Partial < Omit < Config , 'beforePublish' > > {
1917 baseUrl : string ;
2018}
2119
2220export default function registry ( inputOptions : Input ) {
23- const validationResult = validator . validateRegistryConfiguration (
24- inputOptions
25- ) ;
21+ const validationResult =
22+ validator . validateRegistryConfiguration ( inputOptions ) ;
2623 if ( ! validationResult . isValid ) {
2724 throw validationResult . message ;
2825 }
@@ -47,87 +44,64 @@ export default function registry(inputOptions: Input) {
4744 plugins . push ( Object . assign ( plugin , { callback } ) ) ;
4845 } ;
4946
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
5252 ) => {
5353 // eslint-disable-next-line no-console
5454 const ok = ( msg : string ) => console . log ( colors . green ( msg ) ) ;
55- if ( typeof callback !== 'function' ) {
56- callback = _ . noop ;
57- }
5855 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 ) => {
8270 if ( err ) {
8371 return callback ( err , undefined as any ) ;
8472 }
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+ ) ;
11789 }
90+ }
11891
119- callback ( null , { app, server } ) ;
120- } ) ;
92+ callback ( null , { app, server } ) ;
93+ } ) ;
12194
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 )
12899 } ) ;
129- }
130- ) ;
100+ callback ( error , undefined as any ) ;
101+ } ) ;
102+ } catch ( err ) {
103+ callback ( ( err as any ) ?. msg || err , undefined as any ) ;
104+ }
131105 } ;
132106
133107 return {
0 commit comments