Skip to content

Commit a9d3660

Browse files
Evan JacobsEvan Jacobs
Evan Jacobs
authored and
Evan Jacobs
committed
Add CHOKIDAR_USEPOLLING env variable detection
To override the chokidar instance configuration for an entire stack at once, regardless of how it is used / dependency depth. Have to do string comparison because process.env always sets variables as strings. Going to restrict the possible options to keep documenting and reasoning simple.
1 parent 15c6427 commit a9d3660

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

index.js

+12
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ function FSWatcher(_opts) {
9090
opts.usePolling = process.platform === 'darwin';
9191
}
9292

93+
// Global override (useful for end-developers that need to force polling for all
94+
// instances of chokidar, regardless of usage/dependency depth)
95+
if (process.env.CHOKIDAR_USEPOLLING !== undefined) {
96+
var envLower = process.env.CHOKIDAR_USEPOLLING.toLowerCase();
97+
98+
if (envLower === 'false' || envLower === '0') {
99+
opts.usePolling = false;
100+
} else if (envLower === 'true' || envLower === '1') {
101+
opts.usePolling = true;
102+
}
103+
}
104+
93105
// Editor atomic write normalization enabled by default with fs.watch
94106
if (undef('atomic')) opts.atomic = !opts.usePolling && !opts.useFsEvents;
95107
if (opts.atomic) this._pendingUnlinks = Object.create(null);

test.js

+57
Original file line numberDiff line numberDiff line change
@@ -1765,4 +1765,61 @@ function runTests(baseopts) {
17651765
});
17661766
});
17671767
});
1768+
describe('env variable option override', function() {
1769+
describe('CHOKIDAR_USEPOLLING', function() {
1770+
afterEach(function() {
1771+
delete process.env.CHOKIDAR_USEPOLLING;
1772+
});
1773+
1774+
it('should make options.usePolling `true` when CHOKIDAR_USEPOLLING is set to true', function(done) {
1775+
options.usePolling = false;
1776+
process.env.CHOKIDAR_USEPOLLING = true;
1777+
1778+
watcher = chokidar.watch(fixturesPath, options).on('ready', function() {
1779+
watcher.options.usePolling.should.be.true;
1780+
done();
1781+
});
1782+
});
1783+
1784+
it('should make options.usePolling `true` when CHOKIDAR_USEPOLLING is set to 1', function(done) {
1785+
options.usePolling = false;
1786+
process.env.CHOKIDAR_USEPOLLING = 1;
1787+
1788+
watcher = chokidar.watch(fixturesPath, options).on('ready', function() {
1789+
watcher.options.usePolling.should.be.true;
1790+
done();
1791+
});
1792+
});
1793+
1794+
it('should make options.usePolling `false` when CHOKIDAR_USEPOLLING is set to false', function(done) {
1795+
options.usePolling = true;
1796+
process.env.CHOKIDAR_USEPOLLING = false;
1797+
1798+
watcher = chokidar.watch(fixturesPath, options).on('ready', function() {
1799+
watcher.options.usePolling.should.be.false;
1800+
done();
1801+
});
1802+
});
1803+
1804+
it('should make options.usePolling `false` when CHOKIDAR_USEPOLLING is set to 0', function(done) {
1805+
options.usePolling = true;
1806+
process.env.CHOKIDAR_USEPOLLING = false;
1807+
1808+
watcher = chokidar.watch(fixturesPath, options).on('ready', function() {
1809+
watcher.options.usePolling.should.be.false;
1810+
done();
1811+
});
1812+
});
1813+
1814+
it('should not attenuate options.usePolling when CHOKIDAR_USEPOLLING is set to an arbitrary value', function(done) {
1815+
options.usePolling = true;
1816+
process.env.CHOKIDAR_USEPOLLING = 'foo';
1817+
1818+
watcher = chokidar.watch(fixturesPath, options).on('ready', function() {
1819+
watcher.options.usePolling.should.be.true;
1820+
done();
1821+
});
1822+
});
1823+
});
1824+
});
17681825
}

0 commit comments

Comments
 (0)