diff --git a/tools/npm/package.json b/tools/npm/package.json index 2332964e..c60e1a73 100644 --- a/tools/npm/package.json +++ b/tools/npm/package.json @@ -25,7 +25,6 @@ "README.md" ], "scripts": { - "postinstall": "node ./utils/install.js", "preuninstall": "node ./utils/uninstall.js" } -} +} \ No newline at end of file diff --git a/tools/npm/utils/binary.js b/tools/npm/utils/binary.js index 6ff9ea89..796ea7d0 100755 --- a/tools/npm/utils/binary.js +++ b/tools/npm/utils/binary.js @@ -29,7 +29,7 @@ const run = () => { const install = () => { const binary = getBinary(); - binary.install(); + return binary.install(); }; const uninstall = () => { @@ -37,8 +37,15 @@ const uninstall = () => { binary.uninstall(); }; +const getBinaryPath = () => { + const { join } = require("path"); + const platform_arch = getPlatform(); + return join(__dirname, "bin", "bin", `zcli-${platform_arch}`); +}; + module.exports = { install, run, uninstall, -}; + getBinaryPath, +}; \ No newline at end of file diff --git a/tools/npm/utils/run.js b/tools/npm/utils/run.js index 847da530..b5f757aa 100755 --- a/tools/npm/utils/run.js +++ b/tools/npm/utils/run.js @@ -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); + }); +} else { + run(); +} \ No newline at end of file