(args)
| 7 | } |
| 8 | |
| 9 | function rewrite (args) { |
| 10 | /* jshint -W044 */ |
| 11 | // check if splicable is already in the body text |
| 12 | var re = new RegExp(args.splicable.map(function (line) { |
| 13 | return '\s*' + escapeRegExp(line); |
| 14 | }).join('\n')); |
| 15 | |
| 16 | if (re.test(args.haystack)) { |
| 17 | return args.haystack; |
| 18 | } |
| 19 | |
| 20 | var lines = args.haystack.split('\n'); |
| 21 | |
| 22 | var otherwiseLineIndex = 0; |
| 23 | lines.forEach(function (line, i) { |
| 24 | if (line.indexOf(args.needle) !== -1) { |
| 25 | otherwiseLineIndex = i; |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | var spaces = 0; |
| 30 | while (lines[otherwiseLineIndex].charAt(spaces) === ' ') { |
| 31 | spaces += 1; |
| 32 | } |
| 33 | |
| 34 | var spaceStr = ''; |
| 35 | while ((spaces -= 1) >= 0) { |
| 36 | spaceStr += ' '; |
| 37 | } |
| 38 | |
| 39 | lines.splice(otherwiseLineIndex, 0, args.splicable.map(function (line) { |
| 40 | return spaceStr + line; |
| 41 | }).join('\n')); |
| 42 | |
| 43 | return lines.join('\n'); |
| 44 | } |
| 45 | |
| 46 | function rewriteFile (args) { |
| 47 | args.path = args.path || process.cwd(); |
no test coverage detected