Simplify further

feature/node-rewrite
Sven Slootweg 2 years ago
parent be01fcc4bf
commit 8610aed04c

@ -3,36 +3,26 @@
const Promise = require("bluebird"); const Promise = require("bluebird");
const memoizee = require("memoizee"); const memoizee = require("memoizee");
const fs = Promise.promisifyAll(require("fs")); const fs = Promise.promisifyAll(require("fs"));
const treecutter = require("../../packages/treecutter");
const findmnt = require("../../packages/exec-findmnt"); const findmnt = require("../../packages/exec-findmnt");
const shallowMerge = require("../../packages/shallow-merge");
const All = require("../../packages/graphql-interface/symbols/all"); const All = require("../../packages/graphql-interface/symbols/all");
const treeMapAsync = require("../../packages/tree-map-async");
module.exports = function () { module.exports = function () {
let findmntOnce = memoizee(() => { let findmntOnce = memoizee(() => {
return Promise.try(() => { return Promise.try(() => {
return findmnt(); return findmnt();
}).then((mounts) => { }).then((mounts) => {
return treecutter.flatten(mounts); return treeMapAsync(mounts, async (mount) => {
}).map((mount) => { if (mount.sourceDevice?.startsWith("/")) {
if (mount.sourceDevice?.startsWith("/")) { return {
return Promise.try(() => { ... mount,
return fs.realpathAsync(mount.sourceDevice); sourceDevice: await fs.realpathAsync(mount.sourceDevice)
}).then((actualSourcePath) => { };
return shallowMerge(mount, { } else {
sourceDevice: actualSourcePath // Skip mounts that don't exist at a path at all
}); return mount;
}); }
} else { }, true);
return mount;
}
}).then((list) => {
let tree = treecutter.rebuild(list);
return {
tree: tree,
list: list
};
}); });
}); });

@ -94,6 +94,7 @@ module.exports = function createSources() {
}), }),
}; };
// TODO: Consider moving these to be inline as well, somehow
let factoryModules = { let factoryModules = {
lsblk: require("./lsblk")(), lsblk: require("./lsblk")(),
findmnt: require("./findmnt")() findmnt: require("./findmnt")()

@ -7,29 +7,20 @@ const fs = Promise.promisifyAll(require("fs"));
const unreachable = require("@joepie91/unreachable")("cvm"); const unreachable = require("@joepie91/unreachable")("cvm");
const lsblk = require("../../packages/exec-lsblk"); const lsblk = require("../../packages/exec-lsblk");
const All = require("../../packages/graphql-interface/symbols/all"); const All = require("../../packages/graphql-interface/symbols/all");
const treecutter = require("../../packages/treecutter");
const findInTree = require("../../packages/find-in-tree"); const findInTree = require("../../packages/find-in-tree");
const shallowMerge = require("../../packages/shallow-merge"); const treeMapAsync = require("../../packages/tree-map-async");
module.exports = function () { module.exports = function () {
let lsblkOnce = memoizee(() => { let lsblkOnce = memoizee(() => {
return Promise.try(() => { return Promise.try(() => {
return lsblk(); return lsblk();
}).then((tree) => { }).then((tree) => {
return treecutter.flatten(tree); return treeMapAsync(tree, async (device) => {
}).map((device) => { return {
return Promise.try(() => { ... device,
return fs.realpathAsync(device.path); path: await fs.realpathAsync(device.path)
}).then((actualPath) => { };
return shallowMerge(device, { }, true);
path: actualPath
});
});
}).then((devices) => {
return {
tree: treecutter.rebuild(devices),
list: devices
};
}); });
}); });

@ -1,27 +0,0 @@
"use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
const lvm = require("../../../packages/exec-lvm");
const All = require("../../../packages/graphql-interface/symbols/all");
module.exports = function () {
let getLogicalVolumesOnce = memoizee(lvm.getLogicalVolumes);
return function (paths) {
return Promise.try(() => {
return getLogicalVolumesOnce();
}).then((result) => {
return result.volumes;
}).then((volumes) => {
return paths.map((path) => {
if (path === All) {
return volumes;
} else {
return volumes.find((device) => device.path === path);
}
});
});
};
};

@ -1,27 +0,0 @@
"use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
const lvm = require("../../../packages/exec-lvm");
const All = require("../../../packages/graphql-interface/symbols/all");
module.exports = function () {
let getPhysicalVolumesOnce = memoizee(lvm.getPhysicalVolumes);
return function (paths) {
return Promise.try(() => {
return getPhysicalVolumesOnce();
}).then((result) => {
return result.volumes;
}).then((volumes) => {
return paths.map((path) => {
if (path === All) {
return volumes;
} else {
return volumes.find((device) => device.path === path);
}
});
});
};
};

@ -1,27 +0,0 @@
"use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
const lvm = require("../../../packages/exec-lvm");
const All = require("../../../packages/graphql-interface/symbols/all");
module.exports = function () {
let getVolumeGroupsOnce = memoizee(lvm.getVolumeGroups);
return function (names) {
return Promise.try(() => {
return getVolumeGroupsOnce();
}).then((result) => {
return result.groups;
}).then((groups) => {
return names.map((name) => {
if (name === All) {
return groups;
} else {
return groups.find((group) => group.name === name);
}
});
});
};
};

@ -1,15 +0,0 @@
"use strict";
const Promise = require("bluebird");
const smartctl = require("../../../packages/exec-smartctl");
const dlayerWrap = require("../../../packages/dlayer-wrap");
module.exports = function () {
return function (paths) {
return Promise.map(paths, (path) => {
return dlayerWrap(() => smartctl.attributes({ devicePath: path }), {
allowedErrors: [ smartctl.AttributesError ]
});
});
};
};

@ -1,15 +0,0 @@
"use strict";
const Promise = require("bluebird");
const dlayerWrap = require("../../../packages/dlayer-wrap");
const smartctl = require("../../../packages/exec-smartctl");
module.exports = function () {
return function (paths) {
return Promise.map(paths, (path) => {
return dlayerWrap(() => smartctl.info({ devicePath: path }), {
allowedErrors: [ smartctl.InfoError ]
});
});
};
};

@ -1,25 +0,0 @@
"use strict";
const Promise = require("bluebird");
const memoizee = require("memoizee");
const smartctl = require("../../../packages/exec-smartctl");
const All = require("../../../packages/graphql-interface/symbols/all");
module.exports = function () {
let scanOnce = memoizee(smartctl.scan);
return function (paths) {
return Promise.try(() => {
return scanOnce();
}).then((devices) => {
return paths.map((path) => {
if (path === All) {
return devices;
} else {
return devices.find((device) => device.path === path);
}
});
});
};
};

@ -179,6 +179,7 @@ module.exports = function () {
queue: "QUEUE", queue: "QUEUE",
"": null "": null
}) })
// FIXME: Remaining attributes
}; };
}) })
}; };

@ -0,0 +1,21 @@
"use strict";
const Promise = require("bluebird");
const treecutter = require("../treecutter");
module.exports = function treeMapAsync(tree, mapper, returnBoth = false) {
return Promise.map(treecutter.flatten(tree), (item) => {
return mapper(item);
}).then((items) => {
let newTree = treecutter.rebuild(items);
if (returnBoth) {
return {
tree: newTree,
list: items
};
} else {
return newTree;
}
});
};
Loading…
Cancel
Save