8000 Merge pull request #249 from coderoad/fix/cr-fail-on-startup · liuderchi/coderoad-vscode@91c6267 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91c6267

Browse files
authored
Merge pull request coderoad#249 from coderoad/fix/cr-fail-on-startup
Fix/cr fail on startup
2 parents 6701495 + 15f619c commit 91c6267

File tree

20 files changed

+91
-97
lines changed

20 files changed

+91
-97
lines changed

errors/NoWorkspaceFound.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Open a Workspace Folder
2+
3+
CodeRoad requires a workspace folder to run. Open a new workspace and re-launch CodeRoad.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coderoad",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "Play interactive coding tutorials in your editor",
55
"keywords": [
66
"tutorial",
@@ -26,7 +26,6 @@
2626
"scripts": {
2727
"build": "./scripts/build.sh",
2828
"postinstall": "node ./node_modules/vscode/bin/install",
29-
"publish": "vsce publish -p $PERSONAL_ACCESS_TOKEN --packagePath ./releases/coderoad-$npm_package_version.vsix --baseContentUrl https://github.com/coderoad/coderoad-vscode/blob/master --baseImagesUrl https://github.com/coderoad/coderoad-vscode/blob/master",
3029
"lint": "eslint src/**/*ts",
3130
"package": "./scripts/package.sh",
3231
"storybook": "cd web-app && npm run storybook",

src/actions/setupActions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import * as T from 'typings'
22
import * as TT from 'typings/tutorial'
3-
import * as vscode from 'vscode'
43
import * as git from '../services/git'
54
import loadWatchers from './utils/loadWatchers'
65
import openFiles from './utils/openFiles'
76
import runCommands from './utils/runCommands'
87
import onError from '../services/sentry/onError'
98

109
const setupActions = async (
11-
workspaceRoot: vscode.WorkspaceFolder,
1210
actions: TT.StepActions,
1311
send: (action: T.Action) => void, // send messages to client
1412
): Promise<void> => {
@@ -26,7 +24,7 @@ const setupActions = async (
2624
openFiles(files || [])
2725

2826
// 3. start file watchers
29-
loadWatchers(watchers || [], workspaceRoot.uri)
27+
loadWatchers(watchers || [])
3028

3129
// 4. run command
3230
await runCommands(commands || [], send).catch(onError)

src/actions/solutionActions.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import * as T from 'typings'
22
import * as TT from 'typings/tutorial'
3-
import * as vscode from 'vscode'
43
import * as git from '../services/git'
54
import setupActions from './setupActions'
65
import onError from '../services/sentry/onError'
76

8-
const solutionActions = async (
9-
workspaceRoot: vscode.WorkspaceFolder,
10-
stepActions: TT.StepActions,
11-
send: (action: T.Action) => void,
12-
): Promise<void> => {
7+
const solutionActions = async (stepActions: TT.StepActions, send: (action: T.Action) => void): Promise<void> => {
138
await git.clear()
14-
return setupActions(workspaceRoot, stepActions, send).catch(onError)
9+
return setupActions(stepActions, send).catch(onError)
1510
}
1611

1712
export default solutionActions

src/actions/utils/loadWatchers.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as chokidar from 'chokidar'
22
import * as vscode from 'vscode'
33
import { COMMANDS } from '../../editor/commands'
4+
import environment from '../../environment'
45

56
// NOTE: vscode createFileWatcher doesn't seem to detect changes outside of vscode
67
// such as `npm install` of a package. Went with chokidar instead
@@ -13,7 +14,7 @@ const disposeWatcher = (watcher: string) => {
1314
delete watcherObject[watcher]
1415
}
1516

16-
const loadWatchers = (watchers: string[], workspaceUri: vscode.Uri) => {
17+
const loadWatchers = (watchers: string[]) => {
1718
if (!watchers.length) {
1819
// remove all watchers
1920
for (const watcher of Object.keys(watcherObject)) {
@@ -24,13 +25,8 @@ const loadWatchers = (watchers: string[], workspaceUri: vscode.Uri) => {
2425
if (!watcherObject[watcher]) {
2526
// see how glob patterns are used in VSCode (not like a regex)
2627
// https://code.visualstudio.com/api/references/vscode-api#GlobPattern
27-
const rootUri = vscode.workspace.getWorkspaceFolder(workspaceUri)
28-
if (!rootUri) {
29-
return
30-
}
31-
3228
const fsWatcher: chokidar.FSWatcher = chokidar.watch(watcher, {
33-
cwd: rootUri.uri.path,
29+
cwd: environment.WORKSPACE_ROOT,
3430
interval: 1000,
3531
})
3632

src/actions/utils/runCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as T from 'typings'
2-
import node from '../../services/node'
2+
import { exec } from '../../services/node'
33

44
const runCommands = async (commands: string[], send: (action: T.Action) => void) => {
55
if (!commands.length) {
@@ -13,7 +13,7 @@ const runCommands = async (commands: string[], send: (action: T.Action) => void)
1313
send({ type: 'COMMAND_START', payload: { process: { ...process, status: 'RUNNING' } } })
1414
let result: { stdout: string; stderr: string }
1515
try {
16-
result = await node.exec(command)
16+
result = await exec(command)
1717
} catch (error) {
1818
console.log(error)
1919
send({ type: 'COMMAND_FAIL', payload: { process: { ...process, status: 'FAIL' } } })

src/channel/index.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { openWorkspace, checkWorkspaceEmpty } from '../services/workspace'
1414
import { readFile } from 'fs'
1515
import { join } from 'path'
1616
import { promisify } from 'util'
17-
import { compare } from 'semver'
17+
import environment from '../environment'
1818

1919
const readFileAsync = promisify(readFile)
2020

@@ -26,18 +26,15 @@ interface Channel {
2626
interface ChannelProps {
2727
postMessage: (action: T.Action) => Thenable<boolean>
2828
workspaceState: vscode.Memento
29-
workspaceRoot: vscode.WorkspaceFolder
3029
}
3130

3231
class Channel implements Channel {
3332
private postMessage: (action: T.Action) => Thenable<boolean>
3433
private workspaceState: vscode.Memento
35-
private workspaceRoot: vscode.WorkspaceFolder
3634
private context: Context
37-
constructor({ postMessage, workspaceState, workspaceRoot }: ChannelProps) {
35+
constructor({ postMessage, workspaceState }: ChannelProps) {
3836
// workspaceState used for local storage
3937
this.workspaceState = workspaceState
40-
this.workspaceRoot = workspaceRoot
4138
this.postMessage = postMessage
4239
this.context = new Context(workspaceState)
4340
}
@@ -52,6 +49,22 @@ class Channel implements Channel {
5249

5350
switch (actionType) {
5451
case 'EDITOR_ENV_GET':
52+
// check if a workspace is open, otherwise nothing works
53+
const noActiveWorksapce = !environment.WORKSPACE_ROOT.length
54+
if (noActiveWorksapce) {
55+
const error: E.ErrorMessage = {
56+
type: 'NoWorkspaceFound',
57+
message: '',
58+
actions: [
59+
{
60+
label: 'Open Workspace',
61+
transition: 'REQUEST_WORKSPACE',
62+
},
63+
],
64+
}
65+
this.send({ type: 'NO_WORKSPACE', payload: { error } })
66+
return
67+
}
5568
this.send({
5669
type: 'ENV_LOAD',
5770
payload: {
@@ -180,8 +193,8 @@ class Channel implements Channel {
180193
vscode.commands.executeCommand(COMMANDS.SET_CURRENT_STEP, action.payload)
181194
return
182195
case 'EDITOR_VALIDATE_SETUP':
183-
// 1. check workspace is selected
184-
const isEmptyWorkspace = await checkWorkspaceEmpty(this.workspaceRoot.uri.path)
196+
// check workspace is selected
197+
const isEmptyWorkspace = await checkWorkspaceEmpty()
185198
if (!isEmptyWorkspace) {
186199
const error: E.ErrorMessage = {
187200
type: 'WorkspaceNotEmpty',
@@ -200,7 +213,7 @@ class Channel implements Channel {
200213
this.send({ type: 'VALIDATE_SETUP_FAILED', payload: { error } })
201214
return
202215
}
203-
// 2. check Git is installed.
216+
// check Git is installed.
204217
// Should wait for workspace before running otherwise requires access to root folder
205218
const isGitInstalled = await version('git')
206219
if (!isGitInstalled) {
@@ -225,11 +238,11 @@ class Channel implements Channel {
225238
// load step actions (git commits, commands, open files)
226239
case 'SETUP_ACTIONS':
227240
await vscode.commands.executeCommand(COMMANDS.SET_CURRENT_STEP, action.payload)
228-
setupActions(this.workspaceRoot, action.payload, this.send)
241+
setupActions(action.payload, this.send)
229242
return
230243
// load solution step actions (git commits, commands, open files)
231244
case 'SOLUTION_ACTIONS':
232-
await solutionActions(this.workspaceRoot, action.payload, this.send)
245+
await solutionActions(action.payload, this.send)
233246
// run test following solution to update position
234247
vscode.commands.executeCommand(COMMANDS.RUN_TEST, action.payload)
235248
return

src/editor/commands.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ export const COMMANDS = {
1414
interface CreateCommandProps {
1515
extensionPath: string
1616
workspaceState: vscode.Memento
17-
workspaceRoot: vscode.WorkspaceFolder
1817
}
1918

20-
export const createCommands = ({ extensionPath, workspaceState, workspaceRoot }: CreateCommandProps) => {
19+
export const createCommands = ({ extensionPath, workspaceState }: CreateCommandProps) => {
2120
// React panel webview
2221
let webview: any
2322
let currentStepId = ''
@@ -41,7 +40,6 @@ export const createCommands = ({ extensionPath, workspaceState, workspaceRoot }:
4140
webview = createWebView({
4241
extensionPath,
4342
workspaceState,
44-
workspaceRoot,
4543
})
4644
},
4745
// open React webview

src/editor/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,10 @@ class Editor {
2828
}
2929

3030
private activateCommands = (): void => {
31-
// set workspace root for node executions
32-
const workspaceRoots: vscode.WorkspaceFolder[] | undefined = vscode.workspace.workspaceFolders
33-
if (!workspaceRoots || !workspaceRoots.length) {
34-
throw new Error('No workspace root path')
35-
}
36-
const workspaceRoot: vscode.WorkspaceFolder = workspaceRoots[0]
37-
3831
const commands = createCommands({
3932
extensionPath: this.vscodeExt.extensionPath,
4033
// NOTE: local storage must be bound to the vscodeExt.workspaceState
4134
workspaceState: this.vscodeExt.workspaceState,
42-
workspaceRoot,
4335
})
4436

4537
// register commands

0 commit comments

Comments
 (0)
0