|
| 1 | +import { existsSync } from 'fs'; |
| 2 | +import { resolve } from 'path'; |
| 3 | +import { |
| 4 | + CombinedServer, |
| 5 | + CombinedServerOptions, |
| 6 | + FunctionName, |
| 7 | + FunctionServiceConfig |
| 8 | +} from '@constructive-io/server'; |
| 9 | +import { cliExitWithError, extractFirst } from '@inquirerer/utils'; |
| 10 | +import { CLIOptions, Inquirerer, Question } from 'inquirerer'; |
| 11 | + |
| 12 | +const jobsUsageText = ` |
| 13 | +Constructive Jobs: |
| 14 | +
|
| 15 | + cnc jobs <subcommand> [OPTIONS] |
| 16 | +
|
| 17 | + Start or manage Constructive jobs services. |
| 18 | +
|
| 19 | +Subcommands: |
| 20 | + up Start combined server (jobs runtime) |
| 21 | +
|
| 22 | +Options: |
| 23 | + --help, -h Show this help message |
| 24 | + --cwd <directory> Working directory (default: current directory) |
| 25 | + --with-graphql-server Enable GraphQL server (default: disabled; flag-only) |
| 26 | + --with-jobs-svc Enable jobs service (default: disabled; flag-only) |
| 27 | + --functions <list> Comma-separated functions, optionally with ports (e.g. "fn=8080") |
| 28 | +
|
| 29 | +Examples: |
| 30 | + cnc jobs up |
| 31 | + cnc jobs up --cwd /path/to/constructive |
| 32 | + cnc jobs up --with-graphql-server --functions simple-email,send-email-link=8082 |
| 33 | +`; |
| 34 | + |
| 35 | +const questions: Question[] = [ |
| 36 | + { |
| 37 | + name: 'withGraphqlServer', |
| 38 | + alias: 'with-graphql-server', |
| 39 | + message: 'Enable GraphQL server?', |
| 40 | + type: 'confirm', |
| 41 | + required: false, |
| 42 | + default: false, |
| 43 | + useDefault: true |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'withJobsSvc', |
| 47 | + alias: 'with-jobs-svc', |
| 48 | + message: 'Enable jobs service?', |
| 49 | + type: 'confirm', |
| 50 | + required: false, |
| 51 | + default: false, |
| 52 | + useDefault: true |
| 53 | + } |
| 54 | +]; |
| 55 | + |
| 56 | +const ensureCwd = (cwd: string): string => { |
| 57 | + const resolved = resolve(cwd); |
| 58 | + if (!existsSync(resolved)) { |
| 59 | + throw new Error(`Working directory does not exist: ${resolved}`); |
| 60 | + } |
| 61 | + process.chdir(resolved); |
| 62 | + return resolved; |
| 63 | +}; |
| 64 | + |
| 65 | +type ParsedFunctionsArg = { |
| 66 | + mode: 'all' | 'list'; |
| 67 | + names: string[]; |
| 68 | + ports: Record<string, number>; |
| 69 | +}; |
| 70 | + |
| 71 | +const parseFunctionsArg = (value: unknown): ParsedFunctionsArg | undefined => { |
| 72 | + if (value === undefined) return undefined; |
| 73 | + |
| 74 | + const values = Array.isArray(value) ? value : [value]; |
| 75 | + |
| 76 | + const tokens: string[] = []; |
| 77 | + for (const value of values) { |
| 78 | + if (value === true) { |
| 79 | + tokens.push('all'); |
| 80 | + continue; |
| 81 | + } |
| 82 | + if (value === false || value === undefined || value === null) continue; |
| 83 | + const raw = String(value); |
| 84 | + raw |
| 85 | + .split(',') |
| 86 | + .map((part) => part.trim()) |
| 87 | + .filter(Boolean) |
| 88 | + .forEach((part) => tokens.push(part)); |
| 89 | + } |
| 90 | + |
| 91 | + if (!tokens.length) { |
| 92 | + return { mode: 'list', names: [], ports: {} }; |
| 93 | + } |
| 94 | + |
| 95 | + const hasAll = tokens.some((token) => { |
| 96 | + const normalized = token.trim().toLowerCase(); |
| 97 | + return normalized === 'all' || normalized === '*'; |
| 98 | + }); |
| 99 | + |
| 100 | + if (hasAll) { |
| 101 | + if (tokens.length > 1) { |
| 102 | + throw new Error('Use "all" without other function names.'); |
| 103 | + } |
| 104 | + return { mode: 'all', names: [], ports: {} }; |
| 105 | + } |
| 106 | + |
| 107 | + const names: string[] = []; |
| 108 | + const ports: Record<string, number> = {}; |
| 109 | + |
| 110 | + for (const token of tokens) { |
| 111 | + const trimmed = token.trim(); |
| 112 | + if (!trimmed) continue; |
| 113 | + |
| 114 | + const separatorIndex = trimmed.search(/[:=]/); |
| 115 | + if (separatorIndex === -1) { |
| 116 | + names.push(trimmed); |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + const name = trimmed.slice(0, separatorIndex).trim(); |
| 121 | + const portText = trimmed.slice(separatorIndex + 1).trim(); |
| 122 | + |
| 123 | + if (!name) { |
| 124 | + throw new Error(`Missing function name in "${token}".`); |
| 125 | + } |
| 126 | + if (!portText) { |
| 127 | + throw new Error(`Missing port for function "${name}".`); |
| 128 | + } |
| 129 | + |
| 130 | + const port = Number(portText); |
| 131 | + if (!Number.isFinite(port) || port <= 0) { |
| 132 | + throw new Error(`Invalid port "${portText}" for function "${name}".`); |
| 133 | + } |
| 134 | + |
| 135 | + names.push(name); |
| 136 | + ports[name] = port; |
| 137 | + } |
| 138 | + |
| 139 | + const uniqueNames: string[] = []; |
| 140 | + const seen = new Set<string>(); |
| 141 | + for (const name of names) { |
| 142 | + if (seen.has(name)) continue; |
| 143 | + seen.add(name); |
| 144 | + uniqueNames.push(name); |
| 145 | + } |
| 146 | + |
| 147 | + return { mode: 'list', names: uniqueNames, ports }; |
| 148 | +}; |
| 149 | + |
| 150 | +const buildCombinedServerOptions = ( |
| 151 | + args: Partial<Record<string, any>> |
| 152 | +): CombinedServerOptions => { |
| 153 | + const parsedFunctions = parseFunctionsArg(args.functions); |
| 154 | + |
| 155 | + let functions: CombinedServerOptions['functions']; |
| 156 | + if (parsedFunctions) { |
| 157 | + if (parsedFunctions.mode === 'all') { |
| 158 | + functions = { enabled: true }; |
| 159 | + } else if (parsedFunctions.names.length) { |
| 160 | + const services: FunctionServiceConfig[] = parsedFunctions.names.map( |
| 161 | + (name) => ({ |
| 162 | + name: name as FunctionName, |
| 163 | + port: parsedFunctions.ports[name] |
| 164 | + }) |
| 165 | + ); |
| 166 | + functions = { enabled: true, services }; |
| 167 | + } else { |
| 168 | + functions = undefined; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return { |
| 173 | + graphql: { enabled: args.withGraphqlServer === true }, |
| 174 | + jobs: { enabled: args.withJobsSvc === true }, |
| 175 | + functions |
| 176 | + }; |
| 177 | +}; |
| 178 | + |
| 179 | +export default async ( |
| 180 | + argv: Partial<Record<string, any>>, |
| 181 | + prompter: Inquirerer, |
| 182 | + _options: CLIOptions |
| 183 | +) => { |
| 184 | + if (argv.help || argv.h) { |
| 185 | + console.log(jobsUsageText); |
| 186 | + process.exit(0); |
| 187 | + } |
| 188 | + |
| 189 | + const { first: subcommand, newArgv } = extractFirst(argv); |
| 190 | + const args = newArgv as Partial<Record<string, any>>; |
| 191 | + |
| 192 | + if (!subcommand) { |
| 193 | + console.log(jobsUsageText); |
| 194 | + await cliExitWithError('No subcommand provided. Use "up".'); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + switch (subcommand) { |
| 199 | + case 'up': { |
| 200 | + try { |
| 201 | + ensureCwd((args.cwd as string) || process.cwd()); |
| 202 | + const promptAnswers = await prompter.prompt(args, questions); |
| 203 | + await CombinedServer(buildCombinedServerOptions(promptAnswers)); |
| 204 | + } catch (error) { |
| 205 | + await cliExitWithError( |
| 206 | + `Failed to start combined server: ${(error as Error).message}` |
| 207 | + ); |
| 208 | + } |
| 209 | + break; |
| 210 | + } |
| 211 | + |
| 212 | + default: |
| 213 | + console.log(jobsUsageText); |
| 214 | + await cliExitWithError(`Unknown subcommand: ${subcommand}. Use "up".`); |
| 215 | + } |
| 216 | +}; |
0 commit comments