1 | const watch = require('file-watch-iterator');
|
2 | const anymatch = require('anymatch');
|
3 | const parseGitignore = require('parse-gitignore');
|
4 | const utils = require('./utils');
|
5 |
|
6 | module.exports = async function*(argv = {}) {
|
7 |
|
8 | const isRequireFile = file => anymatch(argv.require, file);
|
9 | const isTestFile = file => anymatch(argv.testFiles, file);
|
10 |
|
11 | const gitignore = utils.flat(utils.arrify(argv.gitignore).map(utils.readFile).map(parseGitignore));
|
12 | const ignored = [...argv.exclude, ...gitignore];
|
13 |
|
14 | const watcher = watch('.', {
|
15 | cwd: argv.cwd || process.cwd(),
|
16 |
|
17 |
|
18 |
|
19 | ignored,
|
20 | });
|
21 |
|
22 | for await (const _files of watcher) {
|
23 | const files = _files.toArray();
|
24 |
|
25 | const R = {
|
26 | sourceFiles: [],
|
27 | testFiles: [],
|
28 | allFiles: [],
|
29 | };
|
30 |
|
31 | for (const { file, changed } of files) {
|
32 |
|
33 | const isValidExtension = argv.extensions.find(ext => file.endsWith(ext))
|
34 |
|
35 | if (!isValidExtension) continue;
|
36 |
|
37 | R.allFiles.push(file);
|
38 |
|
39 | if (isRequireFile(file) || isTestFile(file)) {
|
40 | R.testFiles.push(file);
|
41 | } else {
|
42 | R.sourceFiles.push(file);
|
43 | }
|
44 | }
|
45 |
|
46 | yield R;
|
47 | }
|
48 | };
|