@fabrika/cli 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +39 -0
  2. package/src/index.ts +198 -0
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@fabrika/cli",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/contember/fabrika-platform.git",
8
+ "directory": "packages/cli"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "bin": {
14
+ "fabrika": "./src/index.ts"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "!src/**/__tests__/**"
19
+ ],
20
+ "exports": {
21
+ ".": {
22
+ "bun": "./src/index.ts",
23
+ "types": "./src/index.ts",
24
+ "default": "./src/index.ts"
25
+ }
26
+ },
27
+ "scripts": {
28
+ "typecheck": "tsc --noEmit",
29
+ "test": "bun test"
30
+ },
31
+ "dependencies": {
32
+ "@fabrika/installation-cloudflare": "^0.0.1",
33
+ "@fabrika/installation-contract": "^0.0.1",
34
+ "@fabrika/installation-zerops": "^0.0.1",
35
+ "@fabrika/provider-cloudflare": "^0.0.1",
36
+ "@fabrika/provider-contract": "^0.0.1",
37
+ "@fabrika/provider-zerops": "^0.0.1"
38
+ }
39
+ }
package/src/index.ts ADDED
@@ -0,0 +1,198 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { installationCliFromModule, isInstallationCommand, supportsInstallationCommand } from '@fabrika/installation-contract'
4
+ import { authoredAppProvider } from '@fabrika/provider-contract'
5
+ import { resolve } from 'node:path'
6
+
7
+ const USAGE = `fabrika — application platform CLI
8
+
9
+ Usage:
10
+ fabrika platform init --provider=<cloudflare|zerops> [provider options]
11
+ fabrika platform plan --provider=<cloudflare|zerops> [provider options]
12
+ fabrika platform deploy --provider=<cloudflare|zerops> [provider options]
13
+ fabrika app deploy [--provider=cloudflare] --env=<env> [--config=<path>] [--dry-run]
14
+ fabrika app build [--provider=zerops] --env=<env> [--config=<path>] [--output=<path>]
15
+ fabrika namespace <plan|create|adopt|reconcile> --provider=zerops [provider options]
16
+
17
+ For app commands, the provider is read from the config returned by the selected provider's defineApp().
18
+ An explicit --provider is required only when no app config is available.
19
+
20
+ Which platform commands exist, and how WIDE each one is, is the provider's own answer — Zerops'
21
+ \`platform deploy\` owns the whole ordered sequence while Cloudflare's is one step of a scaffolded
22
+ workflow (ADR-0027). Run \`fabrika platform <command> --provider=<name> --help\` for that surface.
23
+ `
24
+
25
+ export interface ParsedCommand {
26
+ readonly area: string | undefined
27
+ readonly command: string | undefined
28
+ readonly provider: string | undefined
29
+ readonly rest: string[]
30
+ readonly help: boolean
31
+ }
32
+
33
+ export const parseCliArgs = (argv: readonly string[]): ParsedCommand => {
34
+ let area: string | undefined
35
+ let command: string | undefined
36
+ let provider: string | undefined
37
+ let help = false
38
+ const rest: string[] = []
39
+ for (const arg of argv) {
40
+ if (arg === '-h' || arg === '--help') {
41
+ help = true
42
+ } else if (arg.startsWith('--provider=')) {
43
+ provider = arg.slice('--provider='.length)
44
+ } else if (area === undefined && !arg.startsWith('-')) {
45
+ area = arg
46
+ } else if (command === undefined && !arg.startsWith('-')) {
47
+ command = arg
48
+ } else {
49
+ rest.push(arg)
50
+ }
51
+ }
52
+ return { area, command, provider, rest, help }
53
+ }
54
+
55
+ const configPath = (argv: readonly string[]): string => {
56
+ const option = argv.find((arg) => arg.startsWith('--config='))
57
+ return option === undefined ? './fabrika.config.ts' : option.slice('--config='.length)
58
+ }
59
+
60
+ export const providerFromConfig = async (argv: readonly string[]): Promise<string | undefined> => {
61
+ const absolute = resolve(process.cwd(), configPath(argv))
62
+ const file = Bun.file(absolute)
63
+ if (!(await file.exists())) {
64
+ return undefined
65
+ }
66
+ const loaded: unknown = await import(absolute)
67
+ if (typeof loaded !== 'object' || loaded === null) {
68
+ return undefined
69
+ }
70
+ return authoredAppProvider(Reflect.get(loaded, 'default'))
71
+ }
72
+
73
+ const requireExplicitProvider = (parsed: ParsedCommand): string => {
74
+ const provider = parsed.provider
75
+ if (provider === undefined || provider === '') {
76
+ throw new Error('Provider is required. Pass --provider=<cloudflare|zerops>.')
77
+ }
78
+ return provider
79
+ }
80
+
81
+ const selectedAppProvider = async (parsed: ParsedCommand): Promise<string> => {
82
+ const inferred = await providerFromConfig(parsed.rest)
83
+ if (parsed.provider !== undefined && inferred !== undefined && parsed.provider !== inferred) {
84
+ throw new Error(`App config belongs to provider "${inferred}", but --provider=${parsed.provider} was requested.`)
85
+ }
86
+ const provider = inferred ?? parsed.provider
87
+ if (provider === undefined || provider === '') {
88
+ throw new Error('Provider is required. Pass --provider=<cloudflare|zerops> or default-export a provider defineApp() config.')
89
+ }
90
+ return provider
91
+ }
92
+
93
+ const installationModule = async (provider: string): Promise<unknown> => {
94
+ const packageName = provider === 'cloudflare'
95
+ ? '@fabrika/installation-cloudflare'
96
+ : provider === 'zerops'
97
+ ? '@fabrika/installation-zerops'
98
+ : provider
99
+ return import(packageName)
100
+ }
101
+
102
+ export type AppCliRunner = (argv: readonly string[]) => Promise<void>
103
+
104
+ export const appCliRunnerFromModule = (loaded: unknown, packageName: string, exportName: string): AppCliRunner => {
105
+ if (typeof loaded !== 'object' || loaded === null || !(exportName in loaded)) {
106
+ throw new Error(`${packageName} must export \`${exportName}\``)
107
+ }
108
+ const runner = Reflect.get(loaded, exportName)
109
+ if (typeof runner !== 'function') {
110
+ throw new Error(`${packageName} must export \`${exportName}\` as a function`)
111
+ }
112
+ return runner
113
+ }
114
+
115
+ const appCliRunner = async (packageName: string, exportName: string): Promise<AppCliRunner> => {
116
+ const loaded: unknown = await import(packageName)
117
+ return appCliRunnerFromModule(loaded, packageName, exportName)
118
+ }
119
+
120
+ const runPlatform = async (parsed: ParsedCommand): Promise<void> => {
121
+ if (!isInstallationCommand(parsed.command)) {
122
+ throw new Error(`Unknown platform command: ${parsed.command ?? '(missing)'}`)
123
+ }
124
+ const provider = requireExplicitProvider(parsed)
125
+ const expectedProvider = provider === 'cloudflare' || provider === 'zerops' ? provider : undefined
126
+ const installation = installationCliFromModule(await installationModule(provider), expectedProvider)
127
+ if (!supportsInstallationCommand(installation, parsed.command)) {
128
+ throw new Error(`${installation.provider} installation does not support \`platform ${parsed.command}\`\n\n${installation.usage}`)
129
+ }
130
+ await installation.run(parsed.command, parsed.rest)
131
+ }
132
+
133
+ const runApp = async (parsed: ParsedCommand): Promise<void> => {
134
+ const provider = await selectedAppProvider(parsed)
135
+ if (provider === 'cloudflare') {
136
+ if (parsed.command !== 'deploy') {
137
+ throw new Error(`Cloudflare app tooling does not support \`app ${parsed.command ?? '(missing)'}\``)
138
+ }
139
+ const runCloudflareCli = await appCliRunner('@fabrika/provider-cloudflare/cli', 'runCloudflareCli')
140
+ await runCloudflareCli(['deploy', ...parsed.rest])
141
+ return
142
+ }
143
+ if (provider === 'zerops') {
144
+ if (parsed.command !== 'build') {
145
+ throw new Error(`Zerops app tooling does not support \`app ${parsed.command ?? '(missing)'}\``)
146
+ }
147
+ const runZeropsCli = await appCliRunner('@fabrika/provider-zerops/cli', 'runZeropsCli')
148
+ await runZeropsCli(['build', ...parsed.rest])
149
+ return
150
+ }
151
+ throw new Error(`Provider ${provider} does not expose app CLI commands`)
152
+ }
153
+
154
+ const runNamespace = async (parsed: ParsedCommand): Promise<void> => {
155
+ const provider = requireExplicitProvider(parsed)
156
+ if (provider !== 'zerops') {
157
+ throw new Error(`Provider ${provider} does not expose deployment namespaces`)
158
+ }
159
+ if (parsed.command === undefined) {
160
+ throw new Error('Namespace command is required')
161
+ }
162
+ const runZeropsCli = await appCliRunner('@fabrika/provider-zerops/cli', 'runZeropsCli')
163
+ await runZeropsCli(['namespace', parsed.command, ...parsed.rest])
164
+ }
165
+
166
+ export const runCli = async (argv: readonly string[]): Promise<void> => {
167
+ const parsed = parseCliArgs(argv)
168
+ if (parsed.help && parsed.area === 'platform' && parsed.provider !== undefined && parsed.provider !== '') {
169
+ // A platform command's real surface is the installation's, not this router's — and on Zerops that
170
+ // text is what backlog 62 generates the operator's workflow from, so `--help` must reach it.
171
+ console.info(installationCliFromModule(await installationModule(parsed.provider)).usage)
172
+ return
173
+ }
174
+ if (parsed.help || parsed.area === undefined) {
175
+ console.info(USAGE)
176
+ return
177
+ }
178
+ if (parsed.area === 'platform') {
179
+ await runPlatform(parsed)
180
+ return
181
+ }
182
+ if (parsed.area === 'app') {
183
+ await runApp(parsed)
184
+ return
185
+ }
186
+ if (parsed.area === 'namespace') {
187
+ await runNamespace(parsed)
188
+ return
189
+ }
190
+ throw new Error(`Unknown command area: ${parsed.area}\n\n${USAGE}`)
191
+ }
192
+
193
+ if (import.meta.main) {
194
+ await runCli(process.argv.slice(2)).catch((error: unknown) => {
195
+ console.error(`fabrika: ${error instanceof Error ? error.message : String(error)}`)
196
+ process.exit(1)
197
+ })
198
+ }