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

Support for Yarn workspaces #58

Merged
merged 10 commits into from
Jun 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions default.nix
Original file line number Diff line number Diff line change
@@ -44,13 +44,15 @@ in rec {
];

mkYarnModules = {
name,
pname,
version,
packageJSON,
yarnLock,
yarnNix ? mkYarnNix yarnLock,
yarnFlags ? defaultYarnFlags,
pkgConfig ? {},
preBuild ? "",
workspaceDependencies ? {},
}:
let
offlineCache = importOfflineCache yarnNix;
@@ -67,9 +69,24 @@ in rec {
else
""
) (builtins.attrNames pkgConfig));
in
workspaceJSON = pkgs.writeText "${pname}-${version}-workspace-package.json" (builtins.toJSON {
private = true;
workspaces = [pname] ++ lib.mapAttrsToList (name: x: "deps/${x.pname}") workspaceDependencies;
});
workspaceDependencyLinks = lib.concatStringsSep "\n" (lib.mapAttrsToList (name: dep:
''
mkdir -p deps/${dep.pname}
ln -s ${dep.packageJSON} deps/${dep.pname}/package.json
'') workspaceDependencies);
workspaceDependencyRemoves =
"rm -f ${lib.concatMapStringsSep
" "
(x: "node_modules/${x}")
([pname] ++ (lib.mapAttrsToList (name: x: x.pname) workspaceDependencies))}";
in
stdenv.mkDerivation {
inherit name preBuild;
inherit preBuild;
name = "${pname}-modules-${version}";
phases = ["configurePhase" "buildPhase"];
buildInputs = [ yarn nodejs ] ++ extraBuildInputs;

@@ -81,7 +98,9 @@ in rec {
buildPhase = ''
runHook preBuild
cp ${packageJSON} ./package.json
mkdir ${pname}
cp ${packageJSON} ./${pname}/package.json
cp ${workspaceJSON} ./package.json
cp ${yarnLock} ./yarn.lock
chmod +w ./yarn.lock
@@ -90,7 +109,10 @@ in rec {
# Do not look up in the registry, but in the offline cache.
# TODO: Ask upstream to fix this mess.
sed -i -E 's|^(\s*resolved\s*")https?://.*/|\1|' yarn.lock
${workspaceDependencyLinks}
yarn install ${lib.escapeShellArgs yarnFlags}
${workspaceDependencyRemoves}
${lib.concatStringsSep "\n" postInstall}
@@ -122,19 +144,28 @@ in rec {
pkgConfig ? {},
extraBuildInputs ? [],
publishBinsFor ? null,
workspaceDependencies ? {},
...
}@attrs:
let
package = lib.importJSON packageJSON;
pname = reformatPackageName package.name;
version = package.version;
deps = mkYarnModules {
name = "${pname}-modules-${version}";
preBuild = yarnPreBuild;
inherit packageJSON yarnLock yarnNix yarnFlags pkgConfig;
workspaceDependencies = workspaceDependenciesTransitive;
inherit packageJSON pname version yarnLock yarnNix yarnFlags pkgConfig;
};
publishBinsFor_ = unlessNull publishBinsFor [pname];
in stdenv.mkDerivation (builtins.removeAttrs attrs ["pkgConfig"] // {
workspaceDependenciesTransitive = lib.foldl
(a: b: a // b)
{}
(lib.mapAttrsToList (name: dep: dep.workspaceDependencies) workspaceDependencies ++ [workspaceDependencies]);
workspaceDependencyCopy =
lib.concatStringsSep
"\n"
(lib.mapAttrsToList (name: dep: "cp -r --no-preserve=all ${dep.src} node_modules/${dep.pname}") workspaceDependenciesTransitive);
in stdenv.mkDerivation (builtins.removeAttrs attrs ["pkgConfig" "workspaceDependencies"] // {
inherit src;

name = unlessNull name "${pname}-${version}";
@@ -157,8 +188,13 @@ in rec {
fi
mkdir -p node_modules
ln -s $node_modules/* node_modules/
ln -s $node_modules/.bin node_modules/
# ln gets confused when the glob doesn't match anything
if [ "$(ls $node_modules | wc -l)" -gt 0 ]; then
ln -s $node_modules/* node_modules/
ln -s $node_modules/.bin node_modules/
fi
${workspaceDependencyCopy}
if [ -d node_modules/${pname} ]; then
echo "Error! There is already an ${pname} package in the top level node_modules dir!"
@@ -185,7 +221,8 @@ in rec {
'';

passthru = {
inherit package deps;
inherit pname package packageJSON deps;
workspaceDependencies = workspaceDependenciesTransitive;
} // (attrs.passthru or {});

# TODO: populate meta automatically
14 changes: 13 additions & 1 deletion tests/default.nix
Original file line number Diff line number Diff line change
@@ -7,4 +7,16 @@ let
value = yarn2nix.mkYarnPackage { src = ./. + "/${name}"; };
};
in
listToAttrs (build ["wetty" "weave-front-end" "sendgrid-helpers"])
(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;
}
2 changes: 2 additions & 0 deletions tests/workspace/package-one/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log(require("package-two")());
10 changes: 10 additions & 0 deletions tests/workspace/package-one/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "package-one",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"bin": "./index.js",
"dependencies": {
"package-two": "^1.0.0"
}
}
5 changes: 5 additions & 0 deletions tests/workspace/package-two/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const upperCase = require("upper-case");

module.exports = function() {
return upperCase("Hello from package-two!");
}
9 changes: 9 additions & 0 deletions tests/workspace/package-two/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "package-two",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"upper-case": "^1.1.3"
}
}
4 changes: 4 additions & 0 deletions tests/workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"workspaces": ["package-one", "package-two"]
}
7 changes: 7 additions & 0 deletions tests/workspace/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


upper-case@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/upper-case/-/upper-case-1.1.3.tgz#f6b4501c2ec4cdd26ba78be7222961de77621598"