@codapult/cli 0.2.0 → 0.4.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.
@@ -1,25 +1,25 @@
1
1
  import { existsSync, readFileSync } from 'node:fs';
2
2
  import { resolve } from 'node:path';
3
3
  import { findProjectRoot, readJsonFile } from '../utils/project.js';
4
+ import { getAdapters, getAuthMethods, getFeatures, getOauthProviders, FEATURE_ENV, } from '../utils/env-config.js';
4
5
  import { heading, success, fail, info, dim, label } from '../utils/ui.js';
6
+ /** Extracts a quoted string from a TS object literal: `field: 'value'` or `field: "value"`. */
5
7
  function extractTsObjectField(content, field) {
6
- const regex = new RegExp(`${field}:\\s*['"\`]([^'"\`]+)['"\`]`);
8
+ const regex = new RegExp(`\\b${field}\\s*:\\s*['"\`]([^'"\`]+)['"\`]`);
7
9
  const match = regex.exec(content);
8
10
  return match?.[1] ?? null;
9
11
  }
10
- function extractTsBooleanFields(content) {
11
- const result = {};
12
- const regex = /(\w+):\s*(true|false)/g;
13
- let match;
14
- while ((match = regex.exec(content)) !== null) {
15
- result[match[1]] = match[2] === 'true';
16
- }
17
- return result;
12
+ /** Extracts a numeric literal: `field: 123` or `field: 0.5`. */
13
+ function extractTsNumericField(content, field) {
14
+ const regex = new RegExp(`\\b${field}\\s*:\\s*([\\d.]+)`);
15
+ const match = regex.exec(content);
16
+ return match?.[1] ?? null;
18
17
  }
19
- function extractEnvValue(envContent, key) {
20
- const regex = new RegExp(`^${key}=(.*)$`, 'm');
21
- const match = regex.exec(envContent);
22
- return match?.[1]?.trim() ?? null;
18
+ /** Extracts a boolean literal: `field: true|false`. */
19
+ function extractTsBoolField(content, field) {
20
+ const regex = new RegExp(`\\b${field}\\s*:\\s*(true|false)`);
21
+ const match = regex.exec(content);
22
+ return match ? match[1] === 'true' : null;
23
23
  }
24
24
  export function configShowCommand() {
25
25
  const root = findProjectRoot();
@@ -36,6 +36,8 @@ export function configShowCommand() {
36
36
  label('Version', typeof pkg.version === 'string' ? pkg.version : 'unknown');
37
37
  }
38
38
  console.log();
39
+ const envPath = resolve(root, '.env.local');
40
+ const envContent = existsSync(envPath) ? readFileSync(envPath, 'utf-8') : '';
39
41
  // --- App config (src/config/app.ts) ---
40
42
  const appConfigPath = resolve(root, 'src/config/app.ts');
41
43
  if (existsSync(appConfigPath)) {
@@ -43,73 +45,78 @@ export function configShowCommand() {
43
45
  info('Brand');
44
46
  const name = extractTsObjectField(content, 'name');
45
47
  const description = extractTsObjectField(content, 'description');
46
- const url = extractTsObjectField(content, 'url');
47
- const supportEmail = extractTsObjectField(content, 'supportEmail');
48
+ const logo = extractTsObjectField(content, 'logo');
49
+ const favicon = extractTsObjectField(content, 'favicon');
48
50
  if (name)
49
51
  label(' Name', name);
50
52
  if (description)
51
53
  label(' Description', description);
52
- if (url)
53
- label(' URL', url);
54
- if (supportEmail)
55
- label(' Support email', supportEmail);
54
+ if (logo)
55
+ label(' Logo', logo);
56
+ if (favicon)
57
+ label(' Favicon', favicon);
56
58
  console.log();
57
- info('Features');
58
- const features = extractTsBooleanFields(content);
59
- const featureKeys = Object.keys(features).filter((k) => !['magicLink', 'passkeys', 'twoFactor'].includes(k));
60
- const enabled = featureKeys.filter((k) => features[k]);
61
- const disabled = featureKeys.filter((k) => !features[k]);
62
- if (enabled.length > 0) {
63
- for (const f of enabled)
64
- success(f);
65
- }
66
- if (disabled.length > 0) {
67
- for (const f of disabled)
68
- dim(` ○ ${f}`);
69
- }
59
+ info('Company');
60
+ const contactEmail = extractTsObjectField(content, 'contactEmail');
61
+ const githubUrl = extractTsObjectField(content, 'githubUrl');
62
+ if (contactEmail)
63
+ label(' Contact email', contactEmail);
64
+ if (githubUrl)
65
+ label(' GitHub', githubUrl);
70
66
  console.log();
71
- info('Auth');
72
- const magicLink = features.magicLink;
73
- const passkeys = features.passkeys;
74
- const twoFactor = features.twoFactor;
75
- if (magicLink !== undefined)
76
- label(' Magic Link', magicLink ? 'enabled' : 'disabled');
77
- if (passkeys !== undefined)
78
- label(' Passkeys', passkeys ? 'enabled' : 'disabled');
79
- if (twoFactor !== undefined)
80
- label(' 2FA (TOTP)', twoFactor ? 'enabled' : 'disabled');
81
- const oauthMatch = /oauthProviders:\s*\[([^\]]*)\]/.exec(content);
82
- if (oauthMatch) {
83
- const providers = oauthMatch[1]
84
- .split(',')
85
- .map((s) => s.trim().replace(/['"]/g, ''))
86
- .filter(Boolean);
87
- label(' OAuth providers', providers.join(', ') || 'none');
88
- }
67
+ info('AI');
68
+ const defaultModel = extractTsObjectField(content, 'defaultModel');
69
+ const ragEnabled = extractTsBoolField(content, 'ragEnabled');
70
+ const ragMaxChunks = extractTsNumericField(content, 'ragMaxChunks');
71
+ const ragMinScore = extractTsNumericField(content, 'ragMinScore');
72
+ if (defaultModel)
73
+ label(' Default model', defaultModel);
74
+ if (ragEnabled !== null)
75
+ label(' RAG', ragEnabled ? 'enabled' : 'disabled');
76
+ if (ragMaxChunks)
77
+ label(' RAG max chunks', ragMaxChunks);
78
+ if (ragMinScore)
79
+ label(' RAG min score', ragMinScore);
89
80
  }
90
81
  else {
91
82
  dim('src/config/app.ts not found — run `codapult setup`');
92
83
  }
93
84
  console.log();
94
- // --- Adapters from .env.local ---
95
- info('Adapters');
96
- const envPath = resolve(root, '.env.local');
97
- if (existsSync(envPath)) {
98
- const envContent = readFileSync(envPath, 'utf-8');
99
- const adapters = [
100
- ['AUTH_PROVIDER', 'Auth'],
101
- ['PAYMENT_PROVIDER', 'Payments'],
102
- ['STORAGE_PROVIDER', 'Storage'],
103
- ['NOTIFICATION_TRANSPORT', 'Notifications'],
104
- ['JOB_PROVIDER', 'Background jobs'],
105
- ];
106
- for (const [key, name] of adapters) {
107
- const value = extractEnvValue(envContent, key);
108
- label(` ${name}`, value ?? 'not set');
109
- }
110
- }
111
- else {
112
- dim('.env.local not found');
85
+ if (!existsSync(envPath)) {
86
+ dim('.env.local not found — adapters / features / auth methods unavailable');
87
+ console.log();
88
+ return;
113
89
  }
90
+ // --- Adapters (from .env.local) ---
91
+ info('Adapters');
92
+ const adapters = getAdapters(envContent);
93
+ label(' Database', adapters.database);
94
+ label(' Auth', adapters.auth);
95
+ label(' Payments', adapters.payments);
96
+ label(' Storage', adapters.storage);
97
+ label(' Background jobs', adapters.jobs);
98
+ label(' Notifications', adapters.notifications);
99
+ label(' Embedding', adapters.embedding);
100
+ label(' Vector store', adapters.vectorStore);
101
+ console.log();
102
+ // --- Auth methods (from .env.local) ---
103
+ info('Auth methods');
104
+ const authMethods = getAuthMethods(envContent);
105
+ label(' Magic Link', authMethods.magicLink ? 'enabled' : 'disabled');
106
+ label(' Passkeys', authMethods.passkeys ? 'enabled' : 'disabled');
107
+ label(' 2FA (TOTP)', authMethods.twoFactor ? 'enabled' : 'disabled');
108
+ const oauth = getOauthProviders(envContent);
109
+ label(' OAuth providers', oauth.length > 0 ? oauth.join(', ') : 'none');
110
+ console.log();
111
+ // --- Feature toggles (from .env.local + ENABLE_* defaults) ---
112
+ info('Features');
113
+ const features = getFeatures(envContent);
114
+ const featureKeys = Object.keys(FEATURE_ENV);
115
+ const enabled = featureKeys.filter((k) => features[k]);
116
+ const disabled = featureKeys.filter((k) => !features[k]);
117
+ for (const f of enabled)
118
+ success(f);
119
+ for (const f of disabled)
120
+ dim(` ○ ${f}`);
114
121
  console.log();
115
122
  }
@@ -1,7 +1,10 @@
1
- import { existsSync, readFileSync } from 'node:fs';
1
+ import { existsSync, readFileSync, readdirSync } from 'node:fs';
2
2
  import { resolve } from 'node:path';
3
3
  import { execSync } from 'node:child_process';
4
4
  import { findProjectRoot, readJsonFile } from '../utils/project.js';
5
+ import { resolveManifest } from '../utils/manifest.js';
6
+ import { findPageConflicts, findPageBackups } from '../utils/patchers.js';
7
+ import { findProviderIssues, getAdapters, readEnvVar } from '../utils/env-config.js';
5
8
  import { heading, success, fail, warn, info, dim, label } from '../utils/ui.js';
6
9
  function checkExists(root, path, description) {
7
10
  const exists = existsSync(resolve(root, path));
@@ -13,17 +16,6 @@ function checkExists(root, path, description) {
13
16
  }
14
17
  return exists;
15
18
  }
16
- function checkEnvVar(envContent, key, description) {
17
- const regex = new RegExp(`^${key}=.+`, 'm');
18
- const found = regex.test(envContent);
19
- if (found) {
20
- success(description);
21
- }
22
- else {
23
- warn(`${description} — not set: ${key}`);
24
- }
25
- return found;
26
- }
27
19
  function tryExec(cmd, cwd) {
28
20
  try {
29
21
  return execSync(cmd, { cwd, encoding: 'utf-8', stdio: 'pipe' }).trim();
@@ -70,14 +62,33 @@ export function doctorCommand() {
70
62
  else {
71
63
  success('.env.local exists');
72
64
  const envContent = readFileSync(envPath, 'utf-8');
73
- if (!checkEnvVar(envContent, 'TURSO_DATABASE_URL', 'Database URL'))
74
- issues += 1;
75
- if (!checkEnvVar(envContent, 'BETTER_AUTH_SECRET', 'Auth secret'))
76
- warnings += 1;
77
- if (!checkEnvVar(envContent, 'AUTH_PROVIDER', 'Auth provider'))
78
- warnings += 1;
79
- if (!checkEnvVar(envContent, 'PAYMENT_PROVIDER', 'Payment provider'))
65
+ const adapters = getAdapters(envContent);
66
+ label(' DB_PROVIDER', adapters.database);
67
+ label(' AUTH_PROVIDER', adapters.auth);
68
+ label(' PAYMENT_PROVIDER', adapters.payments);
69
+ label(' STORAGE_PROVIDER', adapters.storage);
70
+ label(' JOB_PROVIDER', adapters.jobs);
71
+ label(' NOTIFICATION_TRANSPORT', adapters.notifications);
72
+ const providerIssues = findProviderIssues(envContent);
73
+ if (providerIssues.length === 0) {
74
+ success('All provider-conditional env vars are set');
75
+ }
76
+ else {
77
+ for (const i of providerIssues) {
78
+ if (i.severity === 'error') {
79
+ fail(`${i.key}: ${i.message}`);
80
+ issues += 1;
81
+ }
82
+ else {
83
+ warn(`${i.key}: ${i.message}`);
84
+ warnings += 1;
85
+ }
86
+ }
87
+ }
88
+ if (!readEnvVar(envContent, 'NEXT_PUBLIC_APP_URL')) {
89
+ warn('NEXT_PUBLIC_APP_URL not set — defaults to http://localhost:3000');
80
90
  warnings += 1;
91
+ }
81
92
  }
82
93
  console.log();
83
94
  // --- Dependencies ---
@@ -125,6 +136,49 @@ export function doctorCommand() {
125
136
  success('TypeScript compiles cleanly');
126
137
  }
127
138
  console.log();
139
+ // --- Plugins ---
140
+ info('Plugins');
141
+ const pluginsDir = resolve(root, 'src/plugins');
142
+ const installedPlugins = existsSync(pluginsDir)
143
+ ? readdirSync(pluginsDir)
144
+ .filter((f) => f.endsWith('.ts') && f !== 'index.ts')
145
+ .map((f) => f.replace('.ts', ''))
146
+ : [];
147
+ if (installedPlugins.length === 0) {
148
+ dim('No plugins installed.');
149
+ }
150
+ else {
151
+ let pluginProblems = 0;
152
+ for (const pName of installedPlugins) {
153
+ const resolved = resolveManifest(root, pName);
154
+ if (!resolved?.manifest.install.pages)
155
+ continue;
156
+ const conflicts = findPageConflicts(root, resolved.manifest.install.pages).filter((c) => !c.isStub);
157
+ if (conflicts.length === 0)
158
+ continue;
159
+ pluginProblems += 1;
160
+ warn(`${pName}: ${conflicts.length} page conflict(s) shadow plugin stubs`);
161
+ for (const c of conflicts) {
162
+ dim(` ${c.conflictRel} (shadows ${c.pagePath})`);
163
+ }
164
+ dim(` Fix: codapult plugins add ${pName} (will back up and overwrite)`);
165
+ }
166
+ if (pluginProblems === 0) {
167
+ success(`${installedPlugins.length} plugin(s): no page conflicts`);
168
+ }
169
+ else {
170
+ issues += pluginProblems;
171
+ }
172
+ const orphanBackups = findPageBackups(root);
173
+ if (orphanBackups.length > 0) {
174
+ warn(`${orphanBackups.length} page backup file(s) found:`);
175
+ for (const b of orphanBackups)
176
+ dim(` ${b}`);
177
+ dim(' These are restored automatically when the owning plugin is removed.');
178
+ warnings += 1;
179
+ }
180
+ }
181
+ console.log();
128
182
  // --- Git ---
129
183
  info('Git');
130
184
  const gitRemotes = tryExec('git remote -v', root);
@@ -4,7 +4,7 @@ import { resolve } from 'node:path';
4
4
  import { findProjectRoot } from '../utils/project.js';
5
5
  import { resolveManifest } from '../utils/manifest.js';
6
6
  import { clonePlugin } from '../utils/git.js';
7
- import { patchSchemaImports, patchSchemaTables, patchDbReExports, patchNextConfig, createPluginRegistration, patchPages, patchEnvFile, patchPackageJson, } from '../utils/patchers.js';
7
+ import { patchSchemaImports, patchSchemaTables, patchDbReExports, patchNextConfig, createPluginRegistration, patchPages, findPageConflicts, patchEnvFile, patchPackageJson, } from '../utils/patchers.js';
8
8
  import { heading, success, fail, warn, info, dim, confirm } from '../utils/ui.js';
9
9
  function execQuiet(cmd, cwd) {
10
10
  try {
@@ -113,9 +113,34 @@ export async function pluginsAddCommand(name, options) {
113
113
  }
114
114
  // 7. Create page wrappers
115
115
  if (manifest.install.pages && Object.keys(manifest.install.pages).length > 0) {
116
+ const pageEntries = manifest.install.pages;
117
+ const conflicts = findPageConflicts(root, pageEntries);
118
+ const nonStubConflicts = conflicts.filter((c) => !c.isStub);
119
+ if (nonStubConflicts.length > 0) {
120
+ warn(`Found ${nonStubConflicts.length} existing page file(s) that would collide with the plugin:`);
121
+ for (const c of nonStubConflicts) {
122
+ const hint = c.isExactPath ? 'same path' : 'same route, different extension';
123
+ dim(` ${c.conflictRel} (${hint})`);
124
+ }
125
+ if (options.ci) {
126
+ info(`CI mode: backing up conflicting files as *.codapult-bak-${manifest.name}`);
127
+ }
128
+ else {
129
+ const proceed = await confirm(`Back up conflicting files as *.codapult-bak-${manifest.name} and install?`, true);
130
+ if (!proceed) {
131
+ fail('Aborted by user. Resolve conflicts manually, then re-run.');
132
+ dim('To restore later, rename *.codapult-bak-* back or reinstall the plugin.');
133
+ process.exit(1);
134
+ }
135
+ }
136
+ }
116
137
  info('Creating page wrappers...');
117
- patchPages(root, manifest.name, manifest.install.pages, 'add');
118
- success(`${Object.keys(manifest.install.pages).length} page(s) created`);
138
+ patchPages(root, manifest.name, pageEntries, 'add', { onConflict: 'backup' });
139
+ success(`${Object.keys(pageEntries).length} page(s) created`);
140
+ if (nonStubConflicts.length > 0) {
141
+ dim(`Original files preserved as *.codapult-bak-${manifest.name}. ` +
142
+ `They are restored automatically when you run: codapult plugins remove ${manifest.name}`);
143
+ }
119
144
  }
120
145
  // 8. Register plugin (src/plugins/<name>.ts + regenerate barrel)
121
146
  info('Registering plugin...');