-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cli): binary install (#990)
- Loading branch information
Showing
7 changed files
with
175 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lagon/cli': patch | ||
--- | ||
|
||
Refactor binary installation to be more stable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// All credit goes to Cloudflare wrangler-legacy and | ||
// the initial binary-install package, which this code | ||
// is heavily based on. | ||
// | ||
// Cloudflare wrangler-legacy: https://github.com/cloudflare/wrangler-legacy/blob/master/npm/binary-install.js | ||
// binary-install: https://github.com/EverlastingBugstopper/binary-install#readme | ||
import { existsSync, mkdirSync } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
import { spawnSync } from 'node:child_process'; | ||
|
||
import axios from 'axios'; | ||
import tar from 'tar'; | ||
import { rimraf } from 'rimraf'; | ||
|
||
const error = msg => { | ||
console.error(msg); | ||
process.exit(1); | ||
}; | ||
|
||
export class Binary { | ||
constructor(url, data) { | ||
this.url = url; | ||
this.name = data.name || -1; | ||
this.installDirectory = data.installDirectory; | ||
this.binaryDirectory = -1; | ||
this.binaryPath = -1; | ||
} | ||
|
||
_getInstallDirectory() { | ||
if (!existsSync(this.installDirectory)) { | ||
mkdirSync(this.installDirectory, { recursive: true }); | ||
} | ||
return this.installDirectory; | ||
} | ||
|
||
_getBinaryDirectory() { | ||
const installDirectory = this._getInstallDirectory(); | ||
const binaryDirectory = join(installDirectory, 'bin'); | ||
if (existsSync(binaryDirectory)) { | ||
this.binaryDirectory = binaryDirectory; | ||
} else { | ||
error(`You have not installed ${this.name}`); | ||
} | ||
return this.binaryDirectory; | ||
} | ||
|
||
_getBinaryPath() { | ||
if (this.binaryPath === -1) { | ||
const binaryDirectory = this._getBinaryDirectory(); | ||
this.binaryPath = join(binaryDirectory, this.name); | ||
} | ||
|
||
return this.binaryPath; | ||
} | ||
|
||
install() { | ||
const dir = this._getInstallDirectory(); | ||
if (!existsSync(dir)) { | ||
mkdirSync(dir, { recursive: true }); | ||
} | ||
|
||
this.binaryDirectory = join(dir, 'bin'); | ||
|
||
if (existsSync(this.binaryDirectory)) { | ||
rimraf.sync(this.binaryDirectory); | ||
} | ||
|
||
mkdirSync(this.binaryDirectory, { recursive: true }); | ||
|
||
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); | ||
} | ||
}); | ||
}); | ||
}) | ||
.then(() => { | ||
console.log(`${this.name} has been installed!`); | ||
}) | ||
.catch(e => { | ||
error(`Error fetching release: ${e.message}`); | ||
}); | ||
} | ||
|
||
uninstall() { | ||
if (existsSync(this._getInstallDirectory())) { | ||
rimraf.sync(this.installDirectory); | ||
console.log(`${this.name} has been uninstalled`); | ||
} | ||
} | ||
|
||
run() { | ||
const binaryPath = this._getBinaryPath(); | ||
const [, , ...args] = process.argv; | ||
|
||
const options = { cwd: process.cwd(), stdio: 'inherit' }; | ||
|
||
const result = spawnSync(binaryPath, args, options); | ||
|
||
if (result.error) { | ||
error(result.error); | ||
} | ||
|
||
process.exit(result.status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
#!/usr/bin/env node | ||
|
||
// Prevent exiting with code 1 | ||
// Prevent exiting with code 1 when | ||
// the changeset PR is created | ||
process.exit = () => {}; | ||
|
||
try { | ||
import('./getBinary.js').then(({ getBinary }) => { | ||
getBinary().install(); | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
import { getBinary } from './getBinary.js'; | ||
getBinary().install(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
987642d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./packages/docs
docs-lagon.vercel.app
lagon-docs.vercel.app
docs-git-main-lagon.vercel.app
docs.lagon.app
987642d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
storybook – ./packages/ui
storybook-swart-eight.vercel.app
storybook-git-main-lagon.vercel.app
storybook-lagon.vercel.app
ui.lagon.app
987642d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
www – ./www
www-git-main-lagon.vercel.app
lagon.app
www-lagon.vercel.app
lagon.dev
987642d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
dashboard – ./packages/dashboard
dashboard-git-main-lagon.vercel.app
dash.lagon.app
dashboard-lagon.vercel.app