<commit_before>/// gulp-run
/// ==================================================
/// Pipe to shell commands in gulp.

'use strict';

var child_process = require('child_process');
var pathlib = require('path');
var stream = require('stream');
var util = require('util');

var _ = require('lodash');
var color = require('cli-color');
var Vinyl = require('vinyl');

var Logger = require('./lib/logger');


/// `var cmd = run(command, [options])`
/// --------------------------------------------------
/// Gets a through stream for a shell command to which you can pipe vinyl files. For each file
/// piped, a new process is spawned, the file is read into the processes's stdin, and a file
/// containing the processes's stdout is pushed.
///
/// Additionally, `./node_modules/.bin` is prepended to the PATH for the child process, so you have
/// access to all the binaries provided by your module's dependencies.
///
/// ### Arguments
/// 1. `command` *(String)*: The command to run. It can be a [template] interpolating the vinyl file
///     as the variable `file`.
/// 2. `[options]` *(Object)*:
///     - `env` *(Object)*: The environmental variables for the child process. Defaults to
///         `process.env`. The path `node_modules/.bin` is automatically prepended to the PATH.
///     - `cwd` *(String)*: The initial working directory for the child process. Defaults to
///         `process.cwd()`.
///     - `silent` *(Boolean)*: If true, do not print the command's output. This is the same as
///         setting verbosity to 1. Defaults to `false`.
///     - `verbosity` *(Number)*: Sets the verbosity level. Defaults to `2`.
///         - `0` never outputs anything.
///         - `1` outputs basic logs.
///         - `2` outputs basic logs and the stdout of the child process.
///
/// [template]: http://lodash.com/docs#template
///
/// ### Returns
/// *(stream.Transform in Object Mode)*: The through stream you so desire.
///
/// ### Example
/// ```javascript
/// gulp.task('even-lines', function () {
///     gulp.src('path/to/input/*')            // Get input files.
///         .pipe(run('awk "NR % 2 == 0"'))    // Use awk to extract the even lines.
///         .pipe(gulp.dest('path/to/output')) // Profit.
/// })
/// ```

