diff --git a/packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts b/packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts index 2da991ce827..9ce2584b9d6 100644 --- a/packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts +++ b/packages/cli-kit/src/cli/api/graphql/admin/generated/types.d.ts @@ -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. */ diff --git a/packages/cli-kit/src/public/node/os.test.ts b/packages/cli-kit/src/public/node/os.test.ts index 0e10a277d7a..11deba27ab2 100644 --- a/packages/cli-kit/src/public/node/os.test.ts +++ b/packages/cli-kit/src/public/node/os.test.ts @@ -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 diff --git a/packages/cli-kit/src/public/node/os.ts b/packages/cli-kit/src/public/node/os.ts index 139f798cbb0..560d12d65d0 100644 --- a/packages/cli-kit/src/public/node/os.ts +++ b/packages/cli-kit/src/public/node/os.ts @@ -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 | 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 { +export function username(platform: typeof process.platform = process.platform): Promise { + 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 { outputDebug(outputContent`Obtaining user name...`) const environmentVariable = getEnvironmentVariable() if (environmentVariable) {