|
| 1 | +/* eslint-disable */ |
| 2 | +/** |
| 3 | + * @license |
| 4 | + * Copyright 2023 Lvce Editor |
| 5 | + * SPDX-License-Identifier: MIT |
| 6 | + */ |
| 7 | +import { VError } from '@lvce-editor/verror' |
| 8 | +import { execa } from 'execa' |
| 9 | +import extractZip from 'extract-zip' |
| 10 | +import fsExtra from 'fs-extra' |
| 11 | +import got from 'got' |
| 12 | +import * as os from 'node:os' |
| 13 | +import { dirname, join } from 'node:path' |
| 14 | +import { pathExists } from 'path-exists' |
| 15 | +import { pipeline } from 'node:stream/promises' |
| 16 | +import { temporaryFile } from 'tempy' |
| 17 | +import { fileURLToPath } from 'node:url' |
| 18 | +import { xdgCache } from 'xdg-basedir' |
| 19 | + |
| 20 | +const { mkdir, createWriteStream, move } = fsExtra |
| 21 | + |
| 22 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 23 | + |
| 24 | +const REPOSITORY = `microsoft/ripgrep-prebuilt` |
| 25 | +const VERSION = process.env.RIPGREP_VERSION || 'v13.0.0-10' |
| 26 | +console.log({ VERSION }) |
| 27 | +const BIN_PATH = join(__dirname, '../bin') |
| 28 | + |
| 29 | +const getTarget = () => { |
| 30 | + const arch = process.env.npm_config_arch || os.arch() |
| 31 | + const platform = process.env.platform || os.platform() |
| 32 | + switch (platform) { |
| 33 | + case 'darwin': |
| 34 | + switch (arch) { |
| 35 | + case 'arm64': |
| 36 | + return 'aarch64-apple-darwin.tar.gz' |
| 37 | + default: |
| 38 | + return 'x86_64-apple-darwin.tar.gz' |
| 39 | + } |
| 40 | + case 'win32': |
| 41 | + switch (arch) { |
| 42 | + case 'x64': |
| 43 | + return 'x86_64-pc-windows-msvc.zip' |
| 44 | + case 'arm': |
| 45 | + return 'aarch64-pc-windows-msvc.zip' |
| 46 | + default: |
| 47 | + return 'i686-pc-windows-msvc.zip' |
| 48 | + } |
| 49 | + case 'linux': |
| 50 | + switch (arch) { |
| 51 | + case 'x64': |
| 52 | + return 'x86_64-unknown-linux-musl.tar.gz' |
| 53 | + case 'arm': |
| 54 | + case 'armv7l': |
| 55 | + return 'arm-unknown-linux-gnueabihf.tar.gz' |
| 56 | + case 'arm64': |
| 57 | + return 'aarch64-unknown-linux-gnu.tar.gz' |
| 58 | + case 'ppc64': |
| 59 | + return 'powerpc64le-unknown-linux-gnu.tar.gz' |
| 60 | + case 's390x': |
| 61 | + return 's390x-unknown-linux-gnu.tar.gz' |
| 62 | + default: |
| 63 | + return 'i686-unknown-linux-musl.tar.gz' |
| 64 | + } |
| 65 | + default: |
| 66 | + throw new VError('Unknown platform: ' + platform) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export const downloadFile = async (url, outFile) => { |
| 71 | + try { |
| 72 | + const tmpFile = temporaryFile() |
| 73 | + await pipeline(got.stream(url), createWriteStream(tmpFile)) |
| 74 | + await mkdir(dirname(outFile), { recursive: true }) |
| 75 | + await move(tmpFile, outFile) |
| 76 | + } catch (error) { |
| 77 | + throw new VError(error, `Failed to download "${url}"`) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * @param {string} inFile |
| 83 | + * @param {string} outDir |
| 84 | + */ |
| 85 | +const unzip = async (inFile, outDir) => { |
| 86 | + try { |
| 87 | + await mkdir(outDir, { recursive: true }) |
| 88 | + await extractZip(inFile, { dir: outDir }) |
| 89 | + } catch (error) { |
| 90 | + throw new VError(error, `Failed to unzip "${inFile}"`) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * @param {string} inFile |
| 96 | + * @param {string} outDir |
| 97 | + */ |
| 98 | +const untarGz = async (inFile, outDir) => { |
| 99 | + try { |
| 100 | + await mkdir(outDir, { recursive: true }) |
| 101 | + await execa('tar', ['xvf', inFile, '-C', outDir]) |
| 102 | + } catch (error) { |
| 103 | + throw new VError(error, `Failed to extract "${inFile}"`) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +export const downloadRipGrep = async () => { |
| 108 | + const target = getTarget() |
| 109 | + const url = `https://github.com/${REPOSITORY}/releases/download/${VERSION}/ripgrep-${VERSION}-${target}` |
| 110 | + const downloadPath = `${xdgCache}/vscode-ripgrep/ripgrep-${VERSION}-${target}` |
| 111 | + if (!(await pathExists(downloadPath))) { |
| 112 | + await downloadFile(url, downloadPath) |
| 113 | + } else { |
| 114 | + console.info(`File ${downloadPath} has been cached`) |
| 115 | + } |
| 116 | + if (downloadPath.endsWith('.tar.gz')) { |
| 117 | + await untarGz(downloadPath, BIN_PATH) |
| 118 | + } else if (downloadPath.endsWith('.zip')) { |
| 119 | + await unzip(downloadPath, BIN_PATH) |
| 120 | + } else { |
| 121 | + throw new VError(`Invalid downloadPath ${downloadPath}`) |
| 122 | + } |
| 123 | +} |
0 commit comments