Skip to content

Commit 6282290

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

2 files changed

Lines changed: 136 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: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
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';
1515

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;
16+
const CHANGE_EVENT = 'change';
17+
const DELETE_EVENT = 'delete';
18+
const ADD_EVENT = 'add';
19+
const ALL_EVENT = 'all';
2020
const SUB_NAME = 'sane-sub';
2121

2222
/**
@@ -29,7 +29,7 @@ const SUB_NAME = 'sane-sub';
2929
*/
3030

3131
export default function WatchmanWatcher(dir, opts) {
32-
common.assignOptions(this, opts);
32+
assignOptions(this, opts);
3333
this.root = path.resolve(dir);
3434
this.init();
3535
}
@@ -226,7 +226,7 @@ WatchmanWatcher.prototype.handleFileChange = function (changeDescriptor) {
226226

227227
if (
228228
!(self.capabilities.wildmatch && !this.hasIgnore) &&
229-
!common.isFileIncluded(this.globs, this.dot, this.doIgnore, relativePath)
229+
!isFileIncluded(this.globs, this.dot, this.doIgnore, relativePath)
230230
) {
231231
return;
232232
}
@@ -321,3 +321,80 @@ function handleWarning(resp) {
321321
return false;
322322
}
323323
}
324+
325+
function assignOptions(watcher, opts) {
326+
opts = opts || {};
327+
watcher.globs = opts.glob || [];
328+
watcher.dot = opts.dot || false;
329+
watcher.ignored = opts.ignored || false;
330+
331+
if (!Array.isArray(watcher.globs)) {
332+
watcher.globs = [watcher.globs];
333+
}
334+
watcher.hasIgnore =
335+
Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
336+
watcher.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false;
337+
338+
if (opts.watchman && opts.watchmanPath) {
339+
watcher.watchmanPath = opts.watchmanPath;
340+
}
341+
342+
return opts;
343+
}
344+
345+
function isFileIncluded(globs, dot, doIgnore, relativePath) {
346+
if (doIgnore(relativePath)) {
347+
return false;
348+
}
349+
return globs.length
350+
? micromatch.some(relativePath, globs, {dot})
351+
: dot || micromatch.some(relativePath, '**/*');
352+
}
353+
354+
class RecrawlWarning {
355+
constructor(root, count) {
356+
this.root = root;
357+
this.count = count;
358+
}
359+
360+
static findByRoot(root) {
361+
for (let i = 0; i < this.RECRAWL_WARNINGS.length; i++) {
362+
const warning = this.RECRAWL_WARNINGS[i];
363+
if (warning.root === root) {
364+
return warning;
365+
}
366+
}
367+
}
368+
369+
static isRecrawlWarningDupe(warningMessage) {
370+
if (typeof warningMessage !== 'string') {
371+
return false;
372+
}
373+
const match = warningMessage.match(this.REGEXP);
374+
if (!match) {
375+
return false;
376+
}
377+
const count = Number(match[1]);
378+
const root = match[2];
379+
380+
const warning = this.findByRoot(root);
381+
382+
if (warning) {
383+
// only keep the highest count, assume count to either stay the same or
384+
// increase.
385+
if (warning.count >= count) {
386+
return true;
387+
} else {
388+
// update the existing warning to the latest (highest) count
389+
warning.count = count;
390+
return false;
391+
}
392+
} else {
393+
this.RECRAWL_WARNINGS.push(new RecrawlWarning(root, count));
394+
return false;
395+
}
396+
}
397+
}
398+
399+
RecrawlWarning.RECRAWL_WARNINGS = [];
400+
RecrawlWarning.REGEXP = /Recrawled this watch (\d+) times, most recently because:\n([^:]+)/;

0 commit comments

Comments
 (0)