You basically need to execute the next function where you print your console.log. Basically this:
function createDir(){
fs.mkdirs('./quickstart',function(){
console.log("thao thanh cong");
unZip(); // CALL UNZIP WHEN WE ARE FINISHED
});
}
function unZip(){
var zipfile = fs.createReadStream('./prestashop_cache/' + quickstart);
zipfile.pipe(unzip.Extract({path:'./quickstart'}));
zipfile.on('end',function(){
console.log("giai nen xong");
copydata(); // CALL COPYDATA WHEN WE ARE FINISHED
});
}
function copydata(){
if (path.extname(folder + '/samples' + version + '/data') != '.DS_Store' && path.extname(folder + '/samples' + version + '/data') != '.svn' && path.extname(folder + '/samples' + version + '/data') != '__MACOSX') {
fs.copy(folder + '/samples' + version + '/data','./quickstart/prestashop/install/data/', function () {
console.log("coppy thanh cong toi thu muc data");
});
}
}
But this is not very modular is it? It creates too much dependency between the functions so you can't reuse any of them. No problem! Javascript allows you to pass functions around as arguments:
function createDir(callback){
fs.mkdirs('./quickstart',function(){
console.log("thao thanh cong");
if (callback) {
callback(); // Call a function when finished
}
});
}
function unZip(callback){
var zipfile = fs.createReadStream('./prestashop_cache/' + quickstart);
zipfile.pipe(unzip.Extract({path:'./quickstart'}));
zipfile.on('end',function(){
console.log("giai nen xong");
if (callback) {
callback(); // Call a function when finished
}
});
}
function copydata(callback){
if (path.extname(folder + '/samples' + version + '/data') != '.DS_Store' && path.extname(folder + '/samples' + version + '/data') != '.svn' && path.extname(folder + '/samples' + version + '/data') != '__MACOSX') {
fs.copy(folder + '/samples' + version + '/data','./quickstart/prestashop/install/data/', function () {
console.log("coppy thanh cong toi thu muc data");
if (callback) {
callback(); // Call a function when finished
}
});
}
}
So now you can call them like this:
createDir(function(){
unZip(function(){
copydata()
});
});
Alternatively with newer versions of node.js you can use promises. It basically does the same thing but with chainable syntax:
function createDir(){
return new Promise(function(callback,error_callback){
fs.mkdirs('./quickstart',function(){
console.log("thao thanh cong");
callback();
});
});
}
function unZip(){
return new Promise(function(callback,error_callback){
var zipfile = fs.createReadStream('./prestashop_cache/' + quickstart);
zipfile.pipe(unzip.Extract({path:'./quickstart'}));
zipfile.on('end',callback);
});
}
function copydata(){
return new Promise(function(callback,error_callback){
if (path.extname(folder + '/samples' + version + '/data') != '.DS_Store' && path.extname(folder + '/samples' + version + '/data') != '.svn' && path.extname(folder + '/samples' + version + '/data') != '__MACOSX') {
fs.copy(folder + '/samples' + version + '/data','./quickstart/prestashop/install/data/', function () {
console.log("coppy thanh cong toi thu muc data");
callback();
});
}
else {
error_callback('not found');
}
});
}
Which you can now call like this:
createDir()
.then(unZip)
.then(copydata)
.then(function(){
console.log('all done!');
});