diff --git a/src/extension.ts b/src/extension.ts index 20423df..e89a7c6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,15 +1,20 @@ import * as vscode from 'vscode'; import os from 'os'; import path from 'path'; +import fs from 'fs'; import testCode from './tester'; let resetTimer = () => {} const speedCodePath = path.join(os.homedir(), ".speed-code/"); +if (!fs.existsSync(speedCodePath)) { + fs.mkdirSync(speedCodePath); +} + export async function openAndFocusFile(filePath: string) { - const document = await vscode.workspace.openTextDocument(filePath); - await vscode.window.showTextDocument(document, { preview: false, preserveFocus: false }); + const document = await vscode.workspace.openTextDocument(filePath); + await vscode.window.showTextDocument(document, { preview: false, preserveFocus: false }); } const userCode = ` @@ -19,19 +24,6 @@ function isPalindrome(s) { `; export function activate(context: vscode.ExtensionContext) { - const [isValid, res] = testCode(userCode, [{ - input: ["racecar"], - expected: true - },{ - input: ["hello"], - expected: false - },{ - input: ["madam"], - expected: true - },]); - - vscode.window.showInformationMessage('Speedrun info: ' + JSON.stringify(res)); - const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100); statusBarItem.tooltip = 'Speedrun timer'; statusBarItem.text = "No active Speedrun"; @@ -49,6 +41,26 @@ export function activate(context: vscode.ExtensionContext) { resetTimer(); context.subscriptions.push(vscode.commands.registerCommand('speed-code.startSpeedRun', () => { + const runFilePath = path.join(speedCodePath, 'isPalindrome.js'); + fs.writeFileSync(runFilePath, ''); + + openAndFocusFile(runFilePath); + + setTimeout(() => { + const [isValid, res] = testCode(fs.readFileSync(runFilePath).toString(), [{ + input: ["racecar"], + expected: true + },{ + input: ["hello"], + expected: false + },{ + input: ["madam"], + expected: true + },]); + + vscode.window.showInformationMessage('Speedrun info: ' + JSON.stringify(res) + 'Summary: ' + isValid); + }, 2000); + const startTime = Date.now(); timer = setInterval(() => { const currentTime = Date.now(); diff --git a/src/tester.ts b/src/tester.ts index ba7fb00..1d757a7 100644 --- a/src/tester.ts +++ b/src/tester.ts @@ -38,5 +38,5 @@ export default function testCode(code: string, testCases: TestCase[]): [boolean, } }); - return [tests.every(t => t), tests]; + return [tests.every(t => t.valid), tests]; }