Skip to content

Commit

Permalink
Merge pull request mauricedb#8 from mauricedb/issue-5
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricedb authored Jun 10, 2020
2 parents bb4bfec + 336d969 commit b623822
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 416 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
"vscode": "^1.1.36"
},
"dependencies": {
"@types/jsonfile": "5.0.0",
"jsonfile": "5.0.0"
"jsonc": "^2.0.0"
}
}
30 changes: 15 additions & 15 deletions src/instruction-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { commands, Position, Selection, Uri, window, workspace } from "vscode";
import { join, dirname } from "path";
import { commands, Position, Selection, Uri, window, workspace } from 'vscode';
import { join, dirname } from 'path';

import {
Command,
Expand All @@ -9,15 +9,15 @@ import {
CreateFile,
Wait,
TypeTextFromFile,
} from "./instructions";
} from './instructions';
import {
mkdirIfNotExists,
writeFileAsync,
timeout,
getDelay,
readFileAsync,
} from "./utils";
import { setAwaiter } from "./wait-for-input";
} from './utils';
import { setAwaiter } from './wait-for-input';

export const typeTextFromFile = async (
instruction: TypeTextFromFile
Expand All @@ -27,16 +27,16 @@ export const typeTextFromFile = async (
}

const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
const path = join(workspaceFolder, ".presentation-buddy", instruction.path);
const path = join(workspaceFolder, '.presentation-buddy', instruction.path);

const text = await readFileAsync(path);
const data = Array.from(text.split("\r\n").join("\n"));
const data = Array.from(text.split('\r\n').join('\n'));

await typeTextIntoActiveTextEditor(data, instruction.delay);
};

export const typeText = async (instruction: TypeText): Promise<void> => {
const data = Array.from(instruction.text.join("\n"));
const data = Array.from(instruction.text.join('\n'));

await typeTextIntoActiveTextEditor(data, instruction.delay);
};
Expand Down Expand Up @@ -64,7 +64,7 @@ const typeTextIntoActiveTextEditor = async (
editor.selection = new Selection(pos, pos);

editBuilder.insert(editor.selection.active, char!);
if (char === "\n") {
if (char === '\n') {
pos = new Position(pos.line + 1, pos.character);
} else {
pos = new Position(pos.line, pos.character + 1);
Expand Down Expand Up @@ -92,7 +92,7 @@ export const openFile = async (instruction: OpenFile): Promise<void> => {

const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
const uri = Uri.file(join(workspaceFolder, instruction.path));
await commands.executeCommand("vscode.open", uri);
await commands.executeCommand('vscode.open', uri);
await timeout(getDelay());
};

Expand All @@ -107,7 +107,7 @@ export const createFile = async (instruction: CreateFile): Promise<void> => {
await mkdirIfNotExists(dirname(path));
await writeFileAsync(path);
const uri = Uri.file(join(workspaceFolder, instruction.path));
await commands.executeCommand("vscode.open", uri);
await commands.executeCommand('vscode.open', uri);
await timeout(getDelay());
};

Expand All @@ -128,17 +128,17 @@ export const wait = (instruction: Wait): Promise<void> => {
return new Promise(async (resolve, reject) => {
if (instruction.save) {
await command({
type: "command",
command: "workbench.action.files.saveAll",
type: 'command',
command: 'workbench.action.files.saveAll',
args: [],
repeat: 1,
});
}

if (typeof instruction.delay === "number") {
if (typeof instruction.delay === 'number') {
await timeout(instruction.delay);
resolve();
} else if (instruction.delay === "manual") {
} else if (instruction.delay === 'manual') {
setAwaiter(() => {
resolve();
});
Expand Down
18 changes: 10 additions & 8 deletions src/presentation-buddy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { window, workspace } from 'vscode';
import { join } from 'path';
import { readFile, writeFile } from 'jsonfile';
import { jsonc } from 'jsonc';

import { Instruction, InstructionHandler } from './instructions';
import * as instructionHandlers from './instruction-handlers';
Expand All @@ -12,7 +12,7 @@ export const init = async () => {
}
const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;

const json = await readFile(
const json = await jsonc.read(
join(__dirname, '..', 'examples', 'init', 'instructions.json')
);

Expand All @@ -21,7 +21,7 @@ export const init = async () => {

await mkdirIfNotExists(dir);

await writeFile(fileName, json, { spaces: 2 });
await jsonc.write(fileName, json, { space: 2 });
};

export const start = async () => {
Expand Down Expand Up @@ -52,13 +52,15 @@ export const start = async () => {
console.log(instructions);
};


async function loadInstructions(
workspaceFolder: string
): Promise<(Instruction)[]> {
const instructions: Instruction[] = await readFile(
join(workspaceFolder, '.presentation-buddy', 'instructions.json')
): Promise<Instruction[]> {
const path = join(
workspaceFolder,
'.presentation-buddy',
'instructions.json'
);
const instructions: Instruction[] = await jsonc.read(path);

return instructions.filter(instruction => !instruction.skip);
return instructions.filter((instruction) => !instruction.skip);
}
Loading

0 comments on commit b623822

Please sign in to comment.