UNPKG

3.42 kBJavaScriptView Raw
1/*
2 * grunt
3 * https://github.com/cowboy/grunt
4 *
5 * Copyright (c) 2012 "Cowboy" Ben Alman
6 * Licensed under the MIT license.
7 * http://benalman.com/about/license/
8 */
9
10// Nodejs libs.
11var spawn = require('child_process').spawn;
12
13// The module to be exported.
14var utils = module.exports = {};
15
16// A few internal utilites, exposed.
17utils.task = require('../util/task');
18utils.namespace = require('../util/namespace');
19
20// External libs.
21utils.hooker = require('hooker');
22utils.async = require('async');
23var _ = utils._ = require('underscore');
24
25// Mixin Underscore.string methods.
26_.str = require('underscore.string');
27_.mixin(_.str.exports());
28
29// The line feed char for the current system.
30utils.linefeed = process.platform === 'win32' ? '\r\n' : '\n';
31
32// Normalize linefeeds in a string.
33utils.normalizelf = function(str) {
34 return str.replace(/\r\n|\n/g, utils.linefeed);
35};
36
37// What "kind" is a value?
38// I really need to rework https://github.com/cowboy/javascript-getclass
39var kindsOf = {};
40'Number String Boolean Function RegExp Array Date Error'.split(' ').forEach(function(k) {
41 kindsOf['[object ' + k + ']'] = k.toLowerCase();
42});
43utils.kindOf = function(value) {
44 // Null or undefined.
45 if (value == null) { return String(value); }
46 // Everything else.
47 return kindsOf[kindsOf.toString.call(value)] || 'object';
48};
49
50// Coerce something to an Array.
51utils.toArray = Function.call.bind(Array.prototype.slice);
52
53// Return the string `str` repeated `n` times.
54utils.repeat = function(n, str) {
55 return new Array(n + 1).join(str || ' ');
56};
57
58// Given str of "a/b", If n is 1, return "a" otherwise "b".
59utils.pluralize = function(n, str, separator) {
60 var parts = str.split(separator || '/');
61 return n === 1 ? (parts[0] || '') : (parts[1] || '');
62};
63
64// Recurse through objects and arrays, executing fn for each non-object.
65utils.recurse = function recurse(value, fn, fnContinue) {
66 var obj;
67 if (fnContinue && fnContinue(value) === false) {
68 // Skip value if necessary.
69 return value;
70 } else if (utils.kindOf(value) === 'array') {
71 // If value is an array, recurse.
72 return value.map(function(value) {
73 return recurse(value, fn, fnContinue);
74 });
75 } else if (utils.kindOf(value) === 'object') {
76 // If value is an object, recurse.
77 obj = {};
78 Object.keys(value).forEach(function(key) {
79 obj[key] = recurse(value[key], fn, fnContinue);
80 });
81 return obj;
82 } else {
83 // Otherwise pass value into fn and return.
84 return fn(value);
85 }
86};
87
88// Spawn a child process, capturing its stdout and stderr.
89utils.spawn = function(opts, done) {
90 var child = spawn(opts.cmd, opts.args, opts.opts);
91 var stdout = '';
92 var stderr = '';
93 child.stdout.on('data', function(buf) { stdout += buf; });
94 child.stderr.on('data', function(buf) { stderr += buf; });
95 child.on('exit', function(code) {
96 // Remove trailing whitespace (newline)
97 stdout = _.rtrim(stdout);
98 stderr = _.rtrim(stderr);
99 // To keep JSHint from complaining about using new String().
100 var MyString = String;
101 // Create a new string... with properties.
102 var result = new MyString(code === 0 ? stdout : 'fallback' in opts ? opts.fallback : stderr);
103 result.stdout = stdout;
104 result.stderr = stderr;
105 result.code = code;
106 // On error, pass result object as error object.
107 done(code === 0 || 'fallback' in opts ? null: result, result, code);
108 });
109 return child;
110};