diff --git a/shell.js b/shell.js index a3ef68c..4ba438d 100644 --- a/shell.js +++ b/shell.js @@ -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) { diff --git a/test/cat.js b/test/cat.js index 7f88bb0..8842de6 100644 --- a/test/cat.js +++ b/test/cat.js @@ -16,6 +16,9 @@ function numLines(str) { // save current dir var cur = shell.pwd(); +shell.rm('-rf', 'tmp'); +shell.mkdir('tmp') + // // Invalids // diff --git a/test/cd.js b/test/cd.js index f1e240c..ecb7cf0 100644 --- a/test/cd.js +++ b/test/cd.js @@ -16,6 +16,9 @@ function numLines(str) { // save current dir var cur = shell.pwd(); +shell.rm('-rf', 'tmp'); +shell.mkdir('tmp') + // // Invalids // diff --git a/test/cp.js b/test/cp.js index 6228074..6628dee 100644 --- a/test/cp.js +++ b/test/cp.js @@ -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 // diff --git a/test/env.js b/test/env.js index 278272b..1859dee 100644 --- a/test/env.js +++ b/test/env.js @@ -4,6 +4,9 @@ var assert = require('assert'); silent(); +shell.rm('-rf', 'tmp'); +shell.mkdir('tmp') + // // Valids // diff --git a/test/exists.js b/test/exists.js index c7ead3d..2ac475a 100644 --- a/test/exists.js +++ b/test/exists.js @@ -16,6 +16,9 @@ function numLines(str) { // save current dir var cur = pwd(); +shell.rm('-rf', 'tmp'); +shell.mkdir('tmp') + // // Invalids // diff --git a/test/external.js b/test/external.js index 74a55c3..1fc20a2 100644 --- a/test/external.js +++ b/test/external.js @@ -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 // diff --git a/test/grep.js b/test/grep.js index 1cb8851..76d3265 100644 --- a/test/grep.js +++ b/test/grep.js @@ -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 // diff --git a/test/ls.js b/test/ls.js index b1f1bcd..b490146 100644 --- a/test/ls.js +++ b/test/ls.js @@ -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 // diff --git a/test/mkdir.js b/test/mkdir.js index 8bf7f7d..338cff1 100644 --- a/test/mkdir.js +++ b/test/mkdir.js @@ -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); diff --git a/test/mv.js b/test/mv.js index 3cb9051..cd8db00 100644 --- a/test/mv.js +++ b/test/mv.js @@ -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 diff --git a/test/pwd.js b/test/pwd.js index 34b2f6e..26c704a 100644 --- a/test/pwd.js +++ b/test/pwd.js @@ -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 // diff --git a/test/rm.js b/test/rm.js index cbde770..a023ac7 100644 --- a/test/rm.js +++ b/test/rm.js @@ -9,6 +9,9 @@ fs.existsSync = fs.existsSync || path.existsSync; silent(); +shell.rm('-rf', 'tmp'); +shell.mkdir('tmp') + // // Invalids // diff --git a/test/sed.js b/test/sed.js index e433ac2..4b78ea8 100644 --- a/test/sed.js +++ b/test/sed.js @@ -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); diff --git a/test/tempdir.js b/test/tempdir.js index 65ea822..d8c3d7e 100644 --- a/test/tempdir.js +++ b/test/tempdir.js @@ -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 // diff --git a/test/to.js b/test/to.js index def09a4..5257b6c 100644 --- a/test/to.js +++ b/test/to.js @@ -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 //