Skip to content

Commit

Permalink
feat: add exclude to watch task; add exclude and clear to watch subco…
Browse files Browse the repository at this point in the history
…mmand
  • Loading branch information
rafamel committed Apr 1, 2021
1 parent 0c263ed commit 9be1ae5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
91 changes: 67 additions & 24 deletions src/bin/watch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Task } from '../definitions';
import { series, raises, print, log, combine, watch, context } from '../tasks';
import { run } from '../utils';
import {
series,
raises,
print,
log,
combine,
watch,
context,
clear
} from '../tasks';
import { stripIndent as indent } from 'common-tags';
import { flags, safePairs, splitBy } from 'cli-belt';
import { into } from 'pipettes';
import chalk from 'chalk';
import arg from 'arg';

Expand All @@ -26,7 +37,9 @@ export default async function bin(
Options:
-p, --path <value> Paths to watch
-e, --exclude <value> File or path to exclude
-g, --glob Parse globs in paths
-c, --clear Clear stdout before tasks execution
-i, --initial Run tasks on start, before changes
-s, --symlinks Follow symlinks
--parallel Don't cancel running tasks
Expand All @@ -42,7 +55,9 @@ export default async function bin(

const types = {
'--path': [String] as [StringConstructor],
'--exclude': String,
'--glob': Boolean,
'--clear': Boolean,
'--initial': Boolean,
'--parallel': Boolean,
'--symlinks': Boolean,
Expand All @@ -65,29 +80,57 @@ export default async function bin(

const [names, args] = splitBy(cmd._, '--');
return names.length
? series(
log('debug', 'Working directory:', process.cwd()),
log(
'info',
chalk.bold(opts.bin),
chalk.bold.blue(':watch'),
names.join(' ')
),
print(),
watch(
cmd['--path'] || './',
{
glob: cmd['--glob'],
initial: cmd['--initial'],
parallel: cmd['--parallel'],
debounce: cmd['--debounce'],
depth: cmd['--depth'],
poll: cmd['--poll'],
symlinks: cmd['--symlinks']
},
context({ args }, combine(params.record, names))
)
)
? async (ctx) => {
let first = true;
return into(
series(
log('debug', 'Working directory:', process.cwd()),
log(
'info',
chalk.bold(opts.bin),
chalk.bold.blue(':watch'),
names.join(' ')
),
print(),
watch(
cmd['--path'] || './',
{
glob: cmd['--glob'],
initial: cmd['--initial'],
exclude: cmd['--exclude'] || 'node_modules',
parallel: cmd['--parallel'],
debounce: cmd['--debounce'],
depth: cmd['--depth'],
poll: cmd['--poll'],
symlinks: cmd['--symlinks']
},
async (ctx) => {
return first
? into(combine(params.record, names), (task) => {
first = false;
return run(task, ctx);
})
: into(
series(
cmd['--clear'] ? clear() : null,
log(
'info',
chalk.bold(opts.bin),
chalk.bold.blue(':watch'),
names.join(' ')
),
print(),
combine(params.record, names)
),
(task) => run(task, ctx)
);
}
)
),
context.bind(null, { args }),
(task) => run(task, ctx)
);
}
: series(
print(help + '\n'),
raises(Error(`A command or task is required`))
Expand Down
4 changes: 4 additions & 0 deletions src/tasks/filesystem/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface WatchOptions {
glob?: boolean;
/** Runs the task on start instead of waiting for changes */
initial?: boolean;
/** Files and paths to exclude */
exclude?: string | RegExp;
/** Doesn't cancel tasks in execution when a new task runs */
parallel?: boolean;
/** Avoids rapid task restarts by debouncing by a set number of ms */
Expand Down Expand Up @@ -40,6 +42,7 @@ export function watch(
{
glob: false,
initial: false,
exclude: undefined,
parallel: false,
debounce: -1,
depth: -1,
Expand All @@ -51,6 +54,7 @@ export function watch(

const watcher = chokidar.watch(paths, {
persistent: true,
ignored: opts.exclude,
ignorePermissionErrors: false,
ignoreInitial: true,
cwd: ctx.cwd,
Expand Down

0 comments on commit 9be1ae5

Please sign in to comment.