@ehmicky/dev-tasks
Version:
Automated development tasks for my own projects
73 lines (37 loc) • 1.16 kB
JavaScript
import gulp from"gulp";
import renameFn from"rename-fn";
export const getWatchTask=(files,firstArg,secondArg)=>{
const[watchOptions,task]=parseOptions(firstArg,secondArg);
const watchTask=startWatch.bind(undefined,files,watchOptions,task);
addName(watchTask,task);
addDescription(watchTask,task);
return watchTask
};
const parseOptions=(firstArg,secondArg)=>{
if(typeof firstArg!=="object"||firstArg===null){
return[DEFAULT_WATCH_OPTIONS,firstArg]
}
return[{...DEFAULT_WATCH_OPTIONS,...firstArg},secondArg]
};
const DEFAULT_WATCH_OPTIONS={ignoreInitial:false};
const startWatch=(files,watchOptions,task)=>{
gulp.watch(files,watchOptions,task);
return Promise.resolve()
};
const addName=(watchTask,task)=>{
const name=getName(task);
renameFn(watchTask,name)
};
const getName=(task)=>{
if(typeof task!=="function"||INVALID_NAMES.has(task.name)){
return"watch"
}
return`watch ${task.name}`
};
const INVALID_NAMES=new Set(["","parallel","series","watch"]);
const addDescription=(watchTask,task)=>{
if(typeof task!=="function"||typeof task.description!=="string"){
return
}
watchTask.description=`${task.description} (watch mode)`
};