Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions packages/cli-kit/src/public/node/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {relativizePath, normalizePath, cwd, sniffForPath, commonParentDirectory} from './path.js'
import {describe, test, expect} from 'vitest'
import {relativizePath, normalizePath, cwd, sniffForPath, commonParentDirectory, _resetCwd} from './path.js'
import {describe, test, expect, vi, beforeEach} from 'vitest'

describe('relativize', () => {
test('relativizes the path', () => {
Expand All @@ -16,13 +16,42 @@ describe('relativize', () => {
})

describe('cwd', () => {
beforeEach(() => {
_resetCwd()
})

test.runIf(process.env.INIT_CWD)('returns the initial cwd where the command has been called', () => {
// Given
const path = cwd()

// Then
expect(path).toStrictEqual(normalizePath(process.env.INIT_CWD!))
})

test('memoizes the result', () => {
// Given
const processCwdSpy = vi.spyOn(process, 'cwd').mockReturnValue('/computed/path')
const originalInitCwd = process.env.INIT_CWD
delete process.env.INIT_CWD

// When
const firstCall = cwd()
const secondCall = cwd()

// Then
expect(firstCall).toBe(normalizePath('/computed/path'))
expect(secondCall).toBe(normalizePath('/computed/path'))
expect(processCwdSpy).toHaveBeenCalledTimes(1)

// Resetting
_resetCwd()
cwd()
expect(processCwdSpy).toHaveBeenCalledTimes(2)

// Restore
process.env.INIT_CWD = originalInitCwd
processCwdSpy.mockRestore()
})
})

describe('commonParentDirectory', () => {
Expand Down
17 changes: 16 additions & 1 deletion packages/cli-kit/src/public/node/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ export function moduleDirectory(moduleURL: string | URL): string {
return dirname(fileURLToPath(moduleURL))
}

/**
* Memoized value for the current working directory.
*/
let memoizedCwd: string | undefined

/**
* When running a script using `npm run`, something interesting happens. If the current
* folder does not have a `package.json` or a `node_modules` folder, npm will traverse
Expand All @@ -175,8 +180,18 @@ export function moduleDirectory(moduleURL: string | URL): string {
* @returns The path to the current working directory.
*/
export function cwd(): string {
if (memoizedCwd) return memoizedCwd
// eslint-disable-next-line @shopify/cli/no-process-cwd
return normalize(process.env.INIT_CWD || process.cwd()) // eslint-disable-line @typescript-eslint/prefer-nullish-coalescing -- empty env var should fall through
memoizedCwd = normalize(process.env.INIT_CWD || process.cwd()) // eslint-disable-line @typescript-eslint/prefer-nullish-coalescing -- empty env var should fall through
return memoizedCwd
}

/**
* Resets the memoized value of the current working directory.
* This is useful for testing purposes.
*/
export function _resetCwd(): void {
memoizedCwd = undefined
}

/**
Expand Down
Loading