Skip to content

Commit 85776a0

Browse files
committed
refactor: use os.temp() instead of find-cache-dir
1 parent caf0628 commit 85776a0

File tree

5 files changed

+16
-80
lines changed

5 files changed

+16
-80
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
"packages/*"
7272
],
7373
"dependencies": {
74-
"@types/find-cache-dir": "^3.2.1",
7574
"@types/fs-extra": "^11.0.1",
7675
"@types/lodash-es": "^4.17.8",
7776
"@types/semver": "^7.5.0",
@@ -80,7 +79,6 @@
8079
"chalk": "^5.3.0",
8180
"commander": "^10.0.1",
8281
"fast-glob": "^3.3.1",
83-
"find-cache-dir": "^5.0.0",
8482
"fs-extra": "^11.1.1",
8583
"js-yaml": "^4.1.0",
8684
"lodash-es": "^4.17.21",

src/main/ts/util.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import crypto from 'node:crypto'
33
import { createRequire } from 'node:module'
44
import path, { dirname, join, resolve } from 'node:path'
55
import { fileURLToPath } from 'node:url'
6+
import os from 'node:os'
67

78
import chalk from 'chalk'
8-
import findCacheDir from 'find-cache-dir'
99
import fse, { SymlinkType } from 'fs-extra'
1010
import fg, { Options as GlobOptions } from 'fast-glob'
1111
import yaml from 'js-yaml'
@@ -166,18 +166,18 @@ export const ensureDir = (dir: string): string => {
166166
return dir
167167
}
168168

169-
export const getTemp = (cwd: string, temp?: string): string => {
169+
export const getTemp = (cwd: string, temp?: string) => {
170170
if (temp) {
171-
return ensureDir(resolve(temp))
171+
return ensureDir(resolve(cwd, temp))
172172
}
173173

174-
const id = crypto.randomBytes(16).toString('hex')
175-
const cacheDir = findCacheDir({ name: 'yarn-audit-fix', cwd }) + ''
176-
const tempDir = join(cacheDir, id)
174+
const _temp = path.join(os.tmpdir(), `tempy-${crypto.randomBytes(16).toString('hex')}`)
175+
fse.mkdtempSync(_temp) // Hmm... should return string, but returns undefined
177176

178-
return ensureDir(tempDir)
177+
return _temp
179178
}
180179

180+
181181
export const attempt = <T>(f: () => T): T | null => {
182182
try {
183183
return f()

src/test/ts/runner.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jest.mock('fs-extra')
1111
jest.mock('synp')
1212

1313
const cp = createRequire(import.meta.url)('child_process')
14-
const findCacheDir = (await import('find-cache-dir')).default
1514
const fs = (await import('fs-extra')).default
1615
const synp = (await import('synp')).default
1716

@@ -55,8 +54,9 @@ const audit = revive(readFixture('lockfile/legacy/yarn-audit-report.json'))
5554
const yarnLockBefore = readFixture('lockfile/legacy/yarn.lock.before')
5655
const yarnLockAfter = readFixture('lockfile/legacy/yarn.lock.after')
5756

58-
const temp = findCacheDir({ name: 'yarn-audit-fix', create: true }) + ''
5957
const cwd = process.cwd()
58+
59+
const temp = resolve(__dirname, '../../../.temp')
6060
const stdio = ['inherit', 'inherit', 'inherit']
6161
const stdionull = [null, null, null] // eslint-disable-line
6262

@@ -142,7 +142,7 @@ describe('yarn-audit-fix', () => {
142142
// eslint-disable-next-line
143143
const checkTempAssets = () => {
144144
// Preparing...
145-
expect(fs.emptyDirSync).toHaveBeenCalledWith(expect.stringMatching(temp))
145+
expect(fs.emptyDirSync).toHaveBeenCalledWith(temp)
146146
expect(fs.copyFileSync).toHaveBeenCalledWith(
147147
join(cwd, 'yarn.lock'),
148148
strMatching(temp, 'yarn.lock'),
@@ -170,7 +170,7 @@ describe('yarn-audit-fix', () => {
170170
checkTempAssets()
171171

172172
// Generating package-lock.json from yarn.lock...
173-
expect(synp.yarnToNpm).toHaveBeenCalledWith(strMatching(temp), true)
173+
expect(synp.yarnToNpm).toHaveBeenCalledWith(temp, true)
174174
expect(fs.removeSync).toHaveBeenCalledWith(strMatching(temp, 'yarn.lock'))
175175

176176
// Applying npm audit fix...
@@ -240,6 +240,7 @@ describe('yarn-audit-fix', () => {
240240
it('invokes cmd queue with proper args', async () => {
241241
await run({
242242
flow: 'patch',
243+
temp
243244
})
244245

245246
checkTempAssets()
@@ -250,7 +251,7 @@ describe('yarn-audit-fix', () => {
250251
expect(cp.spawnSync).toHaveBeenCalledWith(
251252
getYarn(),
252253
['audit', '--json'],
253-
{ cwd: strMatching(temp), stdio: stdionull },
254+
{ cwd: temp, stdio: stdionull },
254255
)
255256
expect(lfPatch).toHaveBeenCalledTimes(1)
256257
expect(lfFormat).toHaveBeenCalledTimes(1)
@@ -281,6 +282,7 @@ describe('yarn-audit-fix', () => {
281282
registry: registryUrl,
282283
flow: 'convert',
283284
ignoreEngines: true,
285+
temp,
284286
})
285287
checkConvertFlow()
286288
})

src/test/ts/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ describe('util', () => {
138138
it('properly resolves temp dir path', () => {
139139
const pwd = process.cwd()
140140
const tempdir = resolve(__dirname, '../temp')
141-
const cases: [string, string | undefined, string][] = [
141+
const cases: [string, string | undefined, string | RegExp][] = [
142142
[pwd, tempdir, tempdir],
143-
[pwd, undefined, resolve(pwd, 'node_modules/.cache/yarn-audit-fix')],
143+
[pwd, undefined, /tempy-/],
144144
]
145145

146146
cases.forEach(([cwd, temp, result]) => {

yarn.lock

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,11 +1070,6 @@
10701070
dependencies:
10711071
"@babel/types" "^7.20.7"
10721072

1073-
"@types/find-cache-dir@^3.2.1":
1074-
version "3.2.1"
1075-
resolved "https://registry.yarnpkg.com/@types/find-cache-dir/-/find-cache-dir-3.2.1.tgz#7b959a4b9643a1e6a1a5fe49032693cc36773501"
1076-
integrity sha512-frsJrz2t/CeGifcu/6uRo4b+SzAwT4NYCVPu1GN8IB9XTzrpPkGuV0tmh9mN+/L0PklAlsC3u5Fxt0ju00LXIw==
1077-
10781073
"@types/fs-extra@^11.0.1":
10791074
version "11.0.4"
10801075
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-11.0.4.tgz#e16a863bb8843fba8c5004362b5a73e17becca45"
@@ -1962,11 +1957,6 @@ common-ancestor-path@^1.0.1:
19621957
resolved "https://registry.yarnpkg.com/common-ancestor-path/-/common-ancestor-path-1.0.1.tgz#4f7d2d1394d91b7abdf51871c62f71eadb0182a7"
19631958
integrity sha512-L3sHRo1pXXEqX8VU28kfgUY+YGsk09hPqZiZmLacNib6XNTCM8ubYeT7ryXQw8asB1sKgcU5lkB7ONug08aB8w==
19641959

1965-
common-path-prefix@^3.0.0:
1966-
version "3.0.0"
1967-
resolved "https://registry.yarnpkg.com/common-path-prefix/-/common-path-prefix-3.0.0.tgz#7d007a7e07c58c4b4d5f433131a19141b29f11e0"
1968-
integrity sha512-QE33hToZseCH3jS0qN96O/bSh3kaw/h+Tq7ngyY9eWDUnTlTNUyqfqvCXioLe5Na5jFsL78ra/wuBU4iuEgd4w==
1969-
19701960
component-emitter@^1.2.1:
19711961
version "1.3.1"
19721962
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.1.tgz#ef1d5796f7d93f135ee6fb684340b26403c97d17"
@@ -2675,14 +2665,6 @@ fill-range@^7.0.1:
26752665
dependencies:
26762666
to-regex-range "^5.0.1"
26772667

2678-
find-cache-dir@^5.0.0:
2679-
version "5.0.0"
2680-
resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-5.0.0.tgz#69d1a9b77bbe39aea078dbede99d277a170d3079"
2681-
integrity sha512-OuWNfjfP05JcpAP3JPgAKUhWefjMRfI5iAoSsvE24ANYWJaepAtlSgWECSVEuRgSXpyNEc9DJwG/TZpgcOqyig==
2682-
dependencies:
2683-
common-path-prefix "^3.0.0"
2684-
pkg-dir "^7.0.0"
2685-
26862668
find-up@^4.0.0, find-up@^4.1.0:
26872669
version "4.1.0"
26882670
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19"
@@ -2699,14 +2681,6 @@ find-up@^5.0.0:
26992681
locate-path "^6.0.0"
27002682
path-exists "^4.0.0"
27012683

2702-
find-up@^6.3.0:
2703-
version "6.3.0"
2704-
resolved "https://registry.yarnpkg.com/find-up/-/find-up-6.3.0.tgz#2abab3d3280b2dc7ac10199ef324c4e002c8c790"
2705-
integrity sha512-v2ZsoEuVHYy8ZIlYqwPe/39Cy+cFDzp4dXPaxNvkEuouymu+2Jbz0PxpKarJHYJTmv2HWT3O382qY8l4jMWthw==
2706-
dependencies:
2707-
locate-path "^7.1.0"
2708-
path-exists "^5.0.0"
2709-
27102684
flat-cache@^3.0.4:
27112685
version "3.2.0"
27122686
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.2.0.tgz#2c0c2d5040c99b1632771a9d105725c0115363ee"
@@ -4099,13 +4073,6 @@ locate-path@^6.0.0:
40994073
dependencies:
41004074
p-locate "^5.0.0"
41014075

4102-
locate-path@^7.1.0:
4103-
version "7.2.0"
4104-
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-7.2.0.tgz#69cb1779bd90b35ab1e771e1f2f89a202c2a8a8a"
4105-
integrity sha512-gvVijfZvn7R+2qyPX8mAuKcFGDf6Nc61GdvGafQsHL0sBIxfKzA+usWn4GFC/bk+QdwPUD4kWFJLhElipq+0VA==
4106-
dependencies:
4107-
p-locate "^6.0.0"
4108-
41094076
lodash-es@^4.17.21:
41104077
version "4.17.21"
41114078
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
@@ -4791,13 +4758,6 @@ p-limit@^3.0.2, p-limit@^3.1.0:
47914758
dependencies:
47924759
yocto-queue "^0.1.0"
47934760

4794-
p-limit@^4.0.0:
4795-
version "4.0.0"
4796-
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-4.0.0.tgz#914af6544ed32bfa54670b061cafcbd04984b644"
4797-
integrity sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ==
4798-
dependencies:
4799-
yocto-queue "^1.0.0"
4800-
48014761
p-locate@^4.1.0:
48024762
version "4.1.0"
48034763
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07"
@@ -4812,13 +4772,6 @@ p-locate@^5.0.0:
48124772
dependencies:
48134773
p-limit "^3.0.2"
48144774

4815-
p-locate@^6.0.0:
4816-
version "6.0.0"
4817-
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-6.0.0.tgz#3da9a49d4934b901089dca3302fa65dc5a05c04f"
4818-
integrity sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw==
4819-
dependencies:
4820-
p-limit "^4.0.0"
4821-
48224775
p-map@^4.0.0:
48234776
version "4.0.0"
48244777
resolved "https://registry.yarnpkg.com/p-map/-/p-map-4.0.0.tgz#bb2f95a5eda2ec168ec9274e06a747c3e2904d2b"
@@ -4903,11 +4856,6 @@ path-exists@^4.0.0:
49034856
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
49044857
integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==
49054858

4906-
path-exists@^5.0.0:
4907-
version "5.0.0"
4908-
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-5.0.0.tgz#a6aad9489200b21fab31e49cf09277e5116fb9e7"
4909-
integrity sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ==
4910-
49114859
path-is-absolute@^1.0.0:
49124860
version "1.0.1"
49134861
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
@@ -4958,13 +4906,6 @@ pkg-dir@^4.2.0:
49584906
dependencies:
49594907
find-up "^4.0.0"
49604908

4961-
pkg-dir@^7.0.0:
4962-
version "7.0.0"
4963-
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-7.0.0.tgz#8f0c08d6df4476756c5ff29b3282d0bab7517d11"
4964-
integrity sha512-Ie9z/WINcxxLp27BKOCHGde4ITq9UklYKDzVo1nhk5sqGEXU3FpkwP5GM2voTGJkGd9B3Otl+Q4uwSOeSUtOBA==
4965-
dependencies:
4966-
find-up "^6.3.0"
4967-
49684909
pluralize@^8.0.0:
49694910
version "8.0.0"
49704911
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-8.0.0.tgz#1a6fa16a38d12a1901e0320fa017051c539ce3b1"
@@ -6180,8 +6121,3 @@ yocto-queue@^0.1.0:
61806121
version "0.1.0"
61816122
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
61826123
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
6183-
6184-
yocto-queue@^1.0.0:
6185-
version "1.0.0"
6186-
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
6187-
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==

0 commit comments

Comments
 (0)