Skip to content

"bundle exec" style exec command #2460

Closed
wants to merge 6 commits into from
View
15 doc/cli/exec.md
@@ -0,0 +1,15 @@
+npm-exec(1) -- Execute command using local package bin
+====================================
+
+## SYNOPSIS
+
+ npm exec
@isaacs
npm member
isaacs added a note Nov 6, 2012

Synopsis should match usage string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+## DESCRIPTION
+
+Adds the local package bin path (eg. node_modules/.bin) before executing the
+command.
+
+## SEE ALSO
+
+* npm-bin(1)
View
38 lib/exec.js
@@ -0,0 +1,38 @@
+module.exports = exec
+
+var npm = require("./npm.js")
+ , output = require("./utils/output.js")
@isaacs
npm member
isaacs added a note Nov 6, 2012

utils/output.js has been removed. Either use console.log for "normal" output, or require("npmlog") for log output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ , cp = require("child_process")
+ , os = require("os")
+
+exec.usage = "npm exec <command>\n(execute command using local package bin)"
+
+exec.completion = require("./utils/completion/local-bin.js")
+
+function exec(args, cb) {
+ if (args.length == 0) return cb("Usage:\n"+exec.usage)
+ var b = npm.bin, cmd, o = ""
+ var isWin = process.platform.match(/win/)
+
+ // Create an OS specific call
+ if (isWin)
+ cmd = "SET OLDPATH=%PATH% & SET PATH=" + b + ";%PATH% & " +
+ args.join(" ") + " & SET PATH=%OLDPATH%"
+ else
+ cmd = "PATH=" + b + ":$PATH " + args.join(" ")
+
+ p = cp.exec(cmd, function(error, stdout, stderr) {
+ // Return all captured output
+ cb(null, o)
+ })
+ p.stdout.on('data', function(data) {
+ // Capture stdout output and output to screen
+ console.log(data.toString().replace(/\n$/, ''))
+ o += data.toString()
+ })
+ p.stderr.on('data', function(data) {
+ // Capture stderr output and output to screen
+ console.error(data.toString().replace(/\n$/, ''))
+ o += data.toString()
+ })
+}
View
1 lib/npm.js
@@ -156,6 +156,7 @@ var commandCache = {}
, "restart"
, "run-script"
, "completion"
+ , "exec"
]
, plumbing = [ "build"
, "unbuild"
View
19 lib/utils/completion/local-bin.js
@@ -0,0 +1,19 @@
+module.exports = localBin
+
+var npm = require("../../npm.js")
+ , find = require("../find.js")
+ , path = require("path")
+
+function localBin (filter, cb) {
+ var b = npm.bin;
+ path.exists(b, function(exists) {
+ // Return if there is no local bin dir
+ if (!exists) return cb(null, [])
+ // Search local bin for files
+ find(b, null, 1, function(er, files) {
+ return cb(null, (files || []).map(function(f) {
+ return f.replace(b + '/', '')
+ }))
+ })
+ })
+}
Something went wrong with that request. Please try again.