Skip to content

Commit 8baa29b

Browse files
committed
feature: @putout/cli-cache: convert to ESM
1 parent 7ede62b commit 8baa29b

16 files changed

+115
-258
lines changed

packages/cli-cache/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/cli-cache.svg?style=flat&longCache=true
44
[NPMURL]: https://npmjs.org/package/@putout/cli-cache "npm"
55

6-
Create `cache` of places found by 🐊[Putout](https://github.com/coderaiser/putout) to `places.json`.
6+
Store to `cache` information about files processed by 🐊[Putout](https://github.com/coderaiser/putout) to nearest `node_modules/.cache` directory.
77

88
## Install
99

packages/cli-cache/lib/cache.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
'use strict';
2-
3-
const process = require('node:process');
4-
const {unlink: _unlink} = require('node:fs/promises');
5-
6-
const {createFromFile: _createFromFile} = require('file-entry-cache');
7-
const murmur = require('imurmurhash');
8-
const stringify = require('json-stable-stringify-without-jsonify');
9-
const tryCatch = require('try-catch');
10-
const tryToCatch = require('try-to-catch');
11-
12-
const isNoDefinition = require('./is-no-definition');
13-
const isParserError = require('./is-parser-error');
14-
const containEslintPlugin = require('./contain-eslint-plugin');
15-
const isChanged = require('./is-changed');
16-
const {simpleImport} = require('./simple-import');
1+
import process from 'node:process';
2+
import {unlink as _unlink} from 'node:fs/promises';
3+
import {createFromFile as _createFromFile} from 'file-entry-cache';
4+
import _murmur from 'imurmurhash';
5+
import stringify from 'json-stable-stringify-without-jsonify';
6+
import tryCatch from 'try-catch';
7+
import tryToCatch from 'try-to-catch';
8+
import isNoDefinition from './is-no-definition.js';
9+
import isParserError from './is-parser-error.js';
10+
import containEslintPlugin from './contain-eslint-plugin.js';
11+
import _isChanged from './is-changed.js';
12+
import {findCacheDir as _findCacheDir} from './find-cache-dir.js';
1713

1814
const optionsHashCache = new WeakMap();
1915
const nodeVersion = process.version;
2016

2117
const {assign} = Object;
2218
const returns = (a) => () => a;
23-
const CACHE_FILE = '.putoutcache.json';
2419

2520
const defaultCache = {
2621
getPlaces: returns([]),
@@ -30,7 +25,7 @@ const defaultCache = {
3025
canUseCache: returns(false),
3126
};
3227

33-
const createCache = async (overrides = {}) => {
28+
export const createCache = async (overrides = {}) => {
3429
const {
3530
cache,
3631
fresh,
@@ -39,9 +34,17 @@ const createCache = async (overrides = {}) => {
3934
createFromFile = _createFromFile,
4035
unlink = _unlink,
4136
findCachePath = _findCachePath,
37+
findCacheDir = _findCacheDir,
38+
isChanged = _isChanged,
39+
murmur = _murmur,
4240
} = overrides;
4341

44-
const name = await findCachePath();
42+
const name = await findCachePath({
43+
findCacheDir,
44+
});
45+
46+
if (!name)
47+
return defaultCache;
4548

4649
if (fresh)
4750
await tryToCatch(unlink, name);
@@ -67,6 +70,7 @@ const createCache = async (overrides = {}) => {
6770

6871
const getOptionsHash = createGetOptionsCache({
6972
version,
73+
murmur,
7074
});
7175

7276
assign(fileCache, {
@@ -84,17 +88,15 @@ const createCache = async (overrides = {}) => {
8488
}),
8589
});
8690

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

8993
if (await isChanged(fileCache, {findUp}))
9094
await tryToCatch(unlink, name);
9195

9296
return fileCache;
9397
};
9498

95-
module.exports.createCache = createCache;
96-
module.exports._defaultCache = defaultCache;
97-
module.exports._CACHE_FILE = CACHE_FILE;
99+
export const _defaultCache = defaultCache;
98100

99101
const getPlaces = ({fileCache}) => (name) => fileCache.getFileDescriptor(name).meta.data.places;
100102

@@ -144,26 +146,24 @@ const canUseCache = ({fileCache, getOptionsHash}) => (name, options) => {
144146
return !places.length;
145147
};
146148

147-
const hash = (a) => murmur(a)
149+
const hash = (a, murmur) => murmur(a)
148150
.result()
149151
.toString(36);
150152

151-
const createGetOptionsCache = ({version}) => (options) => {
153+
const createGetOptionsCache = ({version, murmur}) => (options) => {
152154
if (!optionsHashCache.has(options))
153-
optionsHashCache.set(options, hash(`${version}_${nodeVersion}_${stringify(options)}`));
155+
optionsHashCache.set(options, hash(`${version}_${nodeVersion}_${stringify(options)}`, murmur));
154156

155157
return optionsHashCache.get(options);
156158
};
157159

158-
async function _findCachePath() {
159-
const {findCacheDir} = await simpleImport('./find-cache-dir.mjs');
160-
160+
async function _findCachePath({findCacheDir}) {
161161
const cacheDir = await findCacheDir({
162162
name: 'putout',
163163
});
164164

165165
if (cacheDir)
166166
return `${cacheDir}/places.json`;
167167

168-
return CACHE_FILE;
168+
return '';
169169
}

0 commit comments

Comments
 (0)