Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #59 from teozkr/auto-workspace
Browse files Browse the repository at this point in the history
Automatic Yarn Workspace import
  • Loading branch information
zimbatm authored Jun 27, 2018
2 parents 1000845 + d85025e commit d3b5e20
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
42 changes: 42 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,48 @@ in
ln -s "$node_modules" node_modules
'';

mkYarnWorkspace = {
src,
packageJSON ? src+"/package.json",
yarnLock ? src+"/yarn.lock",
packageOverrides ? {},
...
}@attrs:
let
package = lib.importJSON packageJSON;
packageGlobs = package.workspaces;
globElemToRegex = lib.replaceStrings ["*"] [".*"];
# PathGlob -> [PathGlobElem]
splitGlob = lib.splitString "/";
# Path -> [PathGlobElem] -> [Path]
# Note: Only directories are included, everything else is filtered out
expandGlobList = base: globElems:
let
elemRegex = globElemToRegex (lib.head globElems);
rest = lib.tail globElems;
children = lib.attrNames (lib.filterAttrs (name: type: type == "directory") (builtins.readDir base));
matchingChildren = lib.filter (child: builtins.match elemRegex child != null) children;
in if globElems == []
then [ base ]
else lib.concatMap (child: expandGlobList (base+("/"+child)) rest) matchingChildren;
# Path -> PathGlob -> [Path]
expandGlob = base: glob: expandGlobList base (splitGlob glob);
packagePaths = lib.concatMap (expandGlob src) packageGlobs;
packages = lib.listToAttrs (map (src:
let
packageJSON = src+"/package.json";
package = lib.importJSON packageJSON;
allDependencies = lib.foldl (a: b: a // b) {} (map (field: lib.attrByPath [field] {} package) ["dependencies" "devDependencies"]);
in rec {
name = reformatPackageName package.name;
value = mkYarnPackage (builtins.removeAttrs attrs ["packageOverrides"] // {
inherit src packageJSON yarnLock;
workspaceDependencies = lib.mapAttrs (name: version: packages.${name})
(lib.filterAttrs (name: version: packages ? ${name}) allDependencies);
} // lib.attrByPath [name] {} packageOverrides);
}) packagePaths);
in packages;

mkYarnPackage = {
name ? null,
src,
Expand Down
14 changes: 3 additions & 11 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ let
};
in
(listToAttrs (build ["wetty" "weave-front-end" "sendgrid-helpers"])) // {
workspace = rec {
package-one = yarn2nix.mkYarnPackage {
src = ./workspace/package-one;
yarnLock = ./workspace/yarn.lock;
workspaceDependencies = { inherit package-two; };
};
package-two = yarn2nix.mkYarnPackage {
src = ./workspace/package-two;
yarnLock = ./workspace/yarn.lock;
};
}.package-one;
workspace = (yarn2nix.mkYarnWorkspace {
src = ./workspace;
}).package-one;
}
2 changes: 1 addition & 1 deletion tests/workspace/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"private": true,
"workspaces": ["package-one", "package-two"]
"workspaces": ["package-*"]
}

0 comments on commit d3b5e20

Please sign in to comment.