All files / tasks watch.ts

100% Statements 28/28
100% Branches 10/10
100% Functions 5/5
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x   1x 1x 1x 1x   1x     2x   1x   1x   3x       1x           1x 6x 6x 6x   6x     6x   6x 6x 6x   6x 6x         1x     1x 1x 1x 1x 1x    
import chalk from 'chalk'
import { FSWatcher } from 'fs'
import * as gulp from 'gulp'
import * as os from 'os'
import * as path from 'path'
import { devLogger, devLoggerTitle, IFullTimplaConfig, projectSrcPath } from '../internal'
 
export const getTaskPathFor = (taskName: string, timplaConfig: IFullTimplaConfig) => {
  switch (taskName) {
    case 'svg':
      return timplaConfig.svg
    case 'html':
      return timplaConfig.html
    case 'staticFiles':
      return timplaConfig.staticFiles
    default:
      return (timplaConfig as any)[taskName]
  }
}
 
export const initWatch: (
  timplaConfig: IFullTimplaConfig,
  watchableTasks: any
) => (taskName: string) => FSWatcher | undefined = (
  timplaConfig: IFullTimplaConfig,
  watchableTasks: any
) => taskName => {
  const taskOptions = (timplaConfig as any)[taskName]
  const taskPath = getTaskPathFor(taskName, timplaConfig)
  let watchOptions = {}
  if (timplaConfig.watch && timplaConfig.watch.gulpWatch) {
    watchOptions = timplaConfig.watch.gulpWatch[taskName] || timplaConfig.watch.gulpWatch
  }
  if (taskOptions) {
    const srcPath = projectSrcPath(taskPath.src)
    const globPattern =
      '**/*' + (taskOptions.extensions ? '.{' + taskOptions.extensions.join(',') + '}' : '')
    const taskCallback = watchableTasks[taskName]
    const watchPath = path.join(srcPath, globPattern)
    if (taskCallback) {
      devLogger('%s: %s, %O', chalk.green(taskName), chalk.magenta(watchPath), watchOptions)
      return gulp.watch(watchPath, watchOptions, taskCallback)
    }
  }
}
 
export const watch: (timplaConfig: IFullTimplaConfig, watchableTasks: any) => gulp.TaskFunction = (
  timplaConfig,
  watchableTasks
) => cb => {
  devLoggerTitle('Task watchers')
  Object.keys(watchableTasks).forEach(initWatch(timplaConfig, watchableTasks))
  devLogger(os.EOL)
  cb()
}