Skip to content

Commit 4587e53

Browse files
committed
copy over sane helpers
1 parent 694c360 commit 4587e53

2 files changed

Lines changed: 90 additions & 8 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
class RecrawlWarning {
4+
constructor(root, count) {
5+
this.root = root;
6+
this.count = count;
7+
}
8+
9+
static findByRoot(root) {
10+
for (let i = 0; i < this.RECRAWL_WARNINGS.length; i++) {
11+
const warning = this.RECRAWL_WARNINGS[i];
12+
if (warning.root === root) {
13+
return warning;
14+
}
15+
}
16+
}
17+
18+
static isRecrawlWarningDupe(warningMessage) {
19+
if (typeof warningMessage !== 'string') {
20+
return false;
21+
}
22+
const match = warningMessage.match(this.REGEXP);
23+
if (!match) {
24+
return false;
25+
}
26+
const count = Number(match[1]);
27+
const root = match[2];
28+
29+
const warning = this.findByRoot(root);
30+
31+
if (warning) {
32+
// only keep the highest count, assume count to either stay the same or
33+
// increase.
34+
if (warning.count >= count) {
35+
return true;
36+
} else {
37+
// update the existing warning to the latest (highest) count
38+
warning.count = count;
39+
return false;
40+
}
41+
} else {
42+
this.RECRAWL_WARNINGS.push(new RecrawlWarning(root, count));
43+
return false;
44+
}
45+
}
46+
}
47+
48+
RecrawlWarning.RECRAWL_WARNINGS = [];
49+
RecrawlWarning.REGEXP = /Recrawled this watch (\d+) times, most recently because:\n([^:]+)/;
50+
51+
module.exports = RecrawlWarning;

packages/jest-haste-map/src/lib/WatchmanWatcher.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
import assert from 'assert';
99
import {EventEmitter} from 'events';
1010
import path from 'path';
11+
import anymatch from 'anymatch';
1112
import watchman from 'fb-watchman';
1213
import * as fs from 'graceful-fs';
13-
import common from 'sane/src/common';
14-
import RecrawlWarning from 'sane/src/utils/recrawl-warning-dedupe';
14+
import micromatch from 'micromatch';
15+
import RecrawlWarning from './RecrawlWarning';
1516

16-
const CHANGE_EVENT = common.CHANGE_EVENT;
17-
const DELETE_EVENT = common.DELETE_EVENT;
18-
const ADD_EVENT = common.ADD_EVENT;
19-
const ALL_EVENT = common.ALL_EVENT;
17+
const CHANGE_EVENT = 'change';
18+
const DELETE_EVENT = 'delete';
19+
const ADD_EVENT = 'add';
20+
const ALL_EVENT = 'all';
2021
const SUB_NAME = 'sane-sub';
2122

2223
/**
@@ -29,7 +30,7 @@ const SUB_NAME = 'sane-sub';
2930
*/
3031

3132
export default function WatchmanWatcher(dir, opts) {
32-
common.assignOptions(this, opts);
33+
assignOptions(this, opts);
3334
this.root = path.resolve(dir);
3435
this.init();
3536
}
@@ -226,7 +227,7 @@ WatchmanWatcher.prototype.handleFileChange = function (changeDescriptor) {
226227

227228
if (
228229
!(self.capabilities.wildmatch && !this.hasIgnore) &&
229-
!common.isFileIncluded(this.globs, this.dot, this.doIgnore, relativePath)
230+
!isFileIncluded(this.globs, this.dot, this.doIgnore, relativePath)
230231
) {
231232
return;
232233
}
@@ -321,3 +322,33 @@ function handleWarning(resp) {
321322
return false;
322323
}
323324
}
325+
326+
function assignOptions(watcher, opts) {
327+
opts = opts || {};
328+
watcher.globs = opts.glob || [];
329+
watcher.dot = opts.dot || false;
330+
watcher.ignored = opts.ignored || false;
331+
332+
if (!Array.isArray(watcher.globs)) {
333+
watcher.globs = [watcher.globs];
334+
}
335+
watcher.hasIgnore =
336+
Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
337+
watcher.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false;
338+
339+
if (opts.watchman && opts.watchmanPath) {
340+
watcher.watchmanPath = opts.watchmanPath;
341+
}
342+
343+
return opts;
344+
}
345+
346+
function isFileIncluded(globs, dot, doIgnore, relativePath) {
347+
if (doIgnore(relativePath)) {
348+
return false;
349+
}
350+
return globs.length
351+
? micromatch.some(relativePath, globs, {dot})
352+
: dot || micromatch.some(relativePath, '**/*');
353+
}
354+

0 commit comments

Comments
 (0)