From 13c80d8bc52fb76864f7b675de02beee21d4fad8 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Fri, 13 Oct 2017 14:29:58 -0700 Subject: [PATCH] Add support for local packages and git repos --- bin/yarn2nix.js | 113 +++++++++++++++++++++++++++++++++++------------- default.nix | 9 ++++ 2 files changed, 91 insertions(+), 31 deletions(-) diff --git a/bin/yarn2nix.js b/bin/yarn2nix.js index c452210..971ff9b 100755 --- a/bin/yarn2nix.js +++ b/bin/yarn2nix.js @@ -2,45 +2,96 @@ "use strict"; const path = require("path"); +const cp = require("child_process"); -const HEAD = ` -{fetchurl, linkFarm}: rec { - offline_cache = linkFarm "offline" packages; - packages = [ -`.trim(); +const remoteToString = function(remote) { + return ` + { + name = "${remote.file_name}"; + path = fetchurl { + name = "${remote.file_name}"; + url = "${remote.url}"; + sha1 = "${remote.sha1}"; + }; + }`; +} -function generateNix(lockedDependencies) { - let found = {}; +const localToString = function(local) { + return ` + { + name = "${local.file_name}"; + path = ${local.file_name}; + }`; +} - console.log(HEAD) +const arrayToString = function(elements) { + return `[ + ${elements.join("\n")} + ]`; +} - for (var depRange in lockedDependencies) { - let dep = lockedDependencies[depRange]; +const astToString = function(ast) { + const remotes = ast.links + .filter(function(link) { return link.hasOwnProperty('remote'); }) + .map(function(link) { return remoteToString(link.remote); }); - let depRangeParts = depRange.split('@'); - let [url, sha1] = dep["resolved"].split("#"); - let file_name = path.basename(url) + const locals = ast.links + .filter(function(link) { return link.hasOwnProperty('local'); }) + .map(function(link) { return localToString(link.local); }); - if (found.hasOwnProperty(file_name)) { - continue; - } else { - found[file_name] = null; - } + return ` + {fetchurl, linkFarm}: rec { + offline_cache = linkFarm "offline" packages; + packages = ${arrayToString(remotes)}; + localPackages = ${arrayToString(locals)}; + }`; +}; +function yarnToAst(lockedDependencies) { + let found = {}; - console.log(` - { - name = "${file_name}"; - path = fetchurl { - name = "${file_name}"; - url = "${url}"; - sha1 = "${sha1}"; - }; - }`) - } + return { + links: Object.keys(lockedDependencies).map(function(depRange) { + let dep = lockedDependencies[depRange]; + + let depRangeParts = depRange.split('@'); + if ('resolved' in dep) { + let url; + let sha1; + let file_name; + if (/codeload.github.com.*tar.gz\//.test(dep["resolved"])) { + url = dep["resolved"]; + sha1 = cp.execSync("curl -sS " + url + " | shasum | cut -d \" \" -f 1").toString().trim(); + var matches = /tar.gz\/(.*)/.exec(url); + file_name = matches[1]; + } else { + url = dep["resolved"].split("#")[0]; + sha1 = dep["resolved"].split("#")[1]; + file_name = path.basename(url); + } + + if (found.hasOwnProperty(file_name)) { + return {}; + } else { + found[file_name] = null; + } - console.log(" ];") - console.log("}") + return { + remote: { + file_name: file_name, + url: url, + sha1: sha1 + } + }; + } else { + return { + local: { + file_name: depRange.split("@")[1] + } + }; + } + }) + }; } const yarnLock = process.argv[2] || "yarn.lock"; @@ -54,4 +105,4 @@ if (json.type != "success") { throw new Error("yarn.lock parse error") } -generateNix(json.object); +console.log(astToString(yarnToAst(json.object))); diff --git a/default.nix b/default.nix index b1bf9c5..de93ab9 100644 --- a/default.nix +++ b/default.nix @@ -40,6 +40,8 @@ pkgs.lib.fix (self: rec { }: let offlineCache = importOfflineCache yarnNix; + locals = (import yarnNix { inherit fetchurl linkFarm; }).localPackages or []; + copyCommands = lib.concatMapStrings (l: "mkdir -p \$(dirname ${l.name}); cp -R ${l.path} ${l.name};") locals; extraBuildInputs = (lib.flatten (builtins.map (key: pkgConfig.${key} . buildInputs or [] ) (builtins.attrNames pkgConfig))); @@ -68,6 +70,13 @@ pkgs.lib.fix (self: rec { chmod +w ./yarn.lock yarn config --offline set yarn-offline-mirror ${offlineCache} + yarn config --offline set yarn-offline-mirror-pruning false + yarn config set yarn-offline-mirror-pruning false + + ls -l + /usr/local/bin/tree -l ${offlineCache} + + ${copyCommands} # Do not look up in the registry, but in the offline cache. # TODO: Ask upstream to fix this mess.