smartbrowser/ts/index.ts

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2016-09-19 18:00:19 +00:00
import * as plugins from './smartbrowser.plugins'
/**
* the options interface of a Smartbrowser instance
*/
export interface ISmartbrowserOptions {
2017-04-09 10:35:53 +00:00
webroot: string
watchFiles: string[]
2016-09-19 18:00:19 +00:00
}
/**
* 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 {
2017-04-09 10:35:53 +00:00
bsInstance = plugins.browserSync.create()
bsConfig: plugins.browserSync.Options = {
server: {}
}
bsStatus: bsStatus = 'idle'
bsStarted: Promise<void>
constructor (optionsArg: ISmartbrowserOptions) {
this.bsConfig.server.baseDir = optionsArg.webroot
this.bsConfig.files = optionsArg.watchFiles
}
2016-09-19 18:00:19 +00:00
2017-04-09 10:35:53 +00:00
/**
* starts the server and returns the browserSync instance in a resolved Promise
*/
start (): Promise<plugins.browserSync.BrowserSyncInstance> {
let done = plugins.smartq.defer<plugins.browserSync.BrowserSyncInstance>()
if (this.bsStatus === 'idle') {
this.bsStatus = 'starting'
let localDone = plugins.smartq.defer<void>()
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) })
2016-09-19 18:00:19 +00:00
}
2017-04-09 10:35:53 +00:00
return done.promise
}
2016-09-19 18:00:19 +00:00
2017-04-09 10:35:53 +00:00
/**
* stops the smartbrowser instance
*/
stop (): Promise<void> {
let done = plugins.smartq.defer<void>()
this.bsInstance.exit()
this.bsStatus = 'idle'
done.resolve()
return done.promise
}
2016-09-19 18:00:19 +00:00
}