Skip to content

Commit

Permalink
Initial support for respecting NARN_DEFAULT_PM for global installatio…
Browse files Browse the repository at this point in the history
…ns. (#25)

* Initial support for respecting NARN_DEFAULT_PM for global installations.

* Add github workflow

* Self review
  • Loading branch information
jolyndenning authored Feb 26, 2021
1 parent 296a015 commit 6ed2c89
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

github: [joeldenning]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
open_collective: # Replace with a single Open Collective
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Build and Test

on:
- push
- pull_request

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "15"
- uses: pnpm/[email protected]
with:
version: 5.17.3
- run: pnpm install --frozen-lockfile
- run: pnpm test
25 changes: 20 additions & 5 deletions lib/narn-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const validateNpmPackageName = require("validate-npm-package-name");

exports.detectYarn = function detectYarn(args) {
if (args && args.length > 0 && args[0] === "global") {
// all global commands go through yarn
return Promise.resolve(true);
// all global commands go through default PM
return Promise.resolve(false);
} else {
return new Promise((resolve, reject) => {
fs.access(
Expand All @@ -22,7 +22,7 @@ exports.detectYarn = function detectYarn(args) {

exports.detectPnpm = function detectNpm(args) {
if (args && args.length > 0 && args[0] === "global") {
// all global commands go through yarn
// all global commands go through default PM
return Promise.resolve(false);
} else {
return new Promise((resolve, reject) => {
Expand All @@ -39,7 +39,7 @@ exports.detectPnpm = function detectNpm(args) {

exports.detectNpm = function detectNpm(args) {
if (args && args.length > 0 && args[0] === "global") {
// all global commands go through yarn
// all global commands go through default PM
return Promise.resolve(false);
} else {
return new Promise((resolve, reject) => {
Expand All @@ -60,12 +60,18 @@ exports.getNpmArgs = (narnArgs, isPnpm = false) => {
const yarnArgs = minimist(narnArgs, {
boolean: ["dev", "D", "latest", "frozen-lockfile"],
});
const yarnCommands = yarnArgs._;
let yarnCommands = yarnArgs._;
let isGlobal = false;
if (yarnCommands.length > 0 && yarnCommands[0] === "global") {
isGlobal = true;
yarnCommands = yarnCommands.slice(1);
}
const yarnTarget = yarnCommands.length > 0 ? yarnCommands[0] : "install";
const yarnSubCommands = yarnCommands.slice(1);
let npmTarget;
let npmArgs;
let result;

switch (yarnTarget) {
case "install":
npmTarget = "install";
Expand All @@ -83,6 +89,11 @@ exports.getNpmArgs = (narnArgs, isPnpm = false) => {
const packages = yarnSubCommands.map(transformNarnPackageString);
const saveType = yarnArgs.dev || yarnArgs.D ? "--save-dev" : "--save";
npmArgs = [saveType, ...packages];

if (isGlobal) {
npmArgs = npmArgs.slice(1);
}

break;
case "remove":
npmTarget = "uninstall";
Expand Down Expand Up @@ -162,6 +173,10 @@ exports.getNpmArgs = (narnArgs, isPnpm = false) => {
break;
}

if (isGlobal) {
npmArgs.unshift("--global");
}

return result || [npmTarget, ...npmArgs];
};

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"homepage": "https://github.com/joeldenning/narn",
"license": "MIT",
"scripts": {
"test": "jest --env=node && js-correct-lockfile pnpm",
"test": "concurrently \"pnpm:test:*\"",
"test:jest": "jest --env=node",
"test:lockfile": "js-correct-lockfile pnpm",
"format": "prettier . --write",
"check-format": "prettier . --check",
"postinstall": "husky install",
Expand All @@ -28,6 +30,7 @@
],
"devDependencies": {
"@types/jest": "^26.0.20",
"concurrently": "^6.0.0",
"husky": "5",
"jest": "^26.0.1",
"jest-cli": "^26.0.1",
Expand Down
Loading

0 comments on commit 6ed2c89

Please sign in to comment.