|
| 1 | +import*aspathfrom'path'; |
| 2 | +import{fileExists}from'./exists'; |
| 3 | + |
| 4 | +exportfunctionsetGlobals(config:PackageJson){ |
| 5 | +window.coderoad=Object.assign(window.coderoad,{ |
| 6 | +tutorial:config.name, |
| 7 | +suffix:config.config.testSuffix.substring(config.config.testSuffix.lastIndexOf('.')+1,config.config.testSuffix.length), |
| 8 | +tutorialDir:path.join(window.coderoad.dir,'node_modules',config.name,config.config.testDir), |
| 9 | +testRunner:config.config.testRunner, |
| 10 | +testRunnerOptions:config.config.testRunnerOptions||{} |
| 11 | +}); |
| 12 | +// issues, bugs |
| 13 | +loadRepo(config); |
| 14 | +// set PackageDeps |
| 15 | +loadRunnerDep(config); |
| 16 | +} |
| 17 | + |
| 18 | +functionloadRunnerDep(config:PackageJson){ |
| 19 | +// test runner dir |
| 20 | +letflatDep=path.join(window.coderoad.dir,'node_modules',config.config.testRunner,'package.json'); |
| 21 | +lettreeDep=path.join(window.coderoad.dir,'node_modules',config.name,'node_modules',config.config.testRunner,'package.json'); |
| 22 | + |
| 23 | +varrunnerMain; |
| 24 | +varrunnerRoot; |
| 25 | +if(fileExists(flatDep)){ |
| 26 | +runnerMain=require(flatDep).main; |
| 27 | +runnerRoot=flatDep; |
| 28 | +}elseif(fileExists(treeDep)){ |
| 29 | +runnerMain=require(treeDep).main; |
| 30 | +runnerRoot=treeDep; |
| 31 | +}else{ |
| 32 | +letmessage='Error loading test runner. Post an issue. https://github.com/coderoad/atom-coderoad/issues'; |
| 33 | +console.log(message); |
| 34 | +throwmessage; |
| 35 | +} |
| 36 | + |
| 37 | +// fix main path for Windows |
| 38 | +letslash=window.coderoad.win ?'\\' :'/'; |
| 39 | +runnerMain=path.join.apply(null,runnerMain.split(slash)); |
| 40 | +// trim root path to folder |
| 41 | +runnerRoot=runnerRoot.substring(0,runnerRoot.lastIndexOf(slash)); |
| 42 | + |
| 43 | +letpathToMain=path.join(runnerRoot,runnerMain); |
| 44 | + |
| 45 | +if(!!require(pathToMain).default){ |
| 46 | +window.coderoad.runner=require(pathToMain).default; |
| 47 | +}else{ |
| 48 | +window.coderoad.runner=require(pathToMain); |
| 49 | +} |
| 50 | +} |
| 51 | + |
| 52 | +functionloadRepo(config){ |
| 53 | +if(config.bugs&&config.bugs.url){ |
| 54 | +window.coderoad.issuesPath=config.bugs.url; |
| 55 | +} |
| 56 | +if(config.repo&&config.repo.url){ |
| 57 | +letrepo:string=config.repo.url; |
| 58 | +if(!!repo.match(/\.git$/)){ |
| 59 | +repo=repo.slice(0,repo.length-4); |
| 60 | +} |
| 61 | +window.coderoad.repo=repo; |
| 62 | +} |
| 63 | + |
| 64 | +window.coderoad.edit=config.config.edit&&!!window.coderoad.repo||false; |
| 65 | +} |