introduce dlib.construct for safe object cosntruction

This commit is contained in:
DavHau 2022-03-12 14:38:20 +07:00
parent 83c1d872d7
commit 1fbd29bc66
3 changed files with 38 additions and 17 deletions

View File

@ -117,22 +117,23 @@
l.forEach workspacePaths
(wPath: makeWorkspaceProjectInfo tree wPath parentInfo)));
makeWorkspaceProjectInfo = tree: wsRelPath: parentInfo: {
inherit subsystem;
name =
(getPackageJson "${tree.fullPath}/${wsRelPath}").name
or "${parentInfo.name}/${wsRelPath}";
relPath = dlib.sanitizeRelativePath "${tree.relPath}/${wsRelPath}";
translators =
l.unique
(
(lib.filter (trans: l.elem trans ["package-lock" "yarn-lock"]) parentInfo.translators)
++ (getTranslatorNames "${tree.fullPath}/${wsRelPath}")
);
subsystemInfo = {
workspaceParent = tree.relPath;
makeWorkspaceProjectInfo = tree: wsRelPath: parentInfo:
dlib.construct.discoveredProject {
inherit subsystem;
name =
(getPackageJson "${tree.fullPath}/${wsRelPath}").name
or "${parentInfo.name}/${wsRelPath}";
relPath = dlib.sanitizeRelativePath "${tree.relPath}/${wsRelPath}";
translators =
l.unique
(
(lib.filter (trans: l.elem trans ["package-lock" "yarn-lock"]) parentInfo.translators)
++ (getTranslatorNames "${tree.fullPath}/${wsRelPath}")
);
subsystemInfo = {
workspaceParent = tree.relPath;
};
};
};
discoverInternal = {
tree,
@ -160,7 +161,7 @@
foundSubProjects alreadyDiscovered
else let
# project info of current directory
currentProjectInfo = {
currentProjectInfo = dlib.construct.discoveredProject {
inherit subsystem;
inherit (tree) relPath;
name = tree.files."package.json".jsonContent.name or tree.relPath;

18
src/lib/construct.nix Normal file
View File

@ -0,0 +1,18 @@
# constructors to have at least some kind of `type` safety
{lib}: {
discoveredProject = {
name,
relPath,
subsystem,
subsystemInfo,
translators,
}: {
inherit
name
relPath
subsystem
subsystemInfo
translators
;
};
}

View File

@ -10,6 +10,7 @@
inherit
calcInvalidationHash
callViaEnv
construct
containsMatchingFile
dirNames
discoverers
@ -34,8 +35,9 @@
};
# other libs
translators = import ./translators.nix {inherit dlib lib;};
construct = import ./construct.nix {inherit lib;};
discoverers = import ../discoverers {inherit config dlib lib;};
translators = import ./translators.nix {inherit dlib lib;};
parseUtils = import ./parsing.nix {inherit lib;};