|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * PIN (#26332) — a fresh `os create plugin NAME --in-repo` passes its own |
| 5 | + * `typecheck` and `build` scripts. |
| 6 | + * |
| 7 | + * ## The defect |
| 8 | + * |
| 9 | + * Once the in-repo scaffold extended `tsconfig.base.json` and reached its own |
| 10 | + * `src/`, its first `pnpm typecheck` in the new package was red twice over: |
| 11 | + * |
| 12 | + * - TS5103 "Invalid value for '--ignoreDeprecations'": the manifest declared |
| 13 | + * `typescript` as `SCAFFOLD_TYPESCRIPT_RANGE` (`^5.3.0`, installed as |
| 14 | + * 5.9.3), while the base sets `ignoreDeprecations: "6.0"`, a value only |
| 15 | + * TypeScript 6 accepts. Every sibling package declares the workspace's |
| 16 | + * TypeScript 6 range. |
| 17 | + * - TS6133 "'context' is declared but its value is never read": the starter |
| 18 | + * `init(context)` against the base's `noUnusedParameters`. |
| 19 | + * |
| 20 | + * ## What is run, and where |
| 21 | + * |
| 22 | + * The in-repo emission is materialised through `create`'s own renderers into a |
| 23 | + * sandbox laid out like this repository: the real `tsconfig.base.json` copied |
| 24 | + * to the sandbox root, and the scaffold at `packages/plugins/plugin-NAME` |
| 25 | + * beneath it, so the emitted `extends` resolves exactly as it does in the |
| 26 | + * monorepo. The emitted `typecheck` and `build` scripts are then spawned with |
| 27 | + * the workspace's own `tsc`, the compiler the in-repo manifest now declares. |
| 28 | + * |
| 29 | + * The sandbox lives under this package's `node_modules` (git-ignored), the |
| 30 | + * placement `scaffold-emission-typechecks.test.ts` documents: an emitted |
| 31 | + * `import … from '@objectstack/spec/…'` resolves by the ordinary upward walk. |
| 32 | + * TS2307 on the spec package means its `dist/` is not built; build it with |
| 33 | + * `pnpm --filter '@objectstack/cli^...' build`. |
| 34 | + * |
| 35 | + * The TypeScript VERSION a contributor installs cannot be spawned here without |
| 36 | + * an install, so the TS5103 half is pinned on the declaration instead: the |
| 37 | + * in-repo range is the range this package declares for the very `tsc` the |
| 38 | + * cases spawn (the workspace's TypeScript, which the root and every sibling |
| 39 | + * declare too), and its floor is a major that accepts the base's |
| 40 | + * `ignoreDeprecations`. The standalone range is pinned unchanged beside it. |
| 41 | + * |
| 42 | + * ## The control |
| 43 | + * |
| 44 | + * A planted unused parameter must fail the same `typecheck` with TS6133. Green |
| 45 | + * there would mean the scaffold no longer compiles under the base's |
| 46 | + * strictness, and the verdict on the starter would mean nothing. |
| 47 | + */ |
| 48 | + |
| 49 | +import { afterAll, describe, expect, it } from 'vitest'; |
| 50 | +import { spawnSync } from 'node:child_process'; |
| 51 | +import fs from 'node:fs'; |
| 52 | +import { createRequire } from 'node:module'; |
| 53 | +import path from 'node:path'; |
| 54 | +import { fileURLToPath } from 'node:url'; |
| 55 | +import { IN_REPO_TYPESCRIPT_RANGE, templates } from '../src/commands/create.js'; |
| 56 | +import { SCAFFOLD_TYPESCRIPT_RANGE } from '../src/commands/init.js'; |
| 57 | +import { childEnv } from './helpers/serve-process.js'; |
| 58 | + |
| 59 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 60 | +const PKG = path.resolve(HERE, '..'); |
| 61 | +const BASE_TSCONFIG = path.resolve(HERE, '../../..', 'tsconfig.base.json'); |
| 62 | + |
| 63 | +const require = createRequire(import.meta.url); |
| 64 | +const TSC = require.resolve('typescript/bin/tsc'); |
| 65 | +const ts = require('typescript') as typeof import('typescript'); |
| 66 | + |
| 67 | +const PROJECT = 'probe'; |
| 68 | +const template = templates.plugin; |
| 69 | + |
| 70 | +const TMP_ROOT = fs.mkdtempSync(path.join(HERE, '..', 'node_modules', '.create-in-repo-typecheck-')); |
| 71 | +afterAll(() => { |
| 72 | + fs.rmSync(TMP_ROOT, { recursive: true, force: true }); |
| 73 | +}); |
| 74 | + |
| 75 | +interface Manifest { |
| 76 | + scripts: Record<string, string>; |
| 77 | + devDependencies: Record<string, string>; |
| 78 | +} |
| 79 | + |
| 80 | +function manifestFor(placement: 'in-repo' | 'standalone'): Manifest { |
| 81 | + return template.filesFor(placement)['package.json'](PROJECT) as Manifest; |
| 82 | +} |
| 83 | + |
| 84 | +/** Materialise the in-repo emission under a copy of this repo's base; returns the scaffold dir. */ |
| 85 | +function emitInRepo(): string { |
| 86 | + const workspace = fs.mkdtempSync(path.join(TMP_ROOT, 'ws-')); |
| 87 | + fs.copyFileSync(BASE_TSCONFIG, path.join(workspace, 'tsconfig.base.json')); |
| 88 | + const dir = path.join(workspace, template.inRepoDir, template.dirName(PROJECT)); |
| 89 | + for (const [filePath, render] of Object.entries(template.filesFor('in-repo'))) { |
| 90 | + const content = render(PROJECT); |
| 91 | + fs.mkdirSync(path.dirname(path.join(dir, filePath)), { recursive: true }); |
| 92 | + fs.writeFileSync( |
| 93 | + path.join(dir, filePath), |
| 94 | + typeof content === 'string' ? content : JSON.stringify(content, null, 2) + '\n', |
| 95 | + ); |
| 96 | + } |
| 97 | + return dir; |
| 98 | +} |
| 99 | + |
| 100 | +/** Spawn one emitted script, its `tsc` resolved to the workspace's compiler. */ |
| 101 | +function runScript(dir: string, script: string): { code: number | null; output: string } { |
| 102 | + const [bin, ...args] = manifestFor('in-repo').scripts[script].trim().split(/\s+/); |
| 103 | + expect(bin, `the in-repo \`${script}\` script no longer runs tsc`).toBe('tsc'); |
| 104 | + const res = spawnSync(process.execPath, [TSC, '--pretty', 'false', ...args], { |
| 105 | + cwd: dir, |
| 106 | + encoding: 'utf-8', |
| 107 | + env: childEnv({ NO_COLOR: '1' }), |
| 108 | + }); |
| 109 | + return { code: res.status, output: `${res.stdout ?? ''}${res.stderr ?? ''}` }; |
| 110 | +} |
| 111 | + |
| 112 | +/** The `ignoreDeprecations` value the base sets, read through the compiler's own config parser. */ |
| 113 | +function baseIgnoreDeprecations(): string { |
| 114 | + const read = ts.readConfigFile(BASE_TSCONFIG, ts.sys.readFile); |
| 115 | + expect(read.error, 'tsconfig.base.json did not parse').toBeUndefined(); |
| 116 | + return String(read.config.compilerOptions.ignoreDeprecations); |
| 117 | +} |
| 118 | + |
| 119 | +describe('os create plugin --in-repo: the scaffold passes its own scripts', () => { |
| 120 | + it('a planted unused parameter fails `typecheck` with TS6133 (control)', () => { |
| 121 | + const dir = emitInRepo(); |
| 122 | + fs.writeFileSync(path.join(dir, 'src', 'canary.ts'), 'export function canary(unusedProbe: string) {}\n'); |
| 123 | + |
| 124 | + const { code, output } = runScript(dir, 'typecheck'); |
| 125 | + |
| 126 | + expect(code, `the canary compiled, so the scaffold is not under the base's strictness:\n${output}`).not.toBe(0); |
| 127 | + expect(output).toContain('TS6133'); |
| 128 | + expect(output).toContain('unusedProbe'); |
| 129 | + }, 120_000); |
| 130 | + |
| 131 | + it('`typecheck` and `build` exit 0, and `build` leaves the starter test out of dist', () => { |
| 132 | + const dir = emitInRepo(); |
| 133 | + |
| 134 | + const typecheck = runScript(dir, 'typecheck'); |
| 135 | + expect(typecheck.code, `a fresh in-repo scaffold fails its own \`typecheck\`:\n${typecheck.output}`).toBe(0); |
| 136 | + |
| 137 | + const build = runScript(dir, 'build'); |
| 138 | + expect(build.code, `a fresh in-repo scaffold fails its own \`build\`:\n${build.output}`).toBe(0); |
| 139 | + expect(fs.existsSync(path.join(dir, 'dist', 'index.js'))).toBe(true); |
| 140 | + expect(fs.existsSync(path.join(dir, 'dist', 'index.d.ts'))).toBe(true); |
| 141 | + expect(fs.existsSync(path.join(dir, 'dist', 'index.test.js'))).toBe(false); |
| 142 | + }, 120_000); |
| 143 | +}); |
| 144 | + |
| 145 | +describe('os create plugin: the TypeScript each placement declares', () => { |
| 146 | + it('in-repo declares the workspace\'s TypeScript, the range of the tsc the cases above spawn', () => { |
| 147 | + const own = JSON.parse(fs.readFileSync(path.join(PKG, 'package.json'), 'utf8')) as Manifest; |
| 148 | + expect(manifestFor('in-repo').devDependencies.typescript).toBe(own.devDependencies.typescript); |
| 149 | + expect(IN_REPO_TYPESCRIPT_RANGE).toBe(own.devDependencies.typescript); |
| 150 | + }); |
| 151 | + |
| 152 | + it('in-repo\'s TypeScript floor accepts the base\'s ignoreDeprecations (TS5103)', () => { |
| 153 | + const floorMajor = Number(/^\^?(\d+)\./.exec(manifestFor('in-repo').devDependencies.typescript)?.[1]); |
| 154 | + const acceptedFrom = Number(/^(\d+)\./.exec(baseIgnoreDeprecations())?.[1]); |
| 155 | + expect(Number.isFinite(acceptedFrom), 'the base sets no numeric ignoreDeprecations').toBe(true); |
| 156 | + expect(floorMajor).toBeGreaterThanOrEqual(acceptedFrom); |
| 157 | + }); |
| 158 | + |
| 159 | + it('standalone still declares SCAFFOLD_TYPESCRIPT_RANGE', () => { |
| 160 | + expect(manifestFor('standalone').devDependencies.typescript).toBe(SCAFFOLD_TYPESCRIPT_RANGE); |
| 161 | + }); |
| 162 | +}); |
0 commit comments