import * as plugins from './smartbrowser.plugins' /** * the options interface of a Smartbrowser instance */ export interface ISmartbrowserOptions { webroot: string watchFiles: string[] } /** * Type of status that a bsInstance can have */ export type bsStatus = 'idle' | 'starting' | 'running' /** * class smartbrowser controls a browser-sync instance for you */ export class Smartbrowser { bsInstance = plugins.browserSync.create() bsConfig: plugins.browserSync.Options = { server: {} } bsStatus: bsStatus = 'idle' bsStarted: plugins.q.Promise constructor(optionsArg: ISmartbrowserOptions) { this.bsConfig.server.baseDir = optionsArg.webroot this.bsConfig.files = optionsArg.watchFiles } /** * starts the server and returns the browserSync instance in a resolved Promise */ start(): plugins.q.Promise { let done = plugins.q.defer() if (this.bsStatus === 'idle') { this.bsStatus = 'starting' let localDone = plugins.q.defer() this.bsStarted = localDone.promise this.bsInstance.init(this.bsConfig, () => { this.bsStatus = 'running' localDone.resolve() done.resolve(this.bsInstance) }) } else { this.bsStarted.then( () => { done.resolve(this.bsInstance) }) } return done.promise } /** * stops the smartbrowser instance */ stop(): plugins.q.Promise { let done = plugins.q.defer() this.bsInstance.exit() this.bsStatus = 'idle' done.resolve() return done.promise } }