Skip to content

Commit

Permalink
feat(cli): auto-install binary on run if not installed already (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Mar 1, 2024
1 parent 0066bcf commit d248a67
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-tools-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagoss/cli': minor
---

Add auto-installation for cli binary
55 changes: 31 additions & 24 deletions crates/cli/npm/binary-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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() {
Expand All @@ -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;

Expand Down

0 comments on commit d248a67

Please sign in to comment.