Add an StopOnError option to show if it stops there and why
ncp (
path.join(req.body.uploadPath, req.body.packageName, resourceDirectoryName),
path.join(req.body.uploadPath,req.body.packageName),
{stopOnErr: true},
(err) => {
if (err)
{
return console.error(err);
}
console.log("ok");
}
)
SOLUTION
After dealing with this for a little of time, the problem was located:
The user was executing the fs.readdir right after the execution of
NCP. As NCP is asyncronous, fs.readdir was not reading the whole bunch
of copied files, as they were still not copied.
Then, just a delay before fs.readdir was the solution to see all of them. Not a Spaces or special characters thing.
FINALLY, A REAL SOLUTION
As async programming has a first-day-of-school where everybody learns
how to deal with callbacks, there is an example to deal with
callbacks, and solve the question.
var ncp = require('ncp').ncp;
var fs = require('fs');
ncp.limit = 16;
var source="dir1"
var destination="dir2"
console.log('Starting');
console.log(new Date().toISOString());
ncp(source, destination, mycallback );
function mycallback(err)
{
//here we are inside a callback declared in an outer function
if (err) {
return console.log(err);
}
console.log('done!');
console.log(new Date().toISOString());
fs.readdir(destination, function (err, files){
console.log("Files Copied to Dir2 "+ files.length);
//here we are inside fs.readdir callback
copyTwo();
});
//as fs.readdir is also async, I cannot execute copyTwo() here, and I must execute inside fs.reddir callback
}
function copyTwo()
{
console.log('Starting CopyTwo');
console.log(new Date().toISOString());
source="dir2"
destination="dir3"
//This function uses an embedded callback
ncp(source, destination, function (err)
{
// here we are inside the function declared (with-no-name) to use directly asembedded callback
if (err) {
return console.log(err);
}
console.log('done!');
console.log(new Date().toISOString());
fs.readdir(destination, function (err, files){
console.log("Files Copied to Dir3 "+ files.length);
});
});
}
[admin-sp@localhost pruebaApp]$ ls dir1 | wc -l
7777
[admin-sp@localhost pruebaApp]$ ls dir2 | wc -l
0
[admin-sp@localhost pruebaApp]$ ls dir3 | wc -l
0
[admin-sp@localhost pruebaApp]$ node index.js
Starting
2018-05-01T00:55:50.683Z
done!
2018-05-01T00:55:57.063Z
Files Copied to Dir2 7777
Starting CopyTwo
2018-05-01T00:55:57.069Z
done!
2018-05-01T00:56:03.030Z
Files Copied to Dir3 7777
[admin-sp@localhost pruebaApp]$