()
| 122 | // Download + cache the platform bundle from GitHub Releases. Returns |
| 123 | // {command, args}; exits the process with guidance if it can't. |
| 124 | async function selfHealBundle() { |
| 125 | var version = readVersion(); |
| 126 | var bundlesDir = path.join(process.env.CODEGRAPH_INSTALL_DIR || path.join(os.homedir(), '.codegraph'), 'bundles'); |
| 127 | var dest = path.join(bundlesDir, target + '-' + version); |
| 128 | |
| 129 | // Already downloaded by a previous run? Use it even when downloads are |
| 130 | // disabled — CODEGRAPH_NO_DOWNLOAD blocks fetching, not a cached bundle. |
| 131 | var cached = launcherIn(dest); |
| 132 | if (cached) { pruneOldBundles(bundlesDir, dest); return cached; } |
| 133 | |
| 134 | if (process.env.CODEGRAPH_NO_DOWNLOAD) { |
| 135 | fail('the network fallback is disabled (CODEGRAPH_NO_DOWNLOAD is set).'); |
| 136 | } |
| 137 | |
| 138 | var asset = 'codegraph-' + target + (isWindows ? '.zip' : '.tar.gz'); |
| 139 | var base = process.env.CODEGRAPH_DOWNLOAD_BASE || ('https://github.com/' + REPO + '/releases/download'); |
| 140 | var url = base + '/v' + version + '/' + asset; |
| 141 | |
| 142 | process.stderr.write( |
| 143 | 'codegraph: platform bundle missing (registry did not provide ' + pkg + ').\n' + |
| 144 | 'codegraph: downloading ' + asset + ' from GitHub Releases (' + version + ')...\n' |
| 145 | ); |
| 146 | |
| 147 | // Stage inside bundlesDir so the final rename is on the same filesystem (atomic, |
| 148 | // no EXDEV across tmpfs). Strip the archive's top-level codegraph-<target>/ dir. |
| 149 | fs.mkdirSync(bundlesDir, { recursive: true }); |
| 150 | var stage = fs.mkdtempSync(path.join(bundlesDir, '.dl-')); |
| 151 | try { |
| 152 | var archivePath = path.join(stage, asset); |
| 153 | await download(url, archivePath, 6); |
| 154 | await verifyChecksum(archivePath, asset, base, version); |
| 155 | var extracted = path.join(stage, 'bundle'); |
| 156 | fs.mkdirSync(extracted); |
| 157 | extract(archivePath, extracted); |
| 158 | |
| 159 | var raced = launcherIn(dest); // another process may have finished meanwhile |
| 160 | if (raced) { rmrf(stage); return raced; } |
| 161 | try { |
| 162 | fs.renameSync(extracted, dest); |
| 163 | } catch (e) { |
| 164 | var other = launcherIn(dest); // lost the race but theirs is valid |
| 165 | if (other) { rmrf(stage); return other; } |
| 166 | throw e; |
| 167 | } |
| 168 | } catch (e) { |
| 169 | rmrf(stage); |
| 170 | fail('download failed (' + e.message + ').\n URL: ' + url); |
| 171 | } |
| 172 | rmrf(stage); |
| 173 | |
| 174 | var ready = launcherIn(dest); |
| 175 | if (!ready) fail('downloaded bundle is missing its launcher under ' + dest + '.'); |
| 176 | pruneOldBundles(bundlesDir, dest); |
| 177 | process.stderr.write('codegraph: bundle ready.\n'); |
| 178 | return ready; |
| 179 | } |
| 180 | |
| 181 | function readVersion() { |
no test coverage detected