Skip to content

Commit 020c660

Browse files
committed
refactor: reorganize source, errors
1 parent 01a93e4 commit 020c660

12 files changed

Lines changed: 188 additions & 186 deletions

File tree

packages/@sanity/cli-core/package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
],
2626
"type": "module",
2727
"sideEffects": false,
28-
"main": "./dist/index.js",
29-
"types": "dist/index.d.ts",
28+
"main": "./dist/_exports/index.js",
29+
"types": "dist/_exports/index.d.ts",
3030
"exports": {
3131
".": {
32-
"source": "./src/index.ts",
33-
"default": "./dist/index.js"
32+
"source": "./src/_exports/index.ts",
33+
"default": "./dist/_exports/index.js"
34+
},
35+
"./errors": {
36+
"source": "./src/_exports/errors.ts",
37+
"default": "./dist/_exports/errors.js"
3438
},
3539
"./ux": {
3640
"source": "./src/_exports/ux.ts",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from '../errors/CLIError.js'
2+
export * from '../errors/CLIWarning.js'
3+
export * from '../errors/NonInteractiveError.js'
4+
export * from '../errors/NotFoundError.js'
5+
export * from '../errors/ProjectRootNotFoundError.js'
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {deprecate} from 'node:util'
2+
3+
import {NonInteractiveError as _NonInteractiveError} from '../errors/NonInteractiveError.js'
4+
import {NotFoundError as _NotFoundError} from '../errors/NotFoundError.js'
5+
import {ProjectRootNotFoundError as _ProjectRootNotFoundError} from '../errors/ProjectRootNotFoundError.js'
6+
7+
export * from '../config/cli/getCliConfig.js'
8+
export * from '../config/cli/getCliConfigSync.js'
9+
export {type CliConfig} from '../config/cli/types/cliConfig.js'
10+
export {type UserViteConfig} from '../config/cli/types/userViteConfig.js'
11+
export * from '../config/findProjectRoot.js'
12+
export * from '../config/findProjectRootSync.js'
13+
export * from '../config/studio/getStudioConfig.js'
14+
export * from '../config/studio/getStudioWorkspaces.js'
15+
export * from '../config/studio/isStudioConfig.js'
16+
export * from '../config/util/findConfigsPaths.js'
17+
export * from '../config/util/findStudioConfigPath.js'
18+
export {type ProjectRootResult} from '../config/util/recursivelyResolveProjectRoot.js'
19+
export * from '../debug.js'
20+
export * from '../loaders/studio/studioWorkerTask.js'
21+
export * from '../loaders/tsx/tsxWorkerTask.js'
22+
export * from '../SanityCommand.js'
23+
export * from '../services/apiClient.js'
24+
export * from '../services/cliUserConfig.js'
25+
export * from '../services/getCliToken.js'
26+
export * from '../telemetry/getTelemetryBaseInfo.js'
27+
export {
28+
type CLITelemetryStore,
29+
type ConsentInformation,
30+
type TelemetryUserProperties,
31+
} from '../telemetry/types.js'
32+
export {type Output, type SanityOrgUser} from '../types.js'
33+
export {doImport} from '../util/doImport.js'
34+
export * from '../util/environment/mockBrowserEnvironment.js'
35+
export {
36+
clearCliTelemetry,
37+
CLI_TELEMETRY_SYMBOL,
38+
getCliTelemetry,
39+
setCliTelemetry,
40+
} from '../util/getCliTelemetry.js'
41+
export * from '../util/getSanityEnvVar.js'
42+
export * from '../util/getSanityUrl.js'
43+
export * from '../util/getUserConfig.js'
44+
export * from '../util/importModule.js'
45+
export * from '../util/isCi.js'
46+
export * from '../util/isInteractive.js'
47+
export * from '../util/isStaging.js'
48+
export * from '../util/normalizePath.js'
49+
export * from '../util/promisifyWorker.js'
50+
export * from '../util/readPackageJson.js'
51+
export * from '../util/resolveLocalPackage.js'
52+
export * from '../util/safeStructuredClone.js'
53+
export * from '../ux/colorizeJson.js'
54+
export * from '../ux/timer.js'
55+
56+
export const NonInteractiveError = deprecate(
57+
_NonInteractiveError,
58+
'Import `NonInteractiveError` from `@sanity/cli-core/errors`',
59+
)
60+
export const NotFoundError = deprecate(
61+
_NotFoundError,
62+
'Import `NotFoundError` from `@sanity/cli-core/errors`',
63+
)
64+
export const ProjectRootNotFoundError = deprecate(
65+
_ProjectRootNotFoundError,
66+
'Import `ProjectRootNotFoundError` from `@sanity/cli-core/errors`',
67+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {styleText} from 'node:util'
2+
3+
import {type PrettyPrintableError} from '@oclif/core/interfaces'
4+
import cleanStack from 'clean-stack'
5+
6+
/**
7+
* A formatted CLI error that pretty-prints to stderr.
8+
*
9+
* This is a lightweight reimplementation of `@oclif/core`'s `CLIError`.
10+
* We can't import the original because `@oclif/core` is a CJS barrel that
11+
* pulls in the entire oclif runtime (~10MB) when bundled - defeating
12+
* tree-shaking in the standalone `create-sanity` bundle. By owning the
13+
* error class here, code in `@sanity/cli-core` and the init action tree
14+
* can throw formatted errors without depending on oclif at all.
15+
*
16+
* The `oclif` property is shaped so oclif's error handler still recognises
17+
* these errors when thrown inside an oclif command, preserving the correct
18+
* exit code and suppressing redundant stack traces.
19+
*/
20+
export class CLIError extends Error {
21+
code?: string
22+
oclif: {exit?: number} = {exit: 2}
23+
ref?: string
24+
skipOclifErrorHandling?: boolean
25+
suggestions?: string[]
26+
27+
constructor(error: Error | string, options: PrettyPrintableError & {exit?: false | number} = {}) {
28+
super(error instanceof Error ? error.message : error)
29+
if (error instanceof Error && error.stack) {
30+
this.stack = error.stack
31+
}
32+
if (options.exit !== undefined) this.oclif.exit = options.exit || undefined
33+
this.code = options.code
34+
this.suggestions = options.suggestions
35+
this.ref = options.ref
36+
}
37+
38+
get bang(): string | undefined {
39+
return styleText('red', process.platform === 'win32' ? '»' : '›')
40+
}
41+
42+
get prettyStack(): string {
43+
return cleanStack(super.stack!, {pretty: true})
44+
}
45+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {styleText} from 'node:util'
2+
3+
import {CLIError} from './CLIError.js'
4+
5+
/**
6+
* A warning-level CLI error. Identical to {@link CLIError} except the
7+
* bang prefix is yellow instead of red.
8+
*/
9+
export class CLIWarning extends CLIError {
10+
constructor(input: Error | string) {
11+
super(input instanceof Error ? input.message : input)
12+
this.name = 'Warning'
13+
}
14+
15+
override get bang(): string | undefined {
16+
return styleText('yellow', process.platform === 'win32' ? '»' : '›')
17+
}
18+
}

packages/@sanity/cli-core/src/errors/NonInteractiveError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {CLIError} from '../ux/errors.js'
1+
import {CLIError} from './CLIError.js'
22

33
/**
44
* Error thrown when a prompt is attempted in a non-interactive environment

packages/@sanity/cli-core/src/errors/ProjectRootNotFoundError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {isRecord} from '../util/isRecord.js'
2-
import {CLIError} from '../ux/errors.js'
2+
import {CLIError} from './CLIError.js'
33

44
/**
55
* Error thrown when a project root directory cannot be found.

packages/@sanity/cli-core/src/index.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)