Skip to content

Commit

Permalink
feat: adds quickpick list for gists
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Howard committed Nov 21, 2018
1 parent b87de9c commit c60e787
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 54 deletions.
58 changes: 13 additions & 45 deletions src/commands/gists.commands.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,32 @@
import * as fs from 'fs';
import * as path from 'path';
import * as tmp from 'tmp';
import { commands, window, workspace } from 'vscode';
import { window } from 'vscode';

import { list } from '../gists/index';
import { getGists } from '../gists';
import { logger } from '../logger';
// import { logger } from './logger';

const selectGist = async (
favorite = false
): Promise<QuickPickGist | undefined> => {
const items = (await list(favorite)).map((item, i, j) => ({
const _getGists = async (favorite = false): Promise<QuickPickGist[]> =>
(await getGists(favorite)).map((item, _, j) => ({
block: item,
description: `${item.public ? 'PUBLIC' : 'PRIVATE'} - Files: ${
item.fileCount
} - Created: ${item.createdAt} - Updated: ${item.updatedAt}`,
label: `${j.length - 1}. ${item.name}`
}));

return window.showQuickPick(items);
};

const openCodeBlock = async (): Promise<void> => {
try {
const selected = await selectGist();
logger.info('User Activated "openCodeBlock"');

const gists = await _getGists();
const selected = await window.showQuickPick(gists);
if (!selected) {
logger.info('User Aborted "openCodeBlock"');

return;
}
const dir = generateDirectory({ token: selected.block.id });
// TODO: Open the files
} catch (err) {
logger.error(err);
}
};

const generateDirectory = (options: {
prefix?: string;
token: string;
}): string => {
const prefix = `${options.prefix ? options.prefix : 'vscode_gist_'}_${
options.token
}_`;
const directory = tmp.dirSync({ prefix });

return directory.name;
};

const openTextDocument = async (
dir: string,
filename: string,
content: string
): Promise<void> => {
try {
const file = path.join(dir, filename);
fs.writeFileSync(file, content);
const textDocument = await workspace.openTextDocument(file);
await window.showTextDocument(textDocument);
commands.executeCommand('workbench.action.keepEditor');
logger.info(`User Selected Gist: "${selected.label}"`);
} catch (err) {
logger.error(err);
const error: Error = err as Error;
logger.error(error && error.message);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './gists.commands';
8 changes: 2 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import * as vscode from 'vscode';

import { openCodeBlock } from './commands';
import { Levels, logger } from './logger';

export function activate(_: vscode.ExtensionContext): void {
const debug = process.env.DEBUG === 'true';
logger.setLevel(debug ? Levels.DEBUG : Levels.ERROR);

logger.debug('extension activated');
vscode.commands.registerCommand(
'extension.openCodeBlock',
(): void => {
// intentionally left blank
}
);
vscode.commands.registerCommand('extension.openCodeBlock', openCodeBlock);
vscode.commands.registerCommand(
'extension.openFavoriteCodeBlock',
(): void => {
Expand Down
2 changes: 1 addition & 1 deletion src/gists/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const formatList = (gistList: ListResponse): Gist[] =>
/**
* Get a list of gists
*/
export const list = async (starred = false): Promise<Gist[]> =>
export const getGists = async (starred = false): Promise<Gist[]> =>
formatList(
(await gists[starred ? 'getStarred' : 'getAll']({
per_page: GISTS_PER_PAGE
Expand Down
2 changes: 1 addition & 1 deletion src/gists/gists-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GistsService {
public static getInstance = (): GistsService =>
GistsService.instance ? GistsService.instance : new GistsService()

private static instance?: GistsService;
private static readonly instance?: GistsService;

private octokit: Octokit;
private options: Octokit.Options = DEFAULT_OPTIONS;
Expand Down
2 changes: 1 addition & 1 deletion src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Logger {
: new Logger(LOGGER_LEVEL))

private static instance?: Logger;
private static log = (
private static readonly log = (
method: 'debug' | 'log' | 'info' | 'warn' | 'error',
...args: any[]
): void => {
Expand Down

0 comments on commit c60e787

Please sign in to comment.