verify-release-artifact.ts 5.2 KB

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