Skip to content

Commit a3fe34a

Browse files
committed
remove globals, use redux state
1 parent 445bcc9 commit a3fe34a

File tree

40 files changed

+156
-227
lines changed

40 files changed

+156
-227
lines changed

lib/actions/page.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
"use strict";
22
var _types_1 = require('./_types');
33
var store_1 = require('../store');
4+
var _position = {
5+
chapter: 0,
6+
page: 0,
7+
};
48
function pageNext() {
59
var position = store_1.store.getState().position;
610
return { type: _types_1.PAGE_NEXT, payload: { position: position } };
711
}
812
exports.pageNext = pageNext;
913
function pageSet(position) {
10-
if (position === void 0) { position = { chapter: 0, page: 0 }; }
14+
if (position === void 0) { position = _position; }
1115
if (position.completed) {
1216
return {
1317
payload: { route: 'final' },

lib/atom/main.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,9 @@ var polyfills_1 = require('../services/polyfills');
55
var subscriptions_1 = require('./subscriptions');
66
var store_1 = require('../store');
77
var actions_1 = require('../actions');
8-
function setDir() {
9-
if (atom.project.rootDirectories.length > 0) {
10-
return atom.project.rootDirectories[0].path;
11-
}
12-
else {
13-
return null;
14-
}
15-
}
16-
function setWin() {
17-
return navigator.appVersion.indexOf('Win') > -1;
18-
}
198
var Main = (function () {
209
function Main() {
2110
polyfills_1.default();
22-
window.coderoad = {
23-
dir: setDir(),
24-
win: setWin(),
25-
};
2611
store_1.store.dispatch(actions_1.setupVerify());
2712
this.root = mount_1.initRoot();
2813
}

lib/reducers/checks/action-setup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var store_1 = require('../../store');
66
var actions_2 = require('../../actions');
77
var packageData = "{\n \"name\": \"demo\",\n \"dependencies\": {\n \"coderoad-functional-school\": \"^0.2.1\"\n }\n}";
88
function createPackageJson() {
9-
var packagePath = path_1.join(window.coderoad.dir, 'package.json');
9+
var dir = store_1.store.getState().dir;
10+
var packagePath = path_1.join(dir, 'package.json');
1011
return new Promise(function (resolve, reject) {
1112
editor_1.open(packagePath);
1213
setTimeout(function () {

lib/reducers/checks/verify.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function allTrue(obj) {
77
return Object.values(obj).every(function (x) { return x === true; });
88
}
99
function setupVerify() {
10-
var dir = !!window.coderoad.dir;
10+
var dir = !!store_1.store.getState().dir;
1111
var packageJson = false;
1212
var tutorial = false;
1313
var pj = store_1.store.getState().packageJson;

lib/reducers/dir/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
function dirReducer(dir) {
3+
if (dir === void 0) { dir = null; }
4+
if (atom.project.rootDirectories.length > 0) {
5+
return atom.project.rootDirectories[0].path;
6+
}
7+
else {
8+
return null;
9+
}
10+
}
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.default = dirReducer;

lib/reducers/editor-actions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function editorActionsReducer(editorActions, action) {
1414
switch (action.type) {
1515
case _types_1.TESTS_LOAD:
1616
actions = store_1.store.getState().tasks.map(function (task) { return task.actions || []; });
17+
console.log('LOAD', actions);
1718
currentTaskPosition = 0;
1819
handleEditorActions(actions.shift());
1920
return actions;

lib/reducers/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var redux_1 = require('redux');
33
var alert_1 = require('./alert');
44
var checks_1 = require('./checks');
5+
var dir_1 = require('./dir');
56
var editor_actions_1 = require('./editor-actions');
67
var hint_position_1 = require('./hint-position');
78
var package_json_1 = require('./package-json');
@@ -17,7 +18,7 @@ var tutorial_1 = require('./tutorial');
1718
var tutorials_1 = require('./tutorials');
1819
Object.defineProperty(exports, "__esModule", { value: true });
1920
exports.default = redux_1.combineReducers({
20-
alert: alert_1.default, checks: checks_1.default, editorActions: editor_actions_1.default, hintPosition: hint_position_1.default, page: page_1.default,
21+
alert: alert_1.default, checks: checks_1.default, dir: dir_1.default, editorActions: editor_actions_1.default, hintPosition: hint_position_1.default, page: page_1.default,
2122
packageJson: package_json_1.default, position: position_1.default, progress: progress_1.default, route: route_1.default, taskPosition: task_position_1.default,
22-
taskTests: task_tests_1.default, tasks: tasks_1.default, testRun: test_run_1.default, tutorial: tutorial_1.default, tutorials: tutorials_1.default
23+
taskTests: task_tests_1.default, tasks: tasks_1.default, testRun: test_run_1.default, tutorial: tutorial_1.default, tutorials: tutorials_1.default,
2324
});

lib/reducers/package-json/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ var _types_1 = require('../../actions/_types');
33
var path_1 = require('path');
44
var fs_1 = require('fs');
55
var exists_1 = require('../../services/exists');
6+
var store_1 = require('../../store');
67
function packageJsonReducer(pj, action) {
78
if (pj === void 0) { pj = null; }
89
switch (action.type) {
910
case _types_1.PACKAGE_SET:
10-
var pathToPackageJson = path_1.join(window.coderoad.dir, 'package.json');
11+
var dir = store_1.store.getState().dir;
12+
var pathToPackageJson = path_1.join(dir, 'package.json');
1113
if (exists_1.fileExists(pathToPackageJson)) {
1214
return JSON.parse(fs_1.readFileSync(pathToPackageJson, 'utf8'));
1315
}

lib/reducers/tasks/config-task-tests.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
"use strict";
22
var path_1 = require('path');
33
var store_1 = require('../../store');
4+
var system_1 = require('../../services/system');
45
function configTestString(config, name, testPath) {
5-
if (window.coderoad.win) {
6+
if (system_1.isWindows) {
67
testPath = testPath.split('/').join('\\');
78
}
89
var tutorial = store_1.store.getState().tutorial;
910
if (tutorial && tutorial.config.dir) {
1011
testPath = path_1.join(tutorial.config.dir, testPath);
1112
}
1213
else {
13-
testPath = path_1.join(window.coderoad.dir, 'node_modules', name, testPath);
14+
var dir = store_1.store.getState().dir;
15+
testPath = path_1.join(dir, 'node_modules', name, testPath);
1416
}
1517
if (tutorial.config.testSuffix) {
1618
testPath += tutorial.config.testSuffix;

lib/reducers/test-run/parse-loaders.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function parseLoaders(data, fileType) {
3232
pathToFile = path_1.normalize(path_1.join(tutorialDir, fileToLoad));
3333
}
3434
else {
35-
pathToFile = path_1.normalize(path_1.join(window.coderoad.dir, fileToLoad));
35+
var dir = store_1.store.getState().dir;
36+
pathToFile = path_1.normalize(path_1.join(dir, fileToLoad));
3637
}
3738
try {
3839
lines[i] = fs_1.readFileSync(pathToFile, 'utf8');

lib/reducers/test-run/run.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ var parse_loaders_1 = require('./parse-loaders');
77
function runTaskTests(setup) {
88
var tests = store_1.store.getState().taskTests;
99
if (tests && tests.length) {
10+
var dir = store_1.store.getState().dir;
1011
var tutorialConfig = store_1.store.getState().tutorial.config;
1112
var output = parse_loaders_1.default(tests, tutorialConfig.testSuffix);
12-
var target = path_1.join(tutorialConfig.dir || window.coderoad.dir, "_tmp." + tutorialConfig.testSuffix);
13+
var target = path_1.join(tutorialConfig.dir || dir, "_tmp." + tutorialConfig.testSuffix);
1314
fs_1.writeFileSync(target, output, 'utf8');
1415
tutorialConfig.run(target, tutorialConfig, test_result_1.handleResult);
1516
}

lib/reducers/tutorial/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
var _types_1 = require('../../actions/_types');
33
var path_1 = require('path');
44
var tutorial_config_1 = require('./tutorial-config');
5+
var store_1 = require('../../store');
56
var _tutorial = {
67
name: null,
78
info: null,
89
chapters: [],
910
packageJson: null,
10-
config: null
11+
config: null,
1112
};
1213
function tutorialReducer(tutorial, action) {
1314
if (tutorial === void 0) { tutorial = _tutorial; }
1415
switch (action.type) {
1516
case _types_1.TUTORIAL_SET:
1617
var name_1 = action.payload.name;
17-
var packagePath = path_1.join(window.coderoad.dir, 'node_modules', name_1);
18+
var dir = store_1.store.getState().dir;
19+
var packagePath = path_1.join(dir, 'node_modules', name_1);
1820
var packageJson = require(path_1.join(packagePath, 'package.json'));
1921
var config = tutorial_config_1.tutorialConfig(packageJson);
2022
var _a = require(path_1.join(packagePath, packageJson.main)), info = _a.info, chapters = _a.chapters;
@@ -23,7 +25,7 @@ function tutorialReducer(tutorial, action) {
2325
info: info,
2426
chapters: chapters,
2527
packageJson: packageJson,
26-
config: config
28+
config: config,
2729
};
2830
default:
2931
return tutorial;

lib/reducers/tutorial/tutorial-config.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
"use strict";
22
var path_1 = require('path');
33
var exists_1 = require('../../services/exists');
4+
var system_1 = require('../../services/system');
5+
var store_1 = require('../../store');
46
function tutorialConfig(tutorialPj) {
57
var config = tutorialPj.config, name = tutorialPj.name;
68
var repo = loadRepo(tutorialPj.repo);
9+
var dir = store_1.store.getState().dir;
710
return {
8-
dir: path_1.join(window.coderoad.dir, 'node_modules', name, config.dir),
11+
dir: path_1.join(dir, 'node_modules', name, config.dir),
912
testSuffix: config.testSuffix || null,
1013
runner: config.runner,
1114
runnerOptions: config.runnerOptions || null,
12-
run: loadRunner(name, config.runner),
15+
run: loadRunner(name, config.runner, dir),
1316
repo: repo,
1417
edit: tutorialPj.config.edit && repo || false,
1518
issuesPath: getIssuesPath(tutorialPj.bugs)
@@ -19,9 +22,9 @@ exports.tutorialConfig = tutorialConfig;
1922
function getIssuesPath(bugs) {
2023
return bugs && bugs.url ? bugs.url : null;
2124
}
22-
function loadRunner(name, runner) {
23-
var flatDep = path_1.join(window.coderoad.dir, 'node_modules', runner, 'package.json');
24-
var treeDep = path_1.join(window.coderoad.dir, 'node_modules', name, 'node_modules', runner, 'package.json');
25+
function loadRunner(name, runner, dir) {
26+
var flatDep = path_1.join(dir, 'node_modules', runner, 'package.json');
27+
var treeDep = path_1.join(dir, 'node_modules', name, 'node_modules', runner, 'package.json');
2528
var runnerMain;
2629
var runnerRoot;
2730
if (exists_1.fileExists(flatDep)) {
@@ -37,7 +40,7 @@ function loadRunner(name, runner) {
3740
console.log(message);
3841
throw message;
3942
}
40-
var slash = window.coderoad.win ? '\\' : '/';
43+
var slash = system_1.isWindows ? '\\' : '/';
4144
runnerMain = path_1.join.apply(null, runnerMain.split(slash));
4245
runnerRoot = runnerRoot.substring(0, runnerRoot.lastIndexOf(slash));
4346
var pathToMain = path_1.join(runnerRoot, runnerMain);

lib/reducers/tutorials/check-tutorials.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ var path_1 = require('path');
33
var fs_1 = require('fs');
44
var exists_1 = require('../../services/exists');
55
var is_tutorial_1 = require('./is-tutorial');
6+
var store_1 = require('../../store');
67
function searchForTutorials(deps) {
78
if (!!deps && Object.keys(deps).length > 0) {
9+
var dir_1 = store_1.store.getState().dir;
810
return (Object.keys(deps)
911
.filter(function (name) { return is_tutorial_1.isTutorial(name); })
1012
.map(function (name) {
11-
var pathToTutorialPackageJson = path_1.join(window.coderoad.dir, 'node_modules', name, 'package.json');
13+
var pathToTutorialPackageJson = path_1.join(dir_1, 'node_modules', name, 'package.json');
1214
if (!exists_1.fileExists(pathToTutorialPackageJson)) {
1315
console.log("Error with " + name + ": no package.json file found. " + is_tutorial_1.tutorialError);
1416
return {

lib/reducers/tutorials/is-tutorial.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
var path_1 = require('path');
33
var fs_1 = require('fs');
44
var exists_1 = require('../../services/exists');
5+
var store_1 = require('../../store');
56
exports.tutorialError = 'This is an error with the tutorial itself';
67
function isTutorial(name) {
7-
var pathToTutorialPackageJson = path_1.join(window.coderoad.dir, 'node_modules', name, 'package.json');
8+
var dir = store_1.store.getState().dir;
9+
var pathToTutorialPackageJson = path_1.join(dir, 'node_modules', name, 'package.json');
810
if (!exists_1.fileExists(pathToTutorialPackageJson)) {
911
console.log("Error with " + name + ": no package.json file found. " + exports.tutorialError);
1012
return false;
@@ -14,7 +16,7 @@ function isTutorial(name) {
1416
console.log("Error with " + name + ": main does not load a coderoad.json file. " + exports.tutorialError);
1517
return false;
1618
}
17-
var pathToCoderoadJson = path_1.join(window.coderoad.dir, 'node_modules', name, packageJson.main);
19+
var pathToCoderoadJson = path_1.join(dir, 'node_modules', name, packageJson.main);
1820
if (!exists_1.fileExists(pathToCoderoadJson)) {
1921
console.log("Error with " + name + ": no coderoad.json file. " + exports.tutorialError);
2022
return false;

lib/services/system.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
exports.isWindows = navigator.appVersion.indexOf('Win') > -1;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"react-dom": "15.0.1",
4747
"react-redux": "4.4.5",
4848
"react-tap-event-plugin": "1.0.0",
49-
"redux": "3.4.0"
49+
"redux": "3.5.1"
5050
},
5151
"scripts": {
5252
"test": "ava",

src/actions/page.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import {ROUTE_SET, PAGE_SET, PAGE_NEXT} from './_types';
22
import {store} from '../store';
3-
// import TutorialPackage from '../services/tutorial-package';
43

5-
export function pageNext(): CR.Action {
4+
const _position = {
5+
chapter: 0,
6+
page: 0,
7+
};
8+
9+
export function pageNext(): Action {
610
const position: CR.Position = store.getState().position;
711
return { type: PAGE_NEXT, payload: { position }};
812
}
913

10-
export function pageSet(position: CR.Position = { chapter: 0, page: 0 }): CR.Action {
14+
15+
16+
export function pageSet(
17+
position: CR.Position = _position
18+
): Action {
1119
if (position.completed) {
1220
return {
1321
payload: { route: 'final'},

src/actions/progress.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import {
33
} from './_types';
44
import {store} from '../store';
55

6-
export function progressLoad(): CR.Action {
6+
export function progressLoad(): Action {
77
return { type: PROGRESS_LOAD };
88
}
99

10-
export function completePage(): CR.Action {
10+
export function completePage(): Action {
1111
const position: CR.Position = store.getState().position;
1212
const pageLength: number = store.getState().progress.chapters[position.chapter].pages.length;
1313
if (position.page >= pageLength - 1) {
@@ -19,7 +19,7 @@ export function completePage(): CR.Action {
1919
};
2020
}
2121

22-
export function completeChapter(): CR.Action {
22+
export function completeChapter(): Action {
2323
const chapter: number = store.getState().position.chapter;
2424
const chapterLength: number = store.getState().progress.chapters.length;
2525
if (chapter >= chapterLength - 1) {

src/actions/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function testRun(): Action {
77
return { type: TEST_RUN };
88
}
99

10-
export function testResult(result: CR.TestResult): Action {
10+
export function testResult(result: Test.Result): Action {
1111
let actions = store.getState().editorActions;
1212
return {
1313
payload: { result, actions },

src/atom/actions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export function openDevTools(): void {
3434
export function openTerminal(): boolean {
3535
if (atom.packages.isPackageActive('terminal-plus')) {
3636
if (!document.getElementsByClassName('xterm')[0]) {
37-
atom.commands.dispatch(document.getElementsByTagName('atom-workspace')[0], 'terminal-plus:toggle');
37+
atom.commands.dispatch(
38+
document.getElementsByTagName('atom-workspace')[0], 'terminal-plus:toggle'
39+
);
3840
}
3941
return true;
4042
}

src/atom/main.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,11 @@ import {onActivate, onDeactivate, addToStatusBar} from './subscriptions';
77
import {store} from '../store';
88
import {setupVerify} from '../actions';
99

10-
// TODO: find a better place to load globals
11-
12-
function setDir(): string {
13-
if (atom.project.rootDirectories.length > 0) {
14-
return atom.project.rootDirectories[0].path;
15-
} else {
16-
return null;
17-
}
18-
}
19-
function setWin(): boolean {
20-
return navigator.appVersion.indexOf('Win') > -1;
21-
}
22-
2310
class Main {
2411
root: HTMLElement;
2512
statusBarTile: StatusBar.IStatusBarView;
2613
constructor() {
2714
loadPolyfills(); // remove with later version of Chrome
28-
window.coderoad = {
29-
dir: setDir(),
30-
win: setWin(),
31-
};
3215
store.dispatch(setupVerify());
3316
this.root = initRoot();
3417
}

src/components/Alert/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const defaultAlert = {
1515
alertToggle: () => dispatch(Action.alertToggle()),
1616
};
1717
})
18-
export class Alert extends React.Component<{alert: CR.Alert, alertToggle?: any}, CR.Alert> {
18+
export class Alert extends React.Component<{
19+
alert: CR.Alert, alertToggle?: any
20+
}, CR.Alert> {
1921
render() {
2022
const {alert, alertToggle} = this.props;
2123
const {action, open, message, duration} = alert;

0 commit comments

Comments
 (0)