Skip to content

Commit

Permalink
feat: adds open favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Howard committed Dec 1, 2018
1 parent 7c9518e commit 29b9cb3
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 121 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"key": "ctrl+alt+="
},
{
"command": "extension.clearProfiles",
"command": "extension.resetState",
"key": "ctrl+alt+0"
}
]
Expand Down
36 changes: 33 additions & 3 deletions src/commands/__tests__/gists.commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import { commands, window } from 'vscode';
import { TMP_DIRECTORY_PREFIX } from '../../constants';
import { gists } from '../../gists/gists-service';
import { logger } from '../../logger';
import { openCodeBlock, updateCodeBlock } from '../gists.commands';
import { profiles } from '../../profiles';
import {
openCodeBlock,
openFavoriteCodeBlock,
updateCodeBlock,
updateGistAccessKey
} from '../gists.commands';

jest.mock('fs');
jest.mock('path');

const executeCommandSpy = jest.spyOn(commands, 'executeCommand');
const configureSpy = jest.spyOn(gists, 'configure');
const editSpy = jest.spyOn(gists, 'update');
const errorSpy = jest.spyOn(logger, 'error');
// const getSpy = jest.spyOn(gists, 'get');
const infoSpy = jest.spyOn(logger, 'info');
// const listSpy = jest.spyOn(gists, 'list');
const showQuickPickSpy = jest.spyOn(window, 'showQuickPick');
const getProfileSpy = jest.spyOn(profiles, 'get');

