0

I am running a npm publish which embeds a wasm binary file, and I want to check if the compiled file is not missing before actually publishing.

I guess I could use prepublishOnly with a custom shell command?
Is there any more specific rule to put into the package.json to do the job?

ymoreau
  • 3,402
  • 1
  • 22
  • 60
  • 1
    There's no other _"more specific rule to put into package.json"_ that I'm aware of. Use the `prepublishOnly` hook as you've suggested. For _*nix_ only, utilize a compound shell command to test if file exists, if it's missing then exit with a non-zero; E.g. `"prepublishOnly": "[ -f ./some/file.wasm ] || { echo \"Missing file.\" && exit 1; };"`. Or if cross-platform is a requirement then consider utilizing node.js in your npm-script instead. For instance: `"prepublishOnly": "node -e \"if (! require('fs').existsSync('./some/file.wasm')) { console.log('Missing file.'); process.exit(1) }\""` – RobC Jul 28 '20 at 09:24

1 Answers1

0

I used this code:

"prepublishOnly": "if [ -f wasm-file.js ]; then npm run any-custom-command-for-prepublish; else echo \"WASM files are missing.\"; exit 1; fi"

ymoreau
  • 3,402
  • 1
  • 22
  • 60
  • 1
    I suggest checking your compound command using [ShellCheck](https://www.shellcheck.net/) as this is not the logic you want. ShellCheck reports [this error](https://github.com/koalaman/shellcheck/wiki/SC2015). Essentially your current logic is: if `[ -f wasm-file.js ]` is true but `npm run any-custom-command-for-prepublish` returns false, `{ echo \"WASM files are missing.\" && exit 1; }` will run. – RobC Jul 28 '20 at 10:07
  • The logic you want is: `"prepublishOnly": "if [ -f wasm-file.js ]; then npm run any-custom-command-for-prepublish; else { echo \"WASM files are missing.\" && exit 1; }; fi",` – RobC Jul 28 '20 at 10:27