This commit is contained in:
Artur Adib 2012-03-01 21:36:16 -05:00
parent 2c948d7b8e
commit 1739ee3bb3
16 changed files with 101 additions and 53 deletions

View File

@ -364,6 +364,10 @@ exports.mv = wrap('mv', _mv);
//@
//@ + `p`: full path (will create intermediate dirs if necessary)
//@
//@ Examples:
//@
//@ + `mkdir('-p', '/tmp/a/b/c/d')`
//@
//@ Creates directories.
function _mkdir(options, dirs) {
options = parseOptions(options, {
@ -398,6 +402,11 @@ exports.mkdir = wrap('mkdir', _mkdir);
//@
//@ #### cat(file [, file ...]')
//@
//@ Examples:
//@
//@ `var str = cat('file*.txt')`
//@
//@ Returns a string containing the given file, or a concatenated string
//@ containing the files if more than one file is given (a new line character is
//@ introduced between each file). Wildcard `*` accepted.
@ -441,7 +450,7 @@ function _to(options, file) {
String.prototype.to = wrap('to', _to);
//@
//@ #### sed(search_regex, 'replace_str', 'file' [, options])
//@ #### sed([options ,] search_regex, 'replace_str', 'file')
//@ Available options:
//@
//@ + `inplace`: (Default is `false`) If `true` will replace contents of 'file' with
@ -449,7 +458,11 @@ String.prototype.to = wrap('to', _to);
//@
//@ Reads an input string from `file` and performs a JavaScript `replace()` on the input
//@ using the given search regex and replacement string. Returns the modified string.
function _sed(options, regex, replacement, file, options) {
function _sed(options, regex, replacement, file) {
options = parseOptions(options, {
'i': 'inplace'
});
if (typeof replacement === 'string')
replacement = replacement; // no-op
else if (typeof replacement === 'number')
@ -464,7 +477,7 @@ function _sed(options, regex, replacement, file, options) {
error('no such file or directory: ' + file);
var result = fs.readFileSync(file, 'utf8').replace(regex, replacement);
if (options && options.inplace)
if (options.inplace)
result.to(file);
return result;
@ -772,7 +785,7 @@ function wrap(cmd, fn) {
try {
var args = [].slice.call(arguments, 0);
if (args.length === 0 || args[0][0] !== '-')
if (args.length === 0 || typeof args[0] !== 'string' || args[0][0] !== '-')
args.unshift(''); // only add dummy option if '-option' not already present
retValue = fn.apply(this, args);
} catch (e) {

View File

@ -16,6 +16,9 @@ function numLines(str) {
// save current dir
var cur = shell.pwd();
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//

View File

@ -16,6 +16,9 @@ function numLines(str) {
// save current dir
var cur = shell.pwd();
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//

View File

@ -13,6 +13,9 @@ function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//

View File

@ -4,6 +4,9 @@ var assert = require('assert');
silent();
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Valids
//

View File

@ -16,6 +16,9 @@ function numLines(str) {
// save current dir
var cur = pwd();
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//

View File

@ -10,6 +10,9 @@ function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//

View File

@ -13,6 +13,9 @@ function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//

View File

@ -13,6 +13,9 @@ function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//

View File

@ -1,4 +1,4 @@
require('../maker');
var shell = require('..');
var assert = require('assert'),
path = require('path'),
@ -7,27 +7,30 @@ var assert = require('assert'),
// Node shims for < v0.7
fs.existsSync = fs.existsSync || path.existsSync;
silent();
shell.silent();
function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//
mkdir();
assert.ok(error());
shell.mkdir();
assert.ok(shell.error());
var mtime = fs.statSync('tmp').mtime.toString();
mkdir('tmp'); // dir already exists
assert.ok(error());
shell.mkdir('tmp'); // dir already exists
assert.ok(shell.error());
assert.equal(fs.statSync('tmp').mtime.toString(), mtime); // didn't mess with dir
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
mkdir('/asdfasdf/asdfasdf'); // root path does not exist
assert.ok(error());
shell.mkdir('/asdfasdf/asdfasdf'); // root path does not exist
assert.ok(shell.error());
assert.equal(fs.existsSync('/asdfasdf'), false);
//
@ -35,35 +38,28 @@ assert.equal(fs.existsSync('/asdfasdf'), false);
//
assert.equal(fs.existsSync('tmp/t1'), false);
mkdir('tmp/t1'); // simple dir
assert.equal(error(), null);
shell.mkdir('tmp/t1'); // simple dir
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/t1'), true);
assert.equal(fs.existsSync('tmp/t2'), false);
assert.equal(fs.existsSync('tmp/t3'), false);
mkdir('tmp/t2 tmp/t3'); // multiple dirs
assert.equal(error(), null);
shell.mkdir('tmp/t2', 'tmp/t3'); // multiple dirs
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/t2'), true);
assert.equal(fs.existsSync('tmp/t3'), true);
assert.equal(fs.existsSync('tmp/t1'), true);
assert.equal(fs.existsSync('tmp/t4'), false);
mkdir('tmp/t1 tmp/t4'); // one dir exists, one doesn't
assert.equal(numLines(error()), 1);
shell.mkdir('tmp/t1', 'tmp/t4'); // one dir exists, one doesn't
assert.equal(numLines(shell.error()), 1);
assert.equal(fs.existsSync('tmp/t1'), true);
assert.equal(fs.existsSync('tmp/t4'), true);
assert.equal(fs.existsSync('tmp/a'), false);
mkdir('-p tmp/a/b/c');
assert.equal(error(), null);
shell.mkdir('-p', 'tmp/a/b/c');
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/a/b/c'), true);
rm('-Rf tmp/a'); // revert
shell.rm('-Rf', 'tmp/a'); // revert
// comma-syntax
assert.equal(fs.existsSync('tmp/a'), false);
mkdir('-p', 'tmp/a/b/c tmp/d/e/f');
assert.equal(error(), null);
assert.equal(fs.existsSync('tmp/a/b/c'), true);
assert.equal(fs.existsSync('tmp/d/e/f'), true);
exit(123);
shell.exit(123);

View File

@ -13,11 +13,11 @@ function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
// Prepare tmp/
shell.rm('-rf', 'tmp/*');
assert.equal(shell.error(), null);
shell.cp('resources/*', 'tmp');
assert.equal(shell.error(), null);
//
// Invalids

View File

@ -9,6 +9,9 @@ function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Valids
//

View File

@ -9,6 +9,9 @@ fs.existsSync = fs.existsSync || path.existsSync;
silent();
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//

View File

@ -1,4 +1,4 @@
require('../maker');
var shell = require('..');
var assert = require('assert'),
path = require('path'),
@ -7,49 +7,52 @@ var assert = require('assert'),
// Node shims for < v0.7
fs.existsSync = fs.existsSync || path.existsSync;
silent();
shell.silent();
function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//
sed();
assert.ok(error());
shell.sed();
assert.ok(shell.error());
sed(/asdf/g); // too few args
assert.ok(error());
shell.sed(/asdf/g); // too few args
assert.ok(shell.error());
sed(/asdf/g, 'nada'); // too few args
assert.ok(error());
shell.sed(/asdf/g, 'nada'); // too few args
assert.ok(shell.error());
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
sed(/asdf/g, 'nada', '/asdfasdf'); // no such file
assert.ok(error());
shell.sed(/asdf/g, 'nada', '/asdfasdf'); // no such file
assert.ok(shell.error());
//
// Valids
//
cp('-f resources/file1 tmp/file1')
var result = sed('test1', 'hello', 'tmp/file1'); // search string
assert.equal(error(), null);
shell.cp('-f', 'resources/file1', 'tmp/file1')
var result = shell.sed('test1', 'hello', 'tmp/file1'); // search string
assert.equal(shell.error(), null);
assert.equal(result, 'hello');
var result = sed(/test1/, 'hello', 'tmp/file1'); // search regex
assert.equal(error(), null);
var result = shell.sed(/test1/, 'hello', 'tmp/file1'); // search regex
assert.equal(shell.error(), null);
assert.equal(result, 'hello');
var result = sed(/test1/, 1234, 'tmp/file1'); // numeric replacement
assert.equal(error(), null);
var result = shell.sed(/test1/, 1234, 'tmp/file1'); // numeric replacement
assert.equal(shell.error(), null);
assert.equal(result, '1234');
var result = sed(/test1/, 'hello', 'tmp/file1', {inplace:true});
assert.equal(error(), null);
var result = shell.sed('-i', /test1/, 'hello', 'tmp/file1');
assert.equal(shell.error(), null);
assert.equal(result, 'hello');
assert.equal(cat('tmp/file1'), 'hello');
assert.equal(shell.cat('tmp/file1'), 'hello');
exit(123);
shell.exit(123);

View File

@ -13,6 +13,9 @@ function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Valids
//

View File

@ -13,6 +13,9 @@ function numLines(str) {
return typeof str === 'string' ? str.match(/\n/g).length : 0;
}
shell.rm('-rf', 'tmp');
shell.mkdir('tmp')
//
// Invalids
//