describe('Gists Commands Tests', () => {
afterEach(() => {
Expand Down Expand Up @@ -60,6 +66,15 @@ describe('Gists Commands Tests', () => {
);
});
});
describe('#openFavoriteCodeBlock', () => {
test('should call openCodeBlock', async () => {
expect.assertions(1);

await openFavoriteCodeBlock();

expect(infoSpy.mock.calls[0][0]).toContain('openFavoriteCodeBlock');
});
});
describe('#updateCodeBlock', () => {
test('should save', async () => {
expect.assertions(3);
Expand Down Expand Up @@ -102,4 +117,19 @@ describe('Gists Commands Tests', () => {
expect(errorSpy).toHaveBeenCalledWith('updateCodeBlock > Not Found');
});
});
describe('#updateGistAccessKey', () => {
test('should update gist access key', () => {
expect.assertions(2);

getProfileSpy.mockReturnValue({ key: 'foo', url: 'bar' });

updateGistAccessKey();

expect(configureSpy.mock.calls).toHaveLength(1);
expect(configureSpy.mock.calls[0][0]).toStrictEqual({
key: 'foo',
url: 'bar'
});
});
});
});
34 changes: 18 additions & 16 deletions src/commands/__tests__/profile.commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const executeCommandSpy = jest.spyOn(commands, 'executeCommand');
const showQuickPickSpy = jest.spyOn(window, 'showQuickPick');
const showInformationMessageSpy = jest.spyOn(window, 'showInformationMessage');
const showInputBoxSpy = jest.spyOn(window, 'showInputBox');
const getMock = jest.fn();
const mockState: any = {
get: getMock,
update: jest.fn()
};

import {
clearProfiles,
createProfile,
selectProfile
} from '../profile.commands';
import { createProfile, selectProfile } from '../profile.commands';

const mockProfile = {
key: '111',
Expand All @@ -24,20 +25,12 @@ const mockProfile = {
};

describe('Profile Commands Tests', () => {
beforeEach(() => {
profiles.configure({ state: mockState });
});
afterEach(() => {
jest.resetAllMocks();
});
describe('#clearProfiles', () => {
test('should clear profiles', () => {
expect.assertions(1);

clearProfiles();

expect(executeCommandSpy).toHaveBeenCalledWith(
'extension.updateStatusBar'
);
});
});
describe('#createProfile', () => {
test('should show information message prompt', async () => {
expect.assertions(2);
Expand Down Expand Up @@ -161,7 +154,16 @@ describe('Profile Commands Tests', () => {

getAllSpy.mockReturnValue([mockProfile]);

getMock.mockImplementation(() => ({
'test-profile': {
active: true,
key: 'foo',
url: 'foo'
}
}));

showQuickPickSpy.mockResolvedValueOnce({
label: mockProfile.name,
profile: mockProfile
});

Expand Down
22 changes: 17 additions & 5 deletions src/commands/gists.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ const _openDocument = async (file: string): Promise<void> => {
commands.executeCommand('workbench.action.keepEditor');
};

const openCodeBlock = async (): Promise<void> => {
const openCodeBlock = async (favorite = false): Promise<void> => {
let gistName = '';
try {
logger.info('User Activated "openCodeBlock"');
logger.info(
`User Activated ${!favorite ? 'openCodeBlock' : 'openFavoriteCodeBlock'}`
);

const gists = await _getGists();
const gists = await _getGists(favorite);

const selected = await window.showQuickPick(gists);
if (selected) {
Expand All @@ -48,7 +50,10 @@ const openCodeBlock = async (): Promise<void> => {
}

logger.info('Opened Gist');
insights.track('open', undefined, { fileCount });
insights.track('open', undefined, {
fileCount,
isFavorite: Number(favorite)
});
}
} catch (err) {
const error: Error = err as Error;
Expand All @@ -65,6 +70,8 @@ const openCodeBlock = async (): Promise<void> => {
}
};

const openFavoriteCodeBlock = async (): Promise<void> => openCodeBlock(true);

const updateCodeBlock = async (doc: GistTextDocument): Promise<void> => {
let file = '';
try {
Expand Down Expand Up @@ -98,4 +105,9 @@ const updateGistAccessKey = (): void => {
insights.track('updateGistAccessKey', { url });
};

export { updateGistAccessKey, openCodeBlock, updateCodeBlock };
export {
updateGistAccessKey,
openCodeBlock,
openFavoriteCodeBlock,
updateCodeBlock
};
156 changes: 85 additions & 71 deletions src/commands/profile.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,105 @@ import { commands, MessageItem, QuickPickItem, window } from 'vscode';
import { insights } from '../insights';
import { logger } from '../logger';
import { profiles } from '../profiles';
import { notify } from '../utils';

export const createProfile = async (): Promise<void> => {
const { title } = (await window.showInformationMessage(
'Which GitHub Platform?',
{ modal: true },
{ title: 'GitHub.com (common)', isCloseAffordance: true },
{ title: 'GitHub Enterprise' }
)) as MessageItem;

const url =
title === 'GitHub Enterprise'
? await window.showInputBox({ prompt: 'Enter your enterprise API url' })
: 'https://api.github.com';

if (!url) {
logger.debug('User Aborted Create Profile at "url"');

return;
}

const key = await window.showInputBox({ prompt: 'Enter your access token' });
try {
const { title } = (await window.showInformationMessage(
'Which GitHub Platform?',
{ modal: true },
{ title: 'GitHub.com (common)', isCloseAffordance: true },
{ title: 'GitHub Enterprise' }
)) as MessageItem;

if (!key) {
logger.debug('User Aborted Create Profile at "key"');

return;
}
const url =
title === 'GitHub Enterprise'
? await window.showInputBox({ prompt: 'Enter your enterprise API url' })
: 'https://api.github.com';

const name = await window.showInputBox({
prompt: 'Give this profile a name'
});
if (!url) {
logger.debug('User Aborted Create Profile at "url"');

if (!name) {
logger.debug('User Aborted Create Profile at "name"');
return;
}

return;
}
const key = await window.showInputBox({
prompt: 'Enter your access token'
});

profiles.add(name, key, url, true);
commands.executeCommand('extension.updateStatusBar');
commands.executeCommand('extension.updateGistAccessKey');
insights.track('createProfile');
};
if (!key) {
logger.debug('User Aborted Create Profile at "key"');

export const selectProfile = async (): Promise<void> => {
const allProfiles = profiles.getAll();
return;
}

if (!allProfiles || allProfiles.length === 0) {
commands.executeCommand('extension.createProfile');
const name = await window.showInputBox({
prompt: 'Give this profile a name'
});

return;
}
if (!name) {
logger.debug('User Aborted Create Profile at "name"');

const qp = allProfiles.map((profile) => ({
label: profile.name,
profile
}));

const createProfileItem: QuickPickItem = {
label: 'Create New Profile'
};

const selected = (await window.showQuickPick([createProfileItem, ...qp])) as {
label: string;
profile: Profile;
};

if (!selected) {
return;
}

if (selected && selected.label !== 'Create New Profile') {
const { key, name, url } = selected.profile;
return;
}

profiles.add(name, key, url, true);
commands.executeCommand('extension.updateStatusBar');
commands.executeCommand('extension.updateGistAccessKey');
insights.track('slectProfile');
} else {
commands.executeCommand('extension.createProfile');
await commands.executeCommand('extension.updateStatusBar');
await commands.executeCommand('extension.updateGistAccessKey');
insights.track('createProfile');
} catch (err) {
const error: Error = err as Error;
logger.error(`createProfile > ${error && error.message}`);
insights.exception('createProfile', { messsage: error.message });
notify.error('Unable To Create Profile', error.message);
}
};

export const clearProfiles = (): void => {
profiles.reset();
commands.executeCommand('extension.updateStatusBar');
insights.track('clearProfiles');
export const selectProfile = async (): Promise<void> => {
try {
const allProfiles = profiles.getAll();

if (!allProfiles || allProfiles.length === 0) {
commands.executeCommand('extension.createProfile');

return;
}

const qp = allProfiles.map((profile) => ({
label: profile.name,
profile
}));

const createProfileItem: QuickPickItem = {
label: 'Create New Profile'
};

const selected = (await window.showQuickPick([
createProfileItem,
...qp
])) as {
label: string;
profile: Profile;
};

if (!selected) {
return;
}

if (selected && selected.label !== 'Create New Profile') {
const { key, name, url } = selected.profile;

profiles.add(name, key, url, true);
commands.executeCommand('extension.updateStatusBar');
commands.executeCommand('extension.updateGistAccessKey');
insights.track('slectProfile');
} else {
commands.executeCommand('extension.createProfile');
}
} catch (err) {
const error: Error = err as Error;
logger.error(`selectProfile > ${error && error.message}`);
insights.exception('selectProfile', { messsage: error.message });
notify.error('Unable To Select Profile', error.message);
}
};
26 changes: 16 additions & 10 deletions src/commands/status-bar.commands.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { StatusBarAlignment, window } from 'vscode';

import { profiles } from '../profiles';

import { insights } from '../insights';
import { logger } from '../logger';
import { profiles } from '../profiles';

const statusBar = window.createStatusBarItem(StatusBarAlignment.Left);
statusBar.show();

export const updateStatusBar = (): void => {
const activeProfile = profiles.get();
statusBar.text = `GIST ${
activeProfile ? `[${activeProfile.name}]` : '[Create Profile]'
}`;
statusBar.command = activeProfile
? 'extension.toggleProfile'
: 'extension.createProfile';
try {
const activeProfile = profiles.get();
statusBar.text = `GIST ${
activeProfile ? `[${activeProfile.name}]` : '[Create Profile]'
}`;
statusBar.command = activeProfile
? 'extension.toggleProfile'
: 'extension.createProfile';

logger.debug('Status Bar Updated');
logger.debug('Status Bar Updated');
} catch (err) {
const error: Error = err as Error;
logger.error(`updateStatusBar > ${error && error.message}`);
insights.exception('updateStatusBar', { messsage: error.message });
}
};

0 comments on commit 29b9cb3

Please sign in to comment.