Skip to content

Commit 90c054b

Browse files
authored
Merge pull request #5 from ShMcK/feature/new-continue
Feature/new continue
2 parents bf04c34 + 7c2bd8f commit 90c054b

File tree

27 files changed

+243
-275
lines changed

27 files changed

+243
-275
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"compile": "tsc -p ./",
3333
"watch": "tsc -watch -p ./",
3434
"postinstall": "node ./node_modules/vscode/bin/install",
35+
"storybook": "cd web-app && npm run storybook",
3536
"test": "npm run build && node ./node_modules/vscode/bin/test"
3637
},
3738
"devDependencies": {

src/editor/commands/index.ts

+25-18
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import * as vscode from 'vscode'
22
import { join } from 'path'
33
import { setStorage } from '../storage'
44
import ReactWebView from '../ReactWebView'
5+
import { isEmptyWorkspace } from '../workspace'
56
import * as CR from 'typings'
67

78
const COMMANDS = {
89
START: 'coderoad.start',
9-
NEW_OR_CONTINUE: 'coderoad.new_or_continue',
10+
TUTORIAL_LAUNCH: 'coderoad.tutorial_launch',
1011
OPEN_WEBVIEW: 'coderoad.open_webview',
1112
SEND_STATE: 'coderoad.send_state',
1213
SEND_DATA: 'coderoad.send_data',
@@ -20,12 +21,13 @@ interface CreateCommandProps {
2021
machine: CR.StateMachine,
2122
storage: any,
2223
git: any
24+
position: any
2325
}
2426

2527
// React panel webview
2628
let webview: any;
2729

28-
export const createCommands = ({ context, machine, storage, git }: CreateCommandProps) => ({
30+
export const createCommands = ({ context, machine, storage, git, position }: CreateCommandProps) => ({
2931
// initialize
3032
[COMMANDS.START]: () => {
3133
// set local storage workspace
@@ -36,25 +38,30 @@ export const createCommands = ({ context, machine, storage, git }: CreateCommand
3638
console.log('webview', webview.panel.webview.postMessage)
3739
machine.activate()
3840
},
39-
[COMMANDS.NEW_OR_CONTINUE]: async () => {
40-
// verify that the user has a tutorial & progress
41-
// verify git is setup with a coderoad remote
42-
const [tutorial, progress, hasGit, hasGitRemote] = await Promise.all([
43-
storage.getTutorial(),
44-
storage.getProgress(),
45-
git.gitVersion(),
46-
git.gitCheckRemoteExists(),
47-
])
48-
const canContinue = !!(tutorial && progress && hasGit && hasGitRemote)
49-
console.log('canContinue', canContinue)
50-
// if a tutorial exists, 'CONTINUE'
51-
// otherwise start from 'NEW'
52-
machine.send(canContinue ? 'CONTINUE' : 'NEW')
53-
},
5441
// open React webview
5542
[COMMANDS.OPEN_WEBVIEW]: (column: number = vscode.ViewColumn.One) => {
5643
webview.createOrShow(column);
5744
},
45+
// launch a new tutorial
46+
// NOTE: may be better to move into action as logic is primarily non-vscode
47+
[COMMANDS.TUTORIAL_LAUNCH]: async (tutorial: CR.Tutorial) => {
48+
console.log('launch tutorial')
49+
50+
await isEmptyWorkspace()
51+
52+
await git.gitInitIfNotExists()
53+
54+
// TODO: use actual tutorial repo
55+
await Promise.all([git.gitSetupRemote(tutorial.meta.repo), storage.setTutorial(tutorial), storage.resetProgress()])
56+
57+
// TODO: refactor to allow client to call initialization
58+
const pos: CR.Position = await position.getInitial(tutorial)
59+
60+
// eslint-disable-next-line
61+
const { steps } = tutorial.data
62+
const { setup } = steps[pos.stepId].actions
63+
await git.gitLoadCommits(setup)
64+
},
5865
// open a file
5966
[COMMANDS.OPEN_FILE]: async (relativeFilePath: string) => {
6067
console.log(`OPEN_FILE ${JSON.stringify(relativeFilePath)}`)
@@ -79,6 +86,6 @@ export const createCommands = ({ context, machine, storage, git }: CreateCommand
7986
},
8087
[COMMANDS.RECEIVE_ACTION]: (action: string | CR.Action) => {
8188
console.log('onReceiveAction', action)
82-
machine.onReceive(action)
89+
machine.send(action)
8390
}
8491
})

src/editor/commands/start-old.ts

-108
This file was deleted.

src/editor/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as CR from 'typings'
33
import { createCommands } from './commands'
44
import * as storage from '../services/storage'
55
import * as git from '../services/git'
6+
import * as position from '../services/position'
67

78
interface Props {
89
machine: CR.StateMachine,
@@ -33,6 +34,7 @@ class Editor {
3334
machine: this.machine,
3435
storage,
3536
git,
37+
position,
3638
})
3739
for (const cmd in commands) {
3840
const command: vscode.Disposable = vscode.commands.registerCommand(cmd, commands[cmd])

src/services/api/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as CR from 'typings'
22

33
// temporary tutorials
4-
import basicTutorial from '../../state/context/tutorials/basic'
4+
import basicTutorial from 'tutorials/basic'
55

66
interface Options {
77
resource: string

src/state/actions/index.ts

+33-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { assign } from 'xstate'
2+
// NOTE: codesmell - importing machine
3+
import { machine } from '../../extension'
4+
import api from '../../services/api'
25
import * as CR from 'typings'
36
import * as vscode from 'vscode'
7+
import * as storage from '../../services/storage'
8+
import * as git from '../../services/git'
49

510
let initialTutorial: CR.Tutorial | undefined
611
let initialProgress: CR.Progress = {
@@ -11,7 +16,34 @@ let initialProgress: CR.Progress = {
1116
}
1217

1318
export default {
14-
tutorialLoad: assign({
19+
createWebview() {
20+
console.log('execute coderoad.open_webview')
21+
vscode.commands.executeCommand('coderoad.open_webview')
22+
},
23+
async newOrContinue() {
24+
// verify that the user has a tutorial & progress
25+
// verify git is setup with a coderoad remote
26+
const [tutorial, progress, hasGit, hasGitRemote] = await Promise.all([
27+
storage.getTutorial(),
28+
storage.getProgress(),
29+
git.gitVersion(),
30+
git.gitCheckRemoteExists(),
31+
])
32+
const canContinue = !!(tutorial && progress && hasGit && hasGitRemote)
33+
34+
if (canContinue) {
35+
initialTutorial = tutorial
36+
initialProgress = progress
37+
}
38+
39+
machine.send(canContinue ? 'CONTINUE' : 'NEW')
40+
},
41+
async tutorialLaunch() {
42+
// TODO: add selection of tutorial id
43+
const tutorial: CR.Tutorial = await api({ resource: 'getTutorial', params: { id: '1' } })
44+
vscode.commands.executeCommand('coderoad.tutorial_launch', tutorial)
45+
},
46+
tutorialContinue: assign({
1547
// load initial data, progress & position
1648
data(): CR.TutorialData {
1749
console.log('ACTION: tutorialLoad.data')
@@ -45,11 +77,4 @@ export default {
4577
return position
4678
}
4779
}),
48-
createWebview() {
49-
console.log('execute coderoad.open_webview')
50-
vscode.commands.executeCommand('coderoad.open_webview')
51-
},
52-
newOrContinue() {
53-
vscode.commands.executeCommand('coderoad.new_or_continue')
54-
}
5580
}

src/state/context/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import basicTutorialData from './tutorials/basic'
1+
import basicTutorialData from 'tutorials/basic'
22
import * as CR from 'typings'
33

44
const tutorialContext: CR.MachineContext = {

src/state/index.ts

-6
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ class StateMachine {
3636
this.service.stop()
3737
}
3838
send(action: string | CR.Action) {
39-
console.log('machine.send')
40-
console.log(action)
41-
this.service.send(action)
42-
}
43-
onReceive(action: string | CR.Action) {
44-
console.log(action)
4539
this.service.send(action)
4640
}
4741
}

src/state/machine.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const machine = Machine<
1111
CR.MachineEvent
1212
>(
1313
{
14-
id: 'tutorial',
14+
id: 'root',
1515
context: initialContext,
1616
initial: 'SelectTutorial',
1717
states: {
@@ -40,28 +40,28 @@ export const machine = Machine<
4040
},
4141
},
4242
InitializeTutorial: {
43+
onEntry: ['tutorialLaunch'],
4344
on: {
44-
TUTORIAL_LOADED: 'Tutorial'
45+
TUTORIAL_LOADED: '#tutorial'
4546
}
4647
},
4748
}
48-
4949
},
5050
ContinueTutorial: {
51-
onEntry: 'tutorialLoad',
51+
onEntry: ['tutorialContinue'],
5252
on: {
53-
TUTORIAL_START: {
54-
target: 'Tutorial.LoadNext',
55-
}
53+
TUTORIAL_START: '#tutorial-load-next'
5654
}
5755
},
5856
}
5957
},
6058
Tutorial: {
59+
id: 'tutorial',
6160
initial: 'Summary',
6261
states: {
6362
LoadNext: {
64-
onEntry: () => send('LOAD_NEXT'),
63+
id: 'tutorial-load-next',
64+
// onEntry: [() => send('LOAD_NEXT')],
6565
on: {
6666
LOAD_NEXT: [
6767
{
@@ -144,7 +144,7 @@ export const machine = Machine<
144144
cond: 'hasNextLevel',
145145
},
146146
{
147-
target: 'EndTutorial',
147+
target: '#root.Tutorial.EndTutorial',
148148
},
149149
],
150150
},

src/state/message.ts

-13
This file was deleted.

tsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dom"
99
],
1010
"sourceMap": true,
11-
"rootDir": "src",
11+
"rootDirs": ["src", "tutorials"],
1212
"baseUrl": "src",
1313
"strict": true, /* enable all strict type-checking options */
1414
/* Additional Checks */
@@ -23,6 +23,9 @@
2323
"emitDecoratorMetadata": true,
2424
"paths": {
2525
"typings": ["../typings/index.d.ts"],
26+
"tutorials/basic": [
27+
"../tutorials/basic.ts"
28+
]
2629
},
2730
},
2831
"exclude": [

0 commit comments

Comments
 (0)