I have the following files in my folder :
Now most of these are JS files and if you can see there are a bunch of files that start with a .
, and are slightly faded , these files also have a .js
extension. If i open any of these files in a text editor i see the following:
As you can see the message says the file cannot be open as it may be very large or a binary file (i doubt it's because its very large file because when i opened this file in the browser , all i see is a bunch of special characters, don't seem like a JS file at all .. see below).
Below is what i see when i open the JS file in a browser:
Now in my express app , i have a few lines of code that basically beautifies a file if it has a .js extension , the code looks like so:
data = fs.readFileSync( path_Url , 'utf8', ),
js_beautified_content = beautify( data, { indent_size: 2, space_in_empty_paren: true });
console.log( 'PATH URL IS ' + path_Url );
fs.writeFileSync( path_Url , js_beautified_content , 'utf8');
data = fs.readFileSync( path_Url , 'utf-8' );
Now ofcourse the above code is inside a forEach loop and its all Syncronious , but when the for loop runs into one of these binary files , my app throws an error, like so:
{ Error: EPERM: operation not permitted, open 'C:\Users\gautam.fonseca\Desktop\isales_to_veeva_V1\VEEVA_CONVERTED\converted_to_ veeva\demo_EZE_MIX_One_Step_Target01\assets\js._jquery.mobile-1.2.0.2.js' at Object.fs.openSync (fs.js:646:18) at Object.fs.writeFileSync (fs.js:1291:33) at replaceContaint (C:\Users\gautam.fonseca\Desktop\isales_to_veeva_V1\isales_to_veeva_V1\routes\index.js:305:20) at C:\Users\gautam.fonseca\Desktop\isales_to_veeva_V1\isales_to_veeva_V1\routes\index.js:273:13 at Array.forEach () at isalesToVeevaSanitization (C:\Users\gautam.fonseca\Desktop\isales_to_veeva_V1\isales_to_veeva_V1\routes\index.js:257:11)
at C:\Users\gautam.fonseca\Desktop\isales_to_veeva_V1\isales_to_veeva_V1\routes\index.js:271:13
How can i circumvent this ? What do i do when i run into one of these binary files and identify them ?
P.S. currently i am considering wrapping my code inside a try catch block , but i would prefer if i could get a solution that identifies such files that can't be read or rather binary files that can't be read. I am not sure if try catch is a better solution though , so i am open to an answer that accomodates that solution too.