|
| 1 | +var fs = require("fs"); |
| 2 | +var path = require("path"); |
| 3 | +var package = require("./package.json"); |
| 4 | + |
| 5 | +module.exports = function () { |
| 6 | + var os = process.env.BINWRAP_PLATFORM || process.platform; |
| 7 | + var arch = process.env.BINWRAP_ARCH || process.arch; |
| 8 | + |
| 9 | + var requested = `${os}-${arch}`; |
| 10 | + var current = `${process.platform}-${process.arch}`; |
| 11 | + var subPackageName = `@@binaryPackageScope@/@name@-${requested}`; |
| 12 | + |
| 13 | + if (requested !== current) { |
| 14 | + console.error( |
| 15 | + `WARNING: Using binaries for the requested platform (${requested}) instead of for the actual platform (${current}).` |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + if (!(subPackageName in package.optionalDependencies)) { |
| 20 | + exitFailure( |
| 21 | + `The @npmPackageName@ npm package does not support your platform (${requested}).` |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + var fileName = os === "win32" ? "elm-format.exe" : "elm-format"; |
| 26 | + |
| 27 | + try { |
| 28 | + var subBinaryPath = require.resolve(`${subPackageName}/${fileName}`); |
| 29 | + } catch (error) { |
| 30 | + if (error && error.code === "MODULE_NOT_FOUND") { |
| 31 | + exitFailure(missingSubPackageHelp(subPackageName)); |
| 32 | + } else { |
| 33 | + exitFailure( |
| 34 | + `I had trouble requiring the binary package for your platform (${subPackageName}):\n\n${error}` |
| 35 | + ); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Yarn 2 and later ("Berry") always invokes `node` (regardless of configuration) |
| 40 | + // so we cannot do any optimizations there. |
| 41 | + var isYarnBerry = /\byarn\/(?!1\.)/.test( |
| 42 | + process.env.npm_config_user_agent || "" |
| 43 | + ); |
| 44 | + |
| 45 | + // On Windows, npm always invokes `node` so we cannot do any optimizations there either. |
| 46 | + if (os === "win32" || isYarnBerry) { |
| 47 | + return subBinaryPath; |
| 48 | + } |
| 49 | + |
| 50 | + var binaryPath = path.resolve(__dirname, package.bin["elm-format"]); |
| 51 | + var tmpPath = binaryPath + ".tmp"; |
| 52 | + |
| 53 | + try { |
| 54 | + // Atomically replace the file with a hard link to the binary as an optimization. |
| 55 | + fs.linkSync(subBinaryPath, tmpPath); |
| 56 | + fs.renameSync(tmpPath, binaryPath); |
| 57 | + } catch (error) { |
| 58 | + exitFailure( |
| 59 | + `I had some trouble writing file to disk. It is saying:\n\n${error}` |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + return binaryPath; |
| 64 | +}; |
| 65 | + |
| 66 | +function exitFailure(message) { |
| 67 | + console.error( |
| 68 | + ` |
| 69 | +-- ERROR ----------------------------------------------------------------------- |
| 70 | +
|
| 71 | +${message} |
| 72 | +
|
| 73 | +NOTE: You can avoid npm entirely by downloading directly from: |
| 74 | +https://github.com/avh4/elm-format/releases/tag/${package.version} |
| 75 | +All this package does is distributing a file from there. |
| 76 | +
|
| 77 | +-------------------------------------------------------------------------------- |
| 78 | + `.trim() |
| 79 | + ); |
| 80 | + process.exit(1); |
| 81 | +} |
| 82 | + |
| 83 | +function missingSubPackageHelp(subPackageName) { |
| 84 | + return ` |
| 85 | +I support your platform, but I could not find the binary package (${subPackageName}) for it! |
| 86 | +
|
| 87 | +This can happen if you use the "--omit=optional" (or "--no-optional") npm flag. |
| 88 | +The "optionalDependencies" package.json feature is used by elm-format to install the correct |
| 89 | +binary executable for your current platform. Remove that flag to use elm-format. |
| 90 | +
|
| 91 | +This can also happen if the "node_modules" folder was copied between two operating systems |
| 92 | +that need different binaries - including "virtual" operating systems like Docker and WSL. |
| 93 | +If so, try installing with npm rather than copying "node_modules". |
| 94 | + `.trim(); |
| 95 | +} |
0 commit comments