Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ export type OnlineStoreThemeFilesUserErrorsCode =
| 'LESS_THAN_OR_EQUAL_TO'
/** The record with the ID used as the input value couldn't be found. */
| 'NOT_FOUND'
/** Theme contextualization and condition types are not compatible with each other. */
| 'THEME_CONTEXTUALIZATION_NOT_COMPATIBLE_WITH_CONDITION_TYPES'
/** There are theme files with conflicts. */
| 'THEME_FILES_CONFLICT'
/** This action is not available on your current plan. Please upgrade to access theme editing features. */
Expand Down
47 changes: 45 additions & 2 deletions packages/cli-kit/src/public/node/os.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,51 @@
import {platformAndArch} from './os.js'
import {describe, test, expect, vi} from 'vitest'
import {platformAndArch, username, _resetUsernameCache} from './os.js'
import {describe, test, expect, vi, beforeEach} from 'vitest'

vi.mock('node:process')

describe('username', () => {
beforeEach(() => {
_resetUsernameCache()
})

test('memoizes the username', async () => {
// Given
const platform = process.platform
const firstPromise = username(platform)

// When
const secondPromise = username(platform)

// Then
expect(firstPromise).toBe(secondPromise)
await expect(firstPromise).resolves.toBeDefined()
})

test('returns different promises for different platforms', async () => {
// Given
const firstPromise = username('darwin')

// When
const secondPromise = username('win32')

// Then
expect(firstPromise).not.toBe(secondPromise)
})

test('resets the cache', async () => {
// Given
const platform = process.platform
const firstPromise = username(platform)
_resetUsernameCache()

// When
const secondPromise = username(platform)

// Then
expect(firstPromise).not.toBe(secondPromise)
})
})

describe('platformAndArch', () => {
test("returns the right architecture when it's x64", () => {
// When
Expand Down
18 changes: 17 additions & 1 deletion packages/cli-kit/src/public/node/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,27 @@ import {userInfo as osUserInfo} from 'os'
// This code has been vendored from https://github.com/sindresorhus/username
// because adding it as a transtive dependency causes conflicts with other
// packages that haven't been yet migrated to the latest version.
let usernamePromise: Promise<string | null> | undefined

/**
* @param platform - The platform to get the username for. Defaults to the current platform.
* @returns The username of the current user.
*/
export async function username(platform: typeof process.platform = process.platform): Promise<string | null> {
export function username(platform: typeof process.platform = process.platform): Promise<string | null> {
if (platform !== process.platform) {
return fetchUsername(platform)
}
return (usernamePromise ??= fetchUsername(platform))
}

/**
* Resets the username cache.
*/
export function _resetUsernameCache() {
usernamePromise = undefined
}

async function fetchUsername(platform: typeof process.platform = process.platform): Promise<string | null> {
outputDebug(outputContent`Obtaining user name...`)
const environmentVariable = getEnvironmentVariable()
if (environmentVariable) {
Expand Down
Loading