Skip to content

Commit

Permalink
feat: use separate temp dir for each exec context
Browse files Browse the repository at this point in the history
closes #45
  • Loading branch information
antongolub committed Feb 21, 2021
1 parent c6c51cd commit 7f24b51
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
11 changes: 7 additions & 4 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ const createTempAssets: TCallback = ({ temp }) => {
* @param {TContext} cxt
* @return {void}
*/
const createSymlinks: TCallback = ({ temp, flags, cwd, manifest }) => {
export const createSymlinks: TCallback = ({ temp, flags, cwd, manifest }) => {
const symlinkType = getSymlinkType(flags.symlink)
const workspaces = getWorkspaces(cwd, manifest)
const links = [join(cwd, 'node_modules'), ...workspaces]
const links = [
join(cwd, 'node_modules'),
...workspaces.map((ws) => dirname(ws)),
]

links.forEach((pkgPath: string) => {
const rel = dirname(relative(pkgPath, cwd))
links.forEach((link: string) => {
const rel = relative(cwd, link)
const from = join(cwd, rel)
const to = join(temp, rel)

Expand Down
20 changes: 14 additions & 6 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import chalk from 'chalk'
import cp, { StdioOptions } from 'child_process'
import crypto from 'crypto'
import findCacheDir from 'find-cache-dir'
import { sync as findUp } from 'find-up'
import fs, { ensureDirSync, readFileSync, SymlinkType } from 'fs-extra'
import { GlobbyOptions } from 'globby'
import minimist from 'minimist'
import { resolve } from 'path'
import { join, resolve } from 'path'
import { sync as pkgDir } from 'pkg-dir'

import { glob } from './glob'
Expand Down Expand Up @@ -137,13 +138,20 @@ export const getWorkspaces = (
export const readJson = (path: string): any =>
JSON.parse(readFileSync(path).toString('utf-8').trim())

export const ensureDir = (dir: string): string => {
ensureDirSync(dir)

return dir
}

export const getTemp = (cwd: string, temp?: string): string => {
if (temp) {
const _temp = resolve(temp)
ensureDirSync(_temp)

return _temp
return ensureDir(resolve(temp))
}

return findCacheDir({ name: 'yarn-audit-fix', create: true, cwd }) + ''
const id = crypto.randomBytes(16).toString('hex')
const cacheDir = findCacheDir({ name: 'yarn-audit-fix', cwd }) + ''
const tempDir = join(cacheDir, id)

return ensureDir(tempDir)
}
49 changes: 36 additions & 13 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import cp from 'child_process'
import findCacheDir from 'find-cache-dir'
import fs from 'fs-extra'
import { factory as iop } from 'inside-out-promise'
import { join } from 'path'
import { join, resolve } from 'path'
import synp from 'synp'

import { run } from '../../main/ts'
import { createSymlinks, run } from '../../main/ts'
import { getNpm, getYarn } from '../../main/ts/util'

jest.mock('child_process')
Expand All @@ -14,6 +14,8 @@ jest.mock('npm')
jest.mock('synp')

const registryUrl = 'https://example.com'
const strMatching = (temp: string, ending: string) =>
expect.stringMatching(new RegExp(`${temp}.+${ending}`))

describe('yarn-audit-fix', () => {
beforeEach(() => {
Expand Down Expand Up @@ -51,6 +53,27 @@ describe('yarn-audit-fix', () => {
afterEach(jest.clearAllMocks)
afterAll(jest.resetAllMocks)

describe('createSymlinks', () => {
it('establishes proper links', () => {
const temp = 'foo/bar'
const cwd = resolve(__dirname, '../fixtures/regular-monorepo')
const manifest = {
workspaces: ['packages/*'],
}

createSymlinks({ temp, flags: {}, cwd, manifest })

const links = ['node_modules', 'packages/a', 'packages/b']
links.forEach((link) => {
expect(fs.createSymlinkSync).toHaveBeenCalledWith(
join(cwd, link),
strMatching(temp, link),
'dir',
)
})
})
})

describe('flow', () => {
// eslint-disable-next-line
const checkFlow = (skipPkgLockOnly?: boolean) => {
Expand All @@ -59,31 +82,31 @@ describe('yarn-audit-fix', () => {
const stdio = ['inherit', 'inherit', 'inherit']

// Preparing...
expect(fs.emptyDirSync).toHaveBeenCalledWith(temp)
expect(fs.emptyDirSync).toHaveBeenCalledWith(expect.stringMatching(temp))
expect(fs.copyFileSync).toHaveBeenCalledWith(
'yarn.lock',
join(temp, 'yarn.lock'),
strMatching(temp, 'yarn.lock'),
)
expect(fs.copyFileSync).toHaveBeenCalledWith(
'package.json',
join(temp, 'package.json'),
strMatching(temp, 'package.json'),
)
expect(fs.copyFileSync).toHaveBeenCalledWith(
'.yarnrc',
join(temp, '.yarnrc'),
strMatching(temp, '.yarnrc'),
)
expect(fs.copyFileSync).toHaveBeenCalledWith(
'.npmrc',
join(temp, '.npmrc'),
strMatching(temp, '.npmrc'),
)
expect(fs.createSymlinkSync).toHaveBeenCalledWith(
join(cwd, 'node_modules'),
join(temp, 'node_modules'),
strMatching(temp, 'node_modules'),
'dir',
)

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

// Applying npm audit fix...
expect(cp.spawnSync).toHaveBeenCalledWith(
Expand All @@ -96,22 +119,22 @@ describe('yarn-audit-fix', () => {
'--registry',
registryUrl,
'--prefix',
temp,
expect.stringMatching(temp),
].filter((v) => v !== undefined),
{ cwd: temp, stdio },
{ cwd: expect.stringMatching(temp), stdio },
)

// Updating yarn.lock from package-lock.json...
expect(fs.copyFileSync).toHaveBeenCalledWith(
join(temp, 'yarn.lock'),
strMatching(temp, 'yarn.lock'),
'yarn.lock',
)
expect(cp.spawnSync).toHaveBeenCalledWith(
getYarn(),
['--update-checksums', '--verbose', '--registry', registryUrl],
{ cwd, stdio },
)
expect(fs.emptyDirSync).toHaveBeenCalledWith(temp)
expect(fs.emptyDirSync).toHaveBeenCalledWith(expect.stringMatching(temp))
}

describe('runner', () => {
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 @@ -85,12 +85,12 @@ describe('util', () => {
const pwd = process.cwd()
const tempdir = resolve(__dirname, '../temp')
const cases: [string, string | undefined, string][] = [
[pwd, undefined, resolve(pwd, 'node_modules/.cache/yarn-audit-fix')],
[pwd, tempdir, tempdir],
[pwd, undefined, resolve(pwd, 'node_modules/.cache/yarn-audit-fix')],
]

cases.forEach(([cwd, temp, result]) => {
expect(getTemp(cwd, temp)).toBe(result)
expect(getTemp(cwd, temp)).toMatch(result)
})
})
})
Expand Down

0 comments on commit 7f24b51

Please sign in to comment.