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

Commit

Permalink
Adds support for org names in package.json (#52)
Browse files Browse the repository at this point in the history
* Adds support for org names in package.json

This commit adds a function to reformat the string in the name field of
a package.json into a format that nix can use as a derivation name. This
is done through a combination of nix builtins that may not be ideal.

This is necessary because the organization in npm scoping is incompatible with
the format for nix package names.

* Adds organization name-spacing test
  • Loading branch information
johnchildren authored and zimbatm committed Mar 22, 2018
1 parent 4a431f4 commit 44e16c6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
13 changes: 12 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ in rec {
unlessNull = item: alt:
if item == null then alt else item;

reformatPackageName = pname:
let
# regex adapted from `validate-npm-package-name`
# will produce 3 parts e.g.
# "@someorg/somepackage" -> [ "@someorg/" "someorg" "somepackage" ]
# "somepackage" -> [ null null "somepackage" ]
parts = builtins.tail (builtins.match "^(@([^\/]+?)[\/])?([^\/]+?)$" pname);
# if there is no organisation we need to filter out null values.
non-null = builtins.filter (x: x != null) parts;
in builtins.concatStringsSep "-" non-null;

# Generates the yarn.nix from the yarn.lock file
mkYarnNix = yarnLock:
pkgs.runCommand "yarn.nix" {}
Expand Down Expand Up @@ -115,7 +126,7 @@ in rec {
}@attrs:
let
package = lib.importJSON packageJSON;
pname = package.name;
pname = reformatPackageName package.name;
version = package.version;
deps = mkYarnModules {
name = "${pname}-modules-${version}";
Expand Down
2 changes: 1 addition & 1 deletion tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ let
value = yarn2nix.mkYarnPackage { src = ./. + "/${name}"; };
};
in
listToAttrs (build ["wetty" "weave-front-end"])
listToAttrs (build ["wetty" "weave-front-end" "sendgrid-helpers"])
35 changes: 35 additions & 0 deletions tests/sendgrid-helpers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@sendgrid/helpers",
"description": "SendGrid NodeJS internal helpers",
"version": "6.2.1",
"author": "SendGrid <dx@sendgrid.com> (sendgrid.com)",
"contributors": [
"Kyle Partridge <kyle.partridge@sendgrid.com>",
"David Tomberlin <david.tomberlin@sendgrid.com>",
"Swift <swift@sendgrid.com>",
"Brandon West <brandon.west@sendgrid.com>",
"Scott Motte <scott.motte@sendgrid.com>",
"Robert Acosta <robert.acosta@sendgrid.com>",
"Elmer Thomas <elmer.thomas@sendgrid.com>",
"Adam Reis <adam@reis.nz>"
],
"license": "MIT",
"homepage": "https://sendgrid.com",
"repository": {
"type": "git",
"url": "git://github.com/sendgrid/sendgrid-nodejs.git"
},
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">= 6.0.0"
},
"tags": [
"sendgrid",
"helpers"
],
"dependencies": {
"chalk": "^2.0.1"
}
}
41 changes: 41 additions & 0 deletions tests/sendgrid-helpers/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


ansi-styles@^3.1.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.0.tgz#c159b8d5be0f9e5a6f346dab94f16ce022161b88"
dependencies:
color-convert "^1.9.0"

chalk@^2.0.1:
version "2.3.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba"
dependencies:
ansi-styles "^3.1.0"
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

color-convert@^1.9.0:
version "1.9.1"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.1.tgz#c1261107aeb2f294ebffec9ed9ecad529a6097ed"
dependencies:
color-name "^1.1.1"

color-name@^1.1.1:
version "1.1.3"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"

escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"

has-flag@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51"

supports-color@^4.0.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.5.0.tgz#be7a0de484dec5c5cddf8b3d59125044912f635b"
dependencies:
has-flag "^2.0.0"

0 comments on commit 44e16c6

Please sign in to comment.