From a9fcbbd9408fb51f7fa561add27c0dc62712ada6 Mon Sep 17 00:00:00 2001 From: Joel Denning Date: Mon, 28 Oct 2019 19:17:35 -0600 Subject: [PATCH] Supporting narn global commands (forces yarn). Resolves #5. --- bin/narn.js | 2 +- lib/narn-lib.js | 25 +++++++++++++++---------- test/global.test.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 test/global.test.js diff --git a/bin/narn.js b/bin/narn.js index 777b065..b8b3f41 100755 --- a/bin/narn.js +++ b/bin/narn.js @@ -9,8 +9,8 @@ const narnPackageJson = JSON.parse( ); async function runPackageManager() { - const isYarn = await detectYarn(); const narnArgs = process.argv.slice(2); + const isYarn = await detectYarn(narnArgs); const command = isYarn ? "yarn" : "npm"; const commandArgs = isYarn ? getYarnArgs(narnArgs) : getNpmArgs(narnArgs); diff --git a/lib/narn-lib.js b/lib/narn-lib.js index f75624d..edf0966 100644 --- a/lib/narn-lib.js +++ b/lib/narn-lib.js @@ -3,16 +3,21 @@ const path = require("path"); const minimist = require("minimist"); const validateNpmPackageName = require("validate-npm-package-name"); -exports.detectYarn = function detectYarn() { - return new Promise((resolve, reject) => { - fs.access( - path.resolve(process.cwd(), "package-lock.json"), - fs.constants.F_OK, - noPackageLock => { - resolve(Boolean(noPackageLock)); - } - ); - }); +exports.detectYarn = function detectYarn(args) { + if (args && args.length > 0 && args[0] === "global") { + // all global commands go through yarn + return Promise.resolve(true); + } else { + return new Promise((resolve, reject) => { + fs.access( + path.resolve(process.cwd(), "package-lock.json"), + fs.constants.F_OK, + noPackageLock => { + resolve(Boolean(noPackageLock)); + } + ); + }); + } }; exports.getYarnArgs = narnArgs => narnArgs; diff --git a/test/global.test.js b/test/global.test.js new file mode 100644 index 0000000..7fae246 --- /dev/null +++ b/test/global.test.js @@ -0,0 +1,33 @@ +const { detectYarn } = require("../lib/narn-lib"); +const fs = require("fs"); + +jest.mock("fs", () => ({ + access: jest.fn(), + constants: { + F_OK: 1 + } +})); + +describe("narn global commands", () => { + beforeEach(() => { + fs.access.mockReset(); + }); + + it("supports installing global packages", async () => { + const isYarn = await detectYarn(["global", "add", "lodash@1.0.0"]); + expect(isYarn).toBe(true); + }); + + it("supports uninstalling global packages", async () => { + const isYarn = await detectYarn(["global", "remove", "lodash"]); + expect(isYarn).toBe(true); + }); + + it("doesn't think everything is global", async () => { + fs.access.mockImplementationOnce((path, mode, errBack) => { + errBack(false); + }); + const isYarn = await detectYarn(["add", "lodash@1.0.0"]); + expect(isYarn).toBe(false); + }); +});