verify-release-artifact.ts 5.8 KB

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