Skip to content

Commit

Permalink
feat: npm login check [#3]
Browse files Browse the repository at this point in the history
  • Loading branch information
syi0808 committed Oct 13, 2024
1 parent 35e1265 commit 6c17a67
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/registry/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ export class NpmRegistry extends Registry {
}
}

async isLoggedIn() {
try {
await this.npm(['whoami']);

return true;
} catch (error) {
if (`${error}`.includes('ENEEDAUTH')) {
return false;
}

throw new NpmError('Failed to run `npm whoami`', { cause: error });
}
}

async collaborators() {
try {
return JSON.parse(
Expand Down
6 changes: 6 additions & 0 deletions src/tasks/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export const npmAvailableCheckTasks: ListrTask<Ctx> = {
task: async () => {
const npm = await npmRegistry();

if (!(await npm.isLoggedIn())) {
throw new NpmAvailableError(
'You are not logged in. Please log in first using `npm login`.',
);
}

if (await npm.isPublished()) {
if (!(await npm.hasPermission())) {
throw new NpmAvailableError(
Expand Down

0 comments on commit 6c17a67

Please sign in to comment.