verify-release-artifact.ts 5.7 KB

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