-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
992f4a9
commit 205c6b8
Showing
9 changed files
with
4,045 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,5 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# narn | ||
|
||
Never have to switch between npm and yarn commands ever again |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env node | ||
const { spawn } = require("child_process"); | ||
const { detectYarn, getYarnArgs, getNpmArgs } = require("../lib/narn-lib.js"); | ||
|
||
async function runPackageManager() { | ||
const isYarn = await detectYarn(); | ||
const narnArgs = process.argv.slice(2); | ||
|
||
const command = isYarn ? "yarn" : "npm"; | ||
const commandArgs = isYarn ? getYarnArgs(narnArgs) : getNpmArgs(narnArgs); | ||
|
||
console.info(`${command} ${commandArgs.join(" ")}`); | ||
|
||
spawn(command, commandArgs, { stdio: "inherit" }); | ||
} | ||
|
||
runPackageManager().catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const minimist = require("minimist"); | ||
|
||
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.getYarnArgs = narnArgs => narnArgs; | ||
|
||
exports.getNpmArgs = narnArgs => { | ||
const yarnArgs = minimist(narnArgs); | ||
const yarnCommands = yarnArgs._; | ||
const yarnTarget = yarnCommands.length > 0 ? yarnCommands[0] : "install"; | ||
const yarnSubCommands = yarnCommands.slice(1); | ||
let npmTarget; | ||
let npmArgs; | ||
|
||
switch (yarnTarget) { | ||
case "install": | ||
npmTarget = "install"; | ||
npmArgs = []; | ||
break; | ||
case "add": | ||
npmTarget = "install"; | ||
const packages = yarnSubCommands.map(packageToAdd => | ||
packageToAdd.lastIndexOf("@") > 0 | ||
? packageToAdd | ||
: `${packageToAdd}@latest` | ||
); | ||
npmArgs = ["--save", ...packages]; | ||
break; | ||
case "remove": | ||
npmTarget = "uninstall"; | ||
npmArgs = yarnSubCommands; | ||
break; | ||
default: | ||
npmTarget = "run"; | ||
npmArgs = narnArgs; | ||
break; | ||
} | ||
|
||
return [npmTarget, ...npmArgs]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "narn", | ||
"version": "1.0.0", | ||
"main": "lib/narn-lib.js", | ||
"bin": { | ||
"narn": "bin/narn.js" | ||
}, | ||
"repository": "[email protected]:joeldenning/narn.git", | ||
"author": "Joel Denning <[email protected]>", | ||
"homepage": "https://github.com/joeldenning/narn", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "jest --env=node", | ||
"prettier": "prettier 'bin/**/*' --write" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged" | ||
} | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.19", | ||
"husky": "^3.0.9", | ||
"jest": "^24.9.0", | ||
"jest-cli": "^24.9.0", | ||
"minimist": "^1.2.0", | ||
"prettier": "^1.18.2", | ||
"pretty-quick": "^2.0.0" | ||
}, | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const { getNpmArgs } = require("../lib/narn-lib"); | ||
|
||
describe("narn add", () => { | ||
it("forces packages to be saved", () => { | ||
expect(getNpmArgs(["add", "lodash"]).slice(0, 2)).toEqual([ | ||
"install", | ||
"--save" | ||
]); | ||
}); | ||
|
||
it("can install multiple packages with their semantic version specified", () => { | ||
expect(getNpmArgs(["add", "[email protected]", "react-dom@^16.10.0"])).toEqual([ | ||
"install", | ||
"--save", | ||
"[email protected]", | ||
"react-dom@^16.10.0" | ||
]); | ||
}); | ||
|
||
it("forces latest version of package when semantic version is omitted, same as yarn add", () => { | ||
expect(getNpmArgs(["add", "lodash"])).toEqual([ | ||
"install", | ||
"--save", | ||
"lodash@latest" | ||
]); | ||
}); | ||
|
||
it("works with scoped packages", () => { | ||
expect(getNpmArgs(["add", "@openmrs/esm-api"])).toEqual([ | ||
"install", | ||
"--save", | ||
"@openmrs/esm-api@latest" | ||
]); | ||
expect(getNpmArgs(["add", "@openmrs/esm-api@~1.0.0"])).toEqual([ | ||
"install", | ||
"--save", | ||
"@openmrs/esm-api@~1.0.0" | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { getNpmArgs } = require("../lib/narn-lib"); | ||
|
||
describe("narn install", () => { | ||
it("defaults to npm install if no args are provided", () => { | ||
expect(getNpmArgs([])).toEqual(["install"]); | ||
}); | ||
|
||
it("works when install is specified", () => { | ||
expect(getNpmArgs(["install"])).toEqual(["install"]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { getNpmArgs } = require("../lib/narn-lib"); | ||
|
||
describe("narn remove", () => { | ||
it("removes packages", () => { | ||
expect(getNpmArgs(["remove", "@angular/cli", "@angular/core"])).toEqual([ | ||
"uninstall", | ||
"@angular/cli", | ||
"@angular/core" | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.