|
| 1 | +import*asvscodefrom'vscode' |
| 2 | +import*asTfrom'typings' |
| 3 | +import*asTTfrom'typings/tutorial' |
| 4 | +import*asEfrom'typings/error' |
| 5 | +import{satisfies}from'semver' |
| 6 | +import{onEvent}from'../services/telemetry' |
| 7 | +import{version,compareVersions}from'../services/dependencies' |
| 8 | +importContextfrom'../services/context/context' |
| 9 | +importtutorialConfigfrom'./utils/tutorialConfig' |
| 10 | + |
| 11 | +constonTutorialConfig=async(action:T.Action,context:Context,workspaceState:vscode.Memento,send:any)=>{ |
| 12 | +try{ |
| 13 | +constdata:TT.Tutorial=action.payload.tutorial |
| 14 | + |
| 15 | +onEvent('tutorial_start',{ |
| 16 | +tutorial_id:data.id, |
| 17 | +tutorial_version:data.version, |
| 18 | +tutorial_title:data.summary.title, |
| 19 | +}) |
| 20 | + |
| 21 | +// validate extension version |
| 22 | +constexpectedAppVersion=data.config?.appVersions?.vscode |
| 23 | +if(expectedAppVersion){ |
| 24 | +constextension=vscode.extensions.getExtension('coderoad.coderoad') |
| 25 | +if(extension){ |
| 26 | +constcurrentAppVersion=extension.packageJSON.version |
| 27 | +constsatisfied=satisfies(currentAppVersion,expectedAppVersion) |
| 28 | +if(!satisfied){ |
| 29 | +consterror:E.ErrorMessage={ |
| 30 | +type:'UnmetExtensionVersion', |
| 31 | +message:`Expected CodeRoad v${expectedAppVersion}, but found v${currentAppVersion}`, |
| 32 | +} |
| 33 | +send({type:'TUTORIAL_CONFIGURE_FAIL',payload:{ error}}) |
| 34 | +return |
| 35 | +} |
| 36 | +} |
| 37 | +} |
| 38 | + |
| 39 | +// setup tutorial config (save watcher, test runner, etc) |
| 40 | +awaitcontext.setTutorial(workspaceState,data) |
| 41 | + |
| 42 | +// validate dependencies |
| 43 | +constdependencies=data.config.dependencies |
| 44 | +if(dependencies&&dependencies.length){ |
| 45 | +for(constdepofdependencies){ |
| 46 | +// check dependency is installed |
| 47 | +constcurrentVersion:string|null=awaitversion(dep.name) |
| 48 | +if(!currentVersion){ |
| 49 | +// use a custom error message |
| 50 | +consterror:E.ErrorMessage={ |
| 51 | +type:'MissingTutorialDependency', |
| 52 | +message:dep.message||`Process "${dep.name}" is required but not found. It may need to be installed`, |
| 53 | +actions:[ |
| 54 | +{ |
| 55 | +label:'Check Again', |
| 56 | +transition:'TRY_AGAIN', |
| 57 | +}, |
| 58 | +], |
| 59 | +} |
| 60 | +send({type:'TUTORIAL_CONFIGURE_FAIL',payload:{ error}}) |
| 61 | +return |
| 62 | +} |
| 63 | + |
| 64 | +// check dependency version |
| 65 | +constsatisfiedDependency=awaitcompareVersions(currentVersion,dep.version) |
| 66 | + |
| 67 | +if(!satisfiedDependency){ |
| 68 | +consterror:E.ErrorMessage={ |
| 69 | +type:'UnmetTutorialDependency', |
| 70 | +message:`Expected${dep.name} to have version${dep.version}, but found version${currentVersion}`, |
| 71 | +actions:[ |
| 72 | +{ |
| 73 | +label:'Check Again', |
| 74 | +transition:'TRY_AGAIN', |
| 75 | +}, |
| 76 | +], |
| 77 | +} |
| 78 | +send({type:'TUTORIAL_CONFIGURE_FAIL',payload:{ error}}) |
| 79 | +return |
| 80 | +} |
| 81 | + |
| 82 | +if(satisfiedDependency!==true){ |
| 83 | +consterror:E.ErrorMessage=satisfiedDependency||{ |
| 84 | +type:'UnknownError', |
| 85 | +message:`Something went wrong comparing dependency for${name}`, |
| 86 | +actions:[ |
| 87 | +{ |
| 88 | +label:'Try Again', |
| 89 | +transition:'TRY_AGAIN', |
| 90 | +}, |
| 91 | +], |
| 92 | +} |
| 93 | +send({type:'TUTORIAL_CONFIGURE_FAIL',payload:{ error}}) |
| 94 | +return |
| 95 | +} |
| 96 | +} |
| 97 | +} |
| 98 | + |
| 99 | +consterror:E.ErrorMessage|void=awaittutorialConfig({ data}).catch((error:Error)=>({ |
| 100 | +type:'UnknownError', |
| 101 | +message:`Location: tutorial config.\n\n${error.message}`, |
| 102 | +})) |
| 103 | + |
| 104 | +// has error |
| 105 | +if(error&&error.type){ |
| 106 | +send({type:'TUTORIAL_CONFIGURE_FAIL',payload:{ error}}) |
| 107 | +return |
| 108 | +} |
| 109 | + |
| 110 | +// report back to the webview that setup is complete |
| 111 | +send({type:'TUTORIAL_CONFIGURED'}) |
| 112 | +}catch(e){ |
| 113 | +consterror={ |
| 114 | +type:'UnknownError', |
| 115 | +message:`Location: EditorTutorialConfig.\n\n${e.message}`, |
| 116 | +} |
| 117 | +send({type:'TUTORIAL_CONFIGURE_FAIL',payload:{ error}}) |
| 118 | +} |
| 119 | +} |
| 120 | + |
| 121 | +exportdefaultonTutorialConfig |