verify-release-artifact.ts 5.5 KB

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