Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
run-script-os
Advanced tools
run-script-os is a tool that will let you use generic npm script commands that will pass through to os specific commands.
The run-script-os npm package allows you to run different npm scripts based on the operating system. This is particularly useful for cross-platform projects where certain commands or scripts need to be executed differently depending on the OS.
Run OS-specific scripts
This feature allows you to define OS-specific scripts in your package.json. When you run `npm start`, it will execute the appropriate script based on the operating system.
{
"scripts": {
"start": "run-script-os"
},
"scripts-os": {
"win32": {
"start": "echo Running on Windows"
},
"darwin": {
"start": "echo Running on macOS"
},
"linux": {
"start": "echo Running on Linux"
}
}
}
Fallback script
You can also define a fallback script using the `default` key. This script will run if the operating system does not match any of the specified OS keys.
{
"scripts": {
"start": "run-script-os"
},
"scripts-os": {
"win32": {
"start": "echo Running on Windows"
},
"darwin": {
"start": "echo Running on macOS"
},
"linux": {
"start": "echo Running on Linux"
},
"default": {
"start": "echo Running on an unsupported OS"
}
}
}
The cross-env package allows you to set environment variables across different platforms. While it doesn't provide OS-specific script execution like run-script-os, it is useful for ensuring environment variables are set correctly regardless of the operating system.
The npm-run-all package allows you to run multiple npm scripts sequentially or in parallel. It doesn't provide OS-specific script execution but is useful for managing complex npm script workflows.
The concurrently package allows you to run multiple commands concurrently. While it doesn't offer OS-specific script execution, it is useful for running multiple tasks at the same time, such as starting a server and watching files for changes.
You will be able to use OS specific operations in npm scripts.
If you have experienced the pain of trying to make npm scripts usable across different operating system, this package is for you! Looking at you rm
and del
!
npm install --save-dev run-script-os
Set run-script-os
(or run-os
) as the value of the npm script field that you want different functionality per OS. In the example below, we set test
, but it can be any npm script. It also uses pre
and post
commands (explained more below).
Then create OS specific scripts. In the example below, you can see:
test:win32
test:linux:darwin
test:default
Those can have OS specific logic.
package.json
{
...
"scripts": {
...
"test": "run-script-os",
"test:win32": "echo 'del whatever you want in Windows 32/64'",
"test:darwin:linux": "echo 'You can combine OS tags and rm all the things!'",
"test:default": "echo 'This will run on any platform that does not have its own script'"
...
},
...
}
Windows Output:
> npm test
del whatever you want in Windows 32/64
macOS and Linux Output:
> npm test
You can combine OS tags and rm all the things!
You can use the following aliases:
:windows
- Alias for win32:macos
- Alias for darwin:nix
- This will run on anything considered to be a *nix OS (aix, darwin, freebsd, linux, openbsd, sunos, android):default
- This will run if no platform-specific scripts are foundBy default, run-script-os will detect cygwin/git bash as Windows. If you would rather your platform be detected as Linux under these environments:
Set environment variable:
RUN_OS_WINBASH_IS_LINUX=true
When you call a script like npm test
, npm will first call pretest
if it exists. It will then call test
, which, if you are using run-script-os
, it will then call npm run test:YOUR OS
, which in turn will call pretest:YOUR OS
before actually running test:YOUR OS
. Then posttest:YOUR OS
will run, and then after that posttest
will finally execute.
There is an example showing pre
and post
commands found in the package.json
of this repository.
OS Options: darwin
, freebsd
, linux
, sunos
, win32
More information can be found in Node's process.platform
and Node's os.platform()
.
FAQs
run-script-os is a tool that will let you use generic npm script commands that will pass through to os specific commands.
We found that run-script-os demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.