var run = module.exports = function (command, opts) {
	var command_stream = new stream.Transform({objectMode: true}); // The stream of vinyl files.

	// Options
	opts = _.defaults(opts||{}, {
		cwd: process.cwd(),
		env: process.env,
		silent: false,
		verbosity: (opts && opts.silent) ? 1 : 2
	});

	// If `opts.silent` and `opts.verbosity` disagree, verbosity wins
	opts.silent = (opts && opts.verbosity < 2) ? true : false;

	// Compile the command template.
	var command_template = _.template(command);


	// exec(command, [input], [callback])
	// --------------------------------------------------
	// TODO: Document

	var exec = function (command, input, callback) {
		var child; // The child process.
		var env; // The environmental variables for the child.
		var out_stream; // The contents of the returned vinyl file.

		// Parse arguments.
		if (typeof arguments[1] === 'function') {
			input = null;
			callback = arguments[1];
		}

		// Setup logging.
		var logger = new Logger(opts.verbosity);
		logger.stream.pipe(process.stdout);

		// Log start message.
		var start_message = '$ ' + color.cyan(command);
		if (input && input.relative) {
			start_message += ' < ' + color.magenta(input.relative);
		}
		if (opts.silent) {
			start_message += color.blackBright(' # Silenced');
		}
		logger.log(1, start_message);

		// Setup environment of child process.
		opts.env.PATH = pathlib.join('node_modules', '.bin') + ':' + opts.env.PATH;

		// Spawn the process.
		child = child_process.spawn('sh', ['-c', command], {env:opts.env, cwd:opts.cwd});

		// When the child process is done.
		child.on('close', function (code) {
			var err; // Only defined if an error occured

			// Handle errors<commit_msg>Fix bin path location when setting a different cwd - Fixes #2<commit_after>/// gulp-run
/// ==================================================
/// Pipe to shell commands in gulp.

'use strict';

var child_process = require('child_process');
var pathlib = require('path');
var stream = require('stream');
var util = require('util');

var _ = require('lodash');
var color = require('cli-color');
var Vinyl = require('vinyl');

var Logger = require('./lib/logger');


/// `var cmd = run(command, [options])`
/// --------------------------------------------------
/// Gets a through stream for a shell command to which you can pipe vinyl files. For each file
/// piped, a new process is spawned, the file is read into the processes's stdin, and a file
/// containing the processes's stdout is pushed.
///
/// Additionally, `./node_modules/.bin` is prepended to the PATH for the child process, so you have
/// access to all the binaries provided by your module's dependencies.
///
/// ### Arguments
/// 1. `command` *(String)*: The command to run. It can be a [template] interpolating the vinyl file
///     as the variable `file`.
/// 2. `[options]` *(Object)*:
///     - `env` *(Object)*: The environmental variables for the child process. Defaults to
///         `process.env`. The path `node_modules/.bin` is automatically prepended to the PATH.
///     - `cwd` *(String)*: The initial working directory for the child process. Defaults to
///         `process.cwd()`.
///     - `silent` *(Boolean)*: If true, do not print the command's output. This is the same as
///         setting verbosity to 1. Defaults to `false`.
///     - `verbosity` *(Number)*: Sets the verbosity level. Defaults to `2`.
///         - `0` never outputs anything.
///         - `1` outputs basic logs.
///         - `2` outputs basic logs and the stdout of the child process.
///
/// [template]: http://lodash.com/docs#template
///
/// ### Returns
/// *(stream.Transform in Object Mode)*: The through stream you so desire.
///
/// ### Example
/// ```javascript
/// gulp.task('even-lines', function () {
///     gulp.src('path/to/input/*')            // Get input files.
///         .pipe(run('awk "NR % 2 == 0"'))    // Use awk to extract the even lines.
///         .pipe(gulp.dest('path/to/output')) // Profit.
/// })
/// ```

var run = module.exports = function (command, opts) {
	var command_stream = new stream.Transform({objectMode: true}); // The stream of vinyl files.

	// Options
	opts = _.defaults(opts||{}, {
		cwd: process.cwd(),
		env: process.env,
		silent: false,
		verbosity: (opts && opts.silent) ? 1 : 2
	});

	// If `opts.silent` and `opts.verbosity` disagree, verbosity wins
	opts.silent = (opts && opts.verbosity < 2) ? true : false;

	// Compile the command template.
	var command_template = _.template(command);


	// exec(command, [input], [callback])
	// --------------------------------------------------
	// TODO: Document

	var exec = function (command, input, callback) {
		var child; // The child process.
		var env; // The environmental variables for the child.
		var out_stream; // The contents of the returned vinyl file.

		// Parse arguments.
		if (typeof arguments[1] === 'function') {
			input = null;
			callback = arguments[1];
		}

		// Setup logging.
		var logger = new Logger(opts.verbosity);
		logger.stream.pipe(process.stdout);

		// Log start message.
		var start_message = '$ ' + color.cyan(command);
		if (input && input.relative) {
			start_message += ' < ' + color.magenta(input.relative);
		}
		if (opts.silent) {
			start_message += color.blackBright(' # Silenced');
		}
		logger.log(1, start_message);

		// Setup environment of child process.
		opts.env.PATH = pathlib.join(__dirname, '../../node_modules', '.bin') + ':' + opts.env.PATH;

		// Spawn the process.
		child = child_process.spawn('sh', ['-c', command], {env:opts.env, cwd:opts.cwd});

		// When the child process is done.
		child.on('close', function (code) {
			var err; // Only defined if an error occured

			// Handle errors
{'fraction_non_alphanumeric': 0.19944841419079853, 'fraction_numerical': 0.003886172746646609, 'mean_word_length': 2.580789946140036, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2581297', 'n_tokens_mistral': 2468, 'n_tokens_neox': 2322, 'n_words': 808}