Skip to content

Commit

Permalink
feat: add npm install
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Dec 5, 2020
1 parent 58d7a2d commit 89c46fe
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# vendor/

.idea
bin
dist
coverage.out
release.md
Expand Down
4 changes: 4 additions & 0 deletions npm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin/whatchanged
bin/whatchanged.exe
*-lock.json
*.lock
4 changes: 4 additions & 0 deletions npm/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
*.lock
package-lock.json
dist
Empty file added npm/bin/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions npm/bin/whatchanged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error("what changed command line did not install correctly");
16 changes: 16 additions & 0 deletions npm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "whatchanged",
"version": "0.2.2",
"description": "An elegant changelog generator",
"bin": "./bin/whatchanged",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"postinstall": "node scripts/postinstall.js"
},
"author": "Axetroy",
"license": "ISC",
"dependencies": {
"download": "^8.0.0",
"progress": "^2.0.3"
}
}
68 changes: 68 additions & 0 deletions npm/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const path = require("path");
const os = require("os");
const download = require("download");
const ProgressBar = require("progress");

const pkg = require("../package.json");

function getArch() {
const arch = os.arch();
switch (arch) {
case "ia32":
case "x32":
return "386";
case "x64":
return "amd64";
case "arm":
// @ts-expect-error ignore error
const armv = process.config.variables.arm_version;

if (!armv) return "armv7";

return `armv${armv}`;
default:
return arch;
}
}

function getPlatform() {
const platform = os.platform();
switch (platform) {
case "win32":
return "windows";
default:
return platform;
}
}

function getDownloadURL(version) {
const url = `https://github.com/axetroy/whatchanged/releases/download/${version}/whatchanged_${getPlatform()}_${getArch()}.tar.gz`;
return url;
}

async function install(version) {
const url = getDownloadURL(version);

console.log(`Downloading '${url}'`);

const bar = new ProgressBar("[:bar] :percent :etas", {
complete: "=",
incomplete: " ",
width: 20,
total: 0,
});

await download(url, path.join(__dirname, "..", "bin"), {
extract: true,
}).on("response", (res) => {
bar.total = res.headers["content-length"];
res.on("data", (data) => bar.tick(data.length));
res.on("error", (err) => {
console.error("error");
});
});
}

install("v" + pkg.version).catch((err) => {
throw err;
});

0 comments on commit 89c46fe

Please sign in to comment.