Skip to content

Commit

Permalink
fix(cli): correct worker strategy for new bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and mariuslundgard committed Oct 3, 2022
1 parent e640b72 commit 881fd5e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
29 changes: 26 additions & 3 deletions packages/@sanity/cli/package.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import {defineConfig} from '@sanity/pkg-utils'
/* eslint-disable no-sync */
import fs from 'fs'
import path from 'path'
import {defineConfig, PkgExports} from '@sanity/pkg-utils'
import baseConfig from '../../../package.config'

export default defineConfig({
...baseConfig,
exports: (exports) => ({
...exports,
exports: (existingExports) => ({
...existingExports,
'./cli': {
source: './src/cli.ts',
require: './lib/cli.js',
default: './lib/cli.js',
},
...getWorkerExports(),
}),
runtime: 'node',
})

function getWorkerExports(): PkgExports {
const workersDir = path.join(__dirname, 'src', 'workers')
const sourceFiles = fs
.readdirSync(workersDir)
.filter((file) => file.endsWith('.ts'))
.map((file) => path.basename(file, '.ts'))

const workers: PkgExports = {}
for (const name of sourceFiles) {
workers[`./workers/${name}`] = {
source: `./src/workers/${name}.ts`,
require: `./lib/workers/${name}.js`,
default: `./lib/workers/${name}.js`,
}
}

return workers
}
15 changes: 7 additions & 8 deletions packages/@sanity/cli/src/util/cliWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import pkgDir from 'pkg-dir'
* To make things worse, the built CLI makes it difficult to resolve paths to
* built source files (that contains the unpackaged worker).
*
* This function takes a path _relative to the `src`/`lib` folder, resolves
* This function takes a path relative to the `src/workers` folder, resolves
* the location of that within the installed location (eg global Sanity CLI)
* and ensures we can resolve the actual module before trying to spawn the
* worker thread.
*
* @param workerPath - _RELATIVE_ path (relative to `src`/`lib`) to the worker
* @param workerPath - _RELATIVE_ path (relative to `src/workers`) to the worker
* @returns Full, absolute path to the worker
* @internal
*/
Expand All @@ -24,11 +24,10 @@ export async function getCliWorkerPath(workerPath: string): Promise<string> {
throw new Error('Failed to find root @sanity/cli module directory')
}

const resolvedPath = path.resolve(cliDir, 'lib', workerPath)
const resolvedFile = require.resolve(resolvedPath)
if (!resolvedFile) {
throw new Error(`Unable to resolve path for worker: ${resolvedPath}`)
const resolvedPath = path.resolve(cliDir, 'lib', 'workers', workerPath)
try {
return require.resolve(resolvedPath)
} catch (err) {
throw new Error(`Unable to resolve path for worker: ${workerPath}`)
}

return resolvedFile
}
1 change: 1 addition & 0 deletions packages/@sanity/cli/src/util/clientWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function getCliToken(): string | undefined {
const userConfig = getUserConfig()
return envAuthToken || userConfig.get('authToken')
}

export interface ClientRequirements {
requireUser?: boolean
requireProject?: boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/cli/src/util/getCliConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function getCliConfigSync(cwd: string): CliConfigResult | null {
}

async function getCliConfigForked(cwd: string): Promise<CliConfigResult | null> {
const workerPath = await getCliWorkerPath('util/getCliConfig.worker')
const workerPath = await getCliWorkerPath('getCliConfig')
return new Promise((resolve, reject) => {
const worker = new Worker(workerPath, {workerData: cwd})
worker.on('message', (message) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {parentPort, workerData} from 'worker_threads'
import {getCliConfig} from './getCliConfig'
import {getCliConfig} from '../util/getCliConfig'

// We're communicating with a parent process through a message channel
getCliConfig(workerData, {forked: false})
Expand Down

0 comments on commit 881fd5e

Please sign in to comment.