Skip to content

Commit

Permalink
Use which sync interface
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardovillela committed Feb 17, 2020
1 parent c3997c2 commit a8f28fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
8 changes: 5 additions & 3 deletions packages/jest-haste-map/src/crawlers/__tests__/node.test.js
Expand Up @@ -124,7 +124,7 @@ jest.mock('fs', () => {
};
});

jest.mock('which', () => jest.fn().mockResolvedValue());
jest.mock('which');

const pearMatcher = path => /pear/.test(path);
const createMap = obj => new Map(Object.keys(obj).map(key => [key, obj[key]]));
Expand Down Expand Up @@ -296,7 +296,9 @@ describe('node crawler', () => {
it('uses node fs APIs on Unix based OS without find binary', () => {
process.platform = 'linux';
const which = require('which');
which.mockReturnValueOnce(Promise.reject());
which.sync.mockImplementation(() => {
throw new Error();
});

nodeCrawl = require('../node');

Expand All @@ -316,7 +318,7 @@ describe('node crawler', () => {
}),
);
expect(removedFiles).toEqual(new Map());
expect(which).toBeCalledWith('find');
expect(which.sync).toBeCalledWith('find');
});
});

Expand Down
23 changes: 15 additions & 8 deletions packages/jest-haste-map/src/crawlers/node.ts
Expand Up @@ -22,12 +22,17 @@ type Result = Array<[/* id */ string, /* mtime */ number, /* size */ number]>;

type Callback = (result: Result) => void;

function hasNativeFindSupport(forceNodeFilesystemAPI: boolean): Promise<void> {
if (forceNodeFilesystemAPI) return Promise.reject();
function hasNativeFindSupport(forceNodeFilesystemAPI: boolean): boolean {
if (forceNodeFilesystemAPI || process.platform === 'win32') {
return false;
}

return process.platform === 'win32'
? Promise.reject()
: ((which('find') as unknown) as Promise<void>);
try {
which.sync('find');
return true;
} catch {
return false;
}
}

function find(
Expand Down Expand Up @@ -202,8 +207,10 @@ export = function nodeCrawl(
});
};

hasNativeFindSupport(forceNodeFilesystemAPI)
.then(() => findNative(roots, extensions, ignore, callback))
.catch(() => find(roots, extensions, ignore, callback));
if (hasNativeFindSupport(forceNodeFilesystemAPI)) {
findNative(roots, extensions, ignore, callback);
} else {
find(roots, extensions, ignore, callback);
}
});
};

0 comments on commit a8f28fe

Please sign in to comment.