Browse Source

Base chargement/parsing fichiers Desktop

upgrade-electron
wpetit 4 years ago
parent
commit
8f2ed91f1a
1 changed files with 104 additions and 0 deletions
  1. +104
    -0
      js/fs.js

+ 104
- 0
js/fs.js View File

@@ -0,0 +1,104 @@
(function(Pitaya) {

"use strict";

var ini = require('ini');
var glob = require('glob');
var path = require('path');
var fs = require('fs');

var FS = Pitaya.FS = {};

FS.loadAllDesktopFiles = function(rootDirs) {

return FS.findAllDesktopFiles(rootDirs)
.then(function(filePaths) {

var promises = filePaths.map(function(path) {
return FS.loadDesktopFile(path);
});

return Promise.all(promises)
.then(function(desktopFiles) {
return desktopFiles.map(function(desktop, i) {
return {
path: filePaths[i],
desktop: desktop
};
});
})
;

})
;

};

FS.findAllDesktopFiles = function(rootDirs) {

if(!Array.isArray(rootDirs)) {
rootDirs = [rootDirs];
}

var promises = rootDirs.map(function(rootDir) {

var globPath = path.join(rootDir, '**/*.desktop');

return new Promise(function(resolve, reject) {
glob(globPath, function(err, files) {
if(err) return reject(err);
return resolve(files);
});
});

});

return Promise.all(promises)
.then(function(apps) {
return uniq(flatten(apps));
})
;

};

FS.loadDesktopFile = function(filePath) {
return new Promise(function(resolve, reject) {
fs.readFile(filePath, 'utf8', function(err, content) {
if(err) return reject(err);
try {
var decoded = ini.decode(content);
return resolve(decoded);
} catch(err) {
return reject(err);
}
});
});
};

// Array helpers

function flatten(arr) {
return arr.reduce(function(result, item) {
result = result.concat.apply(result, Array.isArray(item) ? flatten(item) : [item]);
return result;
}, []);
}

function uniq(arr) {
return arr.reduce(function(result, item) {
if(result.indexOf(item) === -1) {
result.push(item);
}
return result;
}, []);
}


}(Pitaya = global.Pitaya || {}));


Pitaya.FS.loadAllDesktopFiles(['/usr/share/applications', '/home/william/.local/share/applications'])
.then(function(apps) {
console.log(apps[5])
})
;

Loading…
Cancel
Save