Skip to content

Commit

Permalink
Support installing packages from http and git urls. Resolves #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldenning committed Oct 29, 2019
1 parent 733bfa1 commit a897240
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
17 changes: 14 additions & 3 deletions lib/narn-lib.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs");
const path = require("path");
const minimist = require("minimist");
const validateNpmPackageName = require("validate-npm-package-name");

exports.detectYarn = function detectYarn() {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -63,7 +64,17 @@ exports.getNpmArgs = narnArgs => {
};

function transformNarnPackageString(packageToAdd) {
return packageToAdd.lastIndexOf("@") > 0
? packageToAdd
: `${packageToAdd}@latest`;
const packageNameWithoutVersion = packageToAdd.slice(
0,
packageToAdd.indexOf("@") > 0
? packageToAdd.lastIndexOf("@")
: packageToAdd.length + 1
);
const isValidPackageName = validateNpmPackageName(packageNameWithoutVersion)
.validForNewPackages;
if (isValidPackageName && packageNameWithoutVersion === packageToAdd) {
return `${packageToAdd}@latest`;
} else {
return packageToAdd;
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@
"prettier": "^1.18.2",
"pretty-quick": "^2.0.0"
},
"dependencies": {}
"dependencies": {
"validate-npm-package-name": "^3.0.0"
}
}
20 changes: 20 additions & 0 deletions test/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,24 @@ describe("narn add", () => {
"jest@latest"
]);
});

it("supports installing from https urls", () => {
expect(
getNpmArgs(["add", "https://github.com/joeldenning/narn#master"])
).toEqual([
"install",
"--save",
"https://github.com/joeldenning/narn#master"
]);
});

it("supports installing from git urls", () => {
expect(
getNpmArgs(["add", "[email protected]:joeldenning/narn.git#master"])
).toEqual([
"install",
"--save",
"[email protected]:joeldenning/narn.git#master"
]);
});
});
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,11 @@ buffer-from@^1.0.0:
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==

builtins@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/builtins/-/builtins-1.0.3.tgz#cb94faeb61c8696451db36534e1422f94f0aee88"
integrity sha1-y5T662HIaWRR2zZTThQi+U8K7og=

cache-base@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/cache-base/-/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2"
Expand Down Expand Up @@ -3713,6 +3718,13 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"

validate-npm-package-name@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/validate-npm-package-name/-/validate-npm-package-name-3.0.0.tgz#5fa912d81eb7d0c74afc140de7317f0ca7df437e"
integrity sha1-X6kS2B630MdK/BQN5zF/DKffQ34=
dependencies:
builtins "^1.0.3"

[email protected]:
version "1.10.0"
resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400"
Expand Down

0 comments on commit a897240

Please sign in to comment.