all files / src/components/ watcher.js

35.71% Statements 10/28
0% Branches 0/6
100% Functions 0/0
37.04% Lines 10/27
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 5113×                                                                                  
import path from 'path'
import createDebugger from 'debug'
import Gaze from 'gaze'
import { modifiedFile } from '../helpers/formatted-file.js'
import { ENV_PATH } from '../constants/paths.js'
import {
    SET_WATCHER_PREPARATIONS,
    CLEAR_WATCHER_PREPARATIONS
} from '../constants/actionTypes.js'
import { setWatcherModifiedFile, handleError } from '../actions/creators.js'
 
const debug = createDebugger('unistack:watcher')
 
const Watcher = ({ dispatch, getState }) => {
    const state = getState()
    const watch = getStateLeaf(state, 'WATCH')
    const watching = getStateLeaf(state, 'WATCHING')
    debug('Checking if it is worth watching the files...')
    if (watch && !watching) {
        const pattern = path.join(ENV_PATH, '{src,dist/css}', '**/*')
        debug('!!!!--Preparing to watch files: %s', pattern)
        dispatch({ type: SET_WATCHER_PREPARATIONS })
        const gaze = new Gaze(pattern)
        gaze.on('all', (event, filepath) => {
            const formattedFile = modifiedFile(filepath, event)
            debug('Emitting file change: %s - %s', event, filepath)
            return dispatch(setWatcherModifiedFile(formattedFile))
        })
        gaze.on('ready', () => {
            debug('@@@@--Successfully watching files...')
            return dispatch({ type: CLEAR_WATCHER_PREPARATIONS })
        })
        gaze.on('error', error => {
            debug('@@@@--Failed to watch files, %s', error.stack)
            return dispatch(handleError(error))
        })
    }
    debug('Nothing to do.')
}
 
const getStateLeaf = (state, leaf) => {
    switch(leaf) {
        case 'WATCH':
            return state.watcher.watch
        case 'WATCHING':
            return state.watcher.watching
    }
}
 
export default Watcher