I'm using this code to hash a single file:
const fs = require('fs');
let result = digest(fs.readFileSync('/users/ispedro/desktop/Test.PDF'));
console.log('Huella Digital MD5: ' + result)
I am trying to achieve the same but of all the files included in a folder, so that the output would be something like this:
The file (name of the file) generates the following MD5 hash: 175GKG3K6UZ6GAEE64BFKUN0MR
I have tried with various codes but have not succeeded ... any ideas please?
Update:
What I want to do is that the script reads the contents of a folder on the desktop without having to compress it in zip, so that I can remove or put files and when executing the script it returns the MD5 hash of those that are currently inside. I have tried to copy part of the code from this link npmjs.com/package/folder-hash which I think does exactly that but I can't get the script to work ...
Thank you very much to all.
Related
I want to have a code where I can unzip a zipped file but even after trying so many things, I am not able to do this.
I want this in my front end because I just want to unzip and process the file inside to do some computation with the data inside.
I tried
https://github.com/nika-begiashvili/libarchivejs
I followed this readme but still i got this error,
https://github.com/nika-begiashvili/libarchivejs#readme
but when I try to unzip the file I get this error
Looking at it, it looks like this error is that it is not able to decompress
i checked another popular solution in StackOverflow this is:
first answer of this:
Unzipping files
but I don't know what is
ZipFile in
var zipFile = new ZipFile(url, doneReading)
How can I get it !!
I am so lost, can anyone please help me !!
This is the zip file that I want to compress !!
This is inside my main zip file:
I'm writing a program, which needs to take some JSON from text file, then modify it and put it back to the same file, something like overwriting. I don't have problem with reading from file and modifying data, but I was searching long and found nothing about writing into files. If it's important - it's server-side file.
Ex. text file: {"name":"John","age":20}
Result which I need: {"name":"John","age":21}
You can use the fs module in node to write to a file!
information is located here on it https://www.w3schools.com/nodejs/nodejs_filesystem.asp
The two methods you will want to look at is .writeFile or .appendFile depending on your specific need.
I am writing a site in Laravel to complement a game written in Node. It pulls information directly from the game's .js files stored on GitHub and the plan is to use Vue to generate some nice HTML to display the data.
The .js files are of the form:
'use strict'
let x = {...};
exports.x = x;
I can import the files using a PHP request (simply using 'file') and pass them to JS with either jQuery.getScript or axios.get (so far). However, I am having real trouble coming up with a way to extract the 'x' values here under exports. If I were writing a JS app in node, I would simply do the following:
var xFile = require('xFile');
var x = xFile.x;
However, I can't figure out how to do that here as both GET methods return a string, not a JS file. JSON.parse() doesn't work, and I would like to come up with a solution that doesn't just replace the non-JSON text as I would need a reusable solution for other files. I don't suppose anybody has any ideas?
Thanks so much!
You could try something like below.
you could create the script tag dynamically and attach the script in the body and add your javascript code using innerHTML.
You call this script in your ajax response code.
let script = document.createElement('script');
script.innerHTML = 'alert("Hi")';
document.querySelector('body').appendChild(script);
I managed to figure it out! It's a little janky, but you can run a node file in PHP with the exec command. I just wrote a node file to import the file and return the useful data. This probably isn't an optimal solution, but it works for me since I'm much more confident with JS than with PHP.
I have a project where I use turbolinks in conjunction with Webpack's dynamic imports.
What I have is that the initial javascript file is as small as possible and then for each page, I load the relevant javascript code:
document.addEventListener('turbolinks:load', () => {
const moduleName = document.body.dataset.route;
if (!moduleName) return;
const chunkName = moduleName.replace('.', '/');
import(`#pages/${chunkName}.js`)
.catch((e) => {console.error(e);});
});
This is a great approach as each page gets its own minimal JS file.
The only issue is that we wait until the page has fully loaded before we fetch the page's javascript which makes initial page loads feel slightly slow. Ideally, I would love to preload the assets for the page when the page loads.
I thought that maybe adding a link rel=preload would solve this issue but the thing is that I do not know which chunks I need to preload on each page. This is logic that only Weboack knows.
My webpack.config.js file looks like:
output: {
chunkFilename: 'js/chunks/[name].js?id=[chunkhash]',
},
So basically each chunk is put in the js/chunks directory and its name is 0.js, 1.js, 2.js etc.
I would love to maybe somehow generate an additional json file where webpack can build a map for me. It would basically look like this (chunk key: modules that are within it):
{
0: ['#pages/tips/index.js', '#pages/tips/show.js'],
1: ['#pages/destinations/index.js', '#pages/tips/show.js'],
}
Then, I would read the file on each page and dynamically create the link rel=preload. For example, say I render the tips/show page now, I would scan the file above for each key that contains the #pages/tips/show.js and render a link rel=preload for each file (0.js and 1.js file in this case).
I'm using Webpack Commons Chunk plugin to extract the same vendors and modules to their own chunk file.
Is doing such a thing is even possible?
Thanks!
Webpack has something that is called stats that contains a-lot of info regarding the compilation.
It also contains the separation of chunks.
You can checkout what react-loadable webpack plugin does in order to generate similar task.
Hope this helps.
EDIT
You will need to write a webpack plugin that hooks on emit phase and then you will have an access to the compilation object that has all the data.
I'm trying to use readFileSync to copy the contents of a Javascript file and then place it into an HTML file. However when I do so the outputted code is getting encoded and causing errors when the JS runs. For example = is getting encoded into =.
I've used different variations for the encoding like so
fs.readFileSync('dist/' + v + '.min.js')
fs.readFileSync('dist/' + v + '.min.js', 'utf8')
and the output doesn't change.
I'm using Webpack and the HtmlWebpackPlugin to read the file and serve it to a Handlebars template. When I log out the contents of the file it looks correct, so it's looking like Handlebars is mashing up the text for some reason.
Thanks for any help!