|
| 1 | +import * as path from 'path'; |
| 2 | +import {fileExists} from './exists'; |
| 3 | + |
| 4 | +export function setGlobals(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 | +function loadRunnerDep(config: PackageJson) { |
| 19 | + // test runner dir |
| 20 | + let flatDep = path.join(window.coderoad.dir, 'node_modules', config.config.testRunner, 'package.json'); |
| 21 | + let treeDep = path.join(window.coderoad.dir, 'node_modules', config.name, 'node_modules', config.config.testRunner, 'package.json'); |
| 22 | + |
| 23 | + var runnerMain; |
| 24 | + var runnerRoot; |
| 25 | + if (fileExists(flatDep)) { |
| 26 | + runnerMain = require(flatDep).main; |
| 27 | + runnerRoot = flatDep; |
| 28 | + } else if (fileExists(treeDep)) { |
| 29 | + runnerMain = require(treeDep).main; |
| 30 | + runnerRoot = treeDep; |
| 31 | + } else { |
| 32 | + let message = 'Error loading test runner. Post an issue. https://github.com/coderoad/atom-coderoad/issues'; |
| 33 | + console.log(message); |
| 34 | + throw message; |
| 35 | + } |
| 36 | + |
| 37 | + // fix main path for Windows |
| 38 | + let slash = 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 | + let pathToMain = 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 | +function loadRepo(config) { |
| 53 | + if (config.bugs && config.bugs.url) { |
| 54 | + window.coderoad.issuesPath = config.bugs.url; |
| 55 | + } |
| 56 | + if (config.repo && config.repo.url) { |
| 57 | + let repo: 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 | +} |
0 commit comments