Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions tools/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"README.md"
],
"scripts": {
"postinstall": "node ./utils/install.js",
"preuninstall": "node ./utils/uninstall.js"
}
}
}
11 changes: 9 additions & 2 deletions tools/npm/utils/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,23 @@ const run = () => {

const install = () => {
const binary = getBinary();
binary.install();
return binary.install();
};

const uninstall = () => {
const binary = getBinary();
binary.uninstall();
};

const getBinaryPath = () => {
const { join } = require("path");
const platform_arch = getPlatform();
return join(__dirname, "bin", "bin", `zcli-${platform_arch}`);
};
Comment on lines +40 to +44
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-derives the installDir/bin/bin/name layout that binary-source.js already owns, so the two can drift. Suggest delegating to a small public getter on the Binary class:

Suggested change
const getBinaryPath = () => {
const { join } = require("path");
const platform_arch = getPlatform();
return join(__dirname, "bin", "bin", `zcli-${platform_arch}`);
};
const getBinaryPath = () => getBinary().getPath();

This needs a matching getPath() on Binary in binary-source.js. It can't reuse the existing _getBinaryPath(), which calls process.exit(1) when the binary isn't installed — the very case we're checking here:

getPath() {
  return join(this.installDirectory, "bin", this.name);
}


module.exports = {
install,
run,
uninstall,
};
getBinaryPath,
};
16 changes: 14 additions & 2 deletions tools/npm/utils/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
#!/usr/bin/env node

const { run } = require("./binary");
run();
const { existsSync } = require("fs");
const { run, install, getBinaryPath } = require("./binary");

if (!existsSync(getBinaryPath())) {
process.stderr.write("binary not found downloading now...\n");
install()
.then(() => run())
.catch((err) => {
process.stderr.write(`download failed: ${err.message}\n`);
process.exit(1);
});
Comment on lines +8 to +13
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

binary-source.install() already handles download failures internally (error()process.exit(1)), so this .catch is unreachable. Simplify to:

Suggested change
install()
.then(() => run())
.catch((err) => {
process.stderr.write(`download failed: ${err.message}\n`);
process.exit(1);
});
install().then(() => run());

} else {
run();
}