packaged-platform-dependency-prune.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { readdir, readFile, rm } from "node:fs/promises";
  2. import { join } from "node:path";
  3. function normalizeConstraint(value) {
  4. if (value == null) return null;
  5. return (Array.isArray(value) ? value : [value]).filter(
  6. (entry) => typeof entry === "string" && entry.length > 0,
  7. );
  8. }
  9. function matchesConstraint(targets, constraint) {
  10. if (!constraint) return true;
  11. if (constraint.length === 1 && constraint[0] === "any") return true;
  12. let hasPositive = false;
  13. let positiveMatch = false;
  14. let negativeCount = 0;
  15. for (const target of targets) {
  16. for (const entry of constraint) {
  17. if (entry.startsWith("!")) {
  18. negativeCount += 1;
  19. if (entry.slice(1) === target) return false;
  20. } else {
  21. hasPositive = true;
  22. positiveMatch ||= entry === target;
  23. }
  24. }
  25. }
  26. return positiveMatch || (!hasPositive && negativeCount === constraint.length);
  27. }
  28. function isCompatible(packageJson, targetPlatform) {
  29. return (
  30. matchesConstraint([targetPlatform.os], normalizeConstraint(packageJson.os)) &&
  31. matchesConstraint([targetPlatform.arch], normalizeConstraint(packageJson.cpu))
  32. );
  33. }
  34. async function prunePackageDirectory(packageDirectory, targetPlatform, summary) {
  35. let packageJson;
  36. try {
  37. packageJson = JSON.parse(await readFile(join(packageDirectory, "package.json"), "utf8"));
  38. } catch (error) {
  39. if (["ENOENT", "ENOTDIR"].includes(error.code)) return;
  40. throw error;
  41. }
  42. if (!isCompatible(packageJson, targetPlatform)) {
  43. await rm(packageDirectory, { recursive: true, force: true });
  44. summary.packagesRemoved += 1;
  45. summary.removedNames.push(packageJson.name ?? packageDirectory);
  46. return;
  47. }
  48. await pruneNodeModulesDirectory(join(packageDirectory, "node_modules"), targetPlatform, summary);
  49. }
  50. async function pruneNodeModulesDirectory(nodeModulesDirectory, targetPlatform, summary) {
  51. let entries;
  52. try {
  53. entries = await readdir(nodeModulesDirectory, { withFileTypes: true });
  54. } catch (error) {
  55. if (error.code === "ENOENT" || error.code === "ENOTDIR") return;
  56. throw error;
  57. }
  58. for (const entry of entries) {
  59. if (!entry.isDirectory()) continue;
  60. const entryPath = join(nodeModulesDirectory, entry.name);
  61. if (entry.name.startsWith("@")) {
  62. const scopedEntries = await readdir(entryPath, { withFileTypes: true });
  63. for (const scopedEntry of scopedEntries) {
  64. if (scopedEntry.isDirectory()) {
  65. await prunePackageDirectory(join(entryPath, scopedEntry.name), targetPlatform, summary);
  66. }
  67. }
  68. } else {
  69. await prunePackageDirectory(entryPath, targetPlatform, summary);
  70. }
  71. }
  72. }
  73. export async function pruneIncompatiblePlatformPackages(rootDirectory, targetPlatform) {
  74. const summary = { packagesRemoved: 0, removedNames: [] };
  75. await pruneNodeModulesDirectory(join(rootDirectory, "node_modules"), targetPlatform, summary);
  76. return summary;
  77. }