|
| 1 | +/** |
| 2 | + * release-postinstall.js |
| 3 | + * |
| 4 | + * XXX: We want to keep this script installable at least with node 4.x. |
| 5 | + * |
| 6 | + * This script is bundled with the `npm` package and executed on release. |
| 7 | + * Since we have a 'fat' NPM package (with all platform binaries bundled), |
| 8 | + * this postinstall script extracts them and puts the current platform's |
| 9 | + * bits in the right place. |
| 10 | + */ |
| 11 | + |
| 12 | +var path = require("path"); |
| 13 | +var cp = require("child_process"); |
| 14 | +var fs = require("fs"); |
| 15 | +var os = require("os"); |
| 16 | +var platform = process.platform; |
| 17 | + |
| 18 | +var packageJson = require("./package.json"); |
| 19 | + |
| 20 | +function copyRecursive(srcDir, dstDir) { |
| 21 | + var results = []; |
| 22 | + var list = fs.readdirSync(srcDir); |
| 23 | + var src, dst; |
| 24 | + list.forEach(function (file) { |
| 25 | + src = path.join(srcDir, file); |
| 26 | + dst = path.join(dstDir, file); |
| 27 | + |
| 28 | + var stat = fs.statSync(src); |
| 29 | + if (stat && stat.isDirectory()) { |
| 30 | + try { |
| 31 | + fs.mkdirSync(dst); |
| 32 | + } catch (e) { |
| 33 | + console.log("directory already exists: " + dst); |
| 34 | + console.error(e); |
| 35 | + } |
| 36 | + results = results.concat(copyRecursive(src, dst)); |
| 37 | + } else { |
| 38 | + try { |
| 39 | + fs.writeFileSync(dst, fs.readFileSync(src)); |
| 40 | + } catch (e) { |
| 41 | + console.log("could't copy file: " + dst); |
| 42 | + console.error(e); |
| 43 | + } |
| 44 | + results.push(src); |
| 45 | + } |
| 46 | + }); |
| 47 | + return results; |
| 48 | +} |
| 49 | + |
| 50 | +function arch() { |
| 51 | + /** |
| 52 | + * On Windows, the most reliable way to detect a 64-bit OS from within a 32-bit |
| 53 | + * app is based on the presence of a WOW64 file: %SystemRoot%\SysNative. |
| 54 | + * See: https://twitter.com/feross/status/776949077208510464 |
| 55 | + */ |
| 56 | + if (process.platform === "win32") { |
| 57 | + var useEnv = false; |
| 58 | + try { |
| 59 | + useEnv = !!( |
| 60 | + process.env.SYSTEMROOT && fs.statSync(process.env.SYSTEMROOT) |
| 61 | + ); |
| 62 | + } catch (err) {} |
| 63 | + |
| 64 | + var sysRoot = useEnv ? process.env.SYSTEMROOT : "C:\\Windows"; |
| 65 | + |
| 66 | + // If %SystemRoot%\SysNative exists, we are in a WOW64 FS Redirected application. |
| 67 | + var isWOW64 = false; |
| 68 | + try { |
| 69 | + isWOW64 = !!fs.statSync(path.join(sysRoot, "sysnative")); |
| 70 | + } catch (err) {} |
| 71 | + |
| 72 | + return isWOW64 ? "x64" : "x86"; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * On Linux, use the `getconf` command to get the architecture. |
| 77 | + */ |
| 78 | + if (process.platform === "linux") { |
| 79 | + var output = cp.execSync("getconf LONG_BIT", { encoding: "utf8" }); |
| 80 | + return output === "64\n" ? "x64" : "x86"; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * If none of the above, assume the architecture is 32-bit. |
| 85 | + */ |
| 86 | + return process.arch; |
| 87 | +} |
| 88 | + |
| 89 | +// implementing it b/c we don't want to depend on fs.copyFileSync which appears |
| 90 | + |
| 91 | +function copyFileSync(sourcePath, destPath) { |
| 92 | + var data; |
| 93 | + try { |
| 94 | + data = fs.readFileSync(sourcePath); |
| 95 | + } catch (e) { |
| 96 | + data = fs.readFileSync(sourcePath + ".exe"); |
| 97 | + sourcePath = sourcePath + ".exe"; |
| 98 | + destPath = destPath + ".exe"; |
| 99 | + } |
| 100 | + var stat = fs.statSync(sourcePath); |
| 101 | + fs.writeFileSync(destPath, data); |
| 102 | + fs.chmodSync(destPath, 0755); |
| 103 | +} |
| 104 | + |
| 105 | +var copyPlatformBinaries = (platformPath) => { |
| 106 | + var platformBuildPath = path.join( |
| 107 | + __dirname, |
| 108 | + "platform-esy-npm-release-" + platformPath |
| 109 | + ); |
| 110 | + |
| 111 | + let foldersToCopy, binariesToCopy; |
| 112 | + |
| 113 | + binariesToCopy = Object.keys(packageJson.bin).map(function (name) { |
| 114 | + return packageJson.bin[name]; |
| 115 | + }); |
| 116 | + |
| 117 | + if (platformPath === "linux") { |
| 118 | + fs.mkdirSync(path.join(__dirname, "lib")); |
| 119 | + foldersToCopy = ["bin", "lib"]; |
| 120 | + } else { |
| 121 | + foldersToCopy = ["bin", "_export"]; |
| 122 | + binariesToCopy = binariesToCopy.concat(["esyInstallRelease.js"]); |
| 123 | + } |
| 124 | + |
| 125 | + foldersToCopy.forEach((folderPath) => { |
| 126 | + var sourcePath = path.join(platformBuildPath, folderPath); |
| 127 | + var destPath = path.join(__dirname, folderPath); |
| 128 | + copyRecursive(sourcePath, destPath); |
| 129 | + }); |
| 130 | + |
| 131 | + binariesToCopy.forEach((binaryPath) => { |
| 132 | + var sourcePath = path.join(platformBuildPath, binaryPath); |
| 133 | + var destPath = path.join(__dirname, binaryPath); |
| 134 | + if (fs.existsSync(destPath)) { |
| 135 | + fs.unlinkSync(destPath); |
| 136 | + } |
| 137 | + copyFileSync(sourcePath, destPath); |
| 138 | + }); |
| 139 | + |
| 140 | + if (platformPath === "linux") { |
| 141 | + fs.chmodSync( |
| 142 | + path.join(__dirname, "lib", "esy", "esyBuildPackageCommand"), |
| 143 | + 0755 |
| 144 | + ); |
| 145 | + fs.chmodSync( |
| 146 | + path.join(__dirname, "lib", "esy", "esySolveCudfCommand"), |
| 147 | + 0755 |
| 148 | + ); |
| 149 | + fs.chmodSync( |
| 150 | + path.join(__dirname, "lib", "esy", "esyRewritePrefixCommand"), |
| 151 | + 0755 |
| 152 | + ); |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +try { |
| 157 | + fs.mkdirSync("_export"); |
| 158 | +} catch (e) { |
| 159 | + console.log("Could not create _export folder"); |
| 160 | +} |
| 161 | + |
| 162 | +const platformArch = arch(); |
| 163 | +switch (platform) { |
| 164 | + case "win32": |
| 165 | + if (platformArch !== "x64") { |
| 166 | + console.warn("error: x86 is currently not supported on Windows"); |
| 167 | + process.exit(1); |
| 168 | + } |
| 169 | + |
| 170 | + copyPlatformBinaries("windows-x64"); |
| 171 | + console.log("Installing native compiler toolchain for Windows..."); |
| 172 | + cp.execSync( |
| 173 | + `npm install @prometheansacrifice/[email protected] --prefix "${__dirname}"` |
| 174 | + ); |
| 175 | + console.log("Native compiler toolchain installed successfully."); |
| 176 | + require("./esyInstallRelease"); |
| 177 | + break; |
| 178 | + case "linux": |
| 179 | + copyPlatformBinaries(`${platform}-${platformArch}`); |
| 180 | + // Statically linked binaries dont need postinstall scripts |
| 181 | + // TODO add support for statically linked binaries |
| 182 | + require("./esyInstallRelease"); |
| 183 | + break; |
| 184 | + case "darwin": |
| 185 | + copyPlatformBinaries(`${platform}-${platformArch}`); |
| 186 | + require("./esyInstallRelease"); |
| 187 | + break; |
| 188 | + default: |
| 189 | + console.warn("error: no release built for the " + platform + " platform"); |
| 190 | + process.exit(1); |
| 191 | +} |
0 commit comments