install.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. var __assign = Object.assign;
  2. var __async = (__this, __arguments, generator) => {
  3. return new Promise((resolve, reject) => {
  4. var fulfilled = (value) => {
  5. try {
  6. step(generator.next(value));
  7. } catch (e) {
  8. reject(e);
  9. }
  10. };
  11. var rejected = (value) => {
  12. try {
  13. step(generator.throw(value));
  14. } catch (e) {
  15. reject(e);
  16. }
  17. };
  18. var step = (result) => {
  19. return result.done ? resolve(result.value) : Promise.resolve(result.value).then(fulfilled, rejected);
  20. };
  21. step((generator = generator.apply(__this, __arguments)).next());
  22. });
  23. };
  24. const fs = require("fs");
  25. const os = require("os");
  26. const path = require("path");
  27. const zlib = require("zlib");
  28. const https = require("https");
  29. const child_process = require("child_process");
  30. const version = "0.7.22";
  31. const binPath = path.join(__dirname, "bin", "esbuild");
  32. function installBinaryFromPackage(name, fromPath, toPath) {
  33. return __async(this, null, function* () {
  34. const cachePath = getCachePath(name);
  35. try {
  36. fs.copyFileSync(cachePath, toPath);
  37. fs.chmodSync(toPath, 493);
  38. validateBinaryVersion(toPath);
  39. const now = new Date();
  40. fs.utimesSync(cachePath, now, now);
  41. return;
  42. } catch (e) {
  43. }
  44. let buffer;
  45. let didFail = false;
  46. try {
  47. buffer = installUsingNPM(name, fromPath);
  48. } catch (err) {
  49. didFail = true;
  50. console.error(`Trying to install "${name}" using npm`);
  51. console.error(`Failed to install "${name}" using npm: ${err && err.message || err}`);
  52. }
  53. if (!buffer) {
  54. const url = `https://registry.npmjs.org/${name}/-/${name}-${version}.tgz`;
  55. console.error(`Trying to download ${JSON.stringify(url)}`);
  56. try {
  57. buffer = extractFileFromTarGzip(yield fetch(url), fromPath);
  58. } catch (err) {
  59. console.error(`Failed to download ${JSON.stringify(url)}: ${err && err.message || err}`);
  60. }
  61. }
  62. if (!buffer) {
  63. console.error(`Install unsuccessful`);
  64. process.exit(1);
  65. }
  66. fs.writeFileSync(toPath, buffer, {mode: 493});
  67. try {
  68. validateBinaryVersion(toPath);
  69. } catch (err) {
  70. console.error(`The version of the downloaded binary is incorrect: ${err && err.message || err}`);
  71. console.error(`Install unsuccessful`);
  72. process.exit(1);
  73. }
  74. try {
  75. fs.mkdirSync(path.dirname(cachePath), {recursive: true});
  76. fs.copyFileSync(toPath, cachePath);
  77. cleanCacheLRU(cachePath);
  78. } catch (e) {
  79. }
  80. if (didFail)
  81. console.error(`Install successful`);
  82. });
  83. }
  84. function validateBinaryVersion(binaryPath) {
  85. const stdout = child_process.execFileSync(binaryPath, ["--version"]).toString().trim();
  86. if (stdout !== version) {
  87. throw new Error(`Expected ${JSON.stringify(version)} but got ${JSON.stringify(stdout)}`);
  88. }
  89. }
  90. function getCachePath(name) {
  91. const home = os.homedir();
  92. const common = ["esbuild", "bin", `${name}@${version}`];
  93. if (process.platform === "darwin")
  94. return path.join(home, "Library", "Caches", ...common);
  95. if (process.platform === "win32")
  96. return path.join(home, "AppData", "Local", "Cache", ...common);
  97. return path.join(home, ".cache", ...common);
  98. }
  99. function cleanCacheLRU(fileToKeep) {
  100. const dir = path.dirname(fileToKeep);
  101. const entries = [];
  102. for (const entry of fs.readdirSync(dir)) {
  103. const entryPath = path.join(dir, entry);
  104. try {
  105. const stats = fs.statSync(entryPath);
  106. entries.push({path: entryPath, mtime: stats.mtime});
  107. } catch (e) {
  108. }
  109. }
  110. entries.sort((a, b) => +b.mtime - +a.mtime);
  111. for (const entry of entries.slice(5)) {
  112. try {
  113. fs.unlinkSync(entry.path);
  114. } catch (e) {
  115. }
  116. }
  117. }
  118. function fetch(url) {
  119. return new Promise((resolve, reject) => {
  120. https.get(url, (res) => {
  121. if ((res.statusCode === 301 || res.statusCode === 302) && res.headers.location)
  122. return fetch(res.headers.location).then(resolve, reject);
  123. if (res.statusCode !== 200)
  124. return reject(new Error(`Server responded with ${res.statusCode}`));
  125. let chunks = [];
  126. res.on("data", (chunk) => chunks.push(chunk));
  127. res.on("end", () => resolve(Buffer.concat(chunks)));
  128. }).on("error", reject);
  129. });
  130. }
  131. function extractFileFromTarGzip(buffer, file) {
  132. try {
  133. buffer = zlib.unzipSync(buffer);
  134. } catch (err) {
  135. throw new Error(`Invalid gzip data in archive: ${err && err.message || err}`);
  136. }
  137. let str = (i, n) => String.fromCharCode(...buffer.subarray(i, i + n)).replace(/\0.*$/, "");
  138. let offset = 0;
  139. file = `package/${file}`;
  140. while (offset < buffer.length) {
  141. let name = str(offset, 100);
  142. let size = parseInt(str(offset + 124, 12), 8);
  143. offset += 512;
  144. if (!isNaN(size)) {
  145. if (name === file)
  146. return buffer.subarray(offset, offset + size);
  147. offset += size + 511 & ~511;
  148. }
  149. }
  150. throw new Error(`Could not find ${JSON.stringify(file)} in archive`);
  151. }
  152. function installUsingNPM(name, file) {
  153. const installDir = path.join(__dirname, ".install");
  154. fs.mkdirSync(installDir, {recursive: true});
  155. fs.writeFileSync(path.join(installDir, "package.json"), "{}");
  156. const env = __assign(__assign({}, process.env), {npm_config_global: void 0});
  157. child_process.execSync(`npm install --loglevel=error --prefer-offline --no-audit --progress=false ${name}@${version}`, {cwd: installDir, stdio: "pipe", env});
  158. const buffer = fs.readFileSync(path.join(installDir, "node_modules", name, file));
  159. removeRecursive(installDir);
  160. return buffer;
  161. }
  162. function removeRecursive(dir) {
  163. for (const entry of fs.readdirSync(dir)) {
  164. const entryPath = path.join(dir, entry);
  165. let stats;
  166. try {
  167. stats = fs.lstatSync(entryPath);
  168. } catch (e) {
  169. continue;
  170. }
  171. if (stats.isDirectory())
  172. removeRecursive(entryPath);
  173. else
  174. fs.unlinkSync(entryPath);
  175. }
  176. fs.rmdirSync(dir);
  177. }
  178. function installOnUnix(name) {
  179. if (process.env.ESBUILD_BIN_PATH_FOR_TESTS) {
  180. fs.unlinkSync(binPath);
  181. fs.symlinkSync(process.env.ESBUILD_BIN_PATH_FOR_TESTS, binPath);
  182. } else {
  183. installBinaryFromPackage(name, "bin/esbuild", binPath).catch((e) => setImmediate(() => {
  184. throw e;
  185. }));
  186. }
  187. }
  188. function installOnWindows(name) {
  189. fs.writeFileSync(binPath, `#!/usr/bin/env node
  190. const path = require('path');
  191. const esbuild_exe = path.join(__dirname, '..', 'esbuild.exe');
  192. const child_process = require('child_process');
  193. child_process.spawnSync(esbuild_exe, process.argv.slice(2), { stdio: 'inherit' });
  194. `);
  195. const exePath = path.join(__dirname, "esbuild.exe");
  196. if (process.env.ESBUILD_BIN_PATH_FOR_TESTS) {
  197. fs.copyFileSync(process.env.ESBUILD_BIN_PATH_FOR_TESTS, exePath);
  198. } else {
  199. installBinaryFromPackage(name, "esbuild.exe", exePath).catch((e) => setImmediate(() => {
  200. throw e;
  201. }));
  202. }
  203. }
  204. const key = `${process.platform} ${os.arch()} ${os.endianness()}`;
  205. const knownWindowsPackages = {
  206. "win32 ia32 LE": "esbuild-windows-32",
  207. "win32 x64 LE": "esbuild-windows-64"
  208. };
  209. const knownUnixlikePackages = {
  210. "darwin x64 LE": "esbuild-darwin-64",
  211. "freebsd arm64 LE": "esbuild-freebsd-arm64",
  212. "freebsd x64 LE": "esbuild-freebsd-64",
  213. "linux arm64 LE": "esbuild-linux-arm64",
  214. "linux ia32 LE": "esbuild-linux-32",
  215. "linux ppc64 LE": "esbuild-linux-ppc64le",
  216. "linux x64 LE": "esbuild-linux-64"
  217. };
  218. if (key in knownWindowsPackages) {
  219. installOnWindows(knownWindowsPackages[key]);
  220. } else if (key in knownUnixlikePackages) {
  221. installOnUnix(knownUnixlikePackages[key]);
  222. } else {
  223. console.error(`Unsupported platform: ${key}`);
  224. process.exit(1);
  225. }