Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commite7a4b94

Browse files
committed
improved docs and commenting in setup & progress
1 parentb5178c1 commite7a4b94

File tree

16 files changed

+176
-24
lines changed

16 files changed

+176
-24
lines changed

‎lib/modules/progress/reducer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"use strict";
22
vartypes_1=require('./types');
33
varlocal_storage_1=require('./utils/local-storage');
4-
var_progress={
4+
exports._progress={
55
completed:false,
66
pages:[]
77
};
88
functionprogress(progress,action){
9-
if(progress===void0){progress=_progress;}
9+
if(progress===void0){progress=exports._progress;}
1010
switch(action.type){
1111
casetypes_1.PROGRESS_LOAD:
1212
varsaved=local_storage_1.loadProgressFromLocalStorage(action.payload.tutorial);

‎lib/modules/progress/utils/local-storage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
functiongetLocalStorageKey(tutorial){
33
return'coderoad:'+tutorial.name;
44
}
5+
exports.getLocalStorageKey=getLocalStorageKey;
56
functionsaveToLocalStorage(tutorial,progress){
67
try{
78
window.localStorage
89
.setItem(getLocalStorageKey(tutorial),JSON.stringify(progress));
910
}
1011
catch(e){
11-
console.log('Error saving progress:',e);
12+
thrownewError("Error saving progress. Invalid progress: "+progress+". "+e);
1213
}
1314
}
1415
exports.saveToLocalStorage=saveToLocalStorage;

‎lib/modules/setup/Checks/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"use strict";
22
vartypes_1=require('../types');
33
varverify_1=require('../utils/verify');
4-
var_checks={
4+
exports._checks={
55
passed:false,
66
system:{
77
node:false,
88
npm:false,
99
xcode:false,
10+
atom:false,
1011
},
1112
setup:{
1213
hasDir:false,
@@ -15,7 +16,7 @@ var _checks = {
1516
}
1617
};
1718
functionchecks(checks,action){
18-
if(checks===void0){checks=_checks;}
19+
if(checks===void0){checks=exports._checks;}
1920
switch(action.type){
2021
casetypes_1.SETUP_VERIFY:
2122
var_a=action.payload,dir=_a.dir,packageJson=_a.packageJson;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"use strict";
22
varpath_1=require('path');
3-
varselectors_1=require('../../../selectors');
4-
varpackageData="{\n \"name\": \"demo\",\n \"dependencies\": {\n \"coderoad-functional-school\": \"^0.2.2\"\n }\n}";
3+
vareditor_1=require('../../editor');
4+
varpackageData="{\n \"name\": \"demo\",\n \"version\": \"0.1.0\",\n \"private\": true,\n \"dependencies\": {\n \"coderoad-functional-school\": \"^1.1.3\"\n }\n}";
55
functioncreatePackageJson(dir){
66
varpackagePath=path_1.join(dir,'package.json');
77
returnnewPromise(function(resolve,reject){
8-
selectors_1.open(packagePath);
8+
editor_1.open(packagePath);
99
setTimeout(function(){returnresolve();});
1010
}).then(function(){
11-
selectors_1.set(packageData);
11+
editor_1.set(packageData);
1212
});
1313
}
1414
exports.createPackageJson=createPackageJson;
1515
functionopenDirectory(){
16-
selectors_1.openFolder();
16+
editor_1.openFolder();
1717
}
1818
exports.openDirectory=openDirectory;

‎src/modules/progress/reducer.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="../../typings/globals/jest/index.d.ts" />
2+
3+
importreducer,{_progress}from'./reducer';
4+
5+
describe('progress reducer',()=>{
6+
7+
it('should do nothing if no action is triggered',()=>{
8+
constaction={type:'unknown'};
9+
expect(reducer(undefined,action)).toEqual(_progress);
10+
});
11+
12+
});

‎src/modules/progress/reducer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import{PROGRESS_COMPLETE_PAGE,PROGRESS_COMPLETE_TUTORIAL,PROGRESS_LOAD}from'./types';
22
import{loadProgressFromLocalStorage,saveToLocalStorage}from'./utils/local-storage';
33

4-
const_progress:CR.Progress={
4+
exportconst_progress:CR.Progress={
55
completed:false,
66
pages:[]
77
};
88

9+
/**
10+
* Progress reducer saves local tutorial progress
11+
*@param {} progress=_progress
12+
*@param {Action} action
13+
*@returns CR.Progress
14+
*/
915
exportdefaultfunctionprogress(
1016
progress=_progress,action:Action
1117
):CR.Progress{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// <reference path="../../../typings/globals/jest/index.d.ts" />
2+
/// <reference path="../../../typings/common/global.d.ts" />
3+
4+
import{getLocalStorageKey,saveToLocalStorage,loadProgressFromLocalStorage}from'./local-storage';
5+
6+
describe('local storage function',()=>{
7+
8+
consttutorial={
9+
name:'example'
10+
};
11+
12+
afterEach(()=>{
13+
global.window=null;
14+
})
15+
16+
it('getLocalStorage creates a key based on the tutorial name',()=>{
17+
consttutorial={
18+
name:'example'
19+
};
20+
expect(getLocalStorageKey(tutorial)).toBe('coderoad:example');
21+
});
22+
23+
it('saveToLocalStorage saves tutorial progress',()=>{
24+
global.window.localStorage={
25+
setItem:jest.fn()
26+
};
27+
constprogress=[true,true,false];
28+
saveToLocalStorage(tutorial,progress);
29+
expect(global.window.localStorage.setItem).toBeCalledWith('coderoad:example',JSON.stringify(progress));
30+
});
31+
32+
it('saveToLocalStorage should throw an error if progress is invalid',()=>{
33+
global.window.localStorage={
34+
setItem:jest.fn()
35+
};
36+
constprogress='invalid';
37+
expect(saveToLocalStorage(tutorial,progress)).toThrowError();
38+
});
39+
40+
it('loadProgressFromLocalStorage should load saved progress',()=>{
41+
constprogress=[true,true,false];
42+
global.window.localStorage={
43+
getItem:key=>JSON.stringify({"completed":false,"pages":progress})
44+
};
45+
expect(loadProgressFromLocalStorage(tutorial)).toEqual({"completed":false,"pages":progress});
46+
});
47+
48+
it('loadProgressFromLocalStorage should return null if no progress saved',()=>{
49+
constprogress=null;
50+
global.window.localStorage={
51+
getItem:key=>null
52+
};
53+
expect(loadProgressFromLocalStorage(tutorial)).toEqual(null);
54+
});
55+
56+
});

‎src/modules/progress/utils/local-storage.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1-
functiongetLocalStorageKey(tutorial:CR.Tutorial){
1+
/**
2+
* create a key for saving progress in local storage
3+
*@param {CR.Tutorial} tutorial
4+
*@returns tutorial
5+
*/
6+
exportfunctiongetLocalStorageKey(tutorial:CR.Tutorial):string{
27
return'coderoad:'+tutorial.name;
38
}
49

10+
/**
11+
* save progress to local storage
12+
*@param {CR.Tutorial} tutorial
13+
*@param {CR.Progress} progress
14+
*@returns void
15+
*/
516
exportfunctionsaveToLocalStorage(
617
tutorial:CR.Tutorial,progress:CR.Progress
7-
):void{
18+
):void|Error{
819
try{
920
window.localStorage
1021
.setItem(getLocalStorageKey(tutorial),JSON.stringify(progress));
1122
}catch(e){
12-
console.log('Error saving progress:',e);
23+
thrownewError(`Error saving progress. Invalid progress:${progress}.${e}`);
1324
}
1425
}
15-
16-
exportfunctionloadProgressFromLocalStorage(tutorial:CR.Tutorial){
17-
constsavedProgress:CR.Progress=JSON.parse(
26+
/**
27+
* load progress from local storage
28+
*@param {CR.Tutorial} tutorial
29+
*/
30+
exportfunctionloadProgressFromLocalStorage(tutorial:CR.Tutorial):CR.Progress|null{
31+
constsavedProgress=JSON.parse(
1832
window.localStorage.getItem(getLocalStorageKey(tutorial))||null
1933
);
2034
if(savedProgress){

‎src/modules/setup/checks/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import{SETUP_VERIFY}from'../types';
22
importsetupVerifyfrom'../utils/verify';
33

4-
const_checks:CR.Checks={
4+
exportconst_checks:CR.Checks={
55
passed:false,
66
system:{
77
node:false,
88
npm:false,
99
xcode:false,
10+
atom:false,
1011
},
1112
setup:{
1213
hasDir:false,
@@ -15,6 +16,12 @@ const _checks: CR.Checks = {
1516
}
1617
};
1718

19+
/**
20+
* setup and system checks reducer
21+
*@param {} checks=_checks
22+
*@param {Action} action
23+
*@returns CR.Checks
24+
*/
1825
exportdefaultfunctionchecks(
1926
checks=_checks,action:Action
2027
):CR.Checks{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="../../../typings/globals/jest/index.d.ts" />
2+
3+
importreducer,{_checks}from'./index';
4+
5+
describe('checks reducer',()=>{
6+
7+
it('does nothing if no action received',()=>{
8+
constaction={type:'unknown'};
9+
expect(reducer(undefined,action)).toEqual(_checks);
10+
});
11+
12+
});

‎src/modules/setup/package-json/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import fileExists from 'node-file-exists';
66

77
constreadParse=p=>JSON.parse(readFileSync(p,'utf8'));
88

9+
/**
10+
* package.json reducer
11+
*@param {} pj=null
12+
*@param {Action} action
13+
*@returns PackageJson
14+
*/
915
exportdefaultfunctionpackageJson(
1016
pj=null,action:Action
11-
):PackageJson{
17+
):PackageJson|null{
1218
switch(action.type){
1319

1420
caseSETUP_PACKAGE:
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
import{join}from'path';
2-
3-
import{open,openFolder,openTerminal,set}from'../../../selectors';
4-
import{setupVerify}from'../actions';
2+
import{open,openFolder,openTerminal,set}from'../../editor';
53

64
constpackageData=`{
75
"name": "demo",
6+
"version": "0.1.0",
7+
"private": true,
88
"dependencies": {
9-
"coderoad-functional-school": "^0.2.2"
9+
"coderoad-functional-school": "^1.1.3"
1010
}
1111
}`;
1212

13+
/**
14+
* creates a basic package.json file in the users directory
15+
*@param {string} dir
16+
*@returns Promise
17+
*/
1318
exportfunctioncreatePackageJson(dir:string):Promise<void>{
1419
constpackagePath=join(dir,'package.json');
1520
returnnewPromise((resolve,reject)=>{
1621
open(packagePath);
1722
setTimeout(()=>resolve());
1823
}).then(()=>{
1924
set(packageData);
20-
// store.dispatch(setupVerify());
2125
});
2226
}
2327

28+
/**
29+
* opens a directory
30+
*@returns void
31+
*/
2432
exportfunctionopenDirectory():void{
2533
openFolder();
2634
}

‎src/modules/setup/utils/action-system.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import{setupVerify}from'../actions';
22
importcommandLinefrom'atom-plugin-command-line';
33

4+
// WIP
45
exportfunctionupdateNpm():void{
56
commandLine('npm','update -g npm')
67
.then((res)=>{

‎src/modules/setup/utils/check-system.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export function nodeMinVersion(): Promise<boolean> {
5959
returnminVersion('node',versions.node);
6060
}
6161

62+
/**
63+
* checks if is a mac
64+
* checks if xcode is installed
65+
* sets true if mac & !xcode, else false
66+
*@returns Promise
67+
*/
6268
exportfunctionrequiresXCode():Promise<boolean>|boolean{
6369
if(!navigator.platform.match(/Mac/)){
6470
returntrue;

‎src/modules/setup/utils/verify.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import{atomMinVersion,nodeMinVersion,npmMinVersion,requiresXCode}from'./check-system';
22
import{tutorials}from'coderoad-cli';
33

4+
/**
5+
* Returns true if all object key values are true
6+
*@param {Object} obj
7+
*@returns boolean
8+
*/
49
functionallTrue(obj:Object):boolean{
510
returnObject.values(obj).every(x=>x===true);
611
}
712

13+
/**
14+
* verifies setup of system & project
15+
*@param {string} dir
16+
*@param {PackageJson} packageJson
17+
*@returns CR
18+
*/
819
exportdefaultfunctionsetupVerify(
920
dir:string,packageJson:PackageJson
1021
):CR.Checks{

‎src/typings/common/global.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,15 @@ declare var global: Global;
33
interfaceGlobal{
44
document:any;
55
atom:any;
6+
window:any;
67
}
8+
9+
10+
declarevarwindow:Window;
11+
12+
interfaceWindow{
13+
localStorage:{
14+
getItem:(item:any)=>any;
15+
setItem:(item:any,value:any)=>any;
16+
}
17+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp