@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.
- package/dist/commands/config-show.js +76 -69
- package/dist/commands/doctor.js +73 -19
- package/dist/commands/plugins.js +28 -3
- package/dist/commands/setup.js +164 -21
- package/dist/mcp/resources.js +1 -1
- package/dist/mcp/tools/plugins.js +9 -2
- package/dist/mcp/tools/project.js +9 -18
- package/dist/mcp/tools/project.test.js +15 -6
- package/dist/utils/env-config.d.ts +65 -0
- package/dist/utils/env-config.js +211 -0
- package/dist/utils/patchers.d.ts +62 -1
- package/dist/utils/patchers.js +269 -8
- package/dist/utils/patchers.test.js +212 -2
- package/package.json +1 -1
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { patchSchemaImports, patchSchemaTables, patchDbReExports, patchNextConfig, regenerateBarrel, createPluginRegistration, patchPages, patchEnvFile, patchPackageJson, } from './patchers.js';
|
|
2
|
+
import { patchSchemaImports, patchSchemaTables, patchDbReExports, patchNextConfig, regenerateBarrel, createPluginRegistration, patchPages, findPageConflicts, findPageBackups, readPageExtensions, PageConflictError, patchEnvFile, patchPackageJson, } from './patchers.js';
|
|
3
3
|
vi.mock('node:fs');
|
|
4
|
-
const { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync, rmSync, rmdirSync } = await import('node:fs');
|
|
4
|
+
const { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync, renameSync, rmSync, rmdirSync, statSync, } = await import('node:fs');
|
|
5
5
|
const mockedExists = vi.mocked(existsSync);
|
|
6
6
|
const mockedRead = vi.mocked(readFileSync);
|
|
7
7
|
const mockedWrite = vi.mocked(writeFileSync);
|
|
8
8
|
const mockedMkdir = vi.mocked(mkdirSync);
|
|
9
9
|
const mockedReaddir = vi.mocked(readdirSync);
|
|
10
|
+
const mockedRename = vi.mocked(renameSync);
|
|
10
11
|
const mockedRm = vi.mocked(rmSync);
|
|
11
12
|
const mockedRmdir = vi.mocked(rmdirSync);
|
|
13
|
+
const mockedStat = vi.mocked(statSync);
|
|
12
14
|
const ROOT = '/project';
|
|
13
15
|
beforeEach(() => {
|
|
14
16
|
vi.resetAllMocks();
|
|
@@ -323,6 +325,214 @@ describe('patchPages', () => {
|
|
|
323
325
|
expect(mockedRm).toHaveBeenCalled();
|
|
324
326
|
expect(mockedRmdir).toHaveBeenCalled();
|
|
325
327
|
});
|
|
328
|
+
it('backs up a user-written page at a different extension before writing the stub', () => {
|
|
329
|
+
const pages = {
|
|
330
|
+
'src/app/[locale]/(marketing)/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome',
|
|
331
|
+
};
|
|
332
|
+
const userTsx = '/project/src/app/[locale]/(marketing)/welcome/page.tsx';
|
|
333
|
+
// Only the .tsx sibling exists on disk; no backup file yet.
|
|
334
|
+
mockedExists.mockImplementation((p) => p === userTsx);
|
|
335
|
+
mockedRead.mockReturnValue('export default function Welcome(): JSX.Element { return <div>old</div>; }');
|
|
336
|
+
patchPages(ROOT, 'onboarding', pages, 'add');
|
|
337
|
+
expect(mockedRename).toHaveBeenCalledWith(userTsx, `${userTsx}.codapult-bak-onboarding`);
|
|
338
|
+
const writtenPath = mockedWrite.mock.calls[0][0];
|
|
339
|
+
const writtenContent = mockedWrite.mock.calls[0][1];
|
|
340
|
+
expect(writtenPath).toContain('page.ts');
|
|
341
|
+
expect(writtenPath).not.toMatch(/page\.tsx$/);
|
|
342
|
+
expect(writtenContent).toContain("export { default } from '@codapult/plugin-onboarding/pages/welcome';");
|
|
343
|
+
});
|
|
344
|
+
it('does not re-backup when a backup already exists (idempotent re-install)', () => {
|
|
345
|
+
const pages = {
|
|
346
|
+
'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome',
|
|
347
|
+
};
|
|
348
|
+
const userTsx = '/project/src/app/welcome/page.tsx';
|
|
349
|
+
const backup = `${userTsx}.codapult-bak-onboarding`;
|
|
350
|
+
mockedExists.mockImplementation((p) => {
|
|
351
|
+
const path = p;
|
|
352
|
+
return path === userTsx || path === backup;
|
|
353
|
+
});
|
|
354
|
+
mockedRead.mockReturnValue('export default function UserPage() {}');
|
|
355
|
+
patchPages(ROOT, 'onboarding', pages, 'add');
|
|
356
|
+
expect(mockedRename).not.toHaveBeenCalled();
|
|
357
|
+
expect(mockedRm).toHaveBeenCalledWith(userTsx);
|
|
358
|
+
});
|
|
359
|
+
it('silently overwrites sibling plugin stubs without creating a backup', () => {
|
|
360
|
+
const pages = {
|
|
361
|
+
'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome',
|
|
362
|
+
};
|
|
363
|
+
const siblingStub = '/project/src/app/welcome/page.tsx';
|
|
364
|
+
mockedExists.mockImplementation((p) => p === siblingStub);
|
|
365
|
+
mockedRead.mockReturnValue("export { default } from 'some-other-package/pages/welcome';\n");
|
|
366
|
+
patchPages(ROOT, 'onboarding', pages, 'add');
|
|
367
|
+
expect(mockedRename).not.toHaveBeenCalled();
|
|
368
|
+
expect(mockedRm).toHaveBeenCalledWith(siblingStub);
|
|
369
|
+
});
|
|
370
|
+
it('is idempotent when the same stub was written previously', () => {
|
|
371
|
+
const pages = {
|
|
372
|
+
'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome',
|
|
373
|
+
};
|
|
374
|
+
const targetPath = '/project/src/app/welcome/page.ts';
|
|
375
|
+
const stub = "export { default } from '@codapult/plugin-onboarding/pages/welcome';\n";
|
|
376
|
+
mockedExists.mockImplementation((p) => p === targetPath);
|
|
377
|
+
mockedRead.mockReturnValue(stub);
|
|
378
|
+
patchPages(ROOT, 'onboarding', pages, 'add');
|
|
379
|
+
expect(mockedRename).not.toHaveBeenCalled();
|
|
380
|
+
expect(mockedRm).not.toHaveBeenCalled();
|
|
381
|
+
// Still writes (idempotent re-write of identical content)
|
|
382
|
+
expect(mockedWrite).toHaveBeenCalled();
|
|
383
|
+
});
|
|
384
|
+
it('throws PageConflictError when onConflict is "fail"', () => {
|
|
385
|
+
const pages = {
|
|
386
|
+
'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome',
|
|
387
|
+
};
|
|
388
|
+
const userTsx = '/project/src/app/welcome/page.tsx';
|
|
389
|
+
mockedExists.mockImplementation((p) => p === userTsx);
|
|
390
|
+
mockedRead.mockReturnValue('export default function User() {}');
|
|
391
|
+
expect(() => patchPages(ROOT, 'onboarding', pages, 'add', { onConflict: 'fail' })).toThrow(PageConflictError);
|
|
392
|
+
expect(mockedRename).not.toHaveBeenCalled();
|
|
393
|
+
expect(mockedWrite).not.toHaveBeenCalled();
|
|
394
|
+
});
|
|
395
|
+
it('restores backup files on remove', () => {
|
|
396
|
+
const pages = {
|
|
397
|
+
'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome',
|
|
398
|
+
};
|
|
399
|
+
// Target page.ts exists; its directory has one matching backup entry.
|
|
400
|
+
mockedExists.mockImplementation((p) => {
|
|
401
|
+
const path = p;
|
|
402
|
+
return (path === '/project/src/app/welcome/page.ts' ||
|
|
403
|
+
path === '/project/src/app/welcome' ||
|
|
404
|
+
path.endsWith('src/app'));
|
|
405
|
+
});
|
|
406
|
+
mockedReaddir.mockImplementation((d) => {
|
|
407
|
+
const dir = d;
|
|
408
|
+
if (dir === '/project/src/app/welcome') {
|
|
409
|
+
return ['page.tsx.codapult-bak-onboarding'];
|
|
410
|
+
}
|
|
411
|
+
return [];
|
|
412
|
+
});
|
|
413
|
+
patchPages(ROOT, 'onboarding', pages, 'remove');
|
|
414
|
+
expect(mockedRm).toHaveBeenCalledWith('/project/src/app/welcome/page.ts');
|
|
415
|
+
expect(mockedRename).toHaveBeenCalledWith('/project/src/app/welcome/page.tsx.codapult-bak-onboarding', '/project/src/app/welcome/page.tsx');
|
|
416
|
+
});
|
|
417
|
+
it('does not restore a backup when a live file already occupies its target name', () => {
|
|
418
|
+
const pages = {
|
|
419
|
+
'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome',
|
|
420
|
+
};
|
|
421
|
+
const live = '/project/src/app/welcome/page.tsx';
|
|
422
|
+
mockedExists.mockImplementation((p) => {
|
|
423
|
+
const path = p;
|
|
424
|
+
return (path === '/project/src/app/welcome/page.ts' ||
|
|
425
|
+
path === live ||
|
|
426
|
+
path === '/project/src/app/welcome');
|
|
427
|
+
});
|
|
428
|
+
mockedReaddir.mockImplementation((d) => {
|
|
429
|
+
const dir = d;
|
|
430
|
+
if (dir === '/project/src/app/welcome') {
|
|
431
|
+
return ['page.ts', 'page.tsx', 'page.tsx.codapult-bak-onboarding'];
|
|
432
|
+
}
|
|
433
|
+
return [];
|
|
434
|
+
});
|
|
435
|
+
patchPages(ROOT, 'onboarding', pages, 'remove');
|
|
436
|
+
expect(mockedRename).not.toHaveBeenCalled();
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
describe('findPageConflicts', () => {
|
|
440
|
+
it('reports a sibling file at a different Next.js page extension', () => {
|
|
441
|
+
const pages = { 'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome' };
|
|
442
|
+
const sibling = '/project/src/app/welcome/page.tsx';
|
|
443
|
+
mockedExists.mockImplementation((p) => p === sibling);
|
|
444
|
+
mockedRead.mockReturnValue('export default function User() {}');
|
|
445
|
+
const conflicts = findPageConflicts(ROOT, pages);
|
|
446
|
+
expect(conflicts).toHaveLength(1);
|
|
447
|
+
expect(conflicts[0].isStub).toBe(false);
|
|
448
|
+
expect(conflicts[0].isExactPath).toBe(false);
|
|
449
|
+
expect(conflicts[0].conflictRel).toBe('src/app/welcome/page.tsx');
|
|
450
|
+
});
|
|
451
|
+
it('returns [] when the only file at the target path is our own stub', () => {
|
|
452
|
+
const pages = { 'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome' };
|
|
453
|
+
const target = '/project/src/app/welcome/page.ts';
|
|
454
|
+
mockedExists.mockImplementation((p) => p === target);
|
|
455
|
+
mockedRead.mockReturnValue("export { default } from '@codapult/plugin-onboarding/pages/welcome';\n");
|
|
456
|
+
expect(findPageConflicts(ROOT, pages)).toEqual([]);
|
|
457
|
+
});
|
|
458
|
+
it('flags foreign stubs as conflicts but marks them isStub=true', () => {
|
|
459
|
+
const pages = { 'src/app/welcome/page.ts': '@codapult/plugin-onboarding/pages/welcome' };
|
|
460
|
+
const sibling = '/project/src/app/welcome/page.tsx';
|
|
461
|
+
mockedExists.mockImplementation((p) => p === sibling);
|
|
462
|
+
mockedRead.mockReturnValue("export { default } from 'some-other/pages/welcome';\n");
|
|
463
|
+
const conflicts = findPageConflicts(ROOT, pages);
|
|
464
|
+
expect(conflicts).toHaveLength(1);
|
|
465
|
+
expect(conflicts[0].isStub).toBe(true);
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
describe('readPageExtensions', () => {
|
|
469
|
+
it('reads a custom pageExtensions array from next.config.ts', () => {
|
|
470
|
+
const config = `import type { NextConfig } from 'next';
|
|
471
|
+
const nextConfig: NextConfig = {
|
|
472
|
+
pageExtensions: ['mdx', 'ts', 'tsx'],
|
|
473
|
+
output: 'standalone',
|
|
474
|
+
};
|
|
475
|
+
export default nextConfig;
|
|
476
|
+
`;
|
|
477
|
+
mockedExists.mockImplementation((p) => p.endsWith('next.config.ts'));
|
|
478
|
+
mockedRead.mockReturnValue(config);
|
|
479
|
+
expect(readPageExtensions(ROOT)).toEqual(['mdx', 'ts', 'tsx']);
|
|
480
|
+
});
|
|
481
|
+
it('supports assignment syntax (`pageExtensions = [...]`)', () => {
|
|
482
|
+
const config = `const pageExtensions = ['page.ts', 'ts', 'tsx'];`;
|
|
483
|
+
mockedExists.mockImplementation((p) => p.endsWith('next.config.js'));
|
|
484
|
+
mockedRead.mockReturnValue(config);
|
|
485
|
+
// 'page.ts' is rejected (contains a dot); the rest pass the shape check.
|
|
486
|
+
expect(readPageExtensions(ROOT)).toEqual(['ts', 'tsx']);
|
|
487
|
+
});
|
|
488
|
+
it('falls back to defaults when the declaration is inside a comment', () => {
|
|
489
|
+
const config = `const nextConfig = {
|
|
490
|
+
// pageExtensions: ['mdx'],
|
|
491
|
+
output: 'standalone',
|
|
492
|
+
};`;
|
|
493
|
+
mockedExists.mockImplementation((p) => p.endsWith('next.config.ts'));
|
|
494
|
+
mockedRead.mockReturnValue(config);
|
|
495
|
+
expect(readPageExtensions(ROOT)).toEqual(['tsx', 'ts', 'jsx', 'js', 'mjs', 'cjs']);
|
|
496
|
+
});
|
|
497
|
+
it('falls back to defaults when next.config is missing', () => {
|
|
498
|
+
mockedExists.mockReturnValue(false);
|
|
499
|
+
expect(readPageExtensions(ROOT)).toEqual(['tsx', 'ts', 'jsx', 'js', 'mjs', 'cjs']);
|
|
500
|
+
});
|
|
501
|
+
it('falls back to defaults when the array cannot be parsed', () => {
|
|
502
|
+
const config = `const nextConfig = { pageExtensions: computeExts() };`;
|
|
503
|
+
mockedExists.mockImplementation((p) => p.endsWith('next.config.ts'));
|
|
504
|
+
mockedRead.mockReturnValue(config);
|
|
505
|
+
expect(readPageExtensions(ROOT)).toEqual(['tsx', 'ts', 'jsx', 'js', 'mjs', 'cjs']);
|
|
506
|
+
});
|
|
507
|
+
});
|
|
508
|
+
describe('findPageBackups', () => {
|
|
509
|
+
it('walks src/app and returns every backup file', () => {
|
|
510
|
+
mockedExists.mockImplementation((p) => {
|
|
511
|
+
const path = p;
|
|
512
|
+
return path === '/project/src/app' || path.startsWith('/project/src/app');
|
|
513
|
+
});
|
|
514
|
+
mockedReaddir.mockImplementation((d) => {
|
|
515
|
+
const dir = d;
|
|
516
|
+
if (dir === '/project/src/app') {
|
|
517
|
+
return ['welcome', 'other.txt'];
|
|
518
|
+
}
|
|
519
|
+
if (dir === '/project/src/app/welcome') {
|
|
520
|
+
return ['page.ts', 'page.tsx.codapult-bak-onboarding'];
|
|
521
|
+
}
|
|
522
|
+
return [];
|
|
523
|
+
});
|
|
524
|
+
mockedStat.mockImplementation((p) => {
|
|
525
|
+
const path = p;
|
|
526
|
+
const isDir = path === '/project/src/app/welcome';
|
|
527
|
+
return { isDirectory: () => isDir };
|
|
528
|
+
});
|
|
529
|
+
const backups = findPageBackups(ROOT);
|
|
530
|
+
expect(backups).toEqual(['src/app/welcome/page.tsx.codapult-bak-onboarding']);
|
|
531
|
+
});
|
|
532
|
+
it('returns [] when src/app does not exist', () => {
|
|
533
|
+
mockedExists.mockReturnValue(false);
|
|
534
|
+
expect(findPageBackups(ROOT)).toEqual([]);
|
|
535
|
+
});
|
|
326
536
|
});
|
|
327
537
|
// ---------------------------------------------------------------------------
|
|
328
538
|
// patchEnvFile
|