diff --git a/lib/narn-lib.js b/lib/narn-lib.js index c8abc54..540bd5c 100644 --- a/lib/narn-lib.js +++ b/lib/narn-lib.js @@ -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) => { @@ -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; + } } diff --git a/package.json b/package.json index 5f75424..7ae31af 100644 --- a/package.json +++ b/package.json @@ -31,5 +31,7 @@ "prettier": "^1.18.2", "pretty-quick": "^2.0.0" }, - "dependencies": {} + "dependencies": { + "validate-npm-package-name": "^3.0.0" + } } diff --git a/test/add.test.js b/test/add.test.js index 4033b98..7bc2faf 100644 --- a/test/add.test.js +++ b/test/add.test.js @@ -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", "git@github.com:joeldenning/narn.git#master"]) + ).toEqual([ + "install", + "--save", + "git@github.com:joeldenning/narn.git#master" + ]); + }); }); diff --git a/yarn.lock b/yarn.lock index 0b0cd04..aa60b40 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" + verror@1.10.0: version "1.10.0" resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400"