Visual Studio Run Guard External Tools - visual-studio-2012

I have set up a basic guard file to watch for css and js in my .net project.
If i do this in the CMD to the project and type "guard" it works great.
How would I get this to be a external command in visual studio? I would need a .bat or .exe to run in the $(project) and pass the guard argument.
What am I missing

I'm not a Windows guy, but you probably want to start Guard in the background with a command like: START /B CMD /C CALL "foo.bat"
You probably also want to set the starting directory to where the Gemfile is, and run bundle exec guard as the command.

Related

How to use Command Prompt after ng serve was compiled

In Angular 5, after creating project folder, installing Angular CLI, Node.js (all latest versions) and ng serve was compiled successfully through command prompt. Then I tried to use command prompt to install bootstrap. I could not control / use command prompt. What might be the cause and effect?
Command prompt stuck after compilation
There are simple ways:
If you use Command Prompt(Terminal), open another Command Prompt inside your project path.
If you use IDE like Visual Studio Code, open Terminal and then you can use Plus(+) sign for openning anothor Terminal like below image:
If you use IDE like WebStrom (Jetbrains IDE), first open IDE Terminal then make right click and select New Session and new terminal openning like below image:
There are couple of ways you can do that.
The official way of deployment of angular app.
Create a start.bat (if you are using windows) and write the ng serve --open command there. Now you can run this file which will internally run your angular app, using forever or pm2 npm modules.
If you do that these will demonise your process to run in background and your same command prompt becomes usable again.
Once the process is finished( compiled successfully), you would notice that the cmd -prompt newline doesn't show any directory and its blank.
This means that, you just have to open an another (new terminal), get to the right directory using "cd command "and implement your desired commands or operations again.
use ctrl + v it will ask weather to terminate say Y, cursor will go back

How can I suppress “Terminate batch job (Y/N)” confirmation in PowerShell?

