Skip to content

Why does sed split files into an array, call replace on each line and rejoin? #645

@toddw

Description

@toddw

Node version (or tell us if you're using electron or some other framework):

v6.4.0

ShellJS version (the most recent version/Github branch you see the bug on):

0.7.6

Operating system:

Mac OS 10.12.2

Description of the bug:

Using shelljs.sed over a folder full of files, if one of the files is a binary file like a png… sed will corrupt the images.

More details:
So say a folder has files of the following types (.js, .css, .json, .png, .jpg). I wanted to use sed to replace a string that occurs only in the js, css, json files. I am 100% certain this string would not exist in the binary formats for the png/jpg files so there should be no harm in just running sed over the entire folder. However, because sed attempts to break every file (including the binary files) into an array, split them up on line breaks and merge it back together the binary files come out the other end completely corrupted.

Looking through the history I see that this file splitting/joining happened in a PR that says it is meant to add support for multiple files. I'm not sure that the splitting of individual files should have been part of that or should be part of the code at all. Is this how normal sed works? If so then maybe it is appropriate, if not then I can quickly create a PR to resolve this.

Example ShellJS command to reproduce the error:

var buildFiles = shelljs.ls("build/**").filter(f => f.substr(0, 6) === "build/");
shelljs.sed("-i", /apples/g, "oranges", buildFiles);

Metadata

Metadata

Assignees

Labels

fixBug/defect, or a fix for such a problem

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions