Skip to content

Commit

Permalink
feat: adds delete file from gist
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Howard committed Dec 17, 2018
1 parent 22ede07 commit 9d6afea
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 42 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"category": "GIST"
},
{
"command": "extension.removeFileFromCodeBlock",
"title": "Remove From Block",
"command": "extension.gist.deleteFile",
"title": "Delete File",
"category": "GIST"
},
{
Expand Down
1 change: 1 addition & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as status from './status-bar';
const commandInitializers: CommandInitializer[] = [
gists.create,
gists.deleteCommand,
gists.deleteFile,
gists.open,
gists.openFavorite,
gists.openInBrowser,
Expand Down
1 change: 1 addition & 0 deletions src/commands/extension-commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
enum GistCommands {
Create = 'extension.gist.create',
Delete = 'extension.gist.delete',
DeleteFile = 'extension.gist.deleteFile',
Open = 'extension.gist.open',
OpenFavorite = 'extension.gist.openFavorite',
OpenInBrowser = 'extension.gist.openInBrowser',
Expand Down
51 changes: 51 additions & 0 deletions src/commands/gists/__tests__/delete-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// tslint:disable:no-any no-magic-numbers
import { window } from 'vscode';

import { deleteFile } from '../delete-file';

const deleteFileMock = jest.fn();
const errorMock = jest.fn();
const utilsMock = jest.genMockFromModule<Utils>('../../../utils');

describe('open gist', () => {
let deleteFileFn: CommandFn;
beforeEach(() => {
const gists = { deleteFile: deleteFileMock };
const insights = { exception: jest.fn() };
const logger = { error: errorMock, info: jest.fn() };
deleteFileFn = deleteFile(
{ gists, insights, logger } as any,
utilsMock as any
)[1];
(<any>window).activeTextEditor = undefined;
(<any>window).visibleTextEditors = [];
(<any>utilsMock.files.extractTextDocumentDetails).mockReturnValue({
filename: 'foo',
id: '123'
});
});
afterEach(() => {
jest.clearAllMocks();
});
test('what happens when errors occur', async () => {
expect.assertions(1);

(<any>window).activeTextEditor = { document: {} };

(<any>utilsMock.input.prompt).mockResolvedValueOnce('DELETE');
deleteFileMock.mockRejectedValueOnce(false);

await deleteFileFn();
expect(errorMock.mock.calls.length).toBe(1);
});
test('it deletes the open file', async () => {
expect.assertions(1);

(<any>window).activeTextEditor = { document: { gist: { id: '123' } } };
(<any>utilsMock.input.prompt).mockResolvedValueOnce('DELETE');

await deleteFileFn();

expect(deleteFileMock).toHaveBeenCalledWith('123', 'foo');
});
});
50 changes: 50 additions & 0 deletions src/commands/gists/delete-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { commands, window } from 'vscode';

import { GistCommands } from '../extension-commands';

const deleteFile: CommandInitializer = (
services: Services,
utils: Utils
): [Command, CommandFn] => {
const { gists, insights, logger } = services;

const command = GistCommands.DeleteFile;

const commandFn = async (): Promise<void> => {
try {
const editor = window.activeTextEditor;
const doc: undefined | GistTextDocument = editor && editor.document;
if (!doc) {
throw new Error('Document Missing');
}
const { id, filename } = utils.files.extractTextDocumentDetails(doc);
if (id) {
const canDelete =
(await utils.input.prompt('Enter "DELETE" to confirm')) === 'DELETE';
if (!canDelete) {
logger.info('User Aborted Deletion');

return;
}
logger.info(`Deleting File "${id}"`);
// tslint:disable-next-line:no-null-keyword
await gists.deleteFile(id, filename);
commands.executeCommand('workbench.action.closeActiveEditor');
utils.notify.info('Deleted File');
insights.track(command);
} else {
logger.info(`"${doc.fileName}" Not a Gist`);
utils.notify.info('Document Is Not a Gist');
}
} catch (err) {
const error: Error = err as Error;
logger.error(`${command} > ${error && error.message}`);
insights.exception(command, { messsage: error.message });
utils.notify.error('Could Not Delete File', `Reason: ${error.message}`);
}
};

return [command, commandFn];
};

export { deleteFile };
5 changes: 1 addition & 4 deletions src/commands/gists/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ const deleteCommand: CommandInitializer = (
const editor = window.activeTextEditor;
const doc: undefined | GistTextDocument = editor && editor.document;
if (!doc) {
utils.notify.error('Document Missing');
insights.exception(command, { message: 'document missing' });

return;
throw new Error('Document Missing');
}
const { id } = utils.files.extractTextDocumentDetails(doc);
if (id) {
Expand Down
1 change: 1 addition & 0 deletions src/commands/gists/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './create';
export * from './delete';
export * from './delete-file';
export * from './open';
export * from './open-favorite';
export * from './open-in-browser';
Expand Down
33 changes: 0 additions & 33 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,6 @@ export function activate(context: ExtensionContext): void {
migrationCount: results.migrated.length
});
});

commands.registerCommand(
'extension.deleteCodeBlock',
(): void => {
// intentionally left blank
}
);
commands.registerCommand(
'extension.removeFileFromCodeBlock',
(): void => {
// intentionally left blank
}
);
commands.registerCommand(
'extension.addToCodeBlock',
(): void => {
// intentionally left blank
}
);
commands.registerCommand(
'extension.changeCodeBlockDescription',
(): void => {
// intentionally left blank
}
);
commands.registerCommand(
'extension.insertCode',
(): void => {
// intentionally left blank
}
);

// context.subscriptions.push(disposable);
}

export function deactivate(): void {
Expand Down
15 changes: 15 additions & 0 deletions src/gists/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
configure,
createGist,
deleteFile,
deleteGist,
getGist,
getGists,
Expand Down Expand Up @@ -93,6 +94,20 @@ describe('Gists API Tests', () => {
error = err;
}

expect(error).toBeUndefined();
});
});
describe('#deleteFile', () => {
test('deletes a file', async () => {
expect.assertions(1);

let error: string | undefined;
try {
await deleteFile('1234', 'foo.txt');
} catch (err) {
error = err;
}

expect(error).toBeUndefined();
});
});
Expand Down
26 changes: 24 additions & 2 deletions src/gists/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const getGists = async (starred = false): Promise<Gist[]> => {
const updateGist = async (
id: string,
filename: string,
content: string
content: string | null
): Promise<Gist> => {
try {
const results = await gists.update({
Expand Down Expand Up @@ -150,4 +150,26 @@ const deleteGist = async (id: string): Promise<void> => {
}
};

export { configure, createGist, deleteGist, getGist, getGists, updateGist };
const deleteFile = async (id: string, filename: string): Promise<void> => {
try {
await gists.update({
// tslint:disable-next-line:no-null-keyword
files: { [filename]: null },
gist_id: id
});
} catch (err) {
const error: Error = prepareError(err as Error);

throw error;
}
};

export {
configure,
createGist,
deleteFile,
deleteGist,
getGist,
getGists,
updateGist
};
7 changes: 6 additions & 1 deletion src/typings/gists.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ interface GistService {
description?: string,
isPublic = true
): Promise<Gist>;
deleteFile(id: string, filename: string): Promise<void>;
deleteGist(id: string): Promise<void>;
getGist(id: string): Promise<Gist>;
getGists(starred?: boolean): Promise<Gist[]>;
updateGist(id: string, filename: string, content: string): Promise<Gist>;
updateGist(
id: string,
filename: string,
content: string | null
): Promise<Gist>;
}

0 comments on commit 9d6afea

Please sign in to comment.