|
| 1 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" |
| 2 | +import { tmpdir } from "node:os" |
| 3 | +import { dirname, join } from "node:path" |
| 4 | +import { pathToFileURL } from "node:url" |
| 5 | + |
| 6 | +const tempRoot = mkdtempSync(join(tmpdir(), "opentui-runtime-plugin-cjs-experiment-")) |
| 7 | +const packageRoot = join(tempRoot, "app") |
| 8 | +const externalPackageDir = join(packageRoot, "node_modules", "runtime-plugin-cjs-fixture") |
| 9 | +const externalPackageEntryPath = join(externalPackageDir, "index.js") |
| 10 | +const repoRoot = join(import.meta.dir, "..") |
| 11 | +const solidPackageRoot = join(repoRoot, "packages", "solid") |
| 12 | +const entryRoot = join(solidPackageRoot, ".runtime-plugin-cjs-experiment") |
| 13 | + |
| 14 | +const installDependency = (cwd: string, packageName: string): void => { |
| 15 | + const result = Bun.spawnSync(["bun", "add", packageName], { |
| 16 | + cwd, |
| 17 | + stdout: "pipe", |
| 18 | + stderr: "pipe", |
| 19 | + env: process.env, |
| 20 | + }) |
| 21 | + |
| 22 | + if (result.exitCode !== 0) { |
| 23 | + throw new Error( |
| 24 | + [`Failed to install ${packageName}`, result.stdout.toString(), result.stderr.toString()].filter(Boolean).join("\n"), |
| 25 | + ) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const writeJson = (path: string, value: unknown): void => { |
| 30 | + mkdirSync(dirname(path), { recursive: true }) |
| 31 | + writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`) |
| 32 | +} |
| 33 | + |
| 34 | +const runImport = (label: string, script: string): void => { |
| 35 | + mkdirSync(entryRoot, { recursive: true }) |
| 36 | + const entryPath = join(entryRoot, `${label}.ts`) |
| 37 | + writeFileSync(entryPath, script) |
| 38 | + |
| 39 | + const result = Bun.spawnSync([process.execPath, entryPath], { |
| 40 | + cwd: solidPackageRoot, |
| 41 | + stdout: "pipe", |
| 42 | + stderr: "pipe", |
| 43 | + env: process.env, |
| 44 | + }) |
| 45 | + |
| 46 | + console.log(`scenario=${label}`) |
| 47 | + console.log(`exitCode=${result.exitCode}`) |
| 48 | + |
| 49 | + const stdout = result.stdout.toString().trim() |
| 50 | + if (stdout) { |
| 51 | + console.log(`stdout=${JSON.stringify(stdout)}`) |
| 52 | + } |
| 53 | + |
| 54 | + const stderr = result.stderr.toString().trim() |
| 55 | + if (stderr) { |
| 56 | + console.log(`stderr=${JSON.stringify(stderr)}`) |
| 57 | + } |
| 58 | + |
| 59 | + console.log("---") |
| 60 | +} |
| 61 | + |
| 62 | +writeJson(join(packageRoot, "package.json"), { |
| 63 | + name: "runtime-plugin-cjs-experiment", |
| 64 | + private: true, |
| 65 | + type: "module", |
| 66 | +}) |
| 67 | + |
| 68 | +installDependency(packageRoot, "jsonc-parser@3.3.1") |
| 69 | +installDependency(packageRoot, "@tarquinen/opencode-dcp@3.1.4") |
| 70 | + |
| 71 | +writeJson(join(externalPackageDir, "package.json"), { |
| 72 | + name: "runtime-plugin-cjs-fixture", |
| 73 | + private: true, |
| 74 | + type: "module", |
| 75 | + exports: "./index.js", |
| 76 | +}) |
| 77 | + |
| 78 | +writeFileSync( |
| 79 | + externalPackageEntryPath, |
| 80 | + [ |
| 81 | + 'import { parse } from "jsonc-parser"', |
| 82 | + 'export const parsed = parse("{\\"value\\":1}")', |
| 83 | + 'console.log(`fixture-value=${parsed.value}`)', |
| 84 | + ].join("\n"), |
| 85 | +) |
| 86 | + |
| 87 | +const jsoncParserPackagePath = join(packageRoot, "node_modules", "jsonc-parser", "package.json") |
| 88 | +const jsoncParserPackage = JSON.parse(readFileSync(jsoncParserPackagePath, "utf8")) as { |
| 89 | + main?: string |
| 90 | + module?: string |
| 91 | +} |
| 92 | + |
| 93 | +const resolveScript = ` |
| 94 | +const externalEntryUrl = ${JSON.stringify(pathToFileURL(externalPackageEntryPath).href)} |
| 95 | +console.log('resolved=' + import.meta.resolve('jsonc-parser', externalEntryUrl)) |
| 96 | +` |
| 97 | + |
| 98 | +const withoutRuntimePluginScript = ` |
| 99 | +await import(${JSON.stringify(pathToFileURL(externalPackageEntryPath).href)}) |
| 100 | +` |
| 101 | + |
| 102 | +const withRuntimePluginScript = ` |
| 103 | +await import(${JSON.stringify(pathToFileURL(join(import.meta.dir, "..", "packages", "solid", "scripts", "runtime-plugin-support.ts")).href)}) |
| 104 | +await import(${JSON.stringify(pathToFileURL(externalPackageEntryPath).href)}) |
| 105 | +` |
| 106 | + |
| 107 | +const realPackageWithoutRuntimePluginScript = ` |
| 108 | +const mod = await import(${JSON.stringify( |
| 109 | + pathToFileURL(join(packageRoot, "node_modules", "@tarquinen", "opencode-dcp", "dist", "index.js")).href, |
| 110 | +)}) |
| 111 | +console.log('default-export=' + typeof mod.default) |
| 112 | +` |
| 113 | + |
| 114 | +const realPackageWithRuntimePluginScript = ` |
| 115 | +await import(${JSON.stringify(pathToFileURL(join(import.meta.dir, "..", "packages", "solid", "scripts", "runtime-plugin-support.ts")).href)}) |
| 116 | +const mod = await import(${JSON.stringify( |
| 117 | + pathToFileURL(join(packageRoot, "node_modules", "@tarquinen", "opencode-dcp", "dist", "index.js")).href, |
| 118 | +)}) |
| 119 | +console.log('default-export=' + typeof mod.default) |
| 120 | +` |
| 121 | + |
| 122 | +try { |
| 123 | + console.log(`jsonc-parser.main=${jsoncParserPackage.main ?? ""}`) |
| 124 | + console.log(`jsonc-parser.module=${jsoncParserPackage.module ?? ""}`) |
| 125 | + console.log("---") |
| 126 | + |
| 127 | + runImport("resolve", resolveScript) |
| 128 | + runImport("without-runtime-plugin-support", withoutRuntimePluginScript) |
| 129 | + runImport("with-runtime-plugin-support", withRuntimePluginScript) |
| 130 | + runImport("real-package-without-runtime-plugin-support", realPackageWithoutRuntimePluginScript) |
| 131 | + runImport("real-package-with-runtime-plugin-support", realPackageWithRuntimePluginScript) |
| 132 | +} finally { |
| 133 | + rmSync(entryRoot, { recursive: true, force: true }) |
| 134 | + rmSync(tempRoot, { recursive: true, force: true }) |
| 135 | +} |
0 commit comments