optimal-cli 0.1.0 → 1.0.0
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.
- package/agents/.gitkeep +0 -0
- package/agents/content-ops.md +227 -0
- package/agents/financial-ops.md +184 -0
- package/agents/infra-ops.md +206 -0
- package/agents/profiles.json +5 -0
- package/bin/optimal.ts +1731 -0
- package/docs/CLI-REFERENCE.md +361 -0
- package/lib/assets/index.ts +225 -0
- package/lib/assets.ts +124 -0
- package/lib/auth/index.ts +189 -0
- package/lib/board/index.ts +309 -0
- package/lib/board/types.ts +124 -0
- package/lib/bot/claim.ts +43 -0
- package/lib/bot/coordinator.ts +254 -0
- package/lib/bot/heartbeat.ts +37 -0
- package/lib/bot/index.ts +9 -0
- package/lib/bot/protocol.ts +99 -0
- package/lib/bot/reporter.ts +42 -0
- package/lib/bot/skills.ts +81 -0
- package/lib/budget/projections.ts +561 -0
- package/lib/budget/scenarios.ts +312 -0
- package/lib/cms/publish-blog.ts +129 -0
- package/lib/cms/strapi-client.ts +302 -0
- package/lib/config/registry.ts +228 -0
- package/lib/config/schema.ts +58 -0
- package/lib/config.ts +247 -0
- package/lib/errors.ts +129 -0
- package/lib/format.ts +120 -0
- package/lib/infra/.gitkeep +0 -0
- package/lib/infra/deploy.ts +70 -0
- package/lib/infra/migrate.ts +141 -0
- package/lib/newsletter/.gitkeep +0 -0
- package/lib/newsletter/distribute.ts +256 -0
- package/{dist/lib/newsletter/generate-insurance.d.ts → lib/newsletter/generate-insurance.ts} +24 -7
- package/lib/newsletter/generate.ts +735 -0
- package/lib/returnpro/.gitkeep +0 -0
- package/lib/returnpro/anomalies.ts +258 -0
- package/lib/returnpro/audit.ts +194 -0
- package/lib/returnpro/diagnose.ts +400 -0
- package/lib/returnpro/kpis.ts +255 -0
- package/lib/returnpro/templates.ts +323 -0
- package/lib/returnpro/upload-income.ts +311 -0
- package/lib/returnpro/upload-netsuite.ts +696 -0
- package/lib/returnpro/upload-r1.ts +563 -0
- package/lib/returnpro/validate.ts +154 -0
- package/lib/social/meta.ts +228 -0
- package/lib/social/post-generator.ts +468 -0
- package/lib/social/publish.ts +301 -0
- package/lib/social/scraper.ts +503 -0
- package/lib/supabase.ts +25 -0
- package/lib/transactions/delete-batch.ts +258 -0
- package/lib/transactions/ingest.ts +659 -0
- package/lib/transactions/stamp.ts +654 -0
- package/package.json +15 -25
- package/dist/bin/optimal.d.ts +0 -2
- package/dist/bin/optimal.js +0 -995
- package/dist/lib/budget/projections.d.ts +0 -115
- package/dist/lib/budget/projections.js +0 -384
- package/dist/lib/budget/scenarios.d.ts +0 -93
- package/dist/lib/budget/scenarios.js +0 -214
- package/dist/lib/cms/publish-blog.d.ts +0 -62
- package/dist/lib/cms/publish-blog.js +0 -74
- package/dist/lib/cms/strapi-client.d.ts +0 -123
- package/dist/lib/cms/strapi-client.js +0 -213
- package/dist/lib/config.d.ts +0 -55
- package/dist/lib/config.js +0 -206
- package/dist/lib/infra/deploy.d.ts +0 -29
- package/dist/lib/infra/deploy.js +0 -58
- package/dist/lib/infra/migrate.d.ts +0 -34
- package/dist/lib/infra/migrate.js +0 -103
- package/dist/lib/kanban.d.ts +0 -46
- package/dist/lib/kanban.js +0 -118
- package/dist/lib/newsletter/distribute.d.ts +0 -52
- package/dist/lib/newsletter/distribute.js +0 -193
- package/dist/lib/newsletter/generate-insurance.js +0 -36
- package/dist/lib/newsletter/generate.d.ts +0 -104
- package/dist/lib/newsletter/generate.js +0 -571
- package/dist/lib/returnpro/anomalies.d.ts +0 -64
- package/dist/lib/returnpro/anomalies.js +0 -166
- package/dist/lib/returnpro/audit.d.ts +0 -32
- package/dist/lib/returnpro/audit.js +0 -147
- package/dist/lib/returnpro/diagnose.d.ts +0 -52
- package/dist/lib/returnpro/diagnose.js +0 -281
- package/dist/lib/returnpro/kpis.d.ts +0 -32
- package/dist/lib/returnpro/kpis.js +0 -192
- package/dist/lib/returnpro/templates.d.ts +0 -48
- package/dist/lib/returnpro/templates.js +0 -229
- package/dist/lib/returnpro/upload-income.d.ts +0 -25
- package/dist/lib/returnpro/upload-income.js +0 -235
- package/dist/lib/returnpro/upload-netsuite.d.ts +0 -37
- package/dist/lib/returnpro/upload-netsuite.js +0 -566
- package/dist/lib/returnpro/upload-r1.d.ts +0 -48
- package/dist/lib/returnpro/upload-r1.js +0 -398
- package/dist/lib/social/post-generator.d.ts +0 -83
- package/dist/lib/social/post-generator.js +0 -333
- package/dist/lib/social/publish.d.ts +0 -66
- package/dist/lib/social/publish.js +0 -226
- package/dist/lib/social/scraper.d.ts +0 -67
- package/dist/lib/social/scraper.js +0 -361
- package/dist/lib/supabase.d.ts +0 -4
- package/dist/lib/supabase.js +0 -20
- package/dist/lib/transactions/delete-batch.d.ts +0 -60
- package/dist/lib/transactions/delete-batch.js +0 -203
- package/dist/lib/transactions/ingest.d.ts +0 -43
- package/dist/lib/transactions/ingest.js +0 -555
- package/dist/lib/transactions/stamp.d.ts +0 -51
- package/dist/lib/transactions/stamp.js +0 -524
package/lib/config.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { createClient, SupabaseClient } from '@supabase/supabase-js'
|
|
2
|
+
import { readFileSync, writeFileSync, existsSync } from 'node:fs'
|
|
3
|
+
import { homedir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
const CONFIG_DIR = join(homedir(), '.optimal')
|
|
7
|
+
const LOCAL_CONFIG_PATH = join(CONFIG_DIR, 'config.json')
|
|
8
|
+
const OPENCLAW_CONFIG_PATH = join(homedir(), '.openclaw', 'openclaw.json')
|
|
9
|
+
|
|
10
|
+
// Get Supabase client for OptimalOS instance (stores CLI configs)
|
|
11
|
+
function getOptimalSupabase(): SupabaseClient {
|
|
12
|
+
const url = process.env.OPTIMAL_SUPABASE_URL
|
|
13
|
+
const key = process.env.OPTIMAL_SUPABASE_SERVICE_KEY
|
|
14
|
+
if (!url || !key) {
|
|
15
|
+
throw new Error('OPTIMAL_SUPABASE_URL and OPTIMAL_SUPABASE_SERVICE_KEY must be set')
|
|
16
|
+
}
|
|
17
|
+
return createClient(url, key)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ConfigRecord {
|
|
21
|
+
id: string
|
|
22
|
+
agent_name: string
|
|
23
|
+
config_json: Record<string, unknown>
|
|
24
|
+
version: string
|
|
25
|
+
created_at: string
|
|
26
|
+
updated_at: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Initialize local config directory
|
|
31
|
+
*/
|
|
32
|
+
export function initConfigDir(): void {
|
|
33
|
+
if (!existsSync(CONFIG_DIR)) {
|
|
34
|
+
import('node:fs').then(fs => fs.mkdirSync(CONFIG_DIR, { recursive: true }))
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Load local openclaw.json
|
|
40
|
+
*/
|
|
41
|
+
export function loadLocalConfig(): Record<string, unknown> | null {
|
|
42
|
+
if (!existsSync(OPENCLAW_CONFIG_PATH)) {
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const raw = readFileSync(OPENCLAW_CONFIG_PATH, 'utf-8')
|
|
47
|
+
return JSON.parse(raw)
|
|
48
|
+
} catch {
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Save config to local openclaw.json
|
|
55
|
+
*/
|
|
56
|
+
export function saveLocalConfig(config: Record<string, unknown>): void {
|
|
57
|
+
writeFileSync(OPENCLAW_CONFIG_PATH, JSON.stringify(config, null, 2))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Push current openclaw.json to Supabase
|
|
62
|
+
*/
|
|
63
|
+
export async function pushConfig(agentName: string): Promise<{ id: string; version: string }> {
|
|
64
|
+
const supabase = getOptimalSupabase()
|
|
65
|
+
const config = loadLocalConfig()
|
|
66
|
+
|
|
67
|
+
if (!config) {
|
|
68
|
+
throw new Error(`No config found at ${OPENCLAW_CONFIG_PATH}`)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Generate version timestamp
|
|
72
|
+
const version = new Date().toISOString()
|
|
73
|
+
|
|
74
|
+
// Check if config exists for this agent
|
|
75
|
+
const { data: existing } = await supabase
|
|
76
|
+
.from('agent_configs')
|
|
77
|
+
.select('id')
|
|
78
|
+
.eq('agent_name', agentName)
|
|
79
|
+
.single()
|
|
80
|
+
|
|
81
|
+
let result
|
|
82
|
+
if (existing) {
|
|
83
|
+
// Update existing
|
|
84
|
+
const { data, error } = await supabase
|
|
85
|
+
.from('agent_configs')
|
|
86
|
+
.update({
|
|
87
|
+
config_json: config,
|
|
88
|
+
version,
|
|
89
|
+
updated_at: version,
|
|
90
|
+
})
|
|
91
|
+
.eq('id', existing.id)
|
|
92
|
+
.select()
|
|
93
|
+
.single()
|
|
94
|
+
|
|
95
|
+
if (error) throw error
|
|
96
|
+
result = data
|
|
97
|
+
} else {
|
|
98
|
+
// Insert new
|
|
99
|
+
const { data, error } = await supabase
|
|
100
|
+
.from('agent_configs')
|
|
101
|
+
.insert({
|
|
102
|
+
agent_name: agentName,
|
|
103
|
+
config_json: config,
|
|
104
|
+
version,
|
|
105
|
+
})
|
|
106
|
+
.select()
|
|
107
|
+
.single()
|
|
108
|
+
|
|
109
|
+
if (error) throw error
|
|
110
|
+
result = data
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { id: result.id, version }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Pull config from Supabase and save to local openclaw.json
|
|
118
|
+
*/
|
|
119
|
+
export async function pullConfig(agentName: string): Promise<ConfigRecord> {
|
|
120
|
+
const supabase = getOptimalSupabase()
|
|
121
|
+
|
|
122
|
+
const { data, error } = await supabase
|
|
123
|
+
.from('agent_configs')
|
|
124
|
+
.select('*')
|
|
125
|
+
.eq('agent_name', agentName)
|
|
126
|
+
.order('updated_at', { ascending: false })
|
|
127
|
+
.limit(1)
|
|
128
|
+
.single()
|
|
129
|
+
|
|
130
|
+
if (error) {
|
|
131
|
+
throw new Error(`No config found for agent: ${agentName}`)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Save to local
|
|
135
|
+
saveLocalConfig(data.config_json)
|
|
136
|
+
|
|
137
|
+
return data as ConfigRecord
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* List all saved agent configs
|
|
142
|
+
*/
|
|
143
|
+
export async function listConfigs(): Promise<Array<{ agent_name: string; version: string; updated_at: string }>> {
|
|
144
|
+
const supabase = getOptimalSupabase()
|
|
145
|
+
|
|
146
|
+
const { data, error } = await supabase
|
|
147
|
+
.from('agent_configs')
|
|
148
|
+
.select('agent_name, version, updated_at')
|
|
149
|
+
.order('updated_at', { ascending: false })
|
|
150
|
+
|
|
151
|
+
if (error) {
|
|
152
|
+
throw new Error(`Failed to list configs: ${error.message}`)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return data || []
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Compare local config with cloud version
|
|
160
|
+
*/
|
|
161
|
+
export async function diffConfig(agentName: string): Promise<{
|
|
162
|
+
local: Record<string, unknown> | null
|
|
163
|
+
cloud: ConfigRecord | null
|
|
164
|
+
differences: string[]
|
|
165
|
+
}> {
|
|
166
|
+
const local = loadLocalConfig()
|
|
167
|
+
let cloud: ConfigRecord | null = null
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
const supabase = getOptimalSupabase()
|
|
171
|
+
const { data } = await supabase
|
|
172
|
+
.from('agent_configs')
|
|
173
|
+
.select('*')
|
|
174
|
+
.eq('agent_name', agentName)
|
|
175
|
+
.single()
|
|
176
|
+
cloud = data as ConfigRecord
|
|
177
|
+
} catch {
|
|
178
|
+
// Cloud config doesn't exist
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const differences: string[] = []
|
|
182
|
+
|
|
183
|
+
if (!local && !cloud) {
|
|
184
|
+
differences.push('No local or cloud config found')
|
|
185
|
+
} else if (!local) {
|
|
186
|
+
differences.push('No local config (cloud exists)')
|
|
187
|
+
} else if (!cloud) {
|
|
188
|
+
differences.push('No cloud config (local exists)')
|
|
189
|
+
} else {
|
|
190
|
+
// Simple diff on top-level keys
|
|
191
|
+
const localKeys = Object.keys(local).sort()
|
|
192
|
+
const cloudKeys = Object.keys(cloud.config_json).sort()
|
|
193
|
+
|
|
194
|
+
if (JSON.stringify(localKeys) !== JSON.stringify(cloudKeys)) {
|
|
195
|
+
differences.push('Top-level keys differ')
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Check version
|
|
199
|
+
const localMeta = (local as any).meta
|
|
200
|
+
if (localMeta?.lastTouchedVersion !== cloud.version) {
|
|
201
|
+
differences.push(`Version mismatch: local=${localMeta?.lastTouchedVersion}, cloud=${cloud.version}`)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { local, cloud, differences }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Sync config (two-way merge)
|
|
210
|
+
*/
|
|
211
|
+
export async function syncConfig(agentName: string): Promise<{
|
|
212
|
+
action: 'pushed' | 'pulled' | 'merged' | 'none'
|
|
213
|
+
message: string
|
|
214
|
+
}> {
|
|
215
|
+
const { local, cloud, differences } = await diffConfig(agentName)
|
|
216
|
+
|
|
217
|
+
if (!local && !cloud) {
|
|
218
|
+
return { action: 'none', message: 'No configs to sync' }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!cloud) {
|
|
222
|
+
// Only local exists - push
|
|
223
|
+
const result = await pushConfig(agentName)
|
|
224
|
+
return { action: 'pushed', message: `Pushed to cloud (version ${result.version})` }
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (!local) {
|
|
228
|
+
// Only cloud exists - pull
|
|
229
|
+
await pullConfig(agentName)
|
|
230
|
+
return { action: 'pulled', message: `Pulled from cloud (version ${cloud.version})` }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Both exist - compare timestamps
|
|
234
|
+
const localTime = (local as any).meta?.lastTouchedAt || '1970-01-01'
|
|
235
|
+
const localVersion = (local as any).meta?.lastTouchedVersion || 'unknown'
|
|
236
|
+
const cloudTime = cloud.updated_at
|
|
237
|
+
|
|
238
|
+
if (localTime > cloudTime) {
|
|
239
|
+
const result = await pushConfig(agentName)
|
|
240
|
+
return { action: 'pushed', message: `Local is newer - pushed to cloud (version ${result.version})` }
|
|
241
|
+
} else if (cloudTime > localTime) {
|
|
242
|
+
await pullConfig(agentName)
|
|
243
|
+
return { action: 'pulled', message: `Cloud is newer - pulled from cloud (version ${cloud.version})` }
|
|
244
|
+
} else {
|
|
245
|
+
return { action: 'none', message: 'Configs are in sync' }
|
|
246
|
+
}
|
|
247
|
+
}
|
package/lib/errors.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized error handling for the Optimal CLI.
|
|
3
|
+
*
|
|
4
|
+
* Provides a typed CliError class, a user-friendly formatter,
|
|
5
|
+
* and a wrapCommand helper for Commander action handlers.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// ── Error codes ──────────────────────────────────────────────────────────────
|
|
9
|
+
|
|
10
|
+
export type ErrorCode =
|
|
11
|
+
| 'MISSING_ENV'
|
|
12
|
+
| 'NOT_FOUND'
|
|
13
|
+
| 'SUPABASE_ERROR'
|
|
14
|
+
| 'VALIDATION_ERROR'
|
|
15
|
+
| 'AUTH_ERROR'
|
|
16
|
+
| 'NETWORK_ERROR'
|
|
17
|
+
| 'FILE_ERROR'
|
|
18
|
+
| 'UNKNOWN'
|
|
19
|
+
|
|
20
|
+
// ── CliError ─────────────────────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
export class CliError extends Error {
|
|
23
|
+
constructor(
|
|
24
|
+
message: string,
|
|
25
|
+
public code: ErrorCode,
|
|
26
|
+
public suggestion?: string,
|
|
27
|
+
) {
|
|
28
|
+
super(message)
|
|
29
|
+
this.name = 'CliError'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
const SUGGESTIONS: Record<string, string> = {
|
|
36
|
+
MISSING_ENV:
|
|
37
|
+
'Ensure the required environment variables are set in your .env file or shell.',
|
|
38
|
+
NOT_FOUND: 'Double-check the identifier (slug, ID, or name) and try again.',
|
|
39
|
+
SUPABASE_ERROR:
|
|
40
|
+
'Verify your Supabase URL and service key are correct and the database is reachable.',
|
|
41
|
+
VALIDATION_ERROR: 'Review the command options with --help.',
|
|
42
|
+
AUTH_ERROR:
|
|
43
|
+
'Check your API token or credentials and make sure they have not expired.',
|
|
44
|
+
NETWORK_ERROR:
|
|
45
|
+
'Check your internet connection and verify the remote service is available.',
|
|
46
|
+
FILE_ERROR: 'Verify the file path exists and you have read/write permissions.',
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function classifyError(err: unknown): { code: ErrorCode; message: string } {
|
|
50
|
+
if (err instanceof CliError) {
|
|
51
|
+
return { code: err.code, message: err.message }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (err instanceof Error) {
|
|
55
|
+
const msg = err.message
|
|
56
|
+
|
|
57
|
+
// Supabase / fetch errors
|
|
58
|
+
if (msg.includes('PGRST') || msg.includes('supabase') || msg.includes('relation')) {
|
|
59
|
+
return { code: 'SUPABASE_ERROR', message: msg }
|
|
60
|
+
}
|
|
61
|
+
if (msg.includes('ENOENT') || msg.includes('no such file')) {
|
|
62
|
+
return { code: 'FILE_ERROR', message: msg }
|
|
63
|
+
}
|
|
64
|
+
if (msg.includes('ECONNREFUSED') || msg.includes('fetch failed') || msg.includes('ETIMEDOUT')) {
|
|
65
|
+
return { code: 'NETWORK_ERROR', message: msg }
|
|
66
|
+
}
|
|
67
|
+
if (
|
|
68
|
+
msg.includes('OPTIMAL_SUPABASE_URL') ||
|
|
69
|
+
msg.includes('OPTIMAL_SUPABASE_SERVICE_KEY') ||
|
|
70
|
+
msg.includes('env')
|
|
71
|
+
) {
|
|
72
|
+
return { code: 'MISSING_ENV', message: msg }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return { code: 'UNKNOWN', message: msg }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return { code: 'UNKNOWN', message: String(err) }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ── handleError ──────────────────────────────────────────────────────────────
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Format an error for CLI output, print it to stderr, and exit with code 1.
|
|
85
|
+
*/
|
|
86
|
+
export function handleError(err: unknown): never {
|
|
87
|
+
const { code, message } = classifyError(err)
|
|
88
|
+
|
|
89
|
+
const suggestion =
|
|
90
|
+
err instanceof CliError && err.suggestion
|
|
91
|
+
? err.suggestion
|
|
92
|
+
: SUGGESTIONS[code] ?? ''
|
|
93
|
+
|
|
94
|
+
const lines: string[] = [
|
|
95
|
+
'',
|
|
96
|
+
` Error [${code}]: ${message}`,
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
if (suggestion) {
|
|
100
|
+
lines.push(` Suggestion: ${suggestion}`)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
lines.push('')
|
|
104
|
+
|
|
105
|
+
process.stderr.write(lines.join('\n'))
|
|
106
|
+
process.exit(1)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ── wrapCommand ──────────────────────────────────────────────────────────────
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Wrap a Commander action handler so any thrown error is routed through
|
|
113
|
+
* handleError, giving the user a consistent, friendly message instead of
|
|
114
|
+
* an unhandled-rejection stack trace.
|
|
115
|
+
*
|
|
116
|
+
* Usage:
|
|
117
|
+
* .action(wrapCommand(async (opts) => { ... }))
|
|
118
|
+
*/
|
|
119
|
+
export function wrapCommand<A extends unknown[]>(
|
|
120
|
+
fn: (...args: A) => Promise<void>,
|
|
121
|
+
): (...args: A) => Promise<void> {
|
|
122
|
+
return async (...args: A) => {
|
|
123
|
+
try {
|
|
124
|
+
await fn(...args)
|
|
125
|
+
} catch (err) {
|
|
126
|
+
handleError(err)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
package/lib/format.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight CLI output formatting — ANSI colors, tables, badges.
|
|
3
|
+
* Zero external dependencies. Respects NO_COLOR env var.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// ── ANSI escape codes ───────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
const CODES: Record<string, [number, number]> = {
|
|
9
|
+
red: [31, 39],
|
|
10
|
+
green: [32, 39],
|
|
11
|
+
yellow: [33, 39],
|
|
12
|
+
blue: [34, 39],
|
|
13
|
+
cyan: [36, 39],
|
|
14
|
+
gray: [90, 39],
|
|
15
|
+
bold: [1, 22],
|
|
16
|
+
dim: [2, 22],
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type Color = 'red' | 'green' | 'yellow' | 'blue' | 'cyan' | 'gray' | 'bold' | 'dim'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Wrap text in ANSI escape codes for the given color/style.
|
|
23
|
+
* Returns plain text when NO_COLOR env var is set.
|
|
24
|
+
*/
|
|
25
|
+
export function colorize(text: string, color: Color): string {
|
|
26
|
+
if (process.env.NO_COLOR !== undefined) return text
|
|
27
|
+
const [open, close] = CODES[color]
|
|
28
|
+
return `\x1b[${open}m${text}\x1b[${close}m`
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ── Table rendering ─────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
/** Strip ANSI escape sequences to measure visible string width. */
|
|
34
|
+
function stripAnsi(s: string): string {
|
|
35
|
+
return s.replace(/\x1b\[\d+m/g, '')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Render a bordered ASCII table with auto-sized columns.
|
|
40
|
+
* Headers are rendered in bold.
|
|
41
|
+
*/
|
|
42
|
+
export function table(headers: string[], rows: string[][]): string {
|
|
43
|
+
// Compute column widths from headers and all rows
|
|
44
|
+
const colWidths = headers.map((h, i) => {
|
|
45
|
+
let max = stripAnsi(h).length
|
|
46
|
+
for (const row of rows) {
|
|
47
|
+
const cell = row[i] ?? ''
|
|
48
|
+
const len = stripAnsi(cell).length
|
|
49
|
+
if (len > max) max = len
|
|
50
|
+
}
|
|
51
|
+
return max
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
function padCell(cell: string, width: number): string {
|
|
55
|
+
const visible = stripAnsi(cell).length
|
|
56
|
+
return cell + ' '.repeat(Math.max(0, width - visible))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const sep = '+-' + colWidths.map(w => '-'.repeat(w)).join('-+-') + '-+'
|
|
60
|
+
const headerRow = '| ' + headers.map((h, i) => padCell(colorize(h, 'bold'), colWidths[i])).join(' | ') + ' |'
|
|
61
|
+
|
|
62
|
+
const bodyRows = rows.map(row =>
|
|
63
|
+
'| ' + row.map((cell, i) => padCell(cell ?? '', colWidths[i])).join(' | ') + ' |'
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
return [sep, headerRow, sep, ...bodyRows, sep].join('\n')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ── Status & priority badges ────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
const STATUS_COLORS: Record<string, Color> = {
|
|
72
|
+
done: 'green',
|
|
73
|
+
in_progress: 'blue',
|
|
74
|
+
blocked: 'red',
|
|
75
|
+
ready: 'cyan',
|
|
76
|
+
backlog: 'gray',
|
|
77
|
+
cancelled: 'dim',
|
|
78
|
+
review: 'yellow',
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Return a colored status string (e.g. "done" in green). */
|
|
82
|
+
export function statusBadge(status: string): string {
|
|
83
|
+
const color = STATUS_COLORS[status] ?? 'dim'
|
|
84
|
+
return colorize(status, color)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const PRIORITY_COLORS: Record<number, Color> = {
|
|
88
|
+
1: 'red',
|
|
89
|
+
2: 'yellow',
|
|
90
|
+
3: 'blue',
|
|
91
|
+
4: 'gray',
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Return a colored priority label (e.g. "P1" in red). */
|
|
95
|
+
export function priorityBadge(p: number): string {
|
|
96
|
+
const color = PRIORITY_COLORS[p] ?? 'gray'
|
|
97
|
+
return colorize(`P${p}`, color)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ── Logging helpers ─────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
/** Print a green success message with a check mark prefix. */
|
|
103
|
+
export function success(msg: string): void {
|
|
104
|
+
console.log(`${colorize('\u2713', 'green')} ${msg}`)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Print a red error message with an X prefix. */
|
|
108
|
+
export function error(msg: string): void {
|
|
109
|
+
console.error(`${colorize('\u2717', 'red')} ${msg}`)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Print a yellow warning message with a warning prefix. */
|
|
113
|
+
export function warn(msg: string): void {
|
|
114
|
+
console.warn(`${colorize('\u26a0', 'yellow')} ${msg}`)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Print a blue info message with an info prefix. */
|
|
118
|
+
export function info(msg: string): void {
|
|
119
|
+
console.log(`${colorize('\u2139', 'blue')} ${msg}`)
|
|
120
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process'
|
|
2
|
+
import { promisify } from 'node:util'
|
|
3
|
+
|
|
4
|
+
const run = promisify(execFile)
|
|
5
|
+
|
|
6
|
+
/** Map of short app names to absolute filesystem paths. */
|
|
7
|
+
const APP_PATHS: Record<string, string> = {
|
|
8
|
+
'dashboard-returnpro': '/home/optimal/dashboard-returnpro',
|
|
9
|
+
'optimalos': '/home/optimal/optimalos',
|
|
10
|
+
'portfolio': '/home/optimal/portfolio-2026',
|
|
11
|
+
'newsletter-preview': '/home/optimal/projects/newsletter-preview',
|
|
12
|
+
'wes': '/home/optimal/wes-dashboard',
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* List all available app names that can be deployed.
|
|
17
|
+
*/
|
|
18
|
+
export function listApps(): string[] {
|
|
19
|
+
return Object.keys(APP_PATHS)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resolve an app name to its absolute filesystem path.
|
|
24
|
+
* Throws if the app name is unknown.
|
|
25
|
+
*/
|
|
26
|
+
export function getAppPath(appName: string): string {
|
|
27
|
+
const appPath = APP_PATHS[appName]
|
|
28
|
+
if (!appPath) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Unknown app: ${appName}. Available: ${Object.keys(APP_PATHS).join(', ')}`
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
return appPath
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Deploy an app to Vercel using the `vercel` CLI.
|
|
38
|
+
*
|
|
39
|
+
* Uses `execFile` (not `exec`) to avoid shell injection.
|
|
40
|
+
* The `--cwd` flag tells Vercel which project directory to deploy.
|
|
41
|
+
*
|
|
42
|
+
* @param appName - Short name from APP_PATHS (e.g. 'portfolio', 'dashboard-returnpro')
|
|
43
|
+
* @param prod - If true, deploys to production (--prod flag). Otherwise preview.
|
|
44
|
+
* @returns The deployment URL printed by Vercel CLI.
|
|
45
|
+
*/
|
|
46
|
+
export async function deploy(appName: string, prod = false): Promise<string> {
|
|
47
|
+
const appPath = getAppPath(appName)
|
|
48
|
+
const args = prod
|
|
49
|
+
? ['--prod', '--cwd', appPath]
|
|
50
|
+
: ['--cwd', appPath]
|
|
51
|
+
const { stdout } = await run('vercel', args, { timeout: 120_000 })
|
|
52
|
+
return stdout.trim()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Run the Optimal workstation health check script.
|
|
57
|
+
*
|
|
58
|
+
* Checks: n8n, Affine (Docker + HTTP), Strapi CMS (systemd + HTTP),
|
|
59
|
+
* Git repo sync status, Docker containers, and OptimalOS dev server.
|
|
60
|
+
*
|
|
61
|
+
* @returns The full text output of the health check script.
|
|
62
|
+
*/
|
|
63
|
+
export async function healthCheck(): Promise<string> {
|
|
64
|
+
const { stdout } = await run(
|
|
65
|
+
'bash',
|
|
66
|
+
['/home/optimal/scripts/health-check.sh'],
|
|
67
|
+
{ timeout: 30_000 }
|
|
68
|
+
)
|
|
69
|
+
return stdout.trim()
|
|
70
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import 'dotenv/config'
|
|
2
|
+
import { execFile } from 'node:child_process'
|
|
3
|
+
import { promisify } from 'node:util'
|
|
4
|
+
import { readdir, writeFile } from 'node:fs/promises'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
|
|
7
|
+
const run = promisify(execFile)
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Types
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
export interface MigrateOptions {
|
|
14
|
+
target: 'returnpro' | 'optimalos'
|
|
15
|
+
dryRun?: boolean // if true, just show what would be pushed
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface MigrateResult {
|
|
19
|
+
success: boolean
|
|
20
|
+
target: string
|
|
21
|
+
output: string
|
|
22
|
+
errors: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Constants
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
/** Hardcoded project directories — these live on Carlos's machine. */
|
|
30
|
+
const PROJECT_DIRS: Record<'returnpro' | 'optimalos', string> = {
|
|
31
|
+
returnpro: '/home/optimal/dashboard-returnpro',
|
|
32
|
+
optimalos: '/home/optimal/optimalos',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Helpers
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
function getProjectDir(target: 'returnpro' | 'optimalos'): string {
|
|
40
|
+
return PROJECT_DIRS[target]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function migrationsDir(target: 'returnpro' | 'optimalos'): string {
|
|
44
|
+
return join(getProjectDir(target), 'supabase', 'migrations')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Generate a timestamp string in YYYYMMDDHHMMSS format (UTC).
|
|
49
|
+
*/
|
|
50
|
+
function timestamp(): string {
|
|
51
|
+
const now = new Date()
|
|
52
|
+
const pad = (n: number, len = 2) => String(n).padStart(len, '0')
|
|
53
|
+
return [
|
|
54
|
+
now.getUTCFullYear(),
|
|
55
|
+
pad(now.getUTCMonth() + 1),
|
|
56
|
+
pad(now.getUTCDate()),
|
|
57
|
+
pad(now.getUTCHours()),
|
|
58
|
+
pad(now.getUTCMinutes()),
|
|
59
|
+
pad(now.getUTCSeconds()),
|
|
60
|
+
].join('')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
// Public API
|
|
65
|
+
// ---------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Run `supabase db push --linked` (or `--dry-run` if requested) against the
|
|
69
|
+
* given target project directory.
|
|
70
|
+
*
|
|
71
|
+
* Uses `execFile` (not `exec`) to avoid shell injection.
|
|
72
|
+
* The `cwd` option switches the Supabase CLI into the correct project.
|
|
73
|
+
*/
|
|
74
|
+
export async function migrateDb(opts: MigrateOptions): Promise<MigrateResult> {
|
|
75
|
+
const { target, dryRun = false } = opts
|
|
76
|
+
const projectDir = getProjectDir(target)
|
|
77
|
+
|
|
78
|
+
const args: string[] = ['db', 'push', '--linked']
|
|
79
|
+
if (dryRun) args.push('--dry-run')
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const { stdout, stderr } = await run('supabase', args, {
|
|
83
|
+
cwd: projectDir,
|
|
84
|
+
timeout: 120_000,
|
|
85
|
+
})
|
|
86
|
+
return {
|
|
87
|
+
success: true,
|
|
88
|
+
target,
|
|
89
|
+
output: stdout.trim(),
|
|
90
|
+
errors: stderr.trim(),
|
|
91
|
+
}
|
|
92
|
+
} catch (err: unknown) {
|
|
93
|
+
const e = err as { stdout?: string; stderr?: string; message?: string }
|
|
94
|
+
return {
|
|
95
|
+
success: false,
|
|
96
|
+
target,
|
|
97
|
+
output: (e.stdout ?? '').trim(),
|
|
98
|
+
errors: (e.stderr ?? e.message ?? String(err)).trim(),
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* List migration `.sql` files in the target's `supabase/migrations/` directory,
|
|
105
|
+
* sorted chronologically (by filename, which starts with a YYYYMMDDHHMMSS prefix).
|
|
106
|
+
*
|
|
107
|
+
* Returns only filenames, not full paths.
|
|
108
|
+
*/
|
|
109
|
+
export async function listPendingMigrations(
|
|
110
|
+
target: 'returnpro' | 'optimalos'
|
|
111
|
+
): Promise<string[]> {
|
|
112
|
+
const dir = migrationsDir(target)
|
|
113
|
+
const entries = await readdir(dir)
|
|
114
|
+
return entries
|
|
115
|
+
.filter((f) => f.endsWith('.sql'))
|
|
116
|
+
.sort() // lexicographic == chronological given the YYYYMMDDHHMMSS prefix
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Create a new empty migration file in the target's `supabase/migrations/`
|
|
121
|
+
* directory.
|
|
122
|
+
*
|
|
123
|
+
* The filename format is `{YYYYMMDDHHMMSS}_{name}.sql` (UTC timestamp).
|
|
124
|
+
* Returns the full absolute path of the created file.
|
|
125
|
+
*/
|
|
126
|
+
export async function createMigration(
|
|
127
|
+
target: 'returnpro' | 'optimalos',
|
|
128
|
+
name: string
|
|
129
|
+
): Promise<string> {
|
|
130
|
+
const sanitized = name.replace(/\s+/g, '_').replace(/[^a-zA-Z0-9_]/g, '')
|
|
131
|
+
const filename = `${timestamp()}_${sanitized}.sql`
|
|
132
|
+
const fullPath = join(migrationsDir(target), filename)
|
|
133
|
+
|
|
134
|
+
await writeFile(
|
|
135
|
+
fullPath,
|
|
136
|
+
`-- Migration: ${sanitized}\n-- Target: ${target}\n-- Created: ${new Date().toISOString()}\n\n`,
|
|
137
|
+
{ encoding: 'utf8' }
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
return fullPath
|
|
141
|
+
}
|