|
| 1 | +"use strict"; |
| 2 | +var path_1 = require('path'); |
| 3 | +var actions_1 = require('../actions/actions'); |
| 4 | +var store_1 = require('../store/store'); |
| 5 | +function configTestString(config, name, test) { |
| 6 | + if (window.coderoad.win) { |
| 7 | + test = test.split('/').join('\\'); |
| 8 | + } |
| 9 | + if (config.testDir) { |
| 10 | + test = path_1.join(window.coderoad.dir, 'node_modules', name, config.testDir, test); |
| 11 | + } |
| 12 | + else { |
| 13 | + test = path_1.join(window.coderoad.dir, 'node_modules', name, test); |
| 14 | + } |
| 15 | + if (config.testSuffix) { |
| 16 | + test += config.testSuffix; |
| 17 | + } |
| 18 | + return test; |
| 19 | +} |
| 20 | +var TutorialPackageService = (function () { |
| 21 | + function TutorialPackageService() { |
| 22 | + this.name = ''; |
| 23 | + this.data = { |
| 24 | + project: {}, |
| 25 | + chapters: [] |
| 26 | + }; |
| 27 | + this.packageJson = null; |
| 28 | + } |
| 29 | + TutorialPackageService.prototype.selectPackage = function (name) { |
| 30 | + var packagePath = path_1.join(window.coderoad.dir, 'node_modules', name); |
| 31 | + this.packageJson = require(path_1.join(packagePath, 'package.json')); |
| 32 | + store_1.store.dispatch(actions_1.setGlobals(this.packageJson)); |
| 33 | + this.data = require(path_1.join(packagePath, this.packageJson.main)); |
| 34 | + this.name = name; |
| 35 | + }; |
| 36 | + TutorialPackageService.prototype.page = function (_a) { |
| 37 | + var chapter = _a.chapter, page = _a.page; |
| 38 | + return this.data.chapters[chapter].pages[page]; |
| 39 | + }; |
| 40 | + TutorialPackageService.prototype.getPackage = function () { |
| 41 | + return this.packageJson; |
| 42 | + }; |
| 43 | + TutorialPackageService.prototype.configTaskTests = function (tasks) { |
| 44 | + var _this = this; |
| 45 | + var config = this.packageJson.config; |
| 46 | + return !tasks ? [] : tasks.map(function (task) { |
| 47 | + if (task.tests) { |
| 48 | + task.tests = task.tests.map(function (test) { |
| 49 | + if (typeof test === 'string') { |
| 50 | + return configTestString(config, _this.name, test); |
| 51 | + } |
| 52 | + else { |
| 53 | + console.error('Invalid task test', test); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + return task; |
| 58 | + }); |
| 59 | + }; |
| 60 | + TutorialPackageService.prototype.getTasks = function (position) { |
| 61 | + var tasks = this.page(position).tasks || []; |
| 62 | + tasks = this.configTaskTests(tasks); |
| 63 | + return tasks; |
| 64 | + }; |
| 65 | + TutorialPackageService.prototype.getPage = function (position) { |
| 66 | + var _a = this.page(position), title = _a.title, description = _a.description, onPageComplete = _a.onPageComplete, completed = _a.completed; |
| 67 | + return { |
| 68 | + title: title, description: description, onPageComplete: onPageComplete, completed: completed || false |
| 69 | + }; |
| 70 | + }; |
| 71 | + TutorialPackageService.prototype.getSavedPosition = function () { |
| 72 | + return { chapter: 0, page: 0 }; |
| 73 | + }; |
| 74 | + TutorialPackageService.prototype.getSavedRoute = function () { |
| 75 | + return 'progress'; |
| 76 | + }; |
| 77 | + TutorialPackageService.prototype.getNextPosition = function (_a) { |
| 78 | + var chapter = _a.chapter, page = _a.page; |
| 79 | + var chapters = this.data.chapters; |
| 80 | + if (page < chapters[chapter].pages.length - 1) { |
| 81 | + return { chapter: chapter, page: page + 1 }; |
| 82 | + } |
| 83 | + else if (chapter < chapters.length - 1) { |
| 84 | + return { chapter: chapter + 1, page: 0 }; |
| 85 | + } |
| 86 | + else { |
| 87 | + store_1.store.dispatch(actions_1.projectComplete()); |
| 88 | + return { chapter: chapter, page: page, completed: true }; |
| 89 | + } |
| 90 | + }; |
| 91 | + TutorialPackageService.prototype.getProject = function () { |
| 92 | + return this.data.project; |
| 93 | + }; |
| 94 | + TutorialPackageService.prototype.getProgress = function () { |
| 95 | + var chapters = this.data.chapters; |
| 96 | + return { |
| 97 | + completed: false, |
| 98 | + chapters: !chapters ? [] : chapters.map(function (_a) { |
| 99 | + var title = _a.title, description = _a.description, completed = _a.completed, pages = _a.pages; |
| 100 | + return { |
| 101 | + title: title, description: description, completed: completed || false, |
| 102 | + pages: !pages ? [] : pages.map(function (page) { |
| 103 | + return { |
| 104 | + title: page.title, |
| 105 | + description: page.description, |
| 106 | + completed: page.completed || false |
| 107 | + }; |
| 108 | + }) |
| 109 | + }; |
| 110 | + }) |
| 111 | + }; |
| 112 | + }; |
| 113 | + return TutorialPackageService; |
| 114 | +}()); |
| 115 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 116 | +exports.default = new TutorialPackageService(); |
0 commit comments