verify-release-artifact.ts 5.8 KB

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