verify-release-artifact.ts 5.7 KB

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