Skip to content

Commit 45f3bca

Browse files
committed
feature: @putout/cli-cache: get rid of find-up
1 parent 0aedf09 commit 45f3bca

File tree

8 files changed

+34
-49
lines changed

8 files changed

+34
-49
lines changed

packages/cli-cache/.eslintrc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
"plugins": [
77
"n",
88
"putout"
9-
],
10-
"ignorePatterns": ["**/fixture"]
9+
]
1110
}

packages/cli-cache/lib/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const createCache = async (overrides = {}) => {
8484
}),
8585
});
8686

87-
const {findUp} = await import('find-up');
87+
const {findUp} = await import('./find-up.mjs');
8888

8989
if (await isChanged(fileCache, {findUp}))
9090
await tryToCatch(unlink, name);

packages/cli-cache/lib/find-cache-dir.mjs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
import {join} from 'node:path';
22
import {mkdir as _mkdir} from 'node:fs/promises';
3-
import process from 'node:process';
4-
import {findNodeModules} from './find-node-modules.mjs';
5-
6-
const {cwd: _cwd} = process;
3+
import {findUp as _findUp} from './find-up.mjs';
74

85
export default async function findCacheDir(overrides = {}) {
96
const {
107
name,
11-
cwd = _cwd,
8+
findUp = _findUp,
129
mkdir = _mkdir,
1310
directory = 'node_modules',
1411
} = overrides;
1512

16-
const path = await findNodeModules({
17-
cwd,
18-
directory,
19-
});
13+
const path = await findUp(directory);
2014

2115
if (!path)
2216
return path;

packages/cli-cache/lib/find-node-modules.mjs

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/cli-cache/lib/find-up.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {join} from 'node:path';
2+
import process from 'node:process';
3+
import escalade from 'escalade';
4+
5+
const {isArray} = Array;
6+
const maybeArray = (a) => isArray(a) ? a : [a];
7+
8+
const includesName = (name) => (dir, names) => {
9+
for (const currentName of maybeArray(name)) {
10+
if (names.includes(currentName))
11+
return join(dir, currentName);
12+
}
13+
14+
return '';
15+
};
16+
17+
export const findUp = async (name, {cwd = process.cwd()} = {}) => {
18+
return await escalade(cwd, includesName(name));
19+
};
20+

packages/cli-cache/lib/is-changed.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
const {simpleImport} = require('./simple-import');
4-
53
module.exports = async (fileCache, {findUp}) => {
64
const result = await Promise.all([
7-
isNodeModulesChanged(fileCache),
5+
isNodeModulesChanged(fileCache, {
6+
findUp,
7+
}),
88
isEslintChanged(fileCache, {
99
findUp,
1010
}),
@@ -18,9 +18,8 @@ module.exports = async (fileCache, {findUp}) => {
1818
module.exports.isNodeModulesChanged = isNodeModulesChanged;
1919
module.exports.isEslintChanged = isEslintChanged;
2020

21-
async function isNodeModulesChanged(fileCache) {
22-
const {findNodeModules} = await simpleImport('./find-node-modules.mjs');
23-
const packagePath = await findNodeModules();
21+
async function isNodeModulesChanged(fileCache, {findUp}) {
22+
const packagePath = await findUp('node_modules');
2423

2524
if (!packagePath)
2625
return false;

packages/cli-cache/lib/is-changed.spec.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ test('putout: cli: cache files: is changed: isNodeModulesChanged: cannot find',
1111
reconcile: stub(),
1212
};
1313

14-
const simpleImport = stub().resolves({
15-
findNodeModules: stub(),
16-
});
17-
18-
mockRequire('./simple-import', {
19-
simpleImport,
20-
});
21-
2214
const {isNodeModulesChanged} = reRequire('./is-changed');
15+
const findUp = stub();
2316

24-
const result = await isNodeModulesChanged(fileCache);
17+
const result = await isNodeModulesChanged(fileCache, {
18+
findUp,
19+
});
2520

2621
stopAll();
2722

packages/cli-cache/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"escalade": "^3.2.0",
2828
"file-entry-cache": "^10.0.2",
2929
"find-cache-dir": "^5.0.0",
30-
"find-up": "^7.0.0",
3130
"imurmurhash": "^0.1.4",
3231
"json-stable-stringify-without-jsonify": "^1.0.1",
3332
"try-catch": "^3.0.1",

0 commit comments

Comments
 (0)