const startVerdaccio = require ( "verdaccio" ) . default ; import * as fs from "fs-extra" ; import * as http from "http" ; export interface Registry { url : string ; max_fails ? : number ; timeout ? : string ; fail_timeout ? : string ; agent_options ? : http . AgentOptions ; } export interface Package { access ? : string ; publish ? : string ; proxy ? : string ; } export interface Log { type ? : "stdout" | "file" ; format ? : "pretty" | "pretty-timestamped" ; level ? : "fatal" | "error" | "warn" | "http" | "info" | "debug" | "trace" ; file ? : string ; } export interface Config { auth : { [ name ] : any ; } ; storage : string ; uplinks : { [ name ] : Registry ; } ; packages : { [ pattern ] : Package ; } ; logs ? : Log [ ] ; self_path ? : string ; listen : string ; clearStorage ? : boolean ; onStart ? : ( ) => Promise < void > ; } export interface Server { httpServer : http . Server ; stop ( this ) : Promise < void > ; } class ServerImpl implements Server { constructor ( public httpServer ) { } stop ( ) { return new Promise ( ( resolve , reject ) => { try { this . httpServer . close ( ( err ) => err ? reject ( err ) : resolve ( ) ) ; } catch ( err ) { reject ( err ) ; } } ) ; } } export async function start ( config , configPath ) { const { clearStorage , storage , onStart } = config ; if ( clearStorage ) await fs . emptyDir ( storage ) ; else await fs . ensureDir ( storage ) ; const server = await new Promise < http . Server > ( ( resolve , reject ) => { try { startVerdaccio ( config , config . listen , configPath , "1.0.0" , "verdaccio" , ( webServer , addr , _pkgName , _pkgVersion ) => { webServer . on ( "error" , reject ) ; webServer . listen ( addr . port || addr . path , addr . host , ( ) => { resolve ( webServer ) ; } ) ; } ) ; } catch ( err ) { reject ( err ) ; } } ) ; try { if ( onStart ) await onStart ( ) ; return new ServerImpl ( server ) ; } catch ( err ) { server . close ( ) ; throw err ; } }