Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute OS specific script in node / Grunt

I have a Grunt task which executes .cmd file on the local machine to do its thing. I need to use this task on the CI server, which is a Linux machine. I have the relevant .sh (shell script for Linux) for that. I need a way to execute these two without changing my Gruntfile.

Currently I have to change my Gruntfile to make it work locally for windows and remote file uses .sh.

Any solution to do same is welcome. Detecting underlying OS? Or a way to call same command which internally calls the OS specific command?

like image 498
Vishwanath Avatar asked May 24 '14 15:05

Vishwanath


People also ask

How do I run a node Program in shell script?

The usual way to run a Node.js program is to run the globally available node command (once you install Node.js) and pass the name of the file you want to execute. If your main Node.js application file is app.js, you can call it by typing: Above, you are explicitly telling the shell to run your script with node.

How do I start Grunt on Linux?

In order to get started, you'll want to install Grunt's command line interface (CLI) globally. You may need to use sudo (for OSX, *nix, BSD etc) or run your command shell as Administrator (for Windows) to do this. This will put the grunt command in your system path, allowing it to be run from any directory.

How do I execute a C++ program in Node JS?

Our Node.js code will then execute that C++ program in a new process, not blocking its other activities, and when complete process its output. Two functions that we will use to execute shell commands are exec and spawn. The exec () function creates a new shell and executes a given command.

Can I run grunt from any folder in my project?

Because of this, you can run grunt from any subfolder in your project. If a locally installed Grunt is found, the CLI loads the local installation of the Grunt library, applies the configuration from your Gruntfile, and executes any tasks you've requested for it to run.


1 Answers

You could take advantage of node's process.platform:

process.platform
What platform you're running on: 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'

console.log('This platform is ' + process.platform);

Then within the code, optionally add the file extensions based on that:

if (process.platform === "win32") {
    ext = ".cmd";
} else {
    ext = ".sh";
}
like image 151
dylants Avatar answered Oct 21 '22 00:10

dylants