Simplify further

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

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

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

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

@ -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",
"": 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