|
| 1 | +const { platform, arch, tmpdir } = require('os'); |
| 2 | +const { execSync } = require('child_process'); |
| 3 | +const { existsSync } = require('fs'); |
| 4 | + |
| 5 | +const miseMarkers = ['mise.toml', '.mise.toml', '.tool-versions']; |
| 6 | + |
| 7 | +if (!miseMarkers.some((f) => existsSync(f))) { |
| 8 | + console.log( |
| 9 | + 'No mise config detected (no mise.toml, .mise.toml, or .tool-versions). Skipping mise install.', |
| 10 | + ); |
| 11 | + return; |
| 12 | +} |
| 13 | + |
| 14 | +// --- inlined install-mise logic (minus setToolVersions, since we use the |
| 15 | +// existing mise config file from the repo) --- |
| 16 | + |
| 17 | +const runInstall = process.env['NX_CLOUD_INPUT_auto-install'] |
| 18 | + ? process.env['NX_CLOUD_INPUT_auto-install'] === 'true' |
| 19 | + : true; |
| 20 | +const miseGhVersion = |
| 21 | + process.env['NX_CLOUD_INPUT_mise-version'] || 'v2025.12.2'; |
| 22 | +const installArgs = process.env['NX_CLOUD_INPUT_install-args'] || ''; |
| 23 | + |
| 24 | +const MISE_INSTALL_DIR = '$HOME/.local/bin'; |
| 25 | +const MISE_SHIM_DIR = '$HOME/.local/share/mise/shims'; |
| 26 | +const TMP_DIR = tmpdir(); |
| 27 | + |
| 28 | +process.env.MISE_TRUSTED_CONFIG_PATHS = process.cwd(); |
| 29 | +process.env.MISE_YES = '1'; |
| 30 | + |
| 31 | +function retryWithBackoff( |
| 32 | + fn, |
| 33 | + maxRetries = 3, |
| 34 | + retryDelayMs = 1000, |
| 35 | + fnName = 'operation', |
| 36 | +) { |
| 37 | + let lastError; |
| 38 | + for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 39 | + try { |
| 40 | + fn(); |
| 41 | + if (attempt > 1) |
| 42 | + console.log(`> ${fnName} succeeded on attempt ${attempt}`); |
| 43 | + return; |
| 44 | + } catch (error) { |
| 45 | + lastError = error; |
| 46 | + console.error(`> ${fnName} failed on attempt ${attempt}/${maxRetries}`); |
| 47 | + console.error(` Error: ${error.message}`); |
| 48 | + if (attempt < maxRetries) { |
| 49 | + const delay = retryDelayMs * Math.pow(2, attempt - 1); |
| 50 | + console.log(`> Retrying in ${delay}ms...`); |
| 51 | + execSync(`sleep ${delay / 1000}`, { stdio: 'inherit' }); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + console.error(`> ${fnName} failed after ${maxRetries} attempts`); |
| 56 | + throw lastError; |
| 57 | +} |
| 58 | + |
| 59 | +if (!miseGhVersion.startsWith('v')) { |
| 60 | + console.error(`Invalid mise_version: ${miseGhVersion}`); |
| 61 | + process.exit(1); |
| 62 | +} |
| 63 | + |
| 64 | +function getVersionUrl(_platform, _arch) { |
| 65 | + let resolvedPlatform; |
| 66 | + let resolvedArch; |
| 67 | + if (_platform === 'darwin') resolvedPlatform = 'macos'; |
| 68 | + else if (_platform === 'win32') resolvedPlatform = 'windows'; |
| 69 | + else resolvedPlatform = 'linux'; |
| 70 | + |
| 71 | + if (_arch === 'x64' || _arch === 'arm64') resolvedArch = _arch; |
| 72 | + else { |
| 73 | + console.error('Unsupported architecture: ', _arch); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + |
| 77 | + const version = `${miseGhVersion}-${resolvedPlatform}-${resolvedArch}`; |
| 78 | + const url = `https://github.com/jdx/mise/releases/download/${miseGhVersion}/mise-${version}.tar.gz`; |
| 79 | + console.log(`> Resolved mise version to download as: ${version}`); |
| 80 | + return url; |
| 81 | +} |
| 82 | + |
| 83 | +function downloadMise(downloadUrl) { |
| 84 | + console.log(`> Downloading mise from ${downloadUrl}`); |
| 85 | + execSync(`mkdir -p ${MISE_INSTALL_DIR}`, { stdio: 'inherit' }); |
| 86 | + retryWithBackoff( |
| 87 | + () => { |
| 88 | + execSync(`curl -fsSL ${downloadUrl} | tar -xzf - -C ${TMP_DIR}`, { |
| 89 | + stdio: 'inherit', |
| 90 | + }); |
| 91 | + }, |
| 92 | + 3, |
| 93 | + 1000, |
| 94 | + 'mise download', |
| 95 | + ); |
| 96 | + console.log(`> Download successful! Moving binary to: ${MISE_INSTALL_DIR}`); |
| 97 | + execSync(`mv ${TMP_DIR}/mise/bin/mise ${MISE_INSTALL_DIR}/mise`, { |
| 98 | + stdio: 'inherit', |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +function setupMise(autoInstall, installCommandArgs) { |
| 103 | + console.log('> Setting up mise...\n\n'); |
| 104 | + execSync(`chmod +x ${MISE_INSTALL_DIR}/mise`, { stdio: 'inherit' }); |
| 105 | + execSync(`mkdir -p ${MISE_SHIM_DIR}`, { stdio: 'inherit' }); |
| 106 | + |
| 107 | + const newPATH = `${MISE_INSTALL_DIR}:${MISE_SHIM_DIR}:$PATH`; |
| 108 | + const setPath = `export PATH="${newPATH}" && echo PATH="${newPATH}" >> $NX_CLOUD_ENV`; |
| 109 | + const setMiseEnvVars = `echo "MISE_YES=1" >> $NX_CLOUD_ENV && echo "MISE_TRUSTED_CONFIG_PATHS=$HOME/workspace" >> $NX_CLOUD_ENV`; |
| 110 | + const whichMise = `echo "mise is located at $(which mise)"`; |
| 111 | + const miseVersion = `echo "mise version is: $(mise version)"`; |
| 112 | + const installMiseTools = autoInstall |
| 113 | + ? `mise install ${installCommandArgs}` |
| 114 | + : `echo "Skipping auto install. You will need to manually run mise install"`; |
| 115 | + |
| 116 | + execSync( |
| 117 | + [setPath, setMiseEnvVars, whichMise, miseVersion, installMiseTools].join( |
| 118 | + ' && ', |
| 119 | + ), |
| 120 | + { stdio: 'inherit' }, |
| 121 | + ); |
| 122 | + console.log('\n> Finished setting up mise!'); |
| 123 | +} |
| 124 | + |
| 125 | +try { |
| 126 | + const dlUrl = getVersionUrl(platform(), arch()); |
| 127 | + downloadMise(dlUrl); |
| 128 | + setupMise(runInstall, installArgs); |
| 129 | +} catch (error) { |
| 130 | + console.error('> Failed to install mise: '); |
| 131 | + console.error(error); |
| 132 | + process.exit(1); |
| 133 | +} |
0 commit comments