Skip to content

Commit ab3a123

Browse files
committed
unminified code in src
1 parent 6d7c878 commit ab3a123

File tree

166 files changed

+5973
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+5973
-0
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src

lib/src/_base.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Store
2+
export {store, initialState, dispatch} from './store/store';
3+
4+
// Reducers
5+
export {default as reducer} from './reducers/reducer';

lib/src/actions/actionTypes.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Action Types
3+
*/
4+
5+
6+
// Project
7+
export const SET_PROJECT = 'SET_PROJECT';
8+
export const LOAD_TUTORIALS = 'LOAD_TUTORIALS';
9+
10+
// Navigation
11+
export const SET_ROUTE = 'SET_ROUTE';
12+
13+
// Page
14+
export const SET_PAGE = 'SET_PAGE';
15+
export const NEXT_PAGE = 'NEXT_PAGE';
16+
17+
// Position
18+
export const SET_POSITION = 'SET_POSITION';
19+
20+
// Progress
21+
export const SET_PROGRESS = 'SET_PROGRESS';
22+
export const PAGE_COMPLETE = 'PAGE_COMPLETE';
23+
export const CHAPTER_COMPLETE = 'CHAPTER_COMPLETE';
24+
export const PROJECT_COMPLETE = 'PROJECT_COMPLETE';
25+
26+
// Tasks
27+
export const SET_TASK_POSITION = 'SET_TASK_POSITION';
28+
export const EDITOR_ACTIONS = 'EDITOR_ACTIONS';
29+
export const SHOW_HINT = 'SHOW_HINT';
30+
export const SHOW_SOLUTION = 'SHOW_SOLUTION';
31+
32+
// Tests
33+
export const RUN_TESTS = 'RUN_TESTS';
34+
export const TEST_COMPLETE = 'TEST_COMPLETE';
35+
export const TEST_RESULT = 'TEST_RESULT';
36+
37+
// Log
38+
export const TOGGLE_LOG = 'CHECK_LOG';
39+
export const LOG_MESSAGE = 'LOG_MESSAGE';
40+
41+
// Alert
42+
export const TOGGLE_ALERT = 'TOGGLE_ALERT';
43+
export const REPLAY_ALERT = 'SHOW_ALERT';
44+
45+
export const NULL = 'NULL';

lib/src/actions/actions.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// import * as Action from '../../lib/actions/actions';
2+
// import {expect} from 'chai';
3+
//
4+
// xdescribe('action creators: ', () => {
5+
//
6+
// it('setProject', () => {
7+
// expect(Action.setProject('./path/to/file.js'))
8+
// .to.deep.equal({ type: 'SET_PROJECT', payload: { filePath: './path/to/file.js' } });
9+
// });
10+
//
11+
// it('setPage with no input', () => {
12+
// expect(Action.setPage())
13+
// .to.deep.equal({ type: 'SET_PAGE', payload: { position: { chapter: 0, page: 0, task: 0 } } });
14+
// });
15+
//
16+
// it('setPage with input', () => {
17+
// expect(Action.setPage({ chapter: 1, page: 1, task: 1 }))
18+
// .to.deep.equal({ type: 'SET_PAGE', payload: { position: { chapter: 1, page: 1, task: 1 } } });
19+
// });
20+
//
21+
// it('setRoute', () => {
22+
// expect(Action.setRoute('page')).to.deep.equal({ type: 'SET_ROUTE', payload: { route: 'page' } });
23+
// });
24+
//
25+
// it('setPosition', () => {
26+
// const position = { chapter: 0, page: 1, task: 2 };
27+
// const action = Action.setPosition(position);
28+
// expect(action).to.deep.equal({ type: 'SET_POSITION', payload: { position } });
29+
// });
30+
//
31+
// it('nextTask', (taskLength: number) => {
32+
// const action = Action.nextTask();
33+
// expect(action).to.deep.equal({ type: 'NEXT_TASK', payload: { taskLength } });
34+
// });
35+
//
36+
// it('prevTask', () => {
37+
// const action = Action.prevTask();
38+
// expect(action).to.deep.equal({ type: 'PREV_TASK' });
39+
// });
40+
//
41+
// it('setCompleted with lowercase input', () => {
42+
// const position = { chapter: 0, page: 0, task: 0 };
43+
// const action = Action.setCompleted('task', true, position);
44+
// expect(action)
45+
// .to.deep.equal({ type: 'SET_COMPLETED', payload: { target: 'TASK', completed: true, position } });
46+
// });
47+
//
48+
// });

