Skip to content

Commit

Permalink
cli: accept /dev/stdin as input file on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ebraminio committed May 20, 2017
1 parent b5ae22d commit e0a4a1e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ Module._findPath = function(request, paths, isMain) {
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryPackage(basePath, exts, isMain);
} else if (rc === 2) {
filename = basePath;
}

if (!filename) {
Expand Down
12 changes: 10 additions & 2 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
}

// Used to speed up module loading. Returns 0 if the path refers to
// a file, 1 when it's a directory or < 0 on error (usually -ENOENT.)
// a file, 1 when it's a directory, 2 when it's anything else, or < 0 on
// error (usually -ENOENT.)
// The speedup comes from not creating thousands of Stat and Error objects.
static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand All @@ -558,7 +559,14 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr);
if (rc == 0) {
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
rc = !!(s->st_mode & S_IFDIR);

if (s->st_mode & S_IFREG) {
rc = 0;
} else if (s->st_mode & S_IFDIR) {
rc = 1;
} else {
rc = 2;
}
}
uv_fs_req_cleanup(&req);

Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-cli-arg-devstdin-posix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common');

if (common.isWindows) {
common.skip('This test does not apply to Windows.');
return;
}

const assert = require('assert');

const expected = '--option-to-be-seen-on-child';

const { spawn } = require('child_process');
const child = spawn(process.execPath, ['/dev/stdin', expected], { stdio: 'pipe' });

child.stdin.end('console.log(process.argv[2])');

let actual = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => actual += chunk);
child.stdout.on('end', common.mustCall(() => {
assert.strictEqual(actual.trim(), expected);
}));

0 comments on commit e0a4a1e

Please sign in to comment.