Search code examples
node.jsgitnpmgithookshusky

When using npm scripts, how can I tell if I am currently running windwos or mac


Since it is a multi-person development, in mac environment, it is required to set chmod + x.usky /pre-commit for projects, but many people may deliberately leave it unset in an attempt to submit the wrong code to the repository

I know you can do this, but this command does not exist on windows. How can I do this command only on mac

scripts:{
  "prepare": "husky install && chmod +x .husky/*"`
}

Solution

  • How can I do this command only on mac

    You can use this package: run-script-os.

    Install:

    npm install --save-dev run-script-os
    

    Then set run-script-os as the value of the npm script field that you want different functionality per OS.

    Then create OS specific scripts.

    scripts:{
      "prepare": "run-script-os",
      "prepare:macos": "husky install && chmod +x .husky/*"
    }
    

    The command will then only work when run on MacOS. In other cases it'll say: run-script-os was unable to execute the script 'prepare'.