lib/src/actions/actions.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
import * as Type from './actionTypes';
3+
import {store} from '../_base';
4+
import Package from '../services/package';
5+
const _ = require('lodash');
6+
7+
/* Project */
8+
export function setProject(): Action {
9+
return { type: Type.SET_PROJECT };
10+
}
11+
12+
export function setProgress(): Action {
13+
return { type: Type.SET_PROGRESS };
14+
}
15+
16+
/* Navigation */
17+
export function setRoute(route: string): Action {
18+
return { type: Type.SET_ROUTE, payload: { route } };
19+
}
20+
21+
/* Position */
22+
export function setPosition(position: cr.Position): Action {
23+
return { type: Type.SET_POSITION, payload: { position } };
24+
}
25+
26+
export function loadTutorial(name) {
27+
Package.selectPackage(name);
28+
store.dispatch(setProject());
29+
store.dispatch(setPosition({chapter: 0, page: 0}));
30+
store.dispatch(setProgress());
31+
}
32+
33+
export function toggleLog(): Action {
34+
let open = !store.getState().log.open;
35+
return { type: Type.TOGGLE_LOG, payload: { open } };
36+
}
37+
38+
export function logMessage(message: string): Action {
39+
return { type: Type.LOG_MESSAGE, payload: { message }};
40+
}
41+
42+
/* Page */
43+
export {setPage, nextPage} from './page-actions';
44+
45+
/* Progress */
46+
export {pageComplete, chapterComplete, projectComplete} from './progress-actions';
47+
48+
/* Tasks */
49+
export {setTaskPosition, showHint, runTests, testComplete, testResult} from './task-actions';
50+
51+
/* Tutorials */
52+
export {loadTutorials} from './tutorials';
53+
54+
/* Alert */
55+
export {toggleAlert, replayAlert} from './alert';

lib/src/actions/alert.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {store} from '../_base';
2+
import * as Type from './actionTypes';
3+
4+
export function toggleAlert(alert?: cr.Alert): Action {
5+
const isOpen = store.getState().alert.open;
6+
if (!alert) {
7+
alert = { message: '', action: '', open: false };
8+
} else {
9+
alert = _.assign(alert, { open: !isOpen });
10+
}
11+
return { type: Type.TOGGLE_ALERT, payload: { alert } };
12+
}
13+
14+
export function replayAlert(): Action {
15+
return { type: Type.REPLAY_ALERT };
16+
}

lib/src/actions/page-actions.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as Type from './actionTypes';
2+
import * as Action from './actions';
3+
import {store} from '../_base';
4+
import Package from '../services/package';
5+
const _ = require('lodash');
6+
7+
export function setPage(selectedPosition: cr.Position = { chapter: 0, page: 0 }): Action {
8+
const page = Package.getPage(selectedPosition);
9+
const tasks = Package.getTasks(selectedPosition);
10+
const taskTests: cr.TaskTest[] = _.flatten(tasks.map((task) => task.tests || []));
11+
let actions: string[] = tasks.map((task) => task.actions || []);
12+
return { type: Type.SET_PAGE, payload: { page, tasks, position: selectedPosition, taskTests, actions } };
13+
}
14+
15+
export function nextPage(): Action {
16+
const position = store.getState().position;
17+
const nextPosition = Package.getNextPosition(position);
18+
return setPage(nextPosition);
19+
}

lib/src/actions/progress-actions.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as Type from './actionTypes';
2+
import {store} from '../_base';
3+
4+
export function pageComplete() {
5+
const position = store.getState().position;
6+
const pageLength = store.getState().progress.chapters[position.chapter].pages.length;
7+
const action = { type: Type.PAGE_COMPLETE, payload: { position } };
8+
if (position.page >= pageLength - 1) {
9+
store.dispatch(chapterComplete());
10+
return action;
11+
}
12+
return action;
13+
}
14+
15+
export function chapterComplete(): Action {
16+
const chapter = store.getState().position.chapter;
17+
const chapterLength = store.getState().progress.chapters.length;
18+
const action = { type: Type.CHAPTER_COMPLETE, payload: { chapter } };
19+
if (chapter >= chapterLength - 1) {
20+
store.dispatch(projectComplete());
21+
return action;
22+
}
23+
return action;
24+
}
25+
26+
export function projectComplete(): Action {
27+
return { type: Type.PROJECT_COMPLETE };
28+
}

