@codapult/cli 0.17.2 → 0.18.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.
@@ -1,24 +1,18 @@
1
1
  import { existsSync, readFileSync, readdirSync } from 'node:fs';
2
2
  import { resolve } from 'node:path';
3
3
  import { z } from 'zod';
4
- import { findProjectRoot } from '../../utils/project.js';
4
+ import { checkProjectRoot } from '../../utils/project.js';
5
5
  import { ENV_FILE_NAME } from '../../utils/project-env.js';
6
6
  import { resolveManifest } from '../../utils/manifest.js';
7
7
  import { patchSchemaImports, patchSchemaImportsPg, patchSchemaTables, patchSchemaTablesPg, patchDbReExports, patchNextConfig, createPluginRegistration, patchPages, findPageConflicts, patchEnvFile, patchPackageJson, } from '../../utils/patchers.js';
8
8
  import { envSourceSchema } from './schemas.js';
9
- function getRoot() {
10
- const root = findProjectRoot();
11
- if (!root)
12
- throw new Error('Not inside a Codapult project');
13
- return root;
14
- }
15
9
  export function registerPluginTools(server) {
16
10
  server.registerTool('codapult_plugins_list', {
17
11
  title: 'List Plugins',
18
12
  description: 'List installed Codapult plugins with package names and versions',
19
13
  inputSchema: {},
20
14
  }, () => {
21
- const root = getRoot();
15
+ const root = checkProjectRoot('throw');
22
16
  const pluginsDir = resolve(root, 'src/plugins');
23
17
  if (!existsSync(pluginsDir)) {
24
18
  return {
@@ -55,7 +49,7 @@ export function registerPluginTools(server) {
55
49
  .describe('Preview changes without modifying the project'),
56
50
  },
57
51
  }, ({ name, env_source, dry_run }) => {
58
- const root = getRoot();
52
+ const root = checkProjectRoot('throw');
59
53
  const result = resolveManifest(root, name);
60
54
  if (!result) {
61
55
  return {
@@ -183,7 +177,7 @@ export function registerPluginTools(server) {
183
177
  .describe('Preview changes without modifying the project'),
184
178
  },
185
179
  }, ({ name, env_source, dry_run }) => {
186
- const root = getRoot();
180
+ const root = checkProjectRoot('throw');
187
181
  const result = resolveManifest(root, name);
188
182
  const manifest = result?.manifest ?? {
189
183
  name,
@@ -277,7 +271,7 @@ export function registerPluginTools(server) {
277
271
  .describe('Plugin name (e.g. "crm"). Omit to migrate all installed plugins.'),
278
272
  },
279
273
  }, ({ name }) => {
280
- const root = getRoot();
274
+ const root = checkProjectRoot('throw');
281
275
  const pluginsDir = resolve(root, 'src/plugins');
282
276
  const pluginNames = name
283
277
  ? [name]
@@ -2,19 +2,14 @@ import { execSync } from 'node:child_process';
2
2
  import { existsSync, readFileSync, readdirSync } from 'node:fs';
3
3
  import { resolve } from 'node:path';
4
4
  import { z } from 'zod';
5
- import { findProjectRoot, readProjectFile } from '../../utils/project.js';
5
+ import { checkProjectRoot, readProjectFile } from '../../utils/project.js';
6
6
  import { getProjectEnvOptions, loadProjectEnv } from '../../utils/project-env.js';
7
7
  import { getAdapters, getAuthMethods, getFeatures, getOauthProviders, } from '../../utils/env-config.js';
8
8
  import { envSourceSchema } from './schemas.js';
9
9
  import { commandResponse, runProjectCommand } from '../../utils/command.js';
10
+ import { runProjectChecks } from '../../utils/project-checks.js';
10
11
  import { collectMcpHealth } from '../../utils/health.js';
11
12
  import { collectAppConfigSummary } from '../../utils/config-report.js';
12
- function getRoot() {
13
- const root = findProjectRoot();
14
- if (!root)
15
- throw new Error('Not inside a Codapult project');
16
- return root;
17
- }
18
13
  export function registerProjectTools(server) {
19
14
  server.registerTool('codapult_project_status', {
20
15
  title: 'Project Status',
@@ -23,7 +18,7 @@ export function registerProjectTools(server) {
23
18
  env_source: envSourceSchema.optional(),
24
19
  },
25
20
  }, ({ env_source }) => {
26
- const root = getRoot();
21
+ const root = checkProjectRoot('throw');
27
22
  const pkg = JSON.parse(readFileSync(resolve(root, 'package.json'), 'utf-8'));
28
23
  const envContent = loadProjectEnv(root, getProjectEnvOptions(env_source)).content;
29
24
  const pluginsDir = resolve(root, 'src/plugins');
@@ -66,7 +61,7 @@ export function registerProjectTools(server) {
66
61
  title: 'Project Config',
67
62
  description: 'Read the app configuration from src/config/app.ts (brand, company)',
68
63
  }, () => {
69
- const root = getRoot();
64
+ const root = checkProjectRoot('throw');
70
65
  const content = readProjectFile(root, 'src/config/app.ts') ?? 'Config file not found';
71
66
  return { content: [{ type: 'text', text: content }] };
72
67
  });
@@ -80,25 +75,9 @@ export function registerProjectTools(server) {
80
75
  .describe('Which checks to run (default: all)'),
81
76
  },
82
77
  }, ({ checks }) => {
83
- const root = getRoot();
78
+ const root = checkProjectRoot('throw');
84
79
  const toRun = checks ?? ['lint', 'typecheck', 'test'];
85
- const commands = {
86
- lint: 'pnpm lint',
87
- typecheck: 'pnpm tsc --noEmit',
88
- test: 'pnpm test --run',
89
- };
90
- const results = {};
91
- for (const check of toRun) {
92
- const cmd = commands[check];
93
- if (!cmd)
94
- continue;
95
- try {
96
- results[check] = runProjectCommand(cmd, root);
97
- }
98
- catch {
99
- results[check] = runProjectCommand(cmd, root);
100
- }
101
- }
80
+ const results = runProjectChecks(root, toRun);
102
81
  return { content: [{ type: 'text', text: JSON.stringify(results, null, 2) }] };
103
82
  });
104
83
  server.registerTool('codapult_doctor', {
@@ -108,7 +87,7 @@ export function registerProjectTools(server) {
108
87
  env_source: envSourceSchema.optional(),
109
88
  },
110
89
  }, ({ env_source }) => {
111
- const root = getRoot();
90
+ const root = checkProjectRoot('throw');
112
91
  const report = collectMcpHealth(root, {
113
92
  envSource: env_source,
114
93
  });
@@ -119,7 +98,7 @@ export function registerProjectTools(server) {
119
98
  description: 'Get a compact, structured overview of the current Codapult project for AI-assisted work',
120
99
  inputSchema: {},
121
100
  }, () => {
122
- const root = getRoot();
101
+ const root = checkProjectRoot('throw');
123
102
  const packageJson = JSON.parse(readFileSync(resolve(root, 'package.json'), 'utf-8'));
124
103
  const files = ['AGENTS.md', '.env.example', 'src/config/app.ts', 'src/config/env.ts'];
125
104
  const existingFiles = files.filter((file) => existsSync(resolve(root, file)));
@@ -161,5 +140,5 @@ export function registerProjectTools(server) {
161
140
  title: 'Build Project',
162
141
  description: 'Run the production build and return structured stdout, stderr, and exit code',
163
142
  inputSchema: {},
164
- }, () => commandResponse(runProjectCommand('pnpm build', getRoot(), { timeout: 300_000 })));
143
+ }, () => commandResponse(runProjectCommand('pnpm build', checkProjectRoot('throw'), { timeout: 300_000 })));
165
144
  }
@@ -3,7 +3,7 @@ import { ENV_FILE_NAME } from '../../utils/project-env.js';
3
3
  vi.mock('node:fs');
4
4
  vi.mock('node:child_process');
5
5
  vi.mock('../../utils/project.js', () => ({
6
- findProjectRoot: vi.fn(),
6
+ checkProjectRoot: vi.fn(),
7
7
  readProjectFile: vi.fn(),
8
8
  readJsonFile: vi.fn(),
9
9
  }));
@@ -15,10 +15,10 @@ vi.mock('../../utils/project-env.js', () => ({
15
15
  }));
16
16
  const { existsSync, readFileSync, readdirSync } = await import('node:fs');
17
17
  const { execSync } = await import('node:child_process');
18
- const { findProjectRoot, readProjectFile, readJsonFile } = await import('../../utils/project.js');
18
+ const { checkProjectRoot, readProjectFile, readJsonFile } = await import('../../utils/project.js');
19
19
  const { loadProjectEnv } = await import('../../utils/project-env.js');
20
20
  const { registerProjectTools } = await import('./project.js');
21
- const mockedFindRoot = vi.mocked(findProjectRoot);
21
+ const mockedCheckRoot = vi.mocked(checkProjectRoot);
22
22
  const mockedReadProject = vi.mocked(readProjectFile);
23
23
  const mockedLoadProjectEnv = vi.mocked(loadProjectEnv);
24
24
  const mockedExists = vi.mocked(existsSync);
@@ -28,7 +28,7 @@ const mockedReadJson = vi.mocked(readJsonFile);
28
28
  const mockedExec = vi.mocked(execSync);
29
29
  beforeEach(() => {
30
30
  vi.resetAllMocks();
31
- mockedFindRoot.mockReturnValue('/project');
31
+ mockedCheckRoot.mockReturnValue('/project');
32
32
  mockedLoadProjectEnv.mockReturnValue({
33
33
  source: 'file',
34
34
  filePath: `/project/${ENV_FILE_NAME}`,
@@ -1,9 +1,12 @@
1
1
  export interface CommandResult {
2
2
  command: string;
3
+ status: 'passed' | 'failed' | 'not-configured';
3
4
  passed: boolean;
4
5
  exitCode: number;
5
6
  stdout: string;
6
7
  stderr: string;
8
+ timedOut?: boolean;
9
+ truncated?: boolean;
7
10
  }
8
11
  export declare function runProjectCommand(command: string, cwd: string, options?: {
9
12
  timeout?: number;
@@ -1,4 +1,18 @@
1
1
  import { execSync } from 'node:child_process';
2
+ const MAX_OUTPUT_CHARS = 20_000;
3
+ const MAX_BUFFER_BYTES = 2_000_000;
4
+ function redactOutput(value) {
5
+ return value
6
+ .replace(/-----BEGIN [A-Z ]+-----[\s\S]*?-----END [A-Z ]+-----/g, '[REDACTED PRIVATE KEY]')
7
+ .replace(/\b(?:sk_(?:live|test)_|pk_(?:live|test)_|AKIA|gh[pousr]_|github_pat_)[A-Za-z0-9_-]+/g, '[REDACTED TOKEN]')
8
+ .replace(/((?:api[_-]?key|secret|token|password|authorization|database[_-]?url)\s*[:=]\s*["']?)[^\s"'`,}]+/gi, '$1[REDACTED]');
9
+ }
10
+ function captureOutput(value) {
11
+ const redacted = redactOutput(value);
12
+ return redacted.length > MAX_OUTPUT_CHARS
13
+ ? { value: `${redacted.slice(0, MAX_OUTPUT_CHARS)}\n[output truncated]`, truncated: true }
14
+ : { value: redacted, truncated: false };
15
+ }
2
16
  export function runProjectCommand(command, cwd, options = {}) {
3
17
  try {
4
18
  const stdout = execSync(command, {
@@ -6,18 +20,35 @@ export function runProjectCommand(command, cwd, options = {}) {
6
20
  env: { ...process.env, ...options.env },
7
21
  stdio: 'pipe',
8
22
  timeout: options.timeout ?? 120_000,
23
+ maxBuffer: MAX_BUFFER_BYTES,
9
24
  }).toString();
10
- return { command, passed: true, exitCode: 0, stdout, stderr: '' };
25
+ const captured = captureOutput(stdout);
26
+ return {
27
+ command,
28
+ status: 'passed',
29
+ passed: true,
30
+ exitCode: 0,
31
+ stdout: captured.value,
32
+ stderr: '',
33
+ truncated: captured.truncated,
34
+ };
11
35
  }
12
36
  catch (error) {
13
37
  const execError = error;
14
38
  const exitCode = execError.status ?? 1;
39
+ const passed = exitCode === 0;
40
+ const stdout = captureOutput(execError.stdout?.toString() ?? '');
41
+ const stderr = captureOutput(execError.stderr?.toString() ?? '');
42
+ const timedOut = execError.code === 'ETIMEDOUT' || execError.signal === 'SIGTERM';
15
43
  return {
16
44
  command,
17
- passed: exitCode === 0,
45
+ status: passed ? 'passed' : 'failed',
46
+ passed,
18
47
  exitCode,
19
- stdout: execError.stdout?.toString() ?? '',
20
- stderr: execError.stderr?.toString() ?? '',
48
+ stdout: stdout.value,
49
+ stderr: timedOut ? `${stderr.value}\n[command timed out]` : stderr.value,
50
+ timedOut,
51
+ truncated: stdout.truncated || stderr.truncated,
21
52
  };
22
53
  }
23
54
  }
@@ -0,0 +1,5 @@
1
+ export declare const config: {
2
+ readonly appName: 'codapult';
3
+ readonly commandName: 'codapult';
4
+ readonly projectName: 'Codapult';
5
+ };
@@ -0,0 +1,5 @@
1
+ export const config = {
2
+ appName: 'codapult',
3
+ commandName: 'codapult',
4
+ projectName: 'Codapult',
5
+ };
@@ -0,0 +1,2 @@
1
+ /** Shared project-check adapter; implementation belongs to codapult-guard. */
2
+ export * from '@codapult/guard/project-checks';
@@ -0,0 +1,2 @@
1
+ /** Shared project-check adapter; implementation belongs to codapult-guard. */
2
+ export * from '@codapult/guard/project-checks';
@@ -3,6 +3,7 @@
3
3
  * (directory containing package.json with codapult-specific markers).
4
4
  */
5
5
  export declare function findProjectRoot(from?: string): string | undefined;
6
+ export declare function checkProjectRoot<T extends 'exit' | 'code' | 'throw'>(exit: T): T extends 'code' ? string | undefined : string;
6
7
  /** Read and parse a JSON file, returning null on failure. */
7
8
  export declare function readJsonFile(filePath: string): Record<string, unknown> | undefined;
8
9
  /** Check if a file exists relative to the project root. */
@@ -1,5 +1,7 @@
1
1
  import { existsSync, readFileSync } from 'node:fs';
2
2
  import { resolve, dirname } from 'node:path';
3
+ import { fail } from './ui.js';
4
+ import { config } from './config.js';
3
5
  /**
4
6
  * Walk upward from cwd to find the Codapult project root
5
7
  * (directory containing package.json with codapult-specific markers).
@@ -27,6 +29,20 @@ export function findProjectRoot(from = process.cwd()) {
27
29
  }
28
30
  return undefined;
29
31
  }
32
+ export function checkProjectRoot(exit) {
33
+ const root = findProjectRoot();
34
+ if (!root) {
35
+ const msg = `Not inside a ${config.projectName} project.`;
36
+ if (exit === 'throw')
37
+ throw new Error(msg);
38
+ fail(msg);
39
+ if (exit === 'exit')
40
+ process.exit(1);
41
+ else if (exit === 'code')
42
+ process.exitCode = 1;
43
+ }
44
+ return root;
45
+ }
30
46
  /** Read and parse a JSON file, returning null on failure. */
31
47
  export function readJsonFile(filePath) {
32
48
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codapult/cli",
3
- "version": "0.17.2",
3
+ "version": "0.18.1",
4
4
  "description": "Codapult CLI — manage, update, and diagnose your Codapult SaaS project",
5
5
  "license": "MIT",
6
6
  "packageManager": "pnpm@11.22.0+sha512.1ff870c4c6133dfd88fb2afc46dd13d47f09c9794b438c6fdb47ca98caf3bc16381ee0be93a091b8e3824cf01f889f46d7d9e20910fb0be1ab0fb5baa80dd621",
@@ -21,7 +21,9 @@
21
21
  "lint:fix": "eslint --fix .",
22
22
  "format": "prettier --write .",
23
23
  "format:check": "prettier --check .",
24
+ "release:check": "pnpm lint && pnpm format:check && pnpm typecheck && pnpm test && pnpm build && npm pack --dry-run",
24
25
  "test": "vitest run",
26
+ "test:coverage": "vitest run --coverage",
25
27
  "test:watch": "vitest",
26
28
  "typecheck": "tsc --noEmit",
27
29
  "prepublishOnly": "tsc",
@@ -44,6 +46,7 @@
44
46
  },
45
47
  "dependencies": {
46
48
  "@modelcontextprotocol/sdk": "^1.30.0",
49
+ "@codapult/guard": "^0.1.0",
47
50
  "commander": "^15.0.0",
48
51
  "picocolors": "^1.1.1",
49
52
  "typescript": "npm:@typescript/typescript6@^6.0.2",
@@ -57,6 +60,7 @@
57
60
  "@release-it/conventional-changelog": "^12.0.0",
58
61
  "@types/node": "^26.2.0",
59
62
  "@typescript/native": "npm:typescript@^7.0.2",
63
+ "@vitest/coverage-v8": "^4.1.11",
60
64
  "eslint": "^9.39.4",
61
65
  "eslint-config-prettier": "^10.1.8",
62
66
  "eslint-plugin-prettier": "^5.5.6",