Skip to content

Commit

Permalink
Supporting narn global commands (forces yarn). Resolves #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldenning committed Oct 29, 2019
1 parent 122e93e commit a9fcbbd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bin/narn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 15 additions & 10 deletions lib/narn-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions test/global.test.js
Original file line number Diff line number Diff line change
@@ -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", "[email protected]"]);
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", "[email protected]"]);
expect(isYarn).toBe(false);
});
});

0 comments on commit a9fcbbd

Please sign in to comment.