You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
964 B
JavaScript
52 lines
964 B
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird");
|
|
const memoizee = require("memoizee");
|
|
|
|
const lsblk = require("../../wrappers/lsblk");
|
|
const All = require("../../graphql/symbols/all");
|
|
|
|
function linearizeDevices(devices) {
|
|
let linearizedDevices = [];
|
|
|
|
function add(list) {
|
|
for (let device of list) {
|
|
linearizedDevices.push(device);
|
|
|
|
if (device.children != null) {
|
|
add(device.children);
|
|
}
|
|
}
|
|
}
|
|
|
|
add(devices);
|
|
|
|
return linearizedDevices;
|
|
}
|
|
|
|
module.exports = function () {
|
|
let lsblkOnce = memoizee(() => {
|
|
return Promise.try(() => {
|
|
return lsblk();
|
|
}).then((devices) => {
|
|
return {
|
|
tree: devices,
|
|
list: linearizeDevices(devices)
|
|
};
|
|
});
|
|
});
|
|
|
|
return function (names) {
|
|
return Promise.try(() => {
|
|
return lsblkOnce();
|
|
}).then(({tree, list}) => {
|
|
return names.map((name) => {
|
|
if (name === All) {
|
|
return tree;
|
|
} else {
|
|
return list.find((device) => device.name === name);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
}; |