aboutsummaryrefslogtreecommitdiff
path: root/cordova/node_modules/shelljs/src/sed.js
diff options
context:
space:
mode:
Diffstat (limited to 'cordova/node_modules/shelljs/src/sed.js')
-rwxr-xr-xcordova/node_modules/shelljs/src/sed.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/cordova/node_modules/shelljs/src/sed.js b/cordova/node_modules/shelljs/src/sed.js
new file mode 100755
index 0000000..65f7cb4
--- /dev/null
+++ b/cordova/node_modules/shelljs/src/sed.js
@@ -0,0 +1,43 @@
+var common = require('./common');
+var fs = require('fs');
+
+//@
+//@ ### sed([options ,] search_regex, replacement, file)
+//@ Available options:
+//@
+//@ + `-i`: Replace contents of 'file' in-place. _Note that no backups will be created!_
+//@
+//@ Examples:
+//@
+//@ ```javascript
+//@ sed('-i', 'PROGRAM_VERSION', 'v0.1.3', 'source.js');
+//@ sed(/.*DELETE_THIS_LINE.*\n/, '', 'source.js');
+//@ ```
+//@
+//@ Reads an input string from `file` and performs a JavaScript `replace()` on the input
+//@ using the given search regex and replacement string or function. Returns the new string after replacement.
+function _sed(options, regex, replacement, file) {
+ options = common.parseOptions(options, {
+ 'i': 'inplace'
+ });
+
+ if (typeof replacement === 'string' || typeof replacement === 'function')
+ replacement = replacement; // no-op
+ else if (typeof replacement === 'number')
+ replacement = replacement.toString(); // fallback
+ else
+ common.error('invalid replacement string');
+
+ if (!file)
+ common.error('no file given');
+
+ if (!fs.existsSync(file))
+ common.error('no such file or directory: ' + file);
+
+ var result = fs.readFileSync(file, 'utf8').replace(regex, replacement);
+ if (options.inplace)
+ fs.writeFileSync(file, result, 'utf8');
+
+ return common.ShellString(result);
+}
+module.exports = _sed;