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
5 changes: 2 additions & 3 deletions packages/@sanity/cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,20 @@
"dependencies": {
"@inquirer/prompts": "^8.2.0",
"@oclif/core": "catalog:",
"@rexxars/jiti": "^2.6.2",
"@rexxars/jiti": "catalog:",
"@sanity/client": "catalog:",
"@sanity/types": "catalog:",
"babel-plugin-react-compiler": "^1.0.0",
"boxen": "^8.0.1",
"configstore": "^7.0.0",
"debug": "catalog:",
"get-it": "^8.7.0",
"read-package-up": "catalog:",
"get-tsconfig": "catalog:",
"import-meta-resolve": "catalog:",
"jsdom": "catalog:",
"json-lexer": "^1.2.0",
"log-symbols": "^7.0.1",
"ora": "catalog:",
"read-package-up": "catalog:",
"rxjs": "catalog:",
"tinyglobby": "catalog:",
"tsx": "catalog:",
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/cli-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export * from './config/util/findStudioConfigPath.js'
export {type ProjectRootResult} from './config/util/recursivelyResolveProjectRoot.js'
export * from './debug.js'
export * from './loaders/studio/studioWorkerTask.js'
export * from './loaders/tsx/tsxWorkerTask.js'
export * from './loaders/ts/tsWorkerTask.js'
export * from './SanityCommand.js'
export * from './services/apiClient.js'
export * from './services/cliUserConfig.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {importModule} from '../../util/importModule.js'

const workerScript = process.env.TS_WORKER_TASK_SCRIPT

if (workerScript) {
await importModule(workerScript)
} else {
throw new Error('`TS_WORKER_TASK_SCRIPT` not defined')
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {fileURLToPath, URL} from 'node:url'
import {Worker, type WorkerOptions} from 'node:worker_threads'

import {getTsconfig} from 'get-tsconfig'

import {type RequireProps} from '../../types.js'
import {isRecord} from '../../util/isRecord.js'
import {promisifyWorker} from '../../util/promisifyWorker.js'
Expand All @@ -12,9 +10,7 @@ import {promisifyWorker} from '../../util/promisifyWorker.js'
*
* @internal
*/
interface TsxWorkerTaskOptions extends RequireProps<WorkerOptions, 'name'> {
rootPath: string
}
type TsWorkerTaskOptions = RequireProps<WorkerOptions, 'name'>

/**
* Executes a worker file with tsx registered. This means you can import other
Expand All @@ -31,19 +27,13 @@ interface TsxWorkerTaskOptions extends RequireProps<WorkerOptions, 'name'> {
* @throws If the worker exits with a non-zero code
* @internal
*/
export function tsxWorkerTask<T = unknown>(
filePath: URL,
options: TsxWorkerTaskOptions,
): Promise<T> {
const tsconfig = getTsconfig(options.rootPath)

export function tsWorkerTask<T = unknown>(filePath: URL, options: TsWorkerTaskOptions): Promise<T> {
const env = {
...(isRecord(options.env) ? options.env : process.env),
...(tsconfig?.path ? {TSX_TSCONFIG_PATH: tsconfig.path} : {}),
TSX_WORKER_TASK_SCRIPT: fileURLToPath(filePath),
TS_WORKER_TASK_SCRIPT: fileURLToPath(filePath),
}

const worker = new Worker(new URL('tsxWorkerLoader.worker.js', import.meta.url), {
const worker = new Worker(new URL('tsWorkerLoader.worker.js', import.meta.url), {
...options,
env,
})
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions packages/@sanity/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@oclif/plugin-help": "catalog:",
"@oclif/plugin-not-found": "catalog:",
"@rexxars/gitconfiglocal": "^3.0.1",
"@rexxars/jiti": "catalog:",
"@sanity/cli-core": "workspace:*",
"@sanity/client": "catalog:",
"@sanity/codegen": "catalog:",
Expand Down
5 changes: 2 additions & 3 deletions packages/@sanity/cli/src/actions/build/renderDocument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ux} from '@oclif/core'
import {tsxWorkerTask} from '@sanity/cli-core'
import {tsWorkerTask} from '@sanity/cli-core'

import {buildDebug} from './buildDebug.js'

Expand Down Expand Up @@ -33,11 +33,10 @@ interface RenderDocumentWorkerResult {
export async function renderDocument(options: RenderDocumentOptions): Promise<string> {
buildDebug('Starting worker thread for %s', import.meta.url)
try {
const msg = await tsxWorkerTask<RenderDocumentWorkerResult>(
const msg = await tsWorkerTask<RenderDocumentWorkerResult>(
new URL(`renderDocument.worker.js`, import.meta.url),
{
name: 'renderDocument',
rootPath: options.studioRootPath,
workerData: {...options, shouldWarn: true},
},
)
Expand Down
18 changes: 9 additions & 9 deletions packages/@sanity/cli/src/actions/exec/execScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
const browserEnvPath = new URL('registerBrowserEnv.worker.js', import.meta.url).href
const configClientPath = new URL('configClient.worker.js', import.meta.url).href

// Use tsx loader for TypeScript support in the spawned child process
// We need to resolve the tsx loader path from the CLI's node_modules since the child
// process will run from the user's script directory where tsx may not be installed.
// Resolve the tsx loader using Node's module resolution relative to package.json
const tsxLoaderPath: string = import.meta.resolve('tsx', import.meta.url)
if (!tsxLoaderPath) {
throw new Error('@sanity/cli not able to resolve tsx loader')
// Use jiti loader for TypeScript support in the spawned child process
// We need to resolve the jiti loader path from the CLI's node_modules since the child
// process will run from the user's script directory where jiti may not be installed.
// Resolve the jiti loader using Node's module resolution relative to package.json
const jitiLoaderPath: string = import.meta.resolve('@rexxars/jiti', import.meta.url)
if (!jitiLoaderPath) {
throw new Error('@sanity/cli not able to resolve jiti loader')
}

const baseArgs = mockBrowserEnv
? ['--import', tsxLoaderPath, '--import', browserEnvPath]
: ['--import', tsxLoaderPath]
? ['--import', jitiLoaderPath, '--import', browserEnvPath]
: ['--import', jitiLoaderPath]
const tokenArgs = withUserToken ? ['--import', configClientPath] : []

const nodeArgs = [...baseArgs, ...tokenArgs, resolvedScriptPath, ...extraArguments]
Expand Down Expand Up @@ -68,7 +68,7 @@
process.removeListener('SIGTERM', handleSigTerm)

if (signal) reject(new Error(`Script terminated by signal: ${signal}`))
else if (code && code !== 0) reject(new Error(`Script exited with code: ${code}`))

Check failure on line 71 in packages/@sanity/cli/src/actions/exec/execScript.ts

View workflow job for this annotation

GitHub Actions / test (20, 4, 4, windows-8core)

[@sanity/cli] src/commands/__tests__/exec.test.ts > #exec > integration tests > executes script with --mock-browser-env flag

Error: Script exited with code: 1 ❯ ChildProcess.<anonymous> src/actions/exec/execScript.ts:71:43

Check failure on line 71 in packages/@sanity/cli/src/actions/exec/execScript.ts

View workflow job for this annotation

GitHub Actions / test (20, 4, 4, windows-8core)

[@sanity/cli] src/commands/__tests__/exec.test.ts > #exec > integration tests > executes script successfully

Error: Script exited with code: 1 ❯ ChildProcess.<anonymous> src/actions/exec/execScript.ts:71:43

Check failure on line 71 in packages/@sanity/cli/src/actions/exec/execScript.ts

View workflow job for this annotation

GitHub Actions / report-coverage

[@sanity/cli] src/commands/__tests__/exec.test.ts > #exec > integration tests > executes script with --mock-browser-env flag

Error: Script exited with code: 1 ❯ ChildProcess.<anonymous> src/actions/exec/execScript.ts:71:43

Check failure on line 71 in packages/@sanity/cli/src/actions/exec/execScript.ts

View workflow job for this annotation

GitHub Actions / report-coverage

[@sanity/cli] src/commands/__tests__/exec.test.ts > #exec > integration tests > executes script successfully

Error: Script exited with code: 1 ❯ ChildProcess.<anonymous> src/actions/exec/execScript.ts:71:43
else resolve()
})
proc.on('error', (err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {MediaDeployAspectCommand} from '../deploy-aspect.js'
const mockFsAccess = vi.hoisted(() => vi.fn())
const mockFsReaddir = vi.hoisted(() => vi.fn())
const mockImportModule = vi.hoisted(() => vi.fn())
const mockGetTsconfig = vi.hoisted(() => vi.fn())

vi.mock('node:fs/promises', () => ({
access: mockFsAccess,
Expand All @@ -31,10 +30,6 @@ vi.mock('@sanity/cli-core', async (importOriginal) => {
}
})

vi.mock('get-tsconfig', () => ({
getTsconfig: mockGetTsconfig,
}))

vi.mock('@sanity/cli-core/ux', async () => {
const actual = await vi.importActual<typeof import('@sanity/cli-core/ux')>('@sanity/cli-core/ux')
return {
Expand Down Expand Up @@ -162,7 +157,6 @@ function setupImportModuleMock(
function setupDefaultMocks() {
mockFsAccess.mockResolvedValue(undefined)
mockFsReaddir.mockResolvedValue([])
mockGetTsconfig.mockReturnValue({path: '/test/tsconfig.json'} as never)
}

describe('#media:deploy-aspect', () => {
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ catalog:
debug: ^4.4.3
jsdom: ^27.4.0
zod: ^4.3.6
'get-tsconfig': ^4.13.6
execa: ^9.6.0
read-package-up: ^12.0.0
ora: ^9.0.0
rxjs: ^7.8.2
tinyglobby: ^0.2.15
'import-meta-resolve': ^4.2.0
'@rexxars/jiti': ^2.6.2

# Build Tools
'@swc/core': ^1.15.11
Expand Down
Loading