Skip to content

Commit

Permalink
pollapo login npmrc (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
disjukr committed Dec 7, 2023
1 parent e500fd1 commit 3e34857
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
50 changes: 50 additions & 0 deletions cli/pollapo/cmds/login/cmds/npmrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Command } from "https://deno.land/x/cliffy@v0.25.2/command/mod.ts";
import { Confirm } from "https://deno.land/x/cliffy@v0.25.2/prompt/confirm.ts";
import Npmrc from "npm:@npmcli/config@8.0.3";
import * as npmrc from "npm:@npmcli/config@8.0.3/lib/definitions/index.js";
import { println } from "../../../misc/stdio.ts";
import { writeGhHosts } from "../../../../../misc/github/auth.ts";
import {
getToken,
GithubNotLoggedInError,
} from "../../../../../misc/github/index.ts";

export default new Command()
.description("Sign in with .npmrc config")
.option("-S, --silent", "Prevents interactive cli functionality")
.action(async ({ silent }) => {
if (!silent) {
try {
await getToken();
const confirmed = await Confirm.prompt(
{
message:
"You're already logged into github.com. Do you want to override?",
default: false,
indent: "",
},
);
if (!confirmed) return;
} catch (err) {
if (!(err instanceof GithubNotLoggedInError)) {
throw err;
}
}
}
const { definitions, flatten, shorthands } = npmrc;
const config = new Npmrc({
npmPath: "",
definitions,
shorthands,
flatten,
argv: [],
});
await config.load();
const { token } = config.getCredentialsByURI("https://npm.pkg.github.com");
if (!token) {
if (!silent) await println("No github credentials found in npmrc.");
return Deno.exit(1);
} else {
await writeGhHosts(token);
}
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Command } from "https://deno.land/x/cliffy@v0.25.2/command/mod.ts";
import { print, println } from "../../misc/stdio.ts";
import { print, println } from "../../../misc/stdio.ts";
import {
getToken,
GithubNotLoggedInError,
} from "../../../../misc/github/index.ts";
} from "../../../../../misc/github/index.ts";

export default new Command()
.description("Print pollapo login token")
Expand Down
4 changes: 2 additions & 2 deletions cli/pollapo/cmds/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
getToken,
GithubNotLoggedInError,
} from "../../../../misc/github/index.ts";
import token from "./token.ts";

export default new Command()
.description("Sign in with GitHub account")
Expand Down Expand Up @@ -54,4 +53,5 @@ export default new Command()
await writeGhHosts(pollTokenResult.accessToken);
await println("You are all set!");
})
.command("token", token);
.command("npmrc", (await import("./cmds/npmrc.ts")).default)
.command("token", (await import("./cmds/token.ts")).default);

0 comments on commit 3e34857

Please sign in to comment.