Skip to content

Commit

Permalink
refactor: use os.temp() instead of find-cache-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Nov 22, 2023
1 parent caf0628 commit 85776a0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 80 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"packages/*"
],
"dependencies": {
"@types/find-cache-dir": "^3.2.1",
"@types/fs-extra": "^11.0.1",
"@types/lodash-es": "^4.17.8",
"@types/semver": "^7.5.0",
Expand All @@ -80,7 +79,6 @@
"chalk": "^5.3.0",
"commander": "^10.0.1",
"fast-glob": "^3.3.1",
"find-cache-dir": "^5.0.0",
"fs-extra": "^11.1.1",
"js-yaml": "^4.1.0",
"lodash-es": "^4.17.21",
Expand Down
14 changes: 7 additions & 7 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import crypto from 'node:crypto'
import { createRequire } from 'node:module'
import path, { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import os from 'node:os'

import chalk from 'chalk'
import findCacheDir from 'find-cache-dir'
import fse, { SymlinkType } from 'fs-extra'
import fg, { Options as GlobOptions } from 'fast-glob'
import yaml from 'js-yaml'
Expand Down Expand Up @@ -166,18 +166,18 @@ export const ensureDir = (dir: string): string => {
return dir
}

export const getTemp = (cwd: string, temp?: string): string => {
export const getTemp = (cwd: string, temp?: string) => {
if (temp) {
return ensureDir(resolve(temp))
return ensureDir(resolve(cwd, temp))
}

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

return ensureDir(tempDir)
return _temp
}


export const attempt = <T>(f: () => T): T | null => {
try {
return f()
Expand Down
12 changes: 7 additions & 5 deletions src/test/ts/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jest.mock('fs-extra')
jest.mock('synp')

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

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

const temp = findCacheDir({ name: 'yarn-audit-fix', create: true }) + ''
const cwd = process.cwd()

const temp = resolve(__dirname, '../../../.temp')
const stdio = ['inherit', 'inherit', 'inherit']
const stdionull = [null, null, null] // eslint-disable-line

Expand Down Expand Up @@ -142,7 +142,7 @@ describe('yarn-audit-fix', () => {
// eslint-disable-next-line
const checkTempAssets = () => {
// Preparing...
expect(fs.emptyDirSync).toHaveBeenCalledWith(expect.stringMatching(temp))
expect(fs.emptyDirSync).toHaveBeenCalledWith(temp)
expect(fs.copyFileSync).toHaveBeenCalledWith(
join(cwd, 'yarn.lock'),
strMatching(temp, 'yarn.lock'),
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('yarn-audit-fix', () => {
checkTempAssets()

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

// Applying npm audit fix...
Expand Down Expand Up @@ -240,6 +240,7 @@ describe('yarn-audit-fix', () => {
it('invokes cmd queue with proper args', async () => {
await run({
flow: 'patch',
temp
})

checkTempAssets()
Expand All @@ -250,7 +251,7 @@ describe('yarn-audit-fix', () => {
expect(cp.spawnSync).toHaveBeenCalledWith(
getYarn(),
['audit', '--json'],
{ cwd: strMatching(temp), stdio: stdionull },
{ cwd: temp, stdio: stdionull },
)
expect(lfPatch).toHaveBeenCalledTimes(1)
expect(lfFormat).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -281,6 +282,7 @@ describe('yarn-audit-fix', () => {
registry: registryUrl,
flow: 'convert',
ignoreEngines: true,
temp,
})
checkConvertFlow()
})
Expand Down
4 changes: 2 additions & 2 deletions src/test/ts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ describe('util', () => {
it('properly resolves temp dir path', () => {
const pwd = process.cwd()
const tempdir = resolve(__dirname, '../temp')
const cases: [string, string | undefined, string][] = [
const cases: [string, string | undefined, string | RegExp][] = [
[pwd, tempdir, tempdir],
[pwd, undefined, resolve(pwd, 'node_modules/.cache/yarn-audit-fix')],
[pwd, undefined, /tempy-/],
]

cases.forEach(([cwd, temp, result]) => {
Expand Down
64 changes: 0 additions & 64 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1070,11 +1070,6 @@
dependencies:
"@babel/types" "^7.20.7"

"@types/find-cache-dir@^3.2.1":
version "3.2.1"
resolved "https://registry.yarnpkg.com/@types/find-cache-dir/-/find-cache-dir-3.2.1.tgz#7b959a4b9643a1e6a1a5fe49032693cc36773501"
integrity sha512-frsJrz2t/CeGifcu/6uRo4b+SzAwT4NYCVPu1GN8IB9XTzrpPkGuV0tmh9mN+/L0PklAlsC3u5Fxt0ju00LXIw==

"@types/fs-extra@^11.0.1":
version "11.0.4"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-11.0.4.tgz#e16a863bb8843fba8c5004362b5a73e17becca45"
Expand Down Expand Up @@ -1962,11 +1957,6 @@ common-ancestor-path@^1.0.1:
resolved "https://registry.yarnpkg.com/common-ancestor-path/-/common-ancestor-path-1.0.1.tgz#4f7d2d1394d91b7abdf51871c62f71eadb0182a7"
integrity sha512-L3sHRo1pXXEqX8VU28kfgUY+YGsk09hPqZiZmLacNib6XNTCM8ubYeT7ryXQw8asB1sKgcU5lkB7ONug08aB8w==

common-path-prefix@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/common-path-prefix/-/common-path-prefix-3.0.0.tgz#7d007a7e07c58c4b4d5f433131a19141b29f11e0"
integrity sha512-QE33hToZseCH3jS0qN96O/bSh3kaw/h+Tq7ngyY9eWDUnTlTNUyqfqvCXioLe5Na5jFsL78ra/wuBU4iuEgd4w==

component-emitter@^1.2.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.1.tgz#ef1d5796f7d93f135ee6fb684340b26403c97d17"
Expand Down Expand Up @@ -2675,14 +2665,6 @@ fill-range@^7.0.1:
dependencies:
to-regex-range "^5.0.1"

find-cache-dir@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-5.0.0.tgz#69d1a9b77bbe39aea078dbede99d277a170d3079"
integrity sha512-OuWNfjfP05JcpAP3JPgAKUhWefjMRfI5iAoSsvE24ANYWJaepAtlSgWECSVEuRgSXpyNEc9DJwG/TZpgcOqyig==
dependencies:
common-path-prefix "^3.0.0"
pkg-dir "^7.0.0"

find-up@^4.0.0, find-up@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19"
Expand All @@ -2699,14 +2681,6 @@ find-up@^5.0.0:
locate-path "^6.0.0"
path-exists "^4.0.0"

find-up@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-6.3.0.tgz#2abab3d3280b2dc7ac10199ef324c4e002c8c790"
integrity sha512-v2ZsoEuVHYy8ZIlYqwPe/39Cy+cFDzp4dXPaxNvkEuouymu+2Jbz0PxpKarJHYJTmv2HWT3O382qY8l4jMWthw==
dependencies:
locate-path "^7.1.0"
path-exists "^5.0.0"

flat-cache@^3.0.4:
version "3.2.0"
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.2.0.tgz#2c0c2d5040c99b1632771a9d105725c0115363ee"
Expand Down Expand Up @@ -4099,13 +4073,6 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

locate-path@^7.1.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-7.2.0.tgz#69cb1779bd90b35ab1e771e1f2f89a202c2a8a8a"
integrity sha512-gvVijfZvn7R+2qyPX8mAuKcFGDf6Nc61GdvGafQsHL0sBIxfKzA+usWn4GFC/bk+QdwPUD4kWFJLhElipq+0VA==
dependencies:
p-locate "^6.0.0"

lodash-es@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
Expand Down Expand Up @@ -4791,13 +4758,6 @@ p-limit@^3.0.2, p-limit@^3.1.0:
dependencies:
yocto-queue "^0.1.0"

p-limit@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-4.0.0.tgz#914af6544ed32bfa54670b061cafcbd04984b644"
integrity sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ==
dependencies:
yocto-queue "^1.0.0"

p-locate@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07"
Expand All @@ -4812,13 +4772,6 @@ p-locate@^5.0.0:
dependencies:
p-limit "^3.0.2"

p-locate@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-6.0.0.tgz#3da9a49d4934b901089dca3302fa65dc5a05c04f"
integrity sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw==
dependencies:
p-limit "^4.0.0"

p-map@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/p-map/-/p-map-4.0.0.tgz#bb2f95a5eda2ec168ec9274e06a747c3e2904d2b"
Expand Down Expand Up @@ -4903,11 +4856,6 @@ path-exists@^4.0.0:
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==

path-exists@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-5.0.0.tgz#a6aad9489200b21fab31e49cf09277e5116fb9e7"
integrity sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ==

path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
Expand Down Expand Up @@ -4958,13 +4906,6 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

pkg-dir@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-7.0.0.tgz#8f0c08d6df4476756c5ff29b3282d0bab7517d11"
integrity sha512-Ie9z/WINcxxLp27BKOCHGde4ITq9UklYKDzVo1nhk5sqGEXU3FpkwP5GM2voTGJkGd9B3Otl+Q4uwSOeSUtOBA==
dependencies:
find-up "^6.3.0"

pluralize@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-8.0.0.tgz#1a6fa16a38d12a1901e0320fa017051c539ce3b1"
Expand Down Expand Up @@ -6180,8 +6121,3 @@ yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

yocto-queue@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==

0 comments on commit 85776a0

Please sign in to comment.