Node js module mkdirp only creates half the directories - node.js

I'm trying to use mkdirp for a project, but when I feed it a var with my dir path I want created, it only creates the first half of it. I've installed the module locally with npm. I'm using Node v0.10.20 on a Raspberry Pi.
This is how it looks:
var filePath = "upload/home/pi/app/temp";
mkdirp(filePath, function(error) {
if(error) {
console.log(error);
} else {
...
}
});
I don't get an error creating the path, but it only creates "upload/home/pi", however if I run my script again, it creates the rest of the directory structure. Upload is a
directory in the current working directory which is the user home.
I emailed the author of the module who suggested that it could be because I'm using a flash drive as my medium, which in turn lies about when IO operations are complete, which I guess confuses node.js to think it has successfully written the path to disk. How should I tackle my problem? I guess I can do a check on if the directory was created, and loop that until it has, but that feels like the wrong thing to do. Any suggestions welcome.
Thanks.

Try doing this synchronously:
var filePath = "upload/home/pi/app/temp";
mkdirp(filePath)

Related

Best way to copy a directory from an external drive to a local folder with electronjs?

Just wondering if anyone has ever attempted to copy a directory from an external drive (connected via USB) to a local folder.
I am using ElectronJS so I can use my JavaScript, HTML/CSS skills to create a desktop application without utilising a C language. (i.e. C# or C++) With ElectronJS there's a lot less to worry about.
Here is the list of things I've tried so far:
basic fs.copyFile (using copyFile intially and will then loop round the directory to copy all files)
var fs = require('fs');
window.test = () => {
fs.moveSync("targetFile","destDir", function(err) {
if(err){
console.log(err);
}else{
console.log("copy complete")
}
});
}
fs.moveSync is not a function even though Visual Studio Code brought up moveSync as a suggestion when I entered fs. (ctrl + space)
using child_process functions to copy files using the command line.
Code is:
var process = require('child_process')
window.test = function(){
process.exec('ipconfig', function(err, stdout, stderr){
if(err){
console.log(err);
}else{
console.log(stdout)
}
})
}
Then bundled with browserify. Bundle.js is then imported into the html file and the test function is called on the click of a button. I'm aware the command is ipconfig for now, this was merely used to see if a command could be executed. It appears it could because I was getting process.exec is not defined.
use the node-hid node module to read and trasfer data from the external drive.
The exposed functions within this module were also reported as being undefined. And I thought about the use case longer I thought a simple copy process would suffice because external drive can be accessed like any other folder in the file explorer.
Unfortunately, all of the above have failed and I've spent the most part of the day looking for alternative modules and/or solutions.
Thanks in advance because any help to achieve this would be much appreciated.
Thanks
Patrick
The npm package fs-extra should solve your problem.
It has the move function, which
Moves a file or directory, even across devices
Ended up adding this to my preload.js for:
window.require = require;
It will work for now but is due to be depreciated.
I'll use this for now and make other updates when I have to.

Where is my Electron App located

I have an electron app, that also operates on files in the same directory. These are not files the user selects, or files bundled inside the electron application, but I do need to reference them
for example:
/Users/test/Documents/myapp.app
/Users/test/Documents/example.zip
I need to know where that application is located, e.g.:
/Users/test/Documents
But instead when test.app is ran I get:
/Users/test/Documents/myapp.app/Contents/Resources/app/
Resulting in errors when it tries to access /Users/test/Documents/myapp.app/Contents/Resources/app/test.zip, or if I use a relative path, /test.zip.
How would I reliably access example.zip in this scenario? Note that I cannot bundle it with my app, it's being placed there by somebody else, so moving it is not an option, and it's a file I'm expecting to be there.
A friend and I looked up how Atom does it and arrived at this solution:
function getAppRoot() {
if ( process.platform === 'win32' ) {
return path.join( app.getAppPath(), '/../../../' );
} else {
return path.join( app.getAppPath(), '/../../../../' );
}
}
I think either app.getPath('exe') or process.execPath is what you are looking for. They both give you the path of the currently used executable.
Edit: Look at the docs for app.getPath() to get the paths of all the different folder (e.g. home, appdata, documents, etc.)
Where are you running the Electron App? If it is running under 'Documents' (I am assuming Windows), you can pass in the documents to app.getPath like so: app.getPath('documents').
If you are calling this from the render process, you might have to import remote and do the following if you are in a render process remote.app.getPath('documents');.
This electron doc will be better at explaining the path options you can pass in.
You can use the fs.readdir function to list all the files that are in a particular directory (could be your app's or anywhere). You can make a directory from the code to put all the new files so it would be easier for you to list them out properly to process.
const testFolder = './tests/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
});
How do you get a list of the names of all files present in a directory in Node.js?

Facing issues when uploaded a zip code in aws lambda

