Sha256: 68c40f1c48cb48618156c86f048acb2b7f4c0b19e1a3874f75e1806d94ac5f73

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

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;

Version data entries

7 entries across 7 versions & 4 rubygems

Version Path
xcodebuild-helper-1.2.5 externals/ios-sim-master/node_modules/shelljs/src/sed.js
xcodebuild-helper-1.2.3 externals/ios-sim-master/node_modules/shelljs/src/sed.js
exercism-analysis-0.1.1 vendor/javascript/node_modules/jshint/node_modules/shelljs/src/sed.js
eslint_node_modules-1.6.0.1 vendor/node_modules/eslint/node_modules/shelljs/src/sed.js
eslint_node_modules-1.6.0 vendor/node_modules/eslint/node_modules/shelljs/src/sed.js
trans-0.5.10 template/node_modules/shelljs/src/sed.js
trans-0.5.9 template/node_modules/shelljs/src/sed.js