Skip to content

Commit

Permalink
refactor: replace find-cache-dir by find-up
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 24, 2024
1 parent d85f420 commit 3b9da9b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 88 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"node": "^18.20.0 || ^20.10.0 || >=22.0.0"
},
"dependencies": {
"find-cache-dir": "^4.0.0"
"find-up": "^5.0.0"
},
"peerDependencies": {
"@babel/core": "^7.12.0",
Expand Down
23 changes: 14 additions & 9 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ const path = require("path");
const zlib = require("zlib");
const { promisify } = require("util");
const { readFile, writeFile, mkdir } = require("fs/promises");
// Lazily instantiated when needed
const findCacheDirP = import("find-cache-dir");

const { sync: findUpSync } = require("find-up");
const { env } = process;
const transform = require("./transform");
const serialize = require("./serialize");
let defaultCacheDirectory = null;
Expand Down Expand Up @@ -162,14 +161,20 @@ module.exports = async function (params) {
if (typeof params.cacheDirectory === "string") {
directory = params.cacheDirectory;
} else {
if (defaultCacheDirectory === null) {
const { default: findCacheDir } = await findCacheDirP;
defaultCacheDirectory =
findCacheDir({ name: "babel-loader" }) || os.tmpdir();
}

defaultCacheDirectory ??= findCacheDir("babel-loader");
directory = defaultCacheDirectory;
}

return await handleCache(directory, params);
};

function findCacheDir(name) {
if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) {
return path.join(env.CACHE_DIR, name);
}
const rootPkgJSONPath = path.dirname(findUpSync("package.json"));
if (rootPkgJSONPath) {
return path.join(rootPkgJSONPath, "node_modules", ".cache", name);
}
return os.tmpdir();
}
79 changes: 1 addition & 78 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,7 @@ __metadata:
eslint: ^9.6.0
eslint-config-prettier: ^9.1.0
eslint-plugin-prettier: ^5.1.3
find-cache-dir: ^4.0.0
find-up: ^5.0.0
globals: ^15.8.0
husky: ^8.0.3
lint-staged: ^13.2.3
Expand Down Expand Up @@ -2304,13 +2304,6 @@ __metadata:
languageName: node
linkType: hard

"common-path-prefix@npm:^3.0.0":
version: 3.0.0
resolution: "common-path-prefix@npm:3.0.0"
checksum: fdb3c4f54e51e70d417ccd950c07f757582de800c0678ca388aedefefc84982039f346f9fd9a1252d08d2da9e9ef4019f580a1d1d3a10da031e4bb3c924c5818
languageName: node
linkType: hard

"concat-map@npm:0.0.1":
version: 0.0.1
resolution: "concat-map@npm:0.0.1"
Expand Down Expand Up @@ -2724,16 +2717,6 @@ __metadata:
languageName: node
linkType: hard

"find-cache-dir@npm:^4.0.0":
version: 4.0.0
resolution: "find-cache-dir@npm:4.0.0"
dependencies:
common-path-prefix: ^3.0.0
pkg-dir: ^7.0.0
checksum: 52a456a80deeb27daa3af6e06059b63bdb9cc4af4d845fc6d6229887e505ba913cd56000349caa60bc3aa59dacdb5b4c37903d4ba34c75102d83cab330b70d2f
languageName: node
linkType: hard

"find-up@npm:^5.0.0":
version: 5.0.0
resolution: "find-up@npm:5.0.0"
Expand All @@ -2744,16 +2727,6 @@ __metadata:
languageName: node
linkType: hard

"find-up@npm:^6.3.0":
version: 6.3.0
resolution: "find-up@npm:6.3.0"
dependencies:
locate-path: ^7.1.0
path-exists: ^5.0.0
checksum: 9a21b7f9244a420e54c6df95b4f6fc3941efd3c3e5476f8274eb452f6a85706e7a6a90de71353ee4f091fcb4593271a6f92810a324ec542650398f928783c280
languageName: node
linkType: hard

