From d248a67c6f90a278700c3147ee815bbbb553fa49 Mon Sep 17 00:00:00 2001 From: Anbraten <6918444+anbraten@users.noreply.github.com> Date: Fri, 1 Mar 2024 09:56:45 +0100 Subject: [PATCH] feat(cli): auto-install binary on run if not installed already (#17) --- .changeset/nasty-tools-tan.md | 5 +++ crates/cli/npm/binary-install.js | 55 ++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 .changeset/nasty-tools-tan.md diff --git a/.changeset/nasty-tools-tan.md b/.changeset/nasty-tools-tan.md new file mode 100644 index 00000000..4aed02e3 --- /dev/null +++ b/.changeset/nasty-tools-tan.md @@ -0,0 +1,5 @@ +--- +'@lagoss/cli': minor +--- + +Add auto-installation for cli binary diff --git a/crates/cli/npm/binary-install.js b/crates/cli/npm/binary-install.js index bd0482d7..438fa7db 100644 --- a/crates/cli/npm/binary-install.js +++ b/crates/cli/npm/binary-install.js @@ -53,7 +53,11 @@ export class Binary { return this.binaryPath; } - install() { + _isInstalled() { + return existsSync(this._getBinaryPath()); + } + + async install() { const dir = this._getInstallDirectory(); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true }); @@ -69,30 +73,29 @@ export class Binary { console.log(`Downloading release from ${this.url}`); - return axios({ url: this.url, responseType: 'stream' }) - .then(res => { - const writer = tar.x({ strip: 1, C: this.binaryDirectory }); - - return new Promise((resolve, reject) => { - res.data.pipe(writer); - let error = null; - writer.on('error', err => { - error = err; - reject(err); - }); - writer.on('close', () => { - if (!error) { - resolve(true); - } - }); + try { + const res = await axios({ url: this.url, responseType: 'stream' }); + + const writer = tar.x({ strip: 1, C: this.binaryDirectory }); + + await new Promise((resolve, reject) => { + res.data.pipe(writer); + let error = null; + writer.on('error', err => { + error = err; + reject(err); + }); + writer.on('close', () => { + if (!error) { + resolve(true); + } }); - }) - .then(() => { - console.log(`${this.name} has been installed!`); - }) - .catch(e => { - error(`Error fetching release: ${e.message}`); }); + + console.log(`${this.name} has been installed!`); + } catch (e) { + error(`Error fetching release: ${e.message}`); + } } uninstall() { @@ -102,7 +105,11 @@ export class Binary { } } - run() { + async run(shouldInstall = true) { + if (!this._isInstalled() && shouldInstall) { + await this.install(); + } + const binaryPath = this._getBinaryPath(); const [, , ...args] = process.argv;