|
| 1 | +import*asvscodefrom'vscode' |
| 2 | +import*asCRfrom'typings' |
| 3 | + |
| 4 | +importfetchfrom'../utils/fetch' |
| 5 | +importtutorialSetupfrom'../services/tutorialSetup' |
| 6 | +import{loadProgressPosition}from'../services/position' |
| 7 | +import*asstoragefrom'../services/storage' |
| 8 | +importrootSetupfrom'../services/rootSetup' |
| 9 | +import{isEmptyWorkspace,openReadme}from'../utils/workspace' |
| 10 | +import*asgitfrom'../services/git' |
| 11 | + |
| 12 | +/* |
| 13 | +new |
| 14 | +if current workspace is empty, use it |
| 15 | +if not, open a new folder then start |
| 16 | +*/ |
| 17 | + |
| 18 | +asyncfunctioncontinueTutorial(){ |
| 19 | +// TODO: verify that tutorial is loaded in workspace |
| 20 | +// TODO: verify progress |
| 21 | +// TODO: verify setup |
| 22 | +awaitloadProgressPosition() |
| 23 | +awaitopenReadme() |
| 24 | +} |
| 25 | + |
| 26 | +asyncfunctionnewTutorial(tutorial:CR.Tutorial){ |
| 27 | +// if workspace isn't empty, clear it out if given permission |
| 28 | +constisEmpty:boolean=awaitisEmptyWorkspace() |
| 29 | +if(!isEmpty){ |
| 30 | +// eslint-disable-next-line |
| 31 | +constoptions=['Open a new folder','I\'ll clear the files and folders myself'] |
| 32 | +constshouldOpenFolder=awaitvscode.window.showQuickPick(options) |
| 33 | +if(shouldOpenFolder===options[0]){ |
| 34 | +awaitvscode.commands.executeCommand('vscode.openFolder') |
| 35 | +} |
| 36 | +} |
| 37 | + |
| 38 | +awaittutorialSetup(tutorial) |
| 39 | +awaitopenReadme() |
| 40 | +} |
| 41 | + |
| 42 | +functiononSaveHook(languageIds:string[]){ |
| 43 | +// trigger command on save |
| 44 | +vscode.workspace.onDidSaveTextDocument((document:vscode.TextDocument)=>{ |
| 45 | +if(languageIds.includes(document.languageId)&&document.uri.scheme==='file'){ |
| 46 | +// do work |
| 47 | +vscode.commands.executeCommand('coderoad.test_run') |
| 48 | +} |
| 49 | +}) |
| 50 | +} |
| 51 | + |
| 52 | +asyncfunctionvalidateCanContinue():Promise<boolean>{ |
| 53 | +// validate tutorial & progress found in local storage |
| 54 | +// validate git is setup with a remote |
| 55 | +const[tutorial,progress,hasGit,hasGitRemote]=awaitPromise.all([ |
| 56 | +storage.getTutorial(), |
| 57 | +storage.getProgress(), |
| 58 | +git.gitVersion(), |
| 59 | +git.gitCheckRemoteExists(), |
| 60 | +]) |
| 61 | +return!!(tutorial&&progress&&hasGit&&hasGitRemote) |
| 62 | +} |
| 63 | + |
| 64 | +exportdefaultasyncfunctiontutorialLoad(context:vscode.ExtensionContext):Promise<void>{ |
| 65 | +// setup connection to workspace |
| 66 | +awaitrootSetup(context) |
| 67 | + |
| 68 | +constmodes=['New'] |
| 69 | + |
| 70 | +constcanContinue=awaitvalidateCanContinue() |
| 71 | +if(canContinue){ |
| 72 | +modes.push('Continue') |
| 73 | +} |
| 74 | + |
| 75 | +constselectedMode:string|undefined=awaitvscode.window.showQuickPick(modes) |
| 76 | + |
| 77 | +if(!selectedMode){ |
| 78 | +thrownewError('No mode selected') |
| 79 | +return |
| 80 | +} |
| 81 | + |
| 82 | +interfaceTutorialQuickPickItemextendsvscode.QuickPickItem{ |
| 83 | +id:string |
| 84 | +} |
| 85 | + |
| 86 | +// load tutorial summaries |
| 87 | +consttutorialsData:{[id:string]:CR.TutorialSummary}=awaitfetch({ |
| 88 | +resource:'getTutorialsSummary', |
| 89 | +}) |
| 90 | +constselectableTutorials:TutorialQuickPickItem[]=Object.keys(tutorialsData).map(id=>{ |
| 91 | +consttutorial=tutorialsData[id] |
| 92 | +return{ |
| 93 | +label:tutorial.title, |
| 94 | +description:tutorial.description, |
| 95 | +// detail: '', // optional additional info |
| 96 | + id, |
| 97 | +} |
| 98 | +}) |
| 99 | +constselectedTutorial:TutorialQuickPickItem|undefined=awaitvscode.window.showQuickPick(selectableTutorials) |
| 100 | + |
| 101 | +if(!selectedTutorial){ |
| 102 | +thrownewError('No tutorial selected') |
| 103 | +} |
| 104 | + |
| 105 | +// load specific tutorial |
| 106 | +consttutorial:CR.Tutorial|undefined=awaitfetch({ |
| 107 | +resource:'getTutorial', |
| 108 | +params:{id:selectedTutorial.id}, |
| 109 | +}) |
| 110 | + |
| 111 | +if(!tutorial){ |
| 112 | +thrownewError('No tutorial found') |
| 113 | +} |
| 114 | + |
| 115 | +switch(selectedMode){ |
| 116 | +// new tutorial |
| 117 | +casemodes[0]: |
| 118 | +awaitnewTutorial(tutorial) |
| 119 | +break |
| 120 | +// continue |
| 121 | +casemodes[1]: |
| 122 | +awaitcontinueTutorial() |
| 123 | +break |
| 124 | +} |
| 125 | + |
| 126 | +// setup hook to run tests on save |
| 127 | +onSaveHook(tutorial.meta.languages) |
| 128 | + |
| 129 | +// TODO: start |
| 130 | +} |