diff --git a/src/actions/setupActions.ts b/src/actions/setupActions.ts index accce3e4..dbca1aad 100644 --- a/src/actions/setupActions.ts +++ b/src/actions/setupActions.ts @@ -36,7 +36,7 @@ const runCommands = async (commands: string[], language: string) => { } -const setupActions = async ({commands, commits, files}: G.StepActions): Promise => { +const setupActions = async (workspaceRoot: vscode.WorkspaceFolder, {commands, commits, files}: G.StepActions): Promise => { // run commits for (const commit of commits) { await git.loadCommit(commit) @@ -49,18 +49,7 @@ const setupActions = async ({commands, commits, files}: G.StepActions): Promise< for (const filePath of files) { console.log(`OPEN_FILE ${filePath}`) try { - // TODO: re-enable after testing - // const workspaceRoots: vscode.WorkspaceFolder[] | undefined = vscode.workspace.workspaceFolders - // if (!workspaceRoots || !workspaceRoots.length) { - // throw new Error('No workspace root path') - // } - // const rootWorkspace: vscode.WorkspaceFolder = workspaceRoots[0] - // const absoluteFilePath = join(rootWorkspace.uri.path, relativeFilePath) - const workspaceRoot = vscode.workspace.rootPath - if (!workspaceRoot) { - throw new Error('No workspace root path') - } - const absoluteFilePath = join(workspaceRoot, filePath) + const absoluteFilePath = join(workspaceRoot.uri.path, filePath) const doc = await vscode.workspace.openTextDocument(absoluteFilePath) await vscode.window.showTextDocument(doc, vscode.ViewColumn.One) // there are times when initialization leave the panel behind any files opened diff --git a/src/channel/index.ts b/src/channel/index.ts index 66a35599..e6e48598 100644 --- a/src/channel/index.ts +++ b/src/channel/index.ts @@ -17,16 +17,19 @@ interface Channel { interface ChannelProps { postMessage: (action: CR.Action) => Thenable workspaceState: vscode.Memento + workspaceRoot: vscode.WorkspaceFolder } class Channel implements Channel { private postMessage: (action: CR.Action) => Thenable private workspaceState: vscode.Memento + private workspaceRoot: vscode.WorkspaceFolder private context: Context - constructor({postMessage, workspaceState}: ChannelProps) { + constructor({postMessage, workspaceState, workspaceRoot}: ChannelProps) { // workspaceState used for local storage this.workspaceState = workspaceState + this.workspaceRoot = workspaceRoot this.postMessage = postMessage this.context = new Context(workspaceState) } @@ -96,7 +99,7 @@ class Channel implements Channel { // load step actions (git commits, commands, open files) case 'SETUP_ACTIONS': vscode.commands.executeCommand('coderoad.set_current_step', action.payload) - setupActions(action.payload) + setupActions(this.workspaceRoot, action.payload) return // load solution step actions (git commits, commands, open files) case 'SOLUTION_ACTIONS': diff --git a/src/editor/ReactWebView.ts b/src/editor/ReactWebView.ts index 5af844a6..1b406bde 100644 --- a/src/editor/ReactWebView.ts +++ b/src/editor/ReactWebView.ts @@ -16,6 +16,7 @@ const getNonce = (): string => { interface ReactWebViewProps { extensionPath: string workspaceState: vscode.Memento + workspaceRoot: vscode.WorkspaceFolder } @@ -30,7 +31,7 @@ class ReactWebView { private disposables: vscode.Disposable[] = [] private channel: Channel - public constructor({extensionPath, workspaceState}: ReactWebViewProps) { + public constructor({extensionPath, workspaceState, workspaceRoot}: ReactWebViewProps) { this.extensionPath = extensionPath // Create and show a new webview panel @@ -71,6 +72,7 @@ class ReactWebView { // channel connects webview to the editor this.channel = new Channel({ workspaceState, + workspaceRoot, postMessage: (action: Action): Thenable => { // console.log(`postMessage ${JSON.stringify(action)}`) return this.panel.webview.postMessage(action) diff --git a/src/editor/commands.ts b/src/editor/commands.ts index cc339a55..88023770 100644 --- a/src/editor/commands.ts +++ b/src/editor/commands.ts @@ -1,7 +1,6 @@ import * as vscode from 'vscode' import ReactWebView from './ReactWebView' import runTest from '../actions/runTest' -import {isEmptyWorkspace} from './workspace' const COMMANDS = { START: 'coderoad.start', @@ -13,9 +12,10 @@ const COMMANDS = { interface CreateCommandProps { extensionPath: string workspaceState: vscode.Memento + workspaceRoot: vscode.WorkspaceFolder } -export const createCommands = ({extensionPath, workspaceState}: CreateCommandProps) => { +export const createCommands = ({extensionPath, workspaceState, workspaceRoot}: CreateCommandProps) => { // React panel webview let webview: any let currentStepId = '' @@ -26,7 +26,7 @@ export const createCommands = ({extensionPath, workspaceState}: CreateCommandPro console.log('start') // TODO: replace with a prompt to open a workspace - await isEmptyWorkspace() + // await isEmptyWorkspace() let webviewState: 'INITIALIZING' | 'RESTARTING' if (!webview) { @@ -43,6 +43,7 @@ export const createCommands = ({extensionPath, workspaceState}: CreateCommandPro webview = new ReactWebView({ extensionPath, workspaceState, + workspaceRoot, }) }, // open React webview diff --git a/src/editor/index.ts b/src/editor/index.ts index 31b46fc5..0b0c507d 100644 --- a/src/editor/index.ts +++ b/src/editor/index.ts @@ -6,14 +6,8 @@ class Editor { // @ts-ignore private vscodeExt: vscode.ExtensionContext - constructor() { - // set workspace root for node executions - const rootPath = vscode.workspace.rootPath - if (!rootPath) { - throw new Error('Requires a workspace. Please open a folder') - } - } public activate = (vscodeExt: vscode.ExtensionContext): void => { + console.log('ACTIVATE!') this.vscodeExt = vscodeExt @@ -31,21 +25,19 @@ class Editor { } private activateCommands = (): void => { - // NOTE: local storage must be bound to the vscodeExt.workspaceState - // store current tutorial id & version - - - // store step progress for current tutorial - // const stepProgress = new Storage<{[stepId: string]: boolean}>({ - // key: 'coderoad:progress', - // storage: this.vscodeExt.workspaceState, - // defaultValue: {}, - // }) + // set workspace root for node executions + const workspaceRoots: vscode.WorkspaceFolder[] | undefined = vscode.workspace.workspaceFolders + if (!workspaceRoots || !workspaceRoots.length) { + throw new Error('No workspace root path') + } + const workspaceRoot: vscode.WorkspaceFolder = workspaceRoots[0] const commands = createCommands({ extensionPath: this.vscodeExt.extensionPath, + // NOTE: local storage must be bound to the vscodeExt.workspaceState workspaceState: this.vscodeExt.workspaceState, + workspaceRoot, }) // register commands diff --git a/src/editor/workspace.ts b/src/editor/workspace.ts deleted file mode 100644 index 60264d06..00000000 --- a/src/editor/workspace.ts +++ /dev/null @@ -1,43 +0,0 @@ -import * as fs from 'fs' -import * as path from 'path' -import * as vscode from 'vscode' -import node from '../services/node' - -export async function isEmptyWorkspace(): Promise { - const {stdout, stderr} = await node.exec('ls') - if (stderr) { - throw new Error('Error validating if project is empty') - } - return !stdout.length -} - -// // TODO: workspace change listener -export async function openReadme(): Promise { - const {stderr} = await node.exec('ls') - if (stderr) { - throw new Error('Error looking for initial file') - } - - const file = 'README.md' - const filePath = path.join(vscode.workspace.rootPath || '', file) - console.log('filePath', filePath) - const hasReadme = await node.exists(file) - - if (!hasReadme) { - // add readme if none exists - try { - const content = '# Welcome to CodeRoad!' - fs.writeFileSync(filePath, content, 'utf8') - } catch (error) { - throw new Error('Error writing README.md') - } - } - - try { - const openPath = vscode.Uri.parse(filePath) - const doc = await vscode.workspace.openTextDocument(openPath) - await vscode.window.showTextDocument(doc) - } catch (error) { - throw new Error('Error opening README doc') - } -} diff --git a/src/services/git/index.ts b/src/services/git/index.ts index b32f3772..d89958c1 100644 --- a/src/services/git/index.ts +++ b/src/services/git/index.ts @@ -1,4 +1,3 @@ -import * as CR from 'typings' import node from '../node' diff --git a/src/services/node/index.ts b/src/services/node/index.ts index f7859da8..5ae93b93 100644 --- a/src/services/node/index.ts +++ b/src/services/node/index.ts @@ -7,19 +7,21 @@ import * as vscode from 'vscode' const asyncExec = promisify(cpExec) class Node { - private workspaceRoot: string + private workspaceRootPath: string constructor() { - this.workspaceRoot = vscode.workspace.rootPath || '' - if (!this.workspaceRoot.length) { - throw new Error('Invalid workspaceRoot') + // set workspace root for node executions + const workspaceRoots: vscode.WorkspaceFolder[] | undefined = vscode.workspace.workspaceFolders + if (!workspaceRoots || !workspaceRoots.length) { + throw new Error('No workspace root path') } - console.log(`workspaceRoot: ${this.workspaceRoot}`) + const workspaceRoot: vscode.WorkspaceFolder = workspaceRoots[0] + this.workspaceRootPath = workspaceRoot.uri.path } public exec = (cmd: string): Promise<{stdout: string; stderr: string}> => asyncExec(cmd, { - cwd: this.workspaceRoot, + cwd: this.workspaceRootPath, }) - public exists = (...paths: string[]): boolean => fs.existsSync(join(this.workspaceRoot, ...paths)) + public exists = (...paths: string[]): boolean => fs.existsSync(join(this.workspaceRootPath, ...paths)) } export default new Node()