Skip to content

Instantly share code, notes, and snippets.

@giggio
Forked from mykohsu/npm.cmd
Created February 2, 2016 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save giggio/1ba8168628c38b397bde to your computer and use it in GitHub Desktop.
Save giggio/1ba8168628c38b397bde to your computer and use it in GitHub Desktop.
Alternative npm.cmd for VS201X node tools integration. Caches npm ls -g result until a different npm command is issued.
@ECHO OFF
SETLOCAL
SET "NODE_EXE=%~dp0\node.exe"
IF NOT EXIST "%NODE_EXE%" (
SET "NODE_EXE=node"
)
SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
)
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (
SET "NPM_CLI_JS=%NPM_PREFIX_NPM_CLI_JS%"
)
SET "OUTDIR=%~dp0"
IF "%1 %2"=="ls -g" (
IF NOT EXIST "%OUTDIR%\npm.ls-g.cache" (
"%NODE_EXE%" "%NPM_CLI_JS%" %* >>"%OUTDIR%\npm.ls-g.cache"
)
FOR /F "tokens=2 delims=:" %%F IN ('CALL CHCP') DO (
SET "LS-GCHCP=%%F"
)
CHCP 65001>nul
TYPE "%OUTDIR%\npm.ls-g.cache"
CHCP %LS-GCHCP%>nul
) ELSE (
ERASE "%OUTDIR%\npm.ls-g.cache" 2> nul
"%NODE_EXE%" "%NPM_CLI_JS%" %*
)
@giggio
Copy link
Author

giggio commented Feb 2, 2016

To use, run on powershell: gcm npm
It will output something like this:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     npm.cmd                                            0.0.0.0    C:\Program Files\nodejs\npm.cmd

Update the file listed on Source.

@mykohsu
Copy link

mykohsu commented Feb 2, 2016

@giggio Actually depending on the node version or where it is installed the original npm script is different. I have both versions, but the one under the user roaming profile used PATHEXT and @ for each command.

You'll want SET "OUTDIR=%%F" in the first FOR statement so that it is set to the global node path rather than the input path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment