Skip to content

Commit

Permalink
using promises instead callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelluchini committed Oct 29, 2014
1 parent ace7dfc commit 53a1fa8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var samsung = require('./index');

/**
2 = Folder to be package
3 = Folder that the server will run (Optional) Default:./pakages
3 = Folder that the server will run (Optional) Default:./packages
4 = App ID (Optional)
5 = App Tile (Optional)
6 = Port that the server will run (Optional) Default:80
Expand Down
53 changes: 30 additions & 23 deletions lib/samsungpackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ var fs = require('fs-extra');
var os = require('os');
var express = require("express");
var zip = require("adm-zip");

var builder = require('xmlbuilder');
var stringlib = require('string');

var childProcess = require('child_process');

var myZip = new zip();
var Q = require('q');

var getNetworkIp = function(){
var ifaces = os.networkInterfaces();
Expand All @@ -23,26 +20,36 @@ var getNetworkIp = function(){
};

var deleteFolders = function(serverFolder) {
var deferred = Q.defer();
console.log('Clean folders');
fs.removeSync(serverFolder);
fs.remove(serverFolder, function(err){
if (err) deferred.reject(err);
deferred.resolve();
});
return deferred.promise;
};

var makeZip = function(serverFolder, folderToArchive, callback){
var makeZip = function(serverFolder, folderToArchive){
var deferred = Q.defer();
if(!folderToArchive){
throw Error('Missing folder path to archive');
deferred.reject(new Error('Missing folder path to archive'));
}
console.log('Creating zip file');
var zipName = 'samsungpackage.zip';
var myZip = new zip();
myZip.addLocalFolder(folderToArchive);
myZip.writeZip('samsungpackage.zip');
fs.move('samsungpackage.zip', serverFolder + '/Widget/samsungpackage.zip', function(err){
if (err) return console.error(err);
if(typeof callback === 'function') callback();
if (err) deferred.reject(err);
fs.chmod(serverFolder, 0777); //Prevent sudo permissions to delete the folder
deferred.resolve();
console.log('Zip has been created');
});
return deferred.promise;
};

var createXML = function(serverFolder, appId, appTitle, callback){
var createXML = function(serverFolder, appId, appTitle){
var deferred = Q.defer();
var xmlFileName = "widgetlist.xml";
var doc = builder.create();
doc.begin('root')
Expand All @@ -62,18 +69,17 @@ var createXML = function(serverFolder, appId, appTitle, callback){

var x = doc.toString();
x = stringlib(x).stripTags('root');

console.log('Creating XML file');
fs.writeFile(xmlFileName, x.s, function(err) {
if(err) return console.log(err);
console.log("The file was saved!");
if(err) deferred.reject(err);
fs.move('widgetlist.xml', serverFolder + '/widgetlist.xml',function(err){
if(err) return console.log(err);
console.log(doc.toString({ pretty: true }));
if(typeof callback === 'function') callback();
if(err) deferred.reject(err);
console.log("XML has been saved!");
deferred.resolve();
});
});
})


return deferred.promise;
};

var startServer = function(serverFolder, port){
Expand All @@ -100,10 +106,11 @@ exports.run = function(folderToArchive, serverFolder, appID, appTitle, port){
_appTitle = appTitle || 'Custom App',
_port = port || 80;

deleteFolders(_serverFolder);
makeZip(_serverFolder, _folderToArchive, function(){
createXML(_serverFolder, _appID, _appTitle, function(){
startServer(_serverFolder, _port);
});
Q.allSettled([
deleteFolders(_serverFolder),
makeZip(_serverFolder, _folderToArchive),
createXML(_serverFolder, _appID, _appTitle),
]).spread(function(a,b,c){
startServer(_serverFolder, _port);
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"express": "^4.10.0",
"fs-extra": "^0.12.0",
"mv": "0.0.5",
"q": "^1.0.1",
"rimraf": "^2.2.8",
"string": "^1.3.0",
"xmlbuilder": "0.4.2"
Expand Down

0 comments on commit 53a1fa8

Please sign in to comment.