verify-release-artifact.ts 5.2 KB

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