Skip to content

Commit 40cd7d8

Browse files
committed
feature: @putout/cli-cache: find node_modules instead of package.json
1 parent 2bf2455 commit 40cd7d8

File tree

7 files changed

+66
-11
lines changed

7 files changed

+66
-11
lines changed

packages/cli-cache/.madrun.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {run} from 'madrun';
22

33
export default {
4-
'test': () => `tape 'lib/*.spec.js'`,
4+
'test': () => `tape 'lib/*.spec.*'`,
55
'watch:test': async () => `nodemon -w lib -x ${await run('test')}`,
66
'lint': () => `putout .`,
77
'fresh:lint': () => run('lint', '--fresh'),

packages/cli-cache/.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.*
2-
*.spec.js
2+
*.spec.*
33
test
44
fixture
55
yarn-error.log

packages/cli-cache/lib/cache.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@ const defaultCache = {
3030
canUseCache: returns(false),
3131
};
3232

33-
const createCache = async ({cache, fresh, version, again, createFromFile = _createFromFile, unlink = _unlink, findCachePath = _findCachePath}) => {
33+
const createCache = async (overrides = {}) => {
34+
const {
35+
cache,
36+
fresh,
37+
version,
38+
again,
39+
createFromFile = _createFromFile,
40+
unlink = _unlink,
41+
findCachePath = _findCachePath,
42+
} = overrides;
43+
3444
const name = await findCachePath();
3545

3646
if (fresh)
@@ -146,10 +156,10 @@ const createGetOptionsCache = ({version}) => (options) => {
146156
};
147157

148158
async function _findCachePath() {
149-
const findCacheDir = await simpleImport('find-cache-dir');
159+
const findCacheDir = await simpleImport('./find-cache-dir.mjs');
160+
150161
const cacheDir = await findCacheDir({
151162
name: 'putout',
152-
create: true,
153163
});
154164

155165
if (cacheDir)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {join} from 'node:path';
2+
import {mkdir as _mkdir} from 'node:fs/promises';
3+
import process from 'node:process';
4+
import _escalade from 'escalade';
5+
6+
const {cwd: _cwd} = process;
7+
8+
const includesName = (directory) => (dir, names) => {
9+
return names.includes(directory) && join(dir, directory);
10+
};
11+
12+
export default async function findCacheDir(overrides = {}) {
13+
const {
14+
name,
15+
cwd = _cwd,
16+
escalade = _escalade,
17+
mkdir = _mkdir,
18+
directory = 'node_modules',
19+
} = overrides;
20+
const input = join(cwd(), 'package.json');
21+
const path = await escalade(input, includesName(directory));
22+
23+
if (!path)
24+
return path;
25+
26+
const cacheDir = join(path, '.cache', name);
27+
28+
await mkdir(cacheDir, {
29+
recursive: true,
30+
});
31+
32+
return cacheDir;
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {test} from 'supertape';
2+
import findCacheDir from './find-cache-dir.mjs';
3+
4+
test('putout: cli-cache: find-cache-dir', async (t) => {
5+
const dir = await findCacheDir({
6+
name: 'putout',
7+
directory: 'abc',
8+
});
9+
10+
t.notOk(dir);
11+
t.end();
12+
});

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

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

3-
const {dirname, join} = require('node:path');
4-
53
module.exports = async (fileCache, {findUp}) => {
64
const result = await Promise.all([
75
isNodeModulesChanged(fileCache, {
@@ -21,14 +19,12 @@ module.exports.isNodeModulesChanged = isNodeModulesChanged;
2119
module.exports.isEslintChanged = isEslintChanged;
2220

2321
async function isNodeModulesChanged(fileCache, {findUp}) {
24-
const packagePath = await findUp('package.json');
22+
const packagePath = await findUp('node_modules');
2523

2624
if (!packagePath)
2725
return false;
2826

29-
const name = join(dirname(packagePath), 'node_modules');
30-
31-
return isChanged(name, fileCache);
27+
return isChanged(packagePath, fileCache);
3228
}
3329

3430
// https://eslint.org/docs/user-guide/configuring#configuration-file-formats
@@ -40,6 +36,9 @@ async function isEslintChanged(fileCache, {findUp}) {
4036
'.eslintrc.yaml',
4137
'.eslintrc.yml',
4238
'.eslint.config.js',
39+
'.eslint.config.mjs',
40+
'.eslint.config.cjs',
41+
'.eslint.config.ts',
4342
]);
4443

4544
if (!name)

packages/cli-cache/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"report": "madrun report"
2525
},
2626
"dependencies": {
27+
"escalade": "^3.2.0",
2728
"file-entry-cache": "^10.0.2",
2829
"find-cache-dir": "^5.0.0",
2930
"find-up": "^7.0.0",

0 commit comments

Comments
 (0)