import { Manifest , removeAuthConfig , getOrCreateAuthConfig , AuthConfig , } from "../filesystem" ; import { startServerAndOpenBrowser } from "./auth" ; import { Poll , Endpoints , Framework } from "../utils" ; import * as Communication from "./communication" ; export * as Communication from "./communication" ; export interface Core { cwd : string ; base : string ; manifest : Manifest ; Repository : { list : ( token : string ) => Promise < Communication . RepoData > ; create : ( apiEndpoint : string , token : string , framework : Framework . FrameworkEnum ) => Communication . CreateRepositoryResponse ; validateName : ( name : string , existingRepo ? : boolean ) => Promise < string > ; } ; } export interface CoreParams { cwd : string ; base : string ; manifest : Manifest ; } export default function createCore ( { cwd , base , manifest } ) { return { cwd , base , manifest , Repository : { list : async ( token ) : Promise < Communication . RepoData > => Communication . listRepositories ( token , base ) , validateName : ( name , existingRepo = false ) : Promise < string > => Communication . validateRepositoryName ( name , base , existingRepo ) , create : async ( domain , token , framework ) : Communication . CreateRepositoryResponse => Communication . createRepository ( domain , token , framework , base ) , } , } ; } export const Auth = { login : async ( base ) : Promise < void > => { const endpoints = Endpoints . buildEndpoints ( base ) ; const { onLoginFail } = await startServerAndOpenBrowser ( endpoints . Dashboard . cliLogin , "login" , base ) ; try { return await Poll . startPolling < Communication . UserInfo | null > ( ( ) => Auth . validateSession ( base ) , ( user ) => ! ! user , 3000 , 60 ) ; } catch ( e ) { onLoginFail ( ) ; } } , signup : async ( base ) : Promise < void > => { const endpoints = Endpoints . buildEndpoints ( base ) ; const { onLoginFail } = await startServerAndOpenBrowser ( endpoints . Dashboard . cliSignup , "signup" , base ) ; try { return await Poll . startPolling < Communication . UserInfo | null > ( ( ) => Auth . validateSession ( base ) , ( user ) => ! ! user , 3000 , 60 ) ; } catch ( e ) { onLoginFail ( ) ; } } , logout : ( ) : void => removeAuthConfig ( ) , validateSession : async ( requiredBase ) : Promise < Communication . UserInfo | null > => { const config = getOrCreateAuthConfig ( ) ; if ( ! config . cookies . length ) return Promise . resolve ( null ) ; if ( requiredBase != config . base ) return Promise . resolve ( null ) ; return Communication . validateSession ( config . cookies , requiredBase ) . catch ( ( ) => null ) ; } , } ;