-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove binary install and bump version
- Loading branch information
Showing
7 changed files
with
261 additions
and
17 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
## Bug fixes | ||
|
||
- Fixed npm install location (node_modules/.bin) | ||
- Remove binary-install dependency and control download of binaries |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
const { existsSync, mkdirSync } = require("fs"); | ||
const { join } = require("path"); | ||
const { spawnSync } = require("child_process"); | ||
|
||
const axios = require("axios"); | ||
const tar = require("tar"); | ||
const rimraf = require("rimraf"); | ||
|
||
const error = (msg) => { | ||
console.error(msg); | ||
process.exit(1); | ||
}; | ||
|
||
class Binary { | ||
constructor(name, url) { | ||
let errors = []; | ||
if (typeof url !== "string") { | ||
errors.push("url must be a string"); | ||
} else { | ||
try { | ||
new URL(url); | ||
} catch (e) { | ||
errors.push(e); | ||
} | ||
} | ||
if (name && typeof name !== "string") { | ||
errors.push("name must be a string"); | ||
} | ||
|
||
if (!name) { | ||
errors.push("You must specify the name of your binary"); | ||
} | ||
if (errors.length > 0) { | ||
let errorMsg = | ||
"One or more of the parameters you passed to the Binary constructor are invalid:\n"; | ||
errors.forEach((error) => { | ||
errorMsg += error; | ||
}); | ||
errorMsg += | ||
'\n\nCorrect usage: new Binary("my-binary", "https://example.com/binary/download.tar.gz")'; | ||
error(errorMsg); | ||
} | ||
this.url = url; | ||
this.name = name; | ||
this.installDirectory = join(__dirname, "node_modules", ".bin"); | ||
|
||
if (!existsSync(this.installDirectory)) { | ||
mkdirSync(this.installDirectory, { recursive: true }); | ||
} | ||
|
||
this.binaryPath = join(this.installDirectory, this.name); | ||
} | ||
|
||
exists() { | ||
return existsSync(this.binaryPath); | ||
} | ||
|
||
install(fetchOptions, suppressLogs = false) { | ||
if (this.exists()) { | ||
if (!suppressLogs) { | ||
console.error( | ||
`${this.name} is already installed, skipping installation.` | ||
); | ||
} | ||
return Promise.resolve(); | ||
} | ||
|
||
if (existsSync(this.installDirectory)) { | ||
rimraf.sync(this.installDirectory); | ||
} | ||
|
||
mkdirSync(this.installDirectory, { recursive: true }); | ||
|
||
if (suppressLogs) { | ||
console.error(`Downloading release from ${this.url}`); | ||
} | ||
|
||
return axios({ ...fetchOptions, url: this.url, responseType: "stream" }) | ||
.then((res) => { | ||
return new Promise((resolve, reject) => { | ||
const sink = res.data.pipe( | ||
tar.x({ strip: 1, C: this.installDirectory }) | ||
); | ||
sink.on("finish", () => resolve()); | ||
sink.on("error", (err) => reject(err)); | ||
}); | ||
}) | ||
.then(() => { | ||
if (suppressLogs) { | ||
console.error(`${this.name} has been installed!`); | ||
} | ||
}) | ||
.catch((e) => { | ||
error(`Error fetching release: ${e.message}`); | ||
}); | ||
} | ||
|
||
run(fetchOptions) { | ||
if (!this.exists()) { | ||
this.install(fetchOptions, true).then(() => { | ||
this.execute(); | ||
}); | ||
return; | ||
} | ||
|
||
this.execute(); | ||
} | ||
|
||
execute(args, options) { | ||
const [, , ...args] = process.argv; | ||
|
||
const options = { cwd: process.cwd(), stdio: "inherit" }; | ||
|
||
const result = spawnSync(this.binaryPath, args, options); | ||
|
||
if (result.error) { | ||
error(result.error); | ||
} | ||
} | ||
} | ||
|
||
module.exports.Binary = Binary; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.