0

I have following files, all of which contain minified JavaScipt code. Each File ends in a comment:

Folder structure

|--static
  |--js
    |--1.1001bbaf.chunk.js
    |--runtime~main.229c360f.js
    |--main.57f2973b.chunk.js

1.1001bbaf.chunk.js

(window.webpackJsonp=window.webpackJsonp||[]).push .....
//# sourceMappingURL=1.1001bbaf.chunk.js.map

runtime~main.229c360f.js

!function(e){function r(r){for .....
//# sourceMappingURL=runtime~main.229c360f.js.map

main.57f2973b.chunk.js

(window.webpackJsonp=window.webpackJsonp||[]).push .....
//# sourceMappingURL=main.57f2973b.chunk.js.map

My requirement is to flush the contents of all the files in a single file main.js, such that the content is appended and not overwritten.

I tried the following solution:

  1. cat static/js/*.js >> main.js

    Works well, but it appends the content of second file at the end of first, that ends in a comment. Something like this:

//#sourceMappingURL=1.1001bbaf.chunk.js.map(window.webpackJsonp=window.webpackJsonp||[]).push .....

Now the entire line is a comment in vim text editor and everything beginning from this line is a comment in Atom text editor.

The default behaviour of my Vim text editor is that if I press o in command mode and if the last line was comment //..., the new line begins with //.

I don't know exactly how to deal with the files ending in commented lines. I can think of things like: appended contents of next file beginning on a new line or to delete the last line comment.

Restriction is to use bash only.

Vim code snapshot

Atom code snap

  • bash isn't a text editor, btw; do you mean that you're restricted to ... a command-line solution? – Jeff Schaller Feb 6 at 17:59
  • Yes. I am restricted to a command line solution. Actually I need to provide the path to the script that performs the task in package.json file for a linux fedora server hosted on aws. – HarshvardhanSharma Feb 6 at 18:06
5

The last line of your file doesn't seem to end with a newline. That's why the last line of one file gets combined with the first line of the next file.

You could try to append a newline after every file with this

for file in static/js/*.js
do
    cat "$file"
    echo
done >> main.js

The script doesn't check if a file ends with a newline or not. If a file ends with a newline it will be followed by an empty line.

3

This may sound a bit hackish, but you can leverage the behavior of sed, which adds a <newline> character between files only if a file does not end with one:

sed ';' static/js/*.js >main.js

It will work with GNU sed; I'm not sure about other implementations.

Example:

$ printf '%s' line1 >file1
$ printf '%s\n' line2 >file2
$ printf '%s' line3 >file3

$ cat file*
line1line2
line3

$ sed ';' file*
line1
line2
line3

It happens because, quoting GNU sed's documentation:

Actually, if sed prints a line without the terminating newline, it will nevertheless print the missing newline as soon as more text is sent to the same output stream, which gives the “least expected surprise” even though it does not make commands like ‘sed -n p’ exactly identical to cat.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.