|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
| 16 | +import fs from 'node:fs'; |
16 | 17 | import {createRequire} from 'node:module'; |
17 | 18 | const require = createRequire(import.meta.url); |
18 | 19 |
|
| 20 | +/** @type {!Map<string, string>} */ |
| 21 | +const platformLookup = new Map([ |
| 22 | + ['darwin', 'macos'], |
| 23 | + ['win32', 'windows'], |
| 24 | + ['linux', 'linux'], |
| 25 | +]); |
| 26 | + |
| 27 | +/** @return {string|undefined} */ |
19 | 28 | export const getNativeImagePath = () => { |
20 | | - if (process.platform === 'darwin') { |
21 | | - try { |
22 | | - return require('google-closure-compiler-macos').default; |
23 | | - } catch (e) { |
24 | | - return; |
25 | | - } |
26 | | - } |
27 | | - if (process.platform === 'win32') { |
28 | | - try { |
29 | | - return require('google-closure-compiler-windows').default; |
30 | | - } catch (e) { |
31 | | - return; |
32 | | - } |
33 | | - } |
34 | | - if (process.platform === 'linux' && ['x64','x32'].includes(process.arch)) { |
35 | | - try { |
36 | | - return require('google-closure-compiler-linux').default; |
37 | | - } catch (e) { |
38 | | - } |
39 | | - } |
40 | | - if (process.platform === 'linux' && ['arm64'].includes(process.arch)) { |
41 | | - try { |
42 | | - return require('google-closure-compiler-linux-arm64').default; |
43 | | - } catch (e) { |
44 | | - } |
| 29 | + let platform = platformLookup.get(process.platform); |
| 30 | + let binarySuffix = ''; |
| 31 | + if (!platform) { |
| 32 | + return; |
| 33 | + } else if (platform === 'linux' && process.arch === 'arm64') { |
| 34 | + platform += '-arm64'; |
| 35 | + } else if (platform === 'windows') { |
| 36 | + binarySuffix = '.exe'; |
45 | 37 | } |
| 38 | + const compilerPath = `google-closure-compiler-${platform}/compiler${binarySuffix}`; |
| 39 | + try { |
| 40 | + return require.resolve(compilerPath); |
| 41 | + } catch {} |
46 | 42 | }; |
47 | 43 |
|
| 44 | +/** |
| 45 | + * @param {!Array<string>} platforms |
| 46 | + * @return {string} |
| 47 | + */ |
48 | 48 | export const getFirstSupportedPlatform = (platforms) => { |
49 | 49 | const platform = platforms.find((platform, index) => { |
50 | 50 | switch (platform.toLowerCase()) { |
|
0 commit comments