package-cli-release.mjs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { copyFileSync, cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
  2. import { createRequire } from 'node:module';
  3. import { createHash } from 'node:crypto';
  4. import { execFileSync } from 'node:child_process';
  5. import { tmpdir } from 'node:os';
  6. import path from 'node:path';
  7. import { fileURLToPath } from 'node:url';
  8. import { c as createTar } from 'tar';
  9. import { cliBuildVersion, cliExternalModules } from './lib/cli-release.mjs';
  10. import { readExtraction, dependencyLicensesPath } from './lib/release-metadata.mjs';
  11. const root = fileURLToPath(new URL('../', import.meta.url));
  12. const json = file => JSON.parse(readFileSync(file, 'utf8'));
  13. const digest = bytes => createHash('sha256').update(bytes).digest('hex');
  14. function resolvePackage(name, importer) {
  15. const require = createRequire(path.join(importer, 'package.json'));
  16. for (const search of require.resolve.paths(`${name}/package.json`) ?? []) {
  17. const candidate = path.join(search, name);
  18. if (existsSync(path.join(candidate, 'package.json'))) return realpathSync(candidate);
  19. }
  20. return undefined;
  21. }
  22. function copyLicenses(stage) {
  23. const licenses = path.join(stage, 'licenses');
  24. mkdirSync(licenses);
  25. for (const name of ['LICENSE', 'NOTICE', 'LICENSE-STATUS.md', 'THIRD_PARTY_NOTICES.md']) copyFileSync(path.join(root, name), path.join(stage, name));
  26. copyFileSync(path.join(root, dependencyLicensesPath), path.join(licenses, 'dependency-licenses.json'));
  27. for (const directory of ['third_party/pi-mono', 'third_party/sandbox-runtime']) {
  28. copyFileSync(path.join(root, directory, 'LICENSE'), path.join(licenses, `${path.basename(directory)}-LICENSE`));
  29. }
  30. const metafile = json(path.join(root, 'dist/metafile.json'));
  31. const packageRoots = new Set();
  32. for (const input of Object.keys(metafile.inputs)) {
  33. if (!input.includes('node_modules/')) continue;
  34. let directory = path.dirname(path.resolve(root, input));
  35. while (directory !== path.dirname(directory)) {
  36. const manifest = path.join(directory, 'package.json');
  37. if (existsSync(manifest) && json(manifest).name) break;
  38. directory = path.dirname(directory);
  39. }
  40. if (existsSync(path.join(directory, 'package.json'))) packageRoots.add(realpathSync(directory));
  41. }
  42. for (const directory of [...packageRoots].sort()) {
  43. const manifest = json(path.join(directory, 'package.json'));
  44. const target = path.join(licenses, `${manifest.name.replaceAll('/', '+')}@${manifest.version}`);
  45. mkdirSync(target, { recursive: true });
  46. for (const name of readdirSync(directory).filter(name => /^(licen[cs]e|copying|notice)([.-]|$)/i.test(name))) {
  47. cpSync(path.join(directory, name), path.join(target, name), { recursive: true });
  48. }
  49. }
  50. }
  51. export function releaseManifest(importers, version) {
  52. const dependencies = {}, optionalDependencies = {};
  53. for (const name of cliExternalModules) {
  54. const versions = new Set(importers.map(importer => resolvePackage(name, importer)).filter(Boolean)
  55. .map(directory => json(path.join(directory, 'package.json')).version));
  56. if (versions.size !== 1) throw new Error(`Expected one installed version for ${name}, found ${[...versions]}`);
  57. const target = name === '@mariozechner/clipboard' ? optionalDependencies : dependencies;
  58. target[name] = [...versions][0];
  59. }
  60. return {
  61. name: '@minimax-ai/code', version, private: true, type: 'module', license: 'MIT',
  62. description: 'MiniMax Code CLI built from the tagged public source.',
  63. bin: { mcode: 'cli.js' },
  64. engines: json(path.join(root, 'package.json')).engines,
  65. repository: { type: 'git', url: 'https://github.com/MiniMax-AI/minimax-code.git' },
  66. dependencies, optionalDependencies,
  67. };
  68. }
  69. export async function packageCliRelease({ tag, out }) {
  70. const version = cliBuildVersion(root, tag);
  71. const dist = path.join(root, 'dist');
  72. if (json(path.join(dist, 'package.json')).version !== version) throw new Error('Build version does not match the release tag. Build with MCODE_RELEASE_TAG first.');
  73. const revision = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: root, encoding: 'utf8' }).trim();
  74. if (execFileSync('git', ['status', '--porcelain', '--untracked-files=no'], { cwd: root, encoding: 'utf8' }).trim()) throw new Error('Packaging requires a clean committed working tree.');
  75. if (json(path.join(dist, 'package.json')).gitHead !== revision) throw new Error('Build revision does not match HEAD. Rebuild with MCODE_RELEASE_TAG.');
  76. const temporary = mkdtempSync(path.join(tmpdir(), 'mcode-release-'));
  77. try {
  78. const stage = path.join(temporary, 'package');
  79. cpSync(dist, stage, { recursive: true, filter: file => path.basename(file) !== 'metafile.json' });
  80. const manifest = releaseManifest(readExtraction(root).packageRoots.map(directory => path.join(root, directory)), version);
  81. writeFileSync(path.join(stage, 'package.json'), JSON.stringify(manifest, null, 2) + '\n');
  82. copyLicenses(stage);
  83. writeFileSync(path.join(stage, 'release.json'), JSON.stringify({ version, tag, revision, buildNode: process.version }, null, 2) + '\n');
  84. writeFileSync(path.join(stage, 'README.md'), `# MiniMax Code ${version}
  85. Built from https://github.com/MiniMax-AI/minimax-code/tree/${revision}.
  86. Install this tar.gz with npm. Node.js must satisfy the package engines requirement.
  87. Keep optional dependencies enabled and allow better-sqlite3 installation scripts.
  88. Update by installing a newer GitHub release archive; the built-in updater follows npm.
  89. The archive uses the same package name, mcode command and user data as the official npm CLI.
  90. See https://github.com/MiniMax-AI/minimax-code/blob/${revision}/docs/installation.md.
  91. `);
  92. mkdirSync(out, { recursive: true });
  93. const archive = path.join(out, `minimax-code-${version}.tar.gz`);
  94. if (existsSync(archive) || existsSync(`${archive}.sha256`)) throw new Error(`Output already exists: ${archive}`);
  95. await createTar({ file: archive, gzip: true, cwd: temporary, portable: true }, ['package']);
  96. writeFileSync(`${archive}.sha256`, `${digest(readFileSync(archive))} ${path.basename(archive)}\n`, { flag: 'wx' });
  97. console.log(archive);
  98. return archive;
  99. } finally {
  100. rmSync(temporary, { recursive: true, force: true });
  101. }
  102. }
  103. if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
  104. const [tag, out] = process.argv.slice(2);
  105. if (!tag || !out) throw new Error('Usage: node scripts/package-cli-release.mjs vX.Y.Z /path/to/output');
  106. await packageCliRelease({ tag, out: path.resolve(out) });
  107. }