|
| 1 | +/** |
| 2 | + * Copyright (C) Daniel Kuschny (Danielku15) and contributors. |
| 3 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style |
| 6 | + * license that can be found in the LICENSE file or at |
| 7 | + * https://opensource.org/licenses/MIT. |
| 8 | + */ |
| 9 | + |
| 10 | +import { exec } from 'node:child_process'; |
| 11 | +import fs from 'node:fs'; |
| 12 | +import os from 'node:os'; |
| 13 | +import path from 'node:path'; |
| 14 | +import * as vscode from 'vscode'; |
| 15 | + |
| 16 | +// this logic is aligned with the ESBuild install script, unfortunately we cannot use pkgAndSubpathForCurrentPlatform directly |
| 17 | +// so we have a copy of the logic, we should reach out to ESBUild to export the API from install.js |
| 18 | + |
| 19 | +type PlatformLookup = { [platform: string]: string }; |
| 20 | + |
| 21 | +const knownWindowsPackages: PlatformLookup = { |
| 22 | + 'win32 arm64 LE': '@esbuild/win32-arm64', |
| 23 | + 'win32 ia32 LE': '@esbuild/win32-ia32', |
| 24 | + 'win32 x64 LE': '@esbuild/win32-x64', |
| 25 | +}; |
| 26 | +const knownUnixlikePackages: PlatformLookup = { |
| 27 | + 'aix ppc64 BE': '@esbuild/aix-ppc64', |
| 28 | + 'android arm64 LE': '@esbuild/android-arm64', |
| 29 | + 'darwin arm64 LE': '@esbuild/darwin-arm64', |
| 30 | + 'darwin x64 LE': '@esbuild/darwin-x64', |
| 31 | + 'freebsd arm64 LE': '@esbuild/freebsd-arm64', |
| 32 | + 'freebsd x64 LE': '@esbuild/freebsd-x64', |
| 33 | + 'linux arm LE': '@esbuild/linux-arm', |
| 34 | + 'linux arm64 LE': '@esbuild/linux-arm64', |
| 35 | + 'linux ia32 LE': '@esbuild/linux-ia32', |
| 36 | + 'linux mips64el LE': '@esbuild/linux-mips64el', |
| 37 | + 'linux ppc64 LE': '@esbuild/linux-ppc64', |
| 38 | + 'linux riscv64 LE': '@esbuild/linux-riscv64', |
| 39 | + 'linux s390x BE': '@esbuild/linux-s390x', |
| 40 | + 'linux x64 LE': '@esbuild/linux-x64', |
| 41 | + 'linux loong64 LE': '@esbuild/linux-loong64', |
| 42 | + 'netbsd x64 LE': '@esbuild/netbsd-x64', |
| 43 | + 'openbsd arm64 LE': '@esbuild/openbsd-arm64', |
| 44 | + 'openbsd x64 LE': '@esbuild/openbsd-x64', |
| 45 | + 'sunos x64 LE': '@esbuild/sunos-x64', |
| 46 | +}; |
| 47 | +const knownWebAssemblyFallbackPackages: PlatformLookup = { |
| 48 | + 'android arm LE': '@esbuild/android-arm', |
| 49 | + 'android x64 LE': '@esbuild/android-x64', |
| 50 | +}; |
| 51 | +function getPlatformPackageName() { |
| 52 | + let pkg: string; |
| 53 | + const platformKey = `${process.platform} ${os.arch()} ${os.endianness()}`; |
| 54 | + if (platformKey in knownWindowsPackages) { |
| 55 | + pkg = knownWindowsPackages[platformKey]; |
| 56 | + } else if (platformKey in knownUnixlikePackages) { |
| 57 | + pkg = knownUnixlikePackages[platformKey]; |
| 58 | + } else if (platformKey in knownWebAssemblyFallbackPackages) { |
| 59 | + pkg = knownWebAssemblyFallbackPackages[platformKey]; |
| 60 | + } else { |
| 61 | + throw new Error(`Unsupported platform: ${platformKey}`); |
| 62 | + } |
| 63 | + return pkg; |
| 64 | +} |
| 65 | + |
| 66 | +export async function esbuildPackageVersion() { |
| 67 | + let pkg: string; |
| 68 | + const platformKey = `${process.platform} ${os.arch()} ${os.endianness()}`; |
| 69 | + if (platformKey in knownWindowsPackages) { |
| 70 | + pkg = knownWindowsPackages[platformKey]; |
| 71 | + } else if (platformKey in knownUnixlikePackages) { |
| 72 | + pkg = knownUnixlikePackages[platformKey]; |
| 73 | + } else if (platformKey in knownWebAssemblyFallbackPackages) { |
| 74 | + pkg = knownWebAssemblyFallbackPackages[platformKey]; |
| 75 | + } else { |
| 76 | + throw new Error(`Unsupported platform: ${platformKey}`); |
| 77 | + } |
| 78 | + return pkg; |
| 79 | +} |
| 80 | + |
| 81 | +// ESBuild needs the platform specific binary for execution |
| 82 | +// here we run the init script coming with ESBuild |
| 83 | +export async function initESBuild( |
| 84 | + context: vscode.ExtensionContext, |
| 85 | + logChannel: vscode.LogOutputChannel, |
| 86 | +) { |
| 87 | + logChannel.debug('Checking ESBuild availability'); |
| 88 | + |
| 89 | + const platformPackageName = getPlatformPackageName(); |
| 90 | + let platformPackageAndVersion: string; |
| 91 | + try { |
| 92 | + logChannel.debug('Determining ESBuild platform package and version'); |
| 93 | + const packageJson = JSON.parse( |
| 94 | + await fs.promises.readFile( |
| 95 | + path.join(context.extensionPath, 'node_modules', 'esbuild', 'package.json'), |
| 96 | + 'utf-8', |
| 97 | + ), |
| 98 | + ); |
| 99 | + |
| 100 | + let packageVersion = packageJson.optionalDependencies?.[platformPackageName]; |
| 101 | + if (!packageVersion) { |
| 102 | + packageVersion = packageJson.version; |
| 103 | + } |
| 104 | + platformPackageAndVersion = `${platformPackageName}@${packageVersion}`; |
| 105 | + logChannel.debug(`Determined ESBuild platform package ${platformPackageAndVersion}`); |
| 106 | + |
| 107 | + // check if already installed |
| 108 | + const platformPackageJsonPath = path.join( |
| 109 | + context.extensionPath, |
| 110 | + 'node_modules', |
| 111 | + ...platformPackageName.split('/'), |
| 112 | + 'package.json', |
| 113 | + ); |
| 114 | + if (fs.existsSync(platformPackageJsonPath)) { |
| 115 | + try { |
| 116 | + const platformPackageJson = JSON.parse( |
| 117 | + await fs.promises.readFile(platformPackageJsonPath, 'utf-8'), |
| 118 | + ); |
| 119 | + if (platformPackageJson.version === packageVersion) { |
| 120 | + logChannel.debug( |
| 121 | + `Determining ESBuild platform package ${platformPackageAndVersion} already installed, skipping install`, |
| 122 | + ); |
| 123 | + return; |
| 124 | + } |
| 125 | + } catch (e) { |
| 126 | + // ignore and trigger install |
| 127 | + } |
| 128 | + } |
| 129 | + } catch (e) { |
| 130 | + logChannel.error( |
| 131 | + `Failed to determine ESBuild platform package`, |
| 132 | + (e as Error).message, |
| 133 | + (e as Error).stack, |
| 134 | + ); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const args = [ |
| 139 | + 'install', |
| 140 | + '--no-save', |
| 141 | + '--omit=dev', |
| 142 | + '--omit=optional', |
| 143 | + '--omit=peer', |
| 144 | + '--prefer-offline', |
| 145 | + '--no-audit', |
| 146 | + '--progress=false', |
| 147 | + platformPackageAndVersion, |
| 148 | + ]; |
| 149 | + logChannel.debug(`Running npm install ${args.join(' ')}`); |
| 150 | + await new Promise<void>((resolve, reject) => { |
| 151 | + exec( |
| 152 | + `npm ${args.join(' ')}`, |
| 153 | + { |
| 154 | + cwd: context.extensionPath, |
| 155 | + env: { |
| 156 | + ...process.env, |
| 157 | + ELECTRON_RUN_AS_NODE: '1', |
| 158 | + }, |
| 159 | + windowsHide: true, |
| 160 | + }, |
| 161 | + (error, stdout, stderr) => { |
| 162 | + if (stdout) { |
| 163 | + logChannel.debug('[ESBuild-stdout]', stdout); |
| 164 | + } |
| 165 | + if (stderr) { |
| 166 | + logChannel.debug('[ESBuild-stderr]', stderr); |
| 167 | + } |
| 168 | + |
| 169 | + if (error) { |
| 170 | + reject(error); |
| 171 | + } else { |
| 172 | + resolve(); |
| 173 | + } |
| 174 | + }, |
| 175 | + ); |
| 176 | + }); |
| 177 | +} |
0 commit comments