When I press Ctrl+C in PowerShell, I receive:
Terminate batch job (Y/N)?
Similar to https://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation, except for Windows PowerShell.
Does PowerShell provide any more control over batch jobs than what CMD does?
The behavior is neither caused by PowerShell nor can PowerShell change it (as evidenced by the PowerShell source-code repo not containing the prompt message).
The behavior is built into cmd.exe - Powershell, in this case, is calling a .cmd file (batch file), which is interpreted by cmd.exe.
If you explicitly control the invocation of the target executable, you can fix this by moving to Powershell - note this has its own considerations, see below.
If you do not explicitly control the invocation of the target executable, you're out of luck (unless you're willing to install third-party cmd.exe replacements) and must press Ctrl+C twice in order to terminate execution.
A[n ill-advised] workaround is to modify the cmd.exe binary - see article with instructions on how to patch the cmd.exe executable in order to suppress the prompt. Additionally, you can post a feature request on GitHub to request that this behavior be fixed at the source, though that is unlikely to happen for reasons of backward compatbility.
To demonstrate the behavior:
The examples assume that Node.js is installed and that node.exe is therefore in your PATH:
First, invoke node.exe directly, with a tight loop that requires you to press Ctrl+C to terminate the process.
PS> node -e "while (true);"
As you'll see, pressing Ctrl+C instantly terminates the process - no confirmation prompt.
Now, let's create a sample batch file that invokes the same command and invoke that batch file:
PS> "#echo off`nnode -e `"while (true);`"" | Set-Content test.cmd
PS> ./test.cmd
As you'll see, pressing Ctrl+C now presents the undesired Terminate batch job (Y/N)? prompt. (You'd get the same behavior if you ran the batch file from cmd.exe.)
To demonstrate that gulp is a cmd file:
You say you're running your command via gulp's CLI.
On Windows, the entry point for the gulp CLI is gulp.cmd [see update in the bottom section] - i.e., a batch file. That is how it works in general for npm-package "binaries" (executables) implemented as either JS files or shell scripts.
That gulp invokes gulp.cmd can be verified as follows:
# Execute from a project folder that has `gulp` installed as a dependency.
# If `gulp` is installed *globally*
# Note: CLI `npx` requires npm version 5.2.0+
PS C:\some\NodeJs\project> npx where gulp
You'll see something like:
C:\some\NodeJs\project\node_modules\.bin\gulp
C:\some\NodeJs\project\node_modules\.bin\gulp.cmd
Note that where.exe also lists the extension-less Unix-shell script, ...\gulp; however, from cmd.exe / Powershell such a shell script isn't directly executable, and it is ...\gulp.cmd - the batch file - that is executed.
(If in doubt, place a command such as #set /p dummy="Press a key" at the start of the gulp.cmd file, and you'll see that this command executes when you invoke gulp without the .cmd extension.
Also note that there is no gulp.exe.)
More generally, on Windows, a project's node_modules\.bin subfolder contains pairs of CLI entry points for the CLIs that come with packages that the project depends on locally:
node_modules\.bin\<some-cli> is the Unix shell script (whose executing interpreter is controlled via its shebang line).
node_modules\.bin\<some-cli>.cmd is the helper batch file for Windows.
Updates and future considerations:
In the context of npm modules, the problem would go away if a PowerShell script (*.ps1) were used as the helper script on Windows. There are tickets for npm, yarn and similar software to do this. There are also some drawbacks:
*.ps1 files aren't directly executable from outside of PowerShell, notably from cmd.exe and File Explorer (and changing that is nontrivial).
PowerShell still hasn't fully replaced cmd.exe as the default shell, as of Windows 10 (and won't anytime soon, if ever).
When called from PowerShell, a *.ps1 file would be found and run in-process, so a possible solution is for the npm project to also provide *.ps1 helper scripts, which would take precedence over *.cmd files of the same name.
Update:
Recent versions of npm (verified in 6.14.10) indeed DO install such *.ps1 files.
Alternative package manager yarn, since v2 does not seem to use batch files anymore at all, so the original problem is bypassed there; (v1, by contrast, still uses batch files (only); upgrading from v1 must be done on a per-project basis see the migration instructions).
As the other answer notes, the correct fix is to replace cmd scripts with ps1 versions.
However another workaround for users of the Hyper shell is 'Hyper yes', a plugin that automatically hits y for you when the prompt comes up.
best way to avoid it is to not start it, in my case, is not to type npm run devStart but instead type nodemon ./server.js localhost 3000
here's how it looks like
#echo off
start /w "" "C:\myfile.bat" 2>nul|findstr /i "termin"
if errorlevel 1 goto bypass
:bypass
echo hello by stexup YouTube channel!
timeout /t 5 >nul

Running Bash Scripts and chaining commands in Powershell [duplicate]

In Ubuntu it's quite simple; I can run the application using:
$ NODE_ENV=production node myapp/app.js
However, this doesn't work on Windows. Is there a configuration file where I can set the attribute?
Current versions of Windows use Powershell as the default shell, so use:
$env:NODE_ENV="production"
Per #jsalonen's answer below. If you're in CMD (which is no longer maintained), use
set NODE_ENV=production
This should be executed in the command prompt where you intend to run your Node.js application.
The above line would set the environment variable NODE_ENV for the command prompt where you execute the command.
To set environment variables globally so they persist beyond just the single command prompt, you can find the tool from System in Control Panel (or by typing 'environment' into the search box in the start menu).
I just found a nice Node.js package that can help a lot to define environment variables using a unique syntax, cross platform.
https://www.npmjs.com/package/cross-env
It allow you to write something like this:
cross-env NODE_ENV=production my-command
Which is pretty convenient! No Windows or Unix specific commands any more!
In PowerShell:
$env:NODE_ENV="production"
It would be ideal if you could set parameters on the same line as your call to start Node.js on Windows. Look at the following carefully, and run it exactly as stated:
You have these two options:
At the command line:
set NODE_ENV=production&&npm start
or
set NODE_ENV=production&&node index.js
The trick for it to work on Windows is you need to remove the whitespace before and after the "&&". Configured your package.json file with start_windows (see below) below. Then Run "npm run start_windows" at the command line.
//package.json
"scripts": {
"start": "node index.js"
"start_windows": "set NODE_ENV=production&&node index.js"
}
You can use
npm run env NODE_ENV=production
It is probably the best way to do it, because it's compatible on both Windows and Unix.
From the npm run-script documentation:
The env script is a special built-in command that can be used to list environment variables that will be available to the script at runtime. If an "env" command is defined in your package it will take precedence over the built-in.
I wrote a module win-node-env with which you can run your command just like you would in *nix.
NODE_ENV=production node myapp/app.js
It works by creating a NODE_ENV.cmd that sets the NODE_ENV environment variable and spawns a child process with the rest of the command and its args.
Just install it (globally), and run your npm script commands, it should automatically make them work.
npm install -g win-node-env
My experience using Node.js on Windows 7 64-bit in Visual Studio 2013 is that you need to use
setx NODE_ENV development
from a cmd window. AND you have to restart Visual Studio in order for the new value to be recognized.
The set syntax only lasts for the duration of the cmd window in which it is set.
Simple test in Node.js:
console.log('process.env.NODE_ENV = ' + process.env.NODE_ENV);
It returns 'undefined' when using set, and it will return 'development' if using setx and restarting Visual Studio.
If you are using Visual Studio with NTVS, you can set the environment variables on the project properties page:
As you can see, the Configuration and Platform dropdowns are disabled (I haven't looked too far into why this is), but if you edit your .njsproj file as follows:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<Environment>NODE_ENV=development</Environment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<Environment>NODE_ENV=production</Environment>
</PropertyGroup>
The 'Debug / Release' dropdown will then control how the variable is set before starting Node.js.
Here is the non-command line method:
In Windows 7 or 10, type environment into the start menu search box, and select Edit the system environment variables.
Alternatively, navigate to Control Panel\System and Security\System, and click Advanced system settings
This should open up the System properties dialog box with the Advanced tab selected. At the bottom, you will see an Environment Variables... button. Click this.
The Environment Variables Dialog Box will open.
At the bottom, under System variables, select New...This will open the New System Variable dialog box.
Enter the variable name and value, and click OK.
You will need to close all cmd prompts and restart your server for the new variable to be available to process.env. If it still doesn't show up, restart your machine.
To run your application in PowerShell (since && is disallowed):
($env:NODE_ENV="production") -and (node myapp/app.js)
Note that the text output of what the server's doing is suppressed, and I am not sure if that's fixable. (Expanding on #jsalonen's answer.)
Just to clarify, and for anyone else that may be pulling their hair out...
If you are using git bash on Windows, set node_env=production&& node whatever.js does not seem to work. Instead, use the native cmd. Then, using set node_env=production&& node whatever.jsworks as expected.
My use case:
I develop on Windows because my workflow is a lot faster, but I needed to make sure that my application's development-specific middleware were not firing in the production environment.
if you are using vs code terminal you have to use this command
$env:NODE_ENV="production"
first in powershell type
$env:NODE_ENV="production"
then type
node fileName.js
It will work perfectly displaying all the outputs.
For Windows
set NODE_ENV=development && react-scripts start
For Ubuntu, Linux, macOs
NODE_ENV=development react-scripts start
In case you are using GITBASH terminal
"set NODE_ENV=production"
will not work, what can you do is type
"export NODE_ENV=production"
For multiple environment variables, an .env file is more convenient:
# .env.example, committed to repo
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
# .env, private, .gitignore it
DB_HOST=real-hostname.example.com
DB_USER=real-user-name
DB_PASS=REAL_PASSWORD
It's easy to use with dotenv-safe:
Install with npm install --save-dev dotenv-safe.
Include it in your code (best at the start of the index.js) and directly use it with the process.env command:
require('dotenv').load()
console.log(process.env.DB_HOST)
Don't forget to ignore the .env file in your VCS.
Your program then fails fast if a variable "defined" in .env.example is unset either as an environment variable or in .env.
Restart VS code if the NODE_ENV or any other environment variable is not providing correct value. This should work after restart.
this will not set a variable but it's usefull in many cases. I will not recommend using this for production, but it should be okay if you're playing around with npm.
npm install --production
I used npm script for running a gulp task without "&&"
NODE_ENV=testcases npm run seed-db
set NODE_ENV=production & node server.js
Finally, the best method I could see is as follows.
"set-env-qa": "npm run env APP_ENV=qa",
"start:qa": "npm run set-env-qa && react-native start",
This will make sure we get the correct env setup for the programs. replace react-native-start with whichever next command you want.
In Windows 10 I have used $env:NODE_ENV="Production"
This worked on my vscode terminal. But if your server is listening first need to stop and set your env variable and start the app again. This way it worked for me.
If you are looking to set environment variable and run npm script in the same line then use:
$env:BASE_URL="https://example.com"; npm run __your_npm_script__
It seems that
{
"start_windows": "set NODE_ENV=test"
}
is not working for me. I'm currently trying this on my Windows machine. When I hit:
npm run start_windows
it would execute on the console without errors but when I try to echo
echo %NODE_ENV%
nothing comes out of it, meaning it does not exist and it wasn't set at all...

Run command line app in visible console using NodeJS under Windows

I'm trying to write a command line tool using NodeJS on Windows.
The tool need to execute several other exe's and according to the exit code of each of them progress or stop.
I'm able to run the other exe's using child-process but mostly for debug purposes I'll like to run the other exe inside their own console window. The other exe is doing complex things inside the console window so simply printing out the stdout is not enough.
I've try both exec and spawn and also prefix my other exe with cmd and start but nothing did the trick.

Run a node.js server from Geany

A simple question: Is it possible to configure the Geany IDE so that Node.js servers can be run directly from Geany using the "Run" button?
When inside a JS file, go to Build > Set Build Commands, there should be a section title Execute commands. To use node to execute your files, put: node "%f" in the "Execute" command textbox.
When you change this, any .js files you are editing will run node in the virtual terminal when you hit F5.
If you want to set up an entire project to run the server whenever you're working somewhere within a given directory structure, you'll have to mess with project-level configuration. (something I don't usually bother with) My solution here just gives you a quick way to execute a single JS file without using an external terminal.
UPDATE: node "%f" seems to be legacy, but nodejs "%f" works

Resources