"flat-cache@npm:^4.0.0":
version: 4.0.1
resolution: "flat-cache@npm:4.0.1"
Expand Down Expand Up @@ -3396,15 +3369,6 @@ __metadata:
languageName: node
linkType: hard

"locate-path@npm:^7.1.0":
version: 7.2.0
resolution: "locate-path@npm:7.2.0"
dependencies:
p-locate: ^6.0.0
checksum: c1b653bdf29beaecb3d307dfb7c44d98a2a98a02ebe353c9ad055d1ac45d6ed4e1142563d222df9b9efebc2bcb7d4c792b507fad9e7150a04c29530b7db570f8
languageName: node
linkType: hard

"lodash.debounce@npm:^4.0.8":
version: 4.0.8
resolution: "lodash.debounce@npm:4.0.8"
Expand Down Expand Up @@ -3761,15 +3725,6 @@ __metadata:
languageName: node
linkType: hard

"p-limit@npm:^4.0.0":
version: 4.0.0
resolution: "p-limit@npm:4.0.0"
dependencies:
yocto-queue: ^1.0.0
checksum: 01d9d70695187788f984226e16c903475ec6a947ee7b21948d6f597bed788e3112cc7ec2e171c1d37125057a5f45f3da21d8653e04a3a793589e12e9e80e756b
languageName: node
linkType: hard

"p-locate@npm:^5.0.0":
version: 5.0.0
resolution: "p-locate@npm:5.0.0"
Expand All @@ -3779,15 +3734,6 @@ __metadata:
languageName: node
linkType: hard

"p-locate@npm:^6.0.0":
version: 6.0.0
resolution: "p-locate@npm:6.0.0"
dependencies:
p-limit: ^4.0.0
checksum: 2bfe5234efa5e7a4e74b30a5479a193fdd9236f8f6b4d2f3f69e3d286d9a7d7ab0c118a2a50142efcf4e41625def635bd9332d6cbf9cc65d85eb0718c579ab38
languageName: node
linkType: hard

"p-map@npm:^4.0.0":
version: 4.0.0
resolution: "p-map@npm:4.0.0"
Expand Down Expand Up @@ -3820,13 +3766,6 @@ __metadata:
languageName: node
linkType: hard

"path-exists@npm:^5.0.0":
version: 5.0.0
resolution: "path-exists@npm:5.0.0"
checksum: 8ca842868cab09423994596eb2c5ec2a971c17d1a3cb36dbf060592c730c725cd524b9067d7d2a1e031fef9ba7bd2ac6dc5ec9fb92aa693265f7be3987045254
languageName: node
linkType: hard

"path-is-absolute@npm:^1.0.0":
version: 1.0.1
resolution: "path-is-absolute@npm:1.0.1"
Expand Down Expand Up @@ -3895,15 +3834,6 @@ __metadata:
languageName: node
linkType: hard

"pkg-dir@npm:^7.0.0":
version: 7.0.0
resolution: "pkg-dir@npm:7.0.0"
dependencies:
find-up: ^6.3.0
checksum: 94298b20a446bfbbd66604474de8a0cdd3b8d251225170970f15d9646f633e056c80520dd5b4c1d1050c9fed8f6a9e5054b141c93806439452efe72e57562c03
languageName: node
linkType: hard

"prelude-ls@npm:^1.2.1":
version: 1.2.1
resolution: "prelude-ls@npm:1.2.1"
Expand Down Expand Up @@ -4772,10 +4702,3 @@ __metadata:
checksum: f77b3d8d00310def622123df93d4ee654fc6a0096182af8bd60679ddcdfb3474c56c6c7190817c84a2785648cdee9d721c0154eb45698c62176c322fb46fc700
languageName: node
linkType: hard

"yocto-queue@npm:^1.0.0":
version: 1.1.1
resolution: "yocto-queue@npm:1.1.1"
checksum: f2e05b767ed3141e6372a80af9caa4715d60969227f38b1a4370d60bffe153c9c5b33a862905609afc9b375ec57cd40999810d20e5e10229a204e8bde7ef255c
languageName: node
linkType: hard

0 comments on commit 3b9da9b

Please sign in to comment.