Skip to content

Commit f159ea5

Browse files
authored
Merge pull request #339 from cipherstash/cli
Cli
2 parents 5605c5f + 4d0dfc5 commit f159ea5

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

.changeset/four-wolves-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cipherstash/cli": minor
3+
---
4+
5+
Fixed peer dependency by lazy loading commands requiring @cipherstash/stack.

packages/cli/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
"posthog-node": "^5.28.9",
5151
"zod": "^4.3.6"
5252
},
53+
"peerDependencies": {
54+
"@cipherstash/stack": ">=0.6.0"
55+
},
56+
"peerDependenciesMeta": {
57+
"@cipherstash/stack": {
58+
"optional": true
59+
}
60+
},
5361
"devDependencies": {
5462
"@cipherstash/stack": "workspace:*",
5563
"@types/pg": "^8.11.11",

packages/cli/src/bin/stash.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,41 @@ import { readFileSync } from 'node:fs'
55
import { dirname, join } from 'node:path'
66
import { fileURLToPath } from 'node:url'
77
import * as p from '@clack/prompts'
8+
// Commands that depend on @cipherstash/stack are lazy-loaded in the switch below.
89
import {
910
authCommand,
10-
builderCommand,
1111
initCommand,
1212
installCommand,
13-
pushCommand,
14-
secretsCommand,
1513
setupCommand,
1614
statusCommand,
1715
testConnectionCommand,
1816
upgradeCommand,
19-
validateCommand,
2017
} from '../commands/index.js'
2118

19+
function isModuleNotFound(err: unknown): boolean {
20+
return (
21+
err instanceof Error &&
22+
'code' in err &&
23+
(err as { code: string }).code === 'ERR_MODULE_NOT_FOUND'
24+
)
25+
}
26+
27+
async function requireStack<T>(importFn: () => Promise<T>): Promise<T> {
28+
try {
29+
return await importFn()
30+
} catch (err: unknown) {
31+
if (isModuleNotFound(err)) {
32+
p.log.error(
33+
'@cipherstash/stack is required for this command.\n' +
34+
' Install it with: npm install @cipherstash/stack\n' +
35+
' Or run: npx @cipherstash/cli init',
36+
)
37+
process.exit(1) as never
38+
}
39+
throw err
40+
}
41+
}
42+
2243
const __dirname = dirname(fileURLToPath(import.meta.url))
2344
const pkg = JSON.parse(
2445
readFileSync(join(__dirname, '../../package.json'), 'utf-8'),
@@ -148,15 +169,19 @@ async function runDbCommand(
148169
out: values.out,
149170
})
150171
break
151-
case 'push':
172+
case 'push': {
173+
const { pushCommand } = await requireStack(() => import('../commands/db/push.js'))
152174
await pushCommand({ dryRun: flags['dry-run'] })
153175
break
154-
case 'validate':
176+
}
177+
case 'validate': {
178+
const { validateCommand } = await requireStack(() => import('../commands/db/validate.js'))
155179
await validateCommand({
156180
supabase: flags.supabase,
157181
excludeOperatorFamily: flags['exclude-operator-family'],
158182
})
159183
break
184+
}
160185
case 'status':
161186
await statusCommand()
162187
break
@@ -179,9 +204,11 @@ async function runSchemaCommand(
179204
flags: Record<string, boolean>,
180205
) {
181206
switch (sub) {
182-
case 'build':
207+
case 'build': {
208+
const { builderCommand } = await requireStack(() => import('../commands/schema/build.js'))
183209
await builderCommand({ supabase: flags.supabase })
184210
break
211+
}
185212
default:
186213
p.log.error(`Unknown schema subcommand: ${sub ?? '(none)'}`)
187214
console.log()
@@ -215,6 +242,7 @@ async function main() {
215242
break
216243
}
217244
case 'secrets': {
245+
const { secretsCommand } = await requireStack(() => import('../commands/secrets/index.js'))
218246
const secretsArgs = subcommand
219247
? [subcommand, ...commandArgs]
220248
: commandArgs

packages/cli/src/commands/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
export { setupCommand } from './db/setup.js'
22
export { installCommand } from './db/install.js'
3-
export { pushCommand } from './db/push.js'
43
export { statusCommand } from './db/status.js'
54
export { testConnectionCommand } from './db/test-connection.js'
65
export { upgradeCommand } from './db/upgrade.js'
7-
export {
8-
validateCommand,
9-
validateEncryptConfig,
10-
reportIssues,
11-
} from './db/validate.js'
12-
export { builderCommand } from './schema/build.js'
136
export { authCommand } from './auth/index.js'
147
export { initCommand } from './init/index.js'
15-
export { secretsCommand } from './secrets/index.js'

0 commit comments

Comments
 (0)