Handle edge cases with npm workspaces:

- fixes #93
- package.json workspaces is an attrset instead of list
- workspaces pointing to non-existent directory
- workspaces pointing to a directory whithout a package.json
This commit is contained in:
DavHau 2022-03-02 18:58:53 +07:00
parent 5b8a09c764
commit 29741906b2

View File

@ -43,19 +43,63 @@ let
if l.hasSuffix "*" glob then if l.hasSuffix "*" glob then
let let
prefix = l.removeSuffix "*" glob; prefix = l.removeSuffix "*" glob;
dirNames = dlib.listDirs "${tree.fullPath}/${prefix}"; path = "${tree.fullPath}/${prefix}";
in
l.map (dname: "${prefix}/${dname}") dirNames dirNames =
if l.pathExists path
then dlib.listDirs path
else else
[ glob ]; l.trace
"WARNING: Detected workspace ${glob} does not exist."
[];
existingWsPaths =
l.filter
(wsPath:
if l.pathExists "${path}/${wsPath}/package.json"
then true
else
let
notExistingPath =
dlib.sanitizeRelativePath "${prefix}/${wsPath}";
in
l.trace
"WARNING: Detected workspace ${notExistingPath} does not exist."
false)
dirNames;
in
l.map (dname: "${prefix}/${dname}") existingWsPaths
else
if l.pathExists "${tree.fullPath}/${glob}/package.json"
then [ glob ]
else
l.trace
"WARNING: Detected workspace ${glob} does not exist."
[];
# collect project info for workspaces defined by current package.json # collect project info for workspaces defined by current package.json
getWorkspaces = tree: parentInfo: getWorkspaces = tree: parentInfo:
let let
packageJson = tree.files."package.json".jsonContent; packageJson = tree.files."package.json".jsonContent;
workspacesRaw = packageJson.workspaces or [];
workspacesFlattened =
if l.isAttrs workspacesRaw
then
l.flatten
(l.mapAttrsToList
(category: workspaces: workspaces)
workspacesRaw)
else if l.isList workspacesRaw
then workspacesRaw
else throw "Error parsing workspaces in ${tree.files."package.json".relPath}";
in in
l.flatten l.flatten
(l.forEach (packageJson.workspaces or []) (l.forEach workspacesFlattened
(glob: (glob:
let let
workspacePaths = getWorkspacePaths glob tree; workspacePaths = getWorkspacePaths glob tree;