MCPcopy
hub / github.com/shelljs/shelljs / wrap

Function wrap

src/common.js:389–488  ·  view source on GitHub ↗
(cmd, fn, options)

Source from the content-addressed store, hash-verified

387// Common wrapper for all Unix-like commands that performs glob expansion,
388// command-logging, and other nice things
389function wrap(cmd, fn, options) {
390 options = options || {};
391 return function () {
392 var retValue = null;
393
394 state.currentCmd = cmd;
395 state.error = null;
396 state.errorCode = 0;
397
398 try {
399 var args = [].slice.call(arguments, 0);
400
401 // Log the command to stderr, if appropriate
402 if (config.verbose) {
403 console.error.apply(console, [cmd].concat(args));
404 }
405
406 // If this is coming from a pipe, let's set the pipedValue (otherwise, set
407 // it to the empty string)
408 state.pipedValue = (this && typeof this.stdout === 'string') ? this.stdout : '';
409
410 if (options.unix === false) { // this branch is for exec()
411 retValue = fn.apply(this, args);
412 } else { // and this branch is for everything else
413 if (isObject(args[0]) && args[0].constructor.name === 'Object') {
414 // a no-op, allowing the syntax `touch({'-r': file}, ...)`
415 } else if (args.length === 0 || typeof args[0] !== 'string' || args[0].length <= 1 || args[0][0] !== '-') {
416 args.unshift(''); // only add dummy option if '-option' not already present
417 }
418
419 // flatten out arrays that are arguments, to make the syntax:
420 // `cp([file1, file2, file3], dest);`
421 // equivalent to:
422 // `cp(file1, file2, file3, dest);`
423 args = args.reduce(function (accum, cur) {
424 if (Array.isArray(cur)) {
425 return accum.concat(cur);
426 }
427 accum.push(cur);
428 return accum;
429 }, []);
430
431 // Convert ShellStrings (basically just String objects) to regular strings
432 args = args.map(function (arg) {
433 if (isObject(arg) && arg.constructor.name === 'String') {
434 return arg.toString();
435 }
436 return arg;
437 });
438
439 // Expand the '~' if appropriate
440 var homeDir = os.homedir();
441 args = args.map(function (arg) {
442 if (typeof arg === 'string' && arg.slice(0, 2) === '~/' || arg === '~') {
443 return arg.replace(/^~/, homeDir);
444 }
445 return arg;
446 });

Callers 1

_registerFunction · 0.85

Calls 3

isObjectFunction · 0.85
expandFunction · 0.85
parseOptionsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…