I am new to aws and just started working around with aws lambda by following some youtube tutorials and was able to write aws lambda functions successfully on the web editor itself.
But I tried with the uploading zip file from my local system in which i wrote a node.js code that use modules "fs" and "fill-pdf". But when I tried to run the code it was giving me error.
"error" : module not found "/var/task/index".
I searched through internet and found some links like :
https://github.com/lob/lambda-pdftk-example
I tried this but it also shows same error.
Here is my code :
var index = require('index');
var fillPdf = require("fill-pdf");
var fs = require('fs');
var formDate = {
'Employee Name': 'MyName',
'Company Name': 'ComapnyName'
};
var pdfTemplatePath = "my.pdf";
fillPdf.generatePdf(formDate, pdfTemplatePath, function(err,
output) {
if ( !err ) {
fs.writeFile('message.pdf', output, function (err) {
if (err) throw err;
console.log('It\'s saved! in same location.');
});
}
});
The thing is that I don't know what could be the reason that this error is coming.Thanks for any help.
Make sure you're not zipping the folder, but its contents. Check that your zip contains index.js in its root level
The error may occur due to the following :
1. Properly zip the folder wait for it's zipping process completion and
then upload.
2. First run the main.js file locally like using node main.js and check
are there any errors showing in the terminal window, if it does then
fix them and then upload.
3. Also there must be handler file that lambda needs, which is must
so if you have the handler.js file then when in aws lambda you
create a lambda function and check the configuration setting there
then do update the name of the handler file name with yours like by
default it is index.js may be you would have lambda.js do change it
with lambda name (example lambda.handler)
Remove the line var index = require('index'); as it is not used in your code. I'm not sure why it can't find the module after installing it, but in you current example you don't need it.
This error occurs it means your zip is
not in valid form in which aws demands.
If you double click on zip you will find your folder inside that your code file,but lambda wants that when you double click on zip it shoud show direct code files.
To achive this:
open terminal
cd your-lambda-folder
zip -r index.zip *
then upload index.zip to lambda

node.js issues with Meteor's file system

I have tried to figure out what i am missing from this puzzle between. Node.js and Meteor.js. Meteor is built on Node.js i know this. But Meteor doesn't not work properly with Node.js. Either I need to do 20 more steps to get the same result, which I don't know what they are. Or there is a serious bug between the two. Standalone Node.js runs the command below just fine. Running the same commands on Meteor cause errors or undefined results. Wish i had a why to solve this or they need to patch this so it will work the way it should work.
examples #1
var fs = require('fs');
fs.readFile('file.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log(data);
});
example #2
var jetpack = require('fs-jetpack');
var data = jetpack.read('file.txt');
console.log(data);
example #3
var fs = require ('fs');
var readMe = fs.readFileSync('file.txt', 'utf8');
console.log(readMe);
You shouldn't try to load files like this because you don't know what the folder structure looks like. Meteor creates builds from your project directory, both in development and production mode. This means that even though you have a file.txt in your project folder, it doesn't end up in the same place in the build (or it isn't even included in the build at all).
For example, your code tries to read the file from the development build folder .meteor/local/build/programs/server. However, this folder doesn't contain file.txt.
Solution: Store file.txt in the private folder of your project and use Assets.getText to read it. If you still want to use the functions from fs to load the file, you can retrieve the absolute path with Assets.absoluteFilePath.

Child Process exits abruptly while executing in node.js

So I have been working on a project and it requires me to convert the office files into PDFs and subsequently images. I've written and integrated everything into one single node.js script, but for some reason the script keeps on bypassing the synchronous child process creation. Here is the code:
down.download(parsed_url);
var f_name=obj.doc;
var ext=f_name.slice(f_name.length-4);
var w_path="C:\\Users\\Akshay\\Desktop\\conv_Scripts\\word_pdf.ps1";
var e_path="C:\\Users\\Akshay\\Desktop\\conv_Scripts\\excel_pdf.ps1";
var p_path="C:\\Users\\Akshay\\Desktop\\conv_Scripts\\power_pdf.ps1";
var file_name=f_name.slice(0,f_name.length-5);
console.log(ext);
console.log(f_name);
console.log(file_name);
if(ext==="docx"){
word.wordpdf(w_path);
}
else if(ext==="xlsx"){
excel.excelpdf(e_path);}
else if(ext==="pptx"){
ppt.pptpdf(p_path);
console.log("Done converting to PD");
}
else if(ext==".pdf"){
img.img(f_name);
}
else{
console.log("Can't convert to PDF");
}
crawlpdf.crawlpdf(file_name,function(collect){
collect.forEach(function(col){
img.img(col);
console.log('Done!');
});
the wordpdf,excelpdf and pptpdf functions are same in their structure. I'll write down the wordpdf module's code here:
var spawn=require('child_process').spawnSync,
child;
exports.wordpdf=function(filepath){
child=spawn("powershell.exe",[filepath]);
};
The trouble is that when I execute the script,it shows me "Done converting to PD" (since the downloaded file was a ppt) but I do not find any pdf of the downloaded file .The .ps1 scripts in the path are already tested and there is no issue with them. If you could shed some light it would be really a massive help to me.
Thanks.
Alright, In case anyone else runs into the same issue as this, here's the explanation for what went wrong:
The message coming from the RabbitMQ messsage queue is small.Hence when the message arrives it is rapidly consumed,leading to race conditions in the code. For better and more accurate performance try parsing and writing the message to a file and then use async module to do the necessary steps.

Resources