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
1814const optionsHashCache = new WeakMap ( ) ;
1915const nodeVersion = process . version ;
2016
2117const { assign} = Object ;
2218const returns = ( a ) => ( ) => a ;
23- const CACHE_FILE = '.putoutcache.json' ;
2419
2520const 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
99101const 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