From 7dddd9a0d02ba7b688bb0e6d216797f95ee00394 Mon Sep 17 00:00:00 2001 From: shmck Date: Sat, 9 May 2020 17:20:22 -0700 Subject: [PATCH] load tutorial from env url on startup Signed-off-by: shmck --- package.json | 2 ++ src/channel/index.ts | 19 ++++++++++++++++--- src/environment.ts | 4 +++- web-app/src/services/state/machine.ts | 4 ++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 7d9e359c..ded21022 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@types/jest": "^25.2.1", "@types/jsdom": "^16.2.1", "@types/node": "^13.13.4", + "@types/node-fetch": "^2.5.7", "@types/semver": "^7.1.0", "@typescript-eslint/eslint-plugin": "^2.29.0", "@typescript-eslint/parser": "^2.29.0", @@ -50,6 +51,7 @@ "git-url-parse": "^11.1.2", "jest": "^25.4.0", "jsdom": "^16.2.2", + "node-fetch": "^2.6.0", "prettier": "^2.0.5", "semver": "^7.3.2", "ts-jest": "^25.4.0", diff --git a/src/channel/index.ts b/src/channel/index.ts index fd7c09cf..ec38dd62 100644 --- a/src/channel/index.ts +++ b/src/channel/index.ts @@ -2,6 +2,7 @@ import * as T from 'typings' import * as TT from 'typings/tutorial' import * as E from 'typings/error' import * as vscode from 'vscode' +import fetch from 'node-fetch' import { satisfies } from 'semver' import saveCommit from '../actions/saveCommit' import { setupActions, solutionActions } from '../actions/setupActions' @@ -15,7 +16,7 @@ import { readFile } from 'fs' import { join } from 'path' import { promisify } from 'util' import { showOutput } from '../services/testRunner/output' -import { WORKSPACE_ROOT } from '../environment' +import { WORKSPACE_ROOT, TUTORIAL_URL } from '../environment' const readFileAsync = promisify(readFile) @@ -52,8 +53,8 @@ class Channel implements Channel { case 'EDITOR_STARTUP': try { // check if a workspace is open, otherwise nothing works - const noActiveWorksapce = !WORKSPACE_ROOT.length - if (noActiveWorksapce) { + const noActiveWorkspace = !WORKSPACE_ROOT.length + if (noActiveWorkspace) { const error: E.ErrorMessage = { type: 'NoWorkspaceFound', message: '', @@ -73,6 +74,18 @@ class Channel implements Channel { sessionId: vscode.env.sessionId, } + // load tutorial from url + if (TUTORIAL_URL) { + try { + const tutorialRes = await fetch(TUTORIAL_URL) + const tutorial = await tutorialRes.json() + this.send({ type: 'START_TUTORIAL_FROM_URL', payload: { tutorial } }) + return + } catch (e) { + console.log(`Failed to load tutorial from url ${TUTORIAL_URL} with error "${e.message}"`) + } + } + // continue from tutorial from local storage const tutorial: TT.Tutorial | null = this.context.tutorial.get() diff --git a/src/environment.ts b/src/environment.ts index 6fd41f4d..23ff02fb 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -10,7 +10,7 @@ export type Env = 'test' | 'local' | 'development' | 'production' export const NODE_ENV: Env = process.env.NODE_ENV || 'production' // toggle logging in development -export const LOG = true +export const LOG = false // error logging tool export const SENTRY_DSN: string | null = null @@ -33,3 +33,5 @@ const supportedOS = [ if (!supportedOS.includes(OS_PLATFORM)) { throw new Error(`OS ${OS_PLATFORM}" not supported with CodeRoad`) } + +export const TUTORIAL_URL: string | null = process.env.CODEROAD_TUTORIAL_URL || null diff --git a/web-app/src/services/state/machine.ts b/web-app/src/services/state/machine.ts index ccf7589a..c9f42c04 100644 --- a/web-app/src/services/state/machine.ts +++ b/web-app/src/services/state/machine.ts @@ -70,6 +70,10 @@ export const createMachine = (options: any) => { target: 'Start', actions: ['setStart'], }, + START_TUTORIAL_FROM_URL: { + target: 'SetupNewTutorial', + actions: ['setTutorialContext'], + }, // TODO: handle completed tutorial differently TUTORIAL_ALREADY_COMPLETE: { target: 'Start',