Skip to content
Merged
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
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import CommandSummary from './src/command-summary.js';
import Config from './src/config.js';
import Github from './src/github.js';
import RepoOptions from './src/repo-options.js';
Expand All @@ -12,6 +13,8 @@ import gistsCommand from './src/commands/gists.js';
UI.printWelcome();

const main = async () => {
let summary;

try {
if (!Config.load()) {
const token = await UI.promptAuth();
Expand All @@ -24,15 +27,15 @@ const main = async () => {
case 'repo':
case 'repository':
case 'repositories':
await reposCommand(RepoOptions.parse(process.argv.slice(3)));
summary = await reposCommand(RepoOptions.parse(process.argv.slice(3)));
break;
case 'codespaces':
case 'codespace':
await codespacesCommand();
summary = await codespacesCommand();
break;
case 'gists':
case 'gist':
await gistsCommand();
summary = await gistsCommand();
break;
case 'help':
UI.printHelp();
Expand All @@ -56,6 +59,17 @@ const main = async () => {
process.exitCode = 1;
return;
}

// Mutations are never replayed: a token-wide failure only invalidates the
// cached credentials so the next invocation authenticates again.
const outcome = CommandSummary.resolveOutcome(summary);

if (outcome.invalidateConfig) {
Config.deleteFile();
UI.printError(summary.authError.message);
}

if (outcome.exitCode !== 0) process.exitCode = outcome.exitCode;
};

main();
16 changes: 16 additions & 0 deletions src/command-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function create() {
return { processed: [], failed: [], authError: undefined };
}

// Translates a command summary into the process-level effects owned by the entry point.
function resolveOutcome(summary) {
return {
invalidateConfig: Boolean(summary?.authError),
exitCode: summary?.failed?.length > 0 ? 1 : 0,
};
}

export default {
create,
resolveOutcome,
};
16 changes: 7 additions & 9 deletions src/commands/codespaces.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import Config from '../config.js';
import CommandSummary from '../command-summary.js';
import UI from '../ui.js';

const codespacesCommand = async () => {
const codespaces = await UI.getCodespaces();
if (!codespaces) {
Config.deleteFile();
return await main();
}

let res = await UI.promptSelectCodespaces(codespaces);

if (res.codespaces.length === 0) {
UI.printNoCodespaceSelected();

return 0;
return CommandSummary.create();
}

const codespacesToDelete = res.codespaces;
const codespaceCount = codespacesToDelete.length;
res = await UI.promptConfirm(codespaceCount, 'codespaces', 'delete');

if (res.confirm === 'Yes') {
await UI.deleteCodespaces(codespacesToDelete);
} else {
if (res.confirm !== 'Yes') {
UI.printNoCodespacesDeleted();

return CommandSummary.create();
}

return await UI.deleteCodespaces(codespacesToDelete);
};

export default codespacesCommand;
11 changes: 7 additions & 4 deletions src/commands/gists.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CommandSummary from '../command-summary.js';
import UI from '../ui.js';

const gistsCommand = async () => {
Expand All @@ -6,17 +7,19 @@ const gistsCommand = async () => {

if (res.gists.length === 0) {
UI.printNoGistSelected();
return 0;
return CommandSummary.create();
}

const gistsToDelete = res.gists;
res = await UI.promptConfirm(gistsToDelete.length, 'gists', 'delete');

if (res.confirm === 'Yes') {
await UI.deleteGists(gistsToDelete);
} else {
if (res.confirm !== 'Yes') {
UI.printNoGistsDeleted();

return CommandSummary.create();
}

return await UI.deleteGists(gistsToDelete);
};

export default gistsCommand;
16 changes: 6 additions & 10 deletions src/commands/repos.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import Config from '../config.js';
import CommandSummary from '../command-summary.js';
import RepoOptions from '../repo-options.js';
import UI from '../ui.js';

const reposCommand = async (options = {}) => {
const { archive = false, force = false } = options;
const repositories = await UI.getRepositories();
if (!repositories) {
Config.deleteFile();
return await main();
}

let reposToProcess = RepoOptions.selectRepositories(repositories, options);

Expand All @@ -25,7 +21,7 @@ const reposCommand = async (options = {}) => {
} else {
UI.printNoReposSelected();
}
return 0;
return CommandSummary.create();
}

const repoCount = reposToProcess.length;
Expand All @@ -40,15 +36,15 @@ const reposCommand = async (options = {}) => {
} else {
UI.printNoReposDeleted();
}
return 0;
return CommandSummary.create();
}
}

if (archive) {
await UI.archiveRepositories(reposToProcess);
} else {
await UI.deleteRepositories(reposToProcess);
return await UI.archiveRepositories(reposToProcess);
}

return await UI.deleteRepositories(reposToProcess);
};

export default reposCommand;
26 changes: 6 additions & 20 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,57 +104,43 @@ function checkPermissions(authScopes, clientScopes) {
}

async function deleteRepository(repository) {
const res = await apiCall(
await apiCall(
'DELETE',
`/repos/${repository}`,
undefined,
undefined,
RESOURCE_SCOPES.deleteRepository
);

if (res.status !== 204) return false;

return true;
}

async function archiveRepository(repository) {
const res = await apiCall(
await apiCall(
'PATCH',
`/repos/${repository}`,
undefined,
{ archived: true },
RESOURCE_SCOPES.repositories
);

if (res.status !== 200) return false;

return true;
}

async function deleteCodespace(codespace) {
const res = await apiCall(
await apiCall(
'DELETE',
`/user/codespaces/${codespace}`,
undefined,
undefined,
RESOURCE_SCOPES.codespaces
);

if (res.status !== 204) return false;

return true;
}

async function deleteGist(gist) {
const res = await apiCall(
'DELETE',
`/gists/${gist}`,
undefined,
undefined,
RESOURCE_SCOPES.gists
);

if (res.status !== 204) return false;
await apiCall('DELETE', `/gists/${gist}`, undefined, undefined, RESOURCE_SCOPES.gists);

return true;
}
Expand Down Expand Up @@ -184,9 +170,9 @@ async function apiCall(method, endpoint, page, data, requiredScopes = []) {
try {
const res = await request(query, params);

const scopes = parseScopes(res.headers) || [];
const scopes = parseScopes(res.headers);

if (!checkPermissions(scopes, requiredScopes)) throw new ScopesError();
if (scopes && !checkPermissions(scopes, requiredScopes)) throw new ScopesError();

return res;
} catch (error) {
Expand Down
Loading