Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(help): Skip version checks if it will cause permission prompts #663

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion command/upgrade/_check_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export async function checkVersion(cmd: Command<any>): Promise<void> {
if (!isUpgradeCommand(upgradeCommand)) {
return;
}

if (!await upgradeCommand.hasRequiredPermissions()) {
// If not all required permissions were pre-granted, skip the version check to prevent prompting user
return;
}
const latestVersion = await upgradeCommand.getLatestVersion();
const currentVersion = mainCommand.getVersion();

Expand All @@ -23,9 +28,11 @@ export async function checkVersion(cmd: Command<any>): Promise<void> {
}

function isUpgradeCommand(command: unknown): command is UpgradeCommandImpl {
return command instanceof Command && "getLatestVersion" in command;
return command instanceof Command && "getLatestVersion" in command &&
"hasRequiredPermissions" in command;
}

interface UpgradeCommandImpl {
hasRequiredPermissions(): Promise<boolean>;
getLatestVersion(): Promise<string>;
}
c4spar marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions command/upgrade/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export abstract class Provider {
this.logger = logger;
}

abstract hasRequiredPermissions(): Promise<boolean>;

c4spar marked this conversation as resolved.
Show resolved Hide resolved
abstract getVersions(name: string): Promise<Versions>;

abstract getRepositoryUrl(name: string, version?: string): string;
Expand Down
12 changes: 11 additions & 1 deletion command/upgrade/provider/deno_land.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ export class DenoLandProvider extends Provider {
name = "deno.land";
private readonly repositoryUrl = "https://deno.land/x/";
private readonly registryUrl = "https://deno.land/x/";
private readonly apiUrl = "https://cdn.deno.land/";
private readonly moduleName?: string;

constructor({ name, main, logger }: DenoLandProviderOptions = {}) {
super({ main, logger });
this.moduleName = name;
}

async hasRequiredPermissions(): Promise<boolean> {
const apiUrl = new URL(this.apiUrl);
const permissionStatus = await Deno.permissions.query({
name: "net",
host: apiUrl.host,
});
return permissionStatus.state === "granted";
}

async getVersions(
name: string,
): Promise<Versions> {
const response = await fetch(
`https://cdn.deno.land/${this.moduleName ?? name}/meta/versions.json`,
`${this.apiUrl}${this.moduleName ?? name}/meta/versions.json`,
);
if (!response.ok) {
throw new Error(
Expand Down
9 changes: 9 additions & 0 deletions command/upgrade/provider/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export class GithubProvider extends Provider {
this.githubToken = token;
}

async hasRequiredPermissions(): Promise<boolean> {
const apiUrl = new URL(this.apiUrl);
const permissionStatus = await Deno.permissions.query({
name: "net",
host: apiUrl.host,
});
return permissionStatus.state === "granted";
}

async getVersions(
_name: string,
): Promise<GithubVersions> {
Expand Down
9 changes: 9 additions & 0 deletions command/upgrade/provider/jsr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export class JsrProvider extends Provider {
: options.name;
}

async hasRequiredPermissions(): Promise<boolean> {
const apiUrl = new URL(this.repositoryUrl);
const permissionStatus = await Deno.permissions.query({
name: "net",
host: apiUrl.host,
});
return permissionStatus.state === "granted";
}

async getVersions(
name: string,
): Promise<Versions> {
Expand Down
12 changes: 11 additions & 1 deletion command/upgrade/provider/nest_land.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ export class NestLandProvider extends Provider {
name = "nest.land";
private readonly repositoryUrl = "https://nest.land/package/";
private readonly registryUrl = "https://x.nest.land/";
private readonly apiUrl = "https://nest.land/api/";
private readonly moduleName?: string;

constructor({ name, main, logger }: NestLandProviderOptions = {}) {
super({ main, logger });
this.moduleName = name;
}

async hasRequiredPermissions(): Promise<boolean> {
const apiUrl = new URL(this.apiUrl);
const permissionStatus = await Deno.permissions.query({
name: "net",
host: apiUrl.host,
});
return permissionStatus.state === "granted";
}

async getVersions(
name: string,
): Promise<Versions> {
const response = await fetch(`https://nest.land/api/package-client`, {
const response = await fetch(`${this.apiUrl}package-client`, {
method: "post",
body: JSON.stringify({ data: { name: this.moduleName ?? name } }),
headers: { "Content-Type": "application/json" },
Expand Down
9 changes: 9 additions & 0 deletions command/upgrade/provider/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export class NpmProvider extends Provider {
}
}

async hasRequiredPermissions(): Promise<boolean> {
const apiUrl = new URL(this.apiUrl);
const permissionStatus = await Deno.permissions.query({
name: "net",
host: apiUrl.host,
});
return permissionStatus.state === "granted";
}

async getVersions(
name: string,
): Promise<Versions> {
Expand Down
4 changes: 4 additions & 0 deletions command/upgrade/upgrade_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ export class UpgradeCommand extends Command {
return versions;
}

public async hasRequiredPermissions(): Promise<boolean> {
return await this.getProvider().hasRequiredPermissions();
}

public async getLatestVersion(): Promise<string> {
const { latest } = await this.getVersions();
return latest;
Expand Down