-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to core_d v5 and implement common package manager file checks
- Loading branch information
Showing
6 changed files
with
282 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const LRU = require('nanolru'); | ||
const resolver = require('./resolver'); | ||
const eslint_path = require('./eslint-path'); | ||
const files_hash = require('./files-hash'); | ||
|
||
const lru_cache = new LRU(10); | ||
const check_files = [ | ||
'package.json', | ||
'package-lock.json', | ||
'npm-shrinkwrap.json', | ||
'yarn.lock', | ||
'pnpm-lock.yaml' | ||
]; | ||
|
||
exports.lru_cache = lru_cache; | ||
|
||
exports.getCache = getCache; | ||
|
||
async function getCache(cwd, eslint_path_arg) { | ||
let cache = lru_cache.get(cwd); | ||
if (!cache) { | ||
cache = createCache(cwd, eslint_path_arg); | ||
if (cache) { | ||
cache.filesChanged = await files_hash.filesHash(cwd, check_files); | ||
} | ||
return cache; | ||
} | ||
const { filesChanged } = cache; | ||
if (filesChanged && await filesChanged()) { | ||
clearRequireCache(cwd); | ||
cache = createCache(cwd, eslint_path_arg); | ||
cache.filesChanged = filesChanged; | ||
} | ||
return cache; | ||
} | ||
|
||
function createCache(cwd, eslint_path_arg) { | ||
const absolute_eslint_path = eslint_path.resolve(cwd, eslint_path_arg); | ||
|
||
if (!absolute_eslint_path) { | ||
return null; | ||
} | ||
|
||
return lru_cache.set(cwd, { | ||
eslint: require(absolute_eslint_path), | ||
// use chalk from eslint | ||
chalk: require(resolver.resolve('chalk', { | ||
paths: [path.dirname(absolute_eslint_path)] | ||
})) | ||
}); | ||
} | ||
|
||
function clearRequireCache(cwd) { | ||
Object.keys(require.cache) | ||
.filter(key => key.startsWith(cwd)) | ||
.forEach((key) => { | ||
delete require.cache[key]; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/*eslint-env mocha*/ | ||
'use strict'; | ||
|
||
const { assert, refute, sinon } = require('@sinonjs/referee-sinon'); | ||
const eslint_path = require('../lib/eslint-path'); | ||
const files_hash = require('../lib/files-hash'); | ||
const { getCache, lru_cache } = require('../lib/caches'); | ||
|
||
describe('test/caches', () => { | ||
const cwd = process.cwd(); | ||
|
||
afterEach(() => { | ||
lru_cache.clear(); | ||
sinon.restore(); | ||
}); | ||
|
||
it('calls eslint_path.resolve with cwd and eslint_path_arg', async () => { | ||
sinon.replace(eslint_path, 'resolve', sinon.fake.returns(null)); | ||
|
||
await getCache(cwd, 'something'); | ||
|
||
assert.calledOnceWith(eslint_path.resolve, cwd, 'something'); | ||
}); | ||
|
||
it('calls filesHash with cwd and common package manager files', async () => { | ||
sinon.replace(files_hash, 'filesHash', sinon.fake.resolves(() => {})); | ||
|
||
await getCache(cwd); | ||
|
||
assert.calledOnceWith(files_hash.filesHash, cwd, [ | ||
'package.json', | ||
'package-lock.json', | ||
'npm-shrinkwrap.json', | ||
'yarn.lock', | ||
'pnpm-lock.yaml' | ||
]); | ||
}); | ||
|
||
it('creates a new cache with filesChanged', async () => { | ||
const filesChanged = sinon.fake(); | ||
sinon.replace(files_hash, 'filesHash', sinon.fake.resolves(filesChanged)); | ||
|
||
const cache = await getCache(cwd); | ||
|
||
refute.isNull(cache); | ||
assert.same(lru_cache.get(cwd), cache); | ||
assert.same(cache.filesChanged, filesChanged); | ||
}); | ||
|
||
it('returns null if absolute eslint path cannot be resolved', async () => { | ||
sinon.replace(files_hash, 'filesHash', sinon.fake()); | ||
sinon.replace(eslint_path, 'resolve', sinon.fake.returns(null)); | ||
|
||
const cache = await getCache('./some/path'); | ||
|
||
assert.isNull(cache); | ||
refute.called(files_hash.filesHash); | ||
}); | ||
|
||
it('returns same cache on second call if filesChanged is null', async () => { | ||
sinon.replace(files_hash, 'filesHash', sinon.fake.resolves(null)); | ||
|
||
const cache_1 = await getCache(cwd); | ||
const cache_2 = await getCache(cwd); | ||
|
||
assert.same(cache_1, cache_2); | ||
}); | ||
|
||
it('returns same cache on second call if filesChanged returns false', | ||
async () => { | ||
const filesChanged = sinon.fake.resolves(false); | ||
sinon.replace(files_hash, 'filesHash', sinon.fake.resolves(filesChanged)); | ||
|
||
const cache_1 = await getCache(cwd); | ||
const cache_2 = await getCache(cwd); | ||
|
||
assert.calledOnce(filesChanged); | ||
assert.same(cache_1, cache_2); | ||
}); | ||
|
||
it('returns new cache on second call if filesChanged returns true', | ||
async () => { | ||
const filesChanged = sinon.fake.resolves(true); | ||
sinon.replace(files_hash, 'filesHash', sinon.fake.resolves(filesChanged)); | ||
|
||
const cache_1 = await getCache(cwd); | ||
const cache_2 = await getCache(cwd); | ||
|
||
assert.calledOnce(filesChanged); | ||
refute.same(cache_1, cache_2); | ||
assert.same(cache_1.filesChanged, cache_2.filesChanged); | ||
}); | ||
|
||
it('returns new cache on second call if cwd is different', | ||
async () => { | ||
sinon.replace(files_hash, 'filesHash', | ||
sinon.fake(() => Promise.resolve(sinon.fake()))); | ||
|
||
const cache_1 = await getCache(cwd); | ||
const cache_2 = await getCache('./other/path'); | ||
|
||
refute.same(cache_1, cache_2); | ||
assert.calledTwice(files_hash.filesHash); | ||
assert.calledWith(files_hash.filesHash, cwd); | ||
assert.calledWith(files_hash.filesHash, './other/path'); | ||
refute.same(cache_1.filesChanged, cache_2.filesChanged); | ||
refute.called(cache_1.filesChanged); | ||
refute.called(cache_2.filesChanged); | ||
}); | ||
}); |
Oops, something went wrong.