verify-release-artifact.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { spawnSync } from 'node:child_process';
  2. import {
  3. copyFileSync,
  4. mkdirSync,
  5. mkdtempSync,
  6. readdirSync,
  7. readFileSync,
  8. rmSync,
  9. writeFileSync,
  10. } from 'node:fs';
  11. import { tmpdir } from 'node:os';
  12. import path from 'node:path';
  13. import { fileURLToPath } from 'node:url';
  14. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  15. const repoRoot = path.resolve(__dirname, '..');
  16. const distDir = path.join(repoRoot, 'dist');
  17. const suspiciousPathPatterns = [
  18. /\/Users\/[^\s'"`]+(?:node_modules|oh-my-opencode-slim)[^\s'"`]*/,
  19. /\/home\/[^\s'"`]+(?:node_modules|oh-my-opencode-slim)[^\s'"`]*/,
  20. ];
  21. const suspiciousImportPatterns = [/from\s+["']vscode-jsonrpc\/node["']/];
  22. const packagedRequiredFiles = [
  23. 'package.json',
  24. 'README.md',
  25. 'LICENSE',
  26. 'dist/index.js',
  27. 'dist/index.d.ts',
  28. 'dist/cli/index.js',
  29. 'oh-my-opencode-slim.schema.json',
  30. 'src/companion/companion-manifest.json',
  31. 'src/skills/simplify/SKILL.md',
  32. 'src/skills/codemap/SKILL.md',
  33. 'src/skills/clonedeps/SKILL.md',
  34. 'src/skills/deepwork/SKILL.md',
  35. 'src/skills/verification-planning/SKILL.md',
  36. 'src/skills/reflect/SKILL.md',
  37. 'src/skills/oh-my-opencode-slim/SKILL.md',
  38. 'src/skills/release-smoke-test/SKILL.md',
  39. 'src/skills/worktrees/SKILL.md',
  40. ];
  41. function fail(message: string): never {
  42. throw new Error(message);
  43. }
  44. function run(command: string, args: string[], options: { cwd?: string } = {}) {
  45. const result = spawnSync(command, args, {
  46. cwd: options.cwd ?? repoRoot,
  47. encoding: 'utf8',
  48. stdio: ['ignore', 'pipe', 'pipe'],
  49. });
  50. if (result.status !== 0) {
  51. const detail = [result.stdout, result.stderr].filter(Boolean).join('\n');
  52. fail(
  53. `Command failed: ${command} ${args.join(' ')}${detail ? `\n${detail}` : ''}`,
  54. );
  55. }
  56. return result.stdout.trim();
  57. }
  58. function parsePackJson(output: string) {
  59. const start = output.indexOf('[');
  60. const end = output.lastIndexOf(']');
  61. if (start === -1 || end === -1 || end < start) {
  62. fail(`Could not locate npm pack JSON output:\n${output}`);
  63. }
  64. return JSON.parse(output.slice(start, end + 1)) as Array<{
  65. filename?: string;
  66. files?: Array<{ path: string }>;
  67. }>;
  68. }
  69. function walkFiles(dir: string): string[] {
  70. const entries = readdirSync(dir, { withFileTypes: true });
  71. return entries.flatMap((entry) => {
  72. const fullPath = path.join(dir, entry.name);
  73. if (entry.isDirectory()) return walkFiles(fullPath);
  74. return [fullPath];
  75. });
  76. }
  77. function verifyDistHasNoLeakedPaths() {
  78. console.log('Checking dist for leaked machine paths...');
  79. const files = walkFiles(distDir).filter((file) =>
  80. /\.(?:js|d\.ts|map|json)$/.test(file),
  81. );
  82. const leaks: string[] = [];
  83. for (const file of files) {
  84. const content = readFileSync(file, 'utf8');
  85. for (const pattern of suspiciousPathPatterns) {
  86. const match = content.match(pattern);
  87. if (!match) continue;
  88. leaks.push(`${path.relative(repoRoot, file)}: ${match[0]}`);
  89. }
  90. for (const pattern of suspiciousImportPatterns) {
  91. const match = content.match(pattern);
  92. if (!match) continue;
  93. leaks.push(`${path.relative(repoRoot, file)}: ${match[0]}`);
  94. }
  95. }
  96. if (leaks.length > 0) {
  97. fail(
  98. `Built artifact contains machine-specific paths:\n${leaks.join('\n')}`,
  99. );
  100. }
  101. }
  102. function packArtifact() {
  103. console.log('Packing npm artifact...');
  104. const output = run('npm', ['pack', '--json', '--ignore-scripts'], {
  105. cwd: repoRoot,
  106. });
  107. const parsed = parsePackJson(output);
  108. const tarball = parsed[0]?.filename;
  109. if (!tarball) {
  110. fail(`npm pack did not return a tarball filename:\n${output}`);
  111. }
  112. const packagedFiles = new Set(
  113. (parsed[0]?.files ?? []).map((file) => file.path),
  114. );
  115. for (const requiredFile of packagedRequiredFiles) {
  116. if (!packagedFiles.has(requiredFile)) {
  117. fail(`npm pack artifact is missing required file: ${requiredFile}`);
  118. }
  119. }
  120. return path.join(repoRoot, tarball);
  121. }
  122. function verifyFreshInstall(tarballPath: string) {
  123. const tempRoot = mkdtempSync(path.join(tmpdir(), 'omos-release-'));
  124. try {
  125. console.log('Installing packed artifact into clean temp project...');
  126. const installDir = path.join(tempRoot, 'install');
  127. const tarballTarget = path.join(tempRoot, path.basename(tarballPath));
  128. copyFileSync(tarballPath, tarballTarget);
  129. mkdirSync(installDir, { recursive: true });
  130. writeFileSync(
  131. path.join(installDir, 'package.json'),
  132. JSON.stringify(
  133. { name: 'verify-release-artifact', private: true },
  134. null,
  135. 2,
  136. ),
  137. );
  138. run('bun', ['add', '--ignore-scripts', tarballTarget], {
  139. cwd: installDir,
  140. });
  141. const installedEntry = path.join(
  142. installDir,
  143. 'node_modules',
  144. 'oh-my-opencode-slim',
  145. 'dist',
  146. 'index.js',
  147. );
  148. const installedEntryContent = readFileSync(installedEntry, 'utf8');
  149. for (const pattern of suspiciousPathPatterns) {
  150. const match = installedEntryContent.match(pattern);
  151. if (match) {
  152. fail(
  153. `Installed package still contains machine-specific path: ${match[0]}`,
  154. );
  155. }
  156. }
  157. const smokeScript = [
  158. "import pkg from 'oh-my-opencode-slim';",
  159. "if (typeof pkg !== 'function') throw new Error('default export is not a function');",
  160. "console.log('package loads');",
  161. 'process.exit(0);',
  162. ].join('\n');
  163. console.log('Importing installed package entrypoint...');
  164. run('node', ['--input-type=module', '--eval', smokeScript], {
  165. cwd: installDir,
  166. });
  167. } finally {
  168. rmSync(tempRoot, { recursive: true, force: true });
  169. }
  170. }
  171. function cleanupTarball(tarballPath: string) {
  172. rmSync(tarballPath, { force: true });
  173. }
  174. function main() {
  175. verifyDistHasNoLeakedPaths();
  176. const tarballPath = packArtifact();
  177. try {
  178. verifyFreshInstall(tarballPath);
  179. } finally {
  180. cleanupTarball(tarballPath);
  181. }
  182. console.log('Release artifact verification passed.');
  183. }
  184. main();