All files / gulpfile.ts/tasks watch.ts

0% Statements 0/28
0% Branches 0/10
0% Functions 0/5
0% Lines 0/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 56                                                                                                               
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'
 
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]
  }
}
 
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()
}