From 6739aed69eb733f064271e1402a323307da8a87f Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Fri, 18 Mar 2016 17:05:14 -0700 Subject: [PATCH] refactor(string): modify string protoype, but only for shelljs/global `require('shelljs/global')` extends String.prototype to have the `.to()` and `.toEnd()` methods again. This also adds tests for the global require. --- README.md | 4 ++-- global.js | 7 +++++++ src/to.js | 2 +- src/toEnd.js | 2 +- test/global.js | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test/global.js diff --git a/README.md b/README.md index 0beb0c4..29fcd22 100644 --- a/README.md +++ b/README.md @@ -330,7 +330,7 @@ containing the files if more than one file is given (a new line character is introduced between each file). Wildcard `*` accepted. -### 'string'.to(file) +### ShellString.prototype.to(file) Examples: @@ -343,7 +343,7 @@ ShellStrings (such as those returned by `cat`, `grep`, etc). _Like Unix redirections, `to()` will overwrite any existing file!_ -### 'string'.toEnd(file) +### ShellString.prototype.toEnd(file) Examples: diff --git a/global.js b/global.js index 97f0033..506cc4a 100644 --- a/global.js +++ b/global.js @@ -1,3 +1,10 @@ var shell = require('./shell.js'); +var common = require('./src/common'); for (var cmd in shell) global[cmd] = shell[cmd]; + +var _to = require('./src/to'); +String.prototype.to = common.wrap('to', _to); + +var _toEnd = require('./src/toEnd'); +String.prototype.toEnd = common.wrap('toEnd', _toEnd); diff --git a/src/to.js b/src/to.js index bab93b1..80d15cb 100644 --- a/src/to.js +++ b/src/to.js @@ -3,7 +3,7 @@ var fs = require('fs'); var path = require('path'); //@ -//@ ### 'string'.to(file) +//@ ### ShellString.prototype.to(file) //@ //@ Examples: //@ diff --git a/src/toEnd.js b/src/toEnd.js index 267ad8e..01058ab 100644 --- a/src/toEnd.js +++ b/src/toEnd.js @@ -3,7 +3,7 @@ var fs = require('fs'); var path = require('path'); //@ -//@ ### 'string'.toEnd(file) +//@ ### ShellString.prototype.toEnd(file) //@ //@ Examples: //@ diff --git a/test/global.js b/test/global.js new file mode 100644 index 0000000..e5db654 --- /dev/null +++ b/test/global.js @@ -0,0 +1,41 @@ +/* globals cat, config, cp, env, error, exit, mkdir, rm */ + +require('../global'); + +var assert = require('assert'); +var fs = require('fs'); + +config.silent = true; + +rm('-rf', 'tmp'); +mkdir('tmp'); + +// +// Valids +// + +assert.equal(process.env, env); + +// cat +var result = cat('resources/cat/file1'); +assert.equal(error(), null); +assert.equal(result.code, 0); +assert.equal(result, 'test1\n'); + +// rm +cp('-f', 'resources/file1', 'tmp/file1'); +assert.equal(fs.existsSync('tmp/file1'), true); +result = rm('tmp/file1'); +assert.equal(error(), null); +assert.equal(result.code, 0); +assert.equal(fs.existsSync('tmp/file1'), false); + +// String.prototype is modified for global require +'foo'.to('tmp/testfile.txt'); +assert.equal('foo', cat('tmp/testfile.txt')); +'bar'.toEnd('tmp/testfile.txt'); +assert.equal('foobar', cat('tmp/testfile.txt')); + +exit(123); + +