patch-incorrect-lockfile.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.patchIncorrectLockfile = patchIncorrectLockfile;
  6. var _fs = require("fs");
  7. require("../server/node-polyfill-fetch");
  8. var Log = _interopRequireWildcard(require("../build/output/log"));
  9. var _findUp = _interopRequireDefault(require("next/dist/compiled/find-up"));
  10. var _childProcess = require("child_process");
  11. var _packageJson = _interopRequireDefault(require("next/package.json"));
  12. var _ciInfo = require("../telemetry/ci-info");
  13. function _interopRequireDefault(obj) {
  14. return obj && obj.__esModule ? obj : {
  15. default: obj
  16. };
  17. }
  18. function _getRequireWildcardCache() {
  19. if (typeof WeakMap !== "function") return null;
  20. var cache = new WeakMap();
  21. _getRequireWildcardCache = function() {
  22. return cache;
  23. };
  24. return cache;
  25. }
  26. function _interopRequireWildcard(obj) {
  27. if (obj && obj.__esModule) {
  28. return obj;
  29. }
  30. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  31. return {
  32. default: obj
  33. };
  34. }
  35. var cache = _getRequireWildcardCache();
  36. if (cache && cache.has(obj)) {
  37. return cache.get(obj);
  38. }
  39. var newObj = {};
  40. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  41. for(var key in obj){
  42. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  43. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  44. if (desc && (desc.get || desc.set)) {
  45. Object.defineProperty(newObj, key, desc);
  46. } else {
  47. newObj[key] = obj[key];
  48. }
  49. }
  50. }
  51. newObj.default = obj;
  52. if (cache) {
  53. cache.set(obj, newObj);
  54. }
  55. return newObj;
  56. }
  57. let registry;
  58. async function fetchPkgInfo(pkg) {
  59. if (!registry) {
  60. try {
  61. const output = (0, _childProcess).execSync("npm config get registry").toString().trim();
  62. if (output.startsWith("http")) {
  63. registry = output;
  64. if (!registry.endsWith("/")) {
  65. registry += "/";
  66. }
  67. }
  68. } catch (_) {
  69. registry = `https://registry.npmjs.org/`;
  70. }
  71. }
  72. const res = await fetch(`${registry}${pkg}`);
  73. if (!res.ok) {
  74. throw new Error(`Failed to fetch registry info for ${pkg}, got status ${res.status}`);
  75. }
  76. const data = await res.json();
  77. const versionData = data.versions[_packageJson.default.version];
  78. return {
  79. os: versionData.os,
  80. cpu: versionData.cpu,
  81. engines: versionData.engines,
  82. tarball: versionData.dist.tarball,
  83. integrity: versionData.dist.integrity
  84. };
  85. }
  86. async function patchIncorrectLockfile(dir) {
  87. if (process.env.NEXT_IGNORE_INCORRECT_LOCKFILE) {
  88. return;
  89. }
  90. const lockfilePath = await (0, _findUp).default("package-lock.json", {
  91. cwd: dir
  92. });
  93. if (!lockfilePath) {
  94. // if no lockfile present there is no action to take
  95. return;
  96. }
  97. const content = await _fs.promises.readFile(lockfilePath, "utf8");
  98. // maintain current line ending
  99. const endingNewline = content.endsWith("\r\n") ? "\r\n" : content.endsWith("\n") ? "\n" : "";
  100. const lockfileParsed = JSON.parse(content);
  101. const lockfileVersion = parseInt(lockfileParsed == null ? void 0 : lockfileParsed.lockfileVersion, 10);
  102. const expectedSwcPkgs = Object.keys(_packageJson.default.optionalDependencies || {});
  103. const patchDependency = (pkg, pkgData)=>{
  104. lockfileParsed.dependencies[pkg] = {
  105. version: _packageJson.default.version,
  106. resolved: pkgData.tarball,
  107. integrity: pkgData.integrity,
  108. optional: true
  109. };
  110. };
  111. const patchPackage = (pkg, pkgData)=>{
  112. lockfileParsed.packages[pkg] = {
  113. version: _packageJson.default.version,
  114. resolved: pkgData.tarball,
  115. integrity: pkgData.integrity,
  116. cpu: pkgData.cpu,
  117. optional: true,
  118. os: pkgData.os,
  119. engines: pkgData.engines
  120. };
  121. };
  122. try {
  123. const supportedVersions = [
  124. 1,
  125. 2,
  126. 3
  127. ];
  128. if (!supportedVersions.includes(lockfileVersion)) {
  129. // bail on unsupported version
  130. return;
  131. }
  132. // v1 only uses dependencies
  133. // v2 uses dependencies and packages
  134. // v3 only uses packages
  135. const shouldPatchDependencies = lockfileVersion === 1 || lockfileVersion === 2;
  136. const shouldPatchPackages = lockfileVersion === 2 || lockfileVersion === 3;
  137. if (shouldPatchDependencies && !lockfileParsed.dependencies || shouldPatchPackages && !lockfileParsed.packages) {
  138. // invalid lockfile so bail
  139. return;
  140. }
  141. const missingSwcPkgs = [];
  142. let pkgPrefix;
  143. if (shouldPatchPackages) {
  144. pkgPrefix = "";
  145. for (const pkg of Object.keys(lockfileParsed.packages)){
  146. if (pkg.endsWith("node_modules/next")) {
  147. pkgPrefix = pkg.substring(0, pkg.length - 4);
  148. }
  149. }
  150. if (!pkgPrefix) {
  151. // unable to locate the next package so bail
  152. return;
  153. }
  154. }
  155. for (const pkg1 of expectedSwcPkgs){
  156. if (shouldPatchDependencies && !lockfileParsed.dependencies[pkg1] || shouldPatchPackages && !lockfileParsed.packages[`${pkgPrefix}${pkg1}`]) {
  157. missingSwcPkgs.push(pkg1);
  158. }
  159. }
  160. if (missingSwcPkgs.length === 0) {
  161. return;
  162. }
  163. Log.warn(`Found lockfile missing swc dependencies,`, _ciInfo.isCI ? "run next locally to automatically patch" : "patching...");
  164. if (_ciInfo.isCI) {
  165. // no point in updating in CI as the user can't save the patch
  166. return;
  167. }
  168. const pkgsData = await Promise.all(missingSwcPkgs.map((pkg)=>fetchPkgInfo(pkg)));
  169. for(let i = 0; i < pkgsData.length; i++){
  170. const pkg = missingSwcPkgs[i];
  171. const pkgData = pkgsData[i];
  172. if (shouldPatchDependencies) {
  173. patchDependency(pkg, pkgData);
  174. }
  175. if (shouldPatchPackages) {
  176. patchPackage(`${pkgPrefix}${pkg}`, pkgData);
  177. }
  178. }
  179. await _fs.promises.writeFile(lockfilePath, JSON.stringify(lockfileParsed, null, 2) + endingNewline);
  180. Log.warn('Lockfile was successfully patched, please run "npm install" to ensure @next/swc dependencies are downloaded');
  181. } catch (err) {
  182. Log.error(`Failed to patch lockfile, please try uninstalling and reinstalling next in this workspace`);
  183. console.error(err);
  184. }
  185. }
  186. //# sourceMappingURL=patch-incorrect-lockfile.js.map