lib/src/actions/task-actions.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as Type from './actionTypes';
2+
import * as Action from './actions';
3+
import {store} from '../_base';
4+
5+
function checkPageComplete(taskPosition: number) {
6+
const taskLength: number = store.getState().taskTests.length;
7+
if (taskPosition > taskLength) {
8+
store.dispatch(Action.pageComplete());
9+
}
10+
}
11+
12+
export function setTaskPosition(taskPosition: number): Action {
13+
const currentTaskPosition = store.getState().taskPosition;
14+
if (currentTaskPosition === taskPosition) {
15+
return { type: Type.NULL };
16+
}
17+
checkPageComplete(taskPosition);
18+
return { type: Type.SET_TASK_POSITION, payload: { taskPosition } };
19+
}
20+
21+
export function showHint(): Action {
22+
return { type: Type.SHOW_HINT };
23+
}
24+
25+
export function runTests(): Action {
26+
return { type: Type.RUN_TESTS };
27+
}
28+
29+
export function testResult(result): Action {
30+
let actions = store.getState().editorActions;
31+
return { type: Type.TEST_RESULT, payload: { result, actions } };
32+
}
33+
34+
export function testComplete(): Action {
35+
return { type: Type.TEST_COMPLETE };
36+
}

lib/src/actions/tutorials.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {store} from '../_base';
2+
import * as Action from './actions';
3+
import * as path from 'path';
4+
import * as fs from 'fs';
5+
import * as Type from './actionTypes';
6+
import {fileExists} from '../services/exists';
7+
8+
export function loadTutorials(): Action {
9+
let tutorials = [];
10+
if (global.coderoad.dir) {
11+
let packageJson = loadRootPackageJson();
12+
if (!packageJson) {
13+
global.coderoad.package = null;
14+
let message = 'No package.json file available. Try running "npm init --y" in terminal';
15+
console.log(message);
16+
store.dispatch(Action.toggleAlert({ message, action: 'tip', duration: 6000 }));
17+
} else {
18+
tutorials = []
19+
.concat(searchForTutorials(packageJson.dependencies))
20+
.concat(searchForTutorials(packageJson.devDependencies));
21+
}
22+
}
23+
return { type: Type.LOAD_TUTORIALS, payload: { tutorials } };
24+
}
25+
26+
27+
function loadRootPackageJson() {
28+
let pathToPackageJson = path.join(global.coderoad.dir, 'package.json');
29+
if (fileExists(pathToPackageJson)) {
30+
return JSON.parse(fs.readFileSync(pathToPackageJson, 'utf8'));
31+
}
32+
return false;
33+
}
34+
35+
function isTutorial(name): boolean {
36+
let pathToTutorialPackageJson = path.join(global.coderoad.dir, 'node_modules', name, 'package.json');
37+
if (fileExists(pathToTutorialPackageJson)) {
38+
// has package.json
39+
let packageJson = JSON.parse(fs.readFileSync(pathToTutorialPackageJson, 'utf8'));
40+
// main path to coderoad.json
41+
if (packageJson.main && packageJson.main.match(/coderoad.json$/)) {
42+
let pathToCoderoadJson = path.join(global.coderoad.dir, 'node_modules', name, packageJson.main);
43+
// coderoad.json file exists
44+
if (fileExists(pathToCoderoadJson)) {
45+
return true;
46+
}
47+
}
48+
}
49+
return false;
50+
}
51+
52+
function searchForTutorials(location): string[] {
53+
if (!!location && Object.keys(location).length > 0) {
54+
return Object.keys(location).filter((name) => isTutorial(name));
55+
} else {
56+
return [];
57+
}
58+
}

0 commit comments

Comments
 (0)