Skip to content

Commit

Permalink
npx: bundle npx with npm itself
Browse files Browse the repository at this point in the history
npx is an npm package runner with a bunch of nice features. While
it is also available on the npm registry as `npx`, it's the sort
of thing that's better off being bundles with npm itself.

Fixes: npm#6053
PR-URL: npm/npm#17685
Credit: @zkat
Reviewed-By: @iarna
  • Loading branch information
zkat authored and iarna committed Jul 11, 2017
1 parent 88aafee commit fb040be
Show file tree
Hide file tree
Showing 779 changed files with 58,510 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ html_docdeps = html/dochead.html \
cli_mandocs = $(shell find doc/cli -name '*.md' \
|sed 's|.md|.1|g' \
|sed 's|doc/cli/|man/man1/|g' ) \
man/man1/npm-README.1
man/man1/npm-README.1 \
man/man1/npx.1

files_mandocs = $(shell find doc/files -name '*.md' \
|sed 's|.md|.5|g' \
Expand Down Expand Up @@ -96,6 +97,9 @@ man/man1/%.1: doc/cli/%.md scripts/doc-build.sh package.json
@[ -d man/man1 ] || mkdir -p man/man1
scripts/doc-build.sh $< $@

man/man1/npx.1: node_modules/libnpx/libnpx.1
cat $< | sed s/libnpx/npx/ > $@

man/man5/npm-json.5: man/man5/package.json.5
cp $< $@

Expand Down
34 changes: 34 additions & 0 deletions bin/npx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh
(set -o igncr) 2>/dev/null && set -o igncr; # cygwin encoding fix

basedir=`dirname "$0"`

case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

NODE_EXE="$basedir/node.exe"
if ! [ -x "$NODE_EXE" ]; then
NODE_EXE=node
fi

NPX_CLI_JS="$basedir/node_modules/npm/bin/npx-cli.js"

case `uname` in
*MINGW*)
NPM_PREFIX=`"$NODE_EXE" "$NPX_CLI_JS" prefix -g`
NPM_PREFIX_NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npx-cli.js"
if [ -f "$NPM_PREFIX_NPX_CLI_JS" ]; then
NPX_CLI_JS="$NPM_PREFIX_NPX_CLI_JS"
fi
;;
*CYGWIN*)
NPM_PREFIX=`"$NODE_EXE" "$NPX_CLI_JS" prefix -g`
NPM_PREFIX_NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npx-cli.js"
if [ -f "$NPM_PREFIX_NPX_CLI_JS" ]; then
NPX_CLI_JS="$NPM_PREFIX_NPX_CLI_JS"
fi
;;
esac

"$NODE_EXE" "$NPX_CLI_JS" "$@"
8 changes: 8 additions & 0 deletions bin/npx-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node

const npx = require('libnpx')
const path = require('path')

const NPM_PATH = path.join(__dirname, 'npm-cli.js')

npx(npx.parseArgs(process.argv, NPM_PATH))
19 changes: 19 additions & 0 deletions bin/npx.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
:: Created by npm, please don't edit manually.
@ECHO OFF

SETLOCAL

SET "NODE_EXE=%~dp0\node.exe"
IF NOT EXIST "%NODE_EXE%" (
SET "NODE_EXE=node"
)

SET "NPX_CLI_JS=%~dp0\node_modules\npm\bin\npx-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPX_CLI_JS%" prefix -g') DO (
SET "NPM_PREFIX_NPX_CLI_JS=%%F\node_modules\npm\bin\npx-cli.js"
)
IF EXIST "%NPM_PREFIX_NPX_CLI_JS%" (
SET "NPX_CLI_JS=%NPM_PREFIX_NPX_CLI_JS%"
)

"%NODE_EXE%" "%NPX_CLI_JS%" %*
Loading

0 comments on commit fb040be

Please sign in to comment.