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.

Commit04174cd

Browse files
committed
update tutorial versions and display latest tutorial version
1 parent8d27b84 commit04174cd

File tree

16 files changed

+128
-83
lines changed

16 files changed

+128
-83
lines changed

‎lib/components/Tutorials/UpdateTutorial/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ var UpdateTutorial = (function (_super) {
2626
UpdateTutorial.prototype.render=function(){
2727
var_a=this.props,tutorial=_a.tutorial,tutorialUpdate=_a.tutorialUpdate;
2828
return(React.createElement("span",null,
29-
React.createElement(update_1.default,{style:styles.icon,color:colors_1.pink500,onTouchTap:tutorialUpdate.bind(this,tutorial.name)})
30-
));
29+
React.createElement(update_1.default,{style:styles.icon,color:colors_1.pink500,onTouchTap:tutorialUpdate.bind(this,tutorial.name)}),
30+
React.createElement("span",null,tutorial.latest)));
3131
};
3232
returnUpdateTutorial;
3333
}(React.Component));

‎lib/components/Tutorials/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var Tutorials = (function (_super) {
3636
),
3737
React.createElement(Table_1.TableRowColumn,null,
3838
tutorial.version,
39-
!tutorial.latest
39+
!tutorial.isLatest
4040
?React.createElement(UpdateTutorial_1.default,{tutorial:tutorial})
4141
:null)));
4242
}))),

‎lib/modules/tutorials/actions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ var actions_2 = require('../tutorial/actions');
55
exports.tutorialSet=actions_2.tutorialSet;
66
functiontutorialUpdate(title){
77
returnfunction(dispatch){
8+
dispatch({type:types_1.TUTORIAL_UPDATE,payload:{title:title}});
89
varalert={
910
message:"run `npm install --save-dev "+title+"`",
1011
action:'note',
1112
duration:3000,
1213
};
13-
dispatch({type:types_1.TUTORIAL_UPDATE,payload:{title:title}});
1414
dispatch(actions_1.alertOpen(alert));
1515
};
1616
}
@@ -27,3 +27,8 @@ function tutorialsFind() {
2727
};
2828
}
2929
exports.tutorialsFind=tutorialsFind;
30+
functiontutorialVersion(_a){
31+
varname=_a.name,latest=_a.latest;
32+
return{type:types_1.TUTORIAL_VERSION,payload:{name:name,latest:latest}};
33+
}
34+
exports.tutorialVersion=tutorialVersion;

‎lib/modules/tutorials/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
2-
varlatestVersion_1=require('./latestVersion');
32
vartypes_1=require('./types');
3+
varlatestVersion_1=require('./utils/latestVersion');
44
varcoderoad_cli_1=require('coderoad-cli');
55
functiontutorialsReducer(t,action){
66
if(t===void0){t=[];}
@@ -11,9 +11,15 @@ function tutorialsReducer(t, action) {
1111
casetypes_1.TUTORIALS_UPDATE:
1212
returnt.map(function(tutorial){
1313
varname=tutorial.name,version=tutorial.version;
14-
if(version){
15-
latestVersion_1.default({name:name,version:version})
16-
.then(function(x){returntutorial.latest=x;});
14+
latestVersion_1.default({name:name,version:version});
15+
returntutorial;
16+
});
17+
casetypes_1.TUTORIAL_VERSION:
18+
var_a=action.payload,name_1=_a.name,latest_1=_a.latest;
19+
t.map(function(tutorial){
20+
if(tutorial.name===name_1){
21+
tutorial.isLatest=false;
22+
tutorial.latest=latest_1;
1723
}
1824
returntutorial;
1925
});

‎lib/modules/tutorials/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
exports.TUTORIALS_FIND='TUTORIALS_FIND';
33
exports.TUTORIALS_UPDATE='TUTORIALS_UPDATE';
44
exports.TUTORIAL_UPDATE='TUTORIAL_UPDATE';
5+
exports.TUTORIAL_VERSION='TUTORIAL_VERSION';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
varstore_1=require('../../../store');
3+
varcompareVersions_1=require('../../../utils/compareVersions');
4+
varfetch_1=require('../../../utils/fetch');
5+
varactions_1=require('./actions');
6+
varnpmApiCall=function(name){return("https://registry.npmjs.org/"+name);};
7+
functiongetLatest(version,data){
8+
returndata['dist-tags'].latest;
9+
}
10+
functionisLatestVersion(_a){
11+
varname=_a.name,version=_a.version;
12+
window.fetch(npmApiCall(name))
13+
.then(fetch_1.status)
14+
.then(fetch_1.json)
15+
.then(getLatest.bind(this,version))
16+
.then(function(latest){
17+
if(!compareVersions_1.isAboveVersion(version,latest)){
18+
store_1.default.dispatch(actions_1.tutorialVersion({name:name,latest:latest}));
19+
}
20+
})
21+
.catch(function(err){returnconsole.log("Error fetching tutorial \""+name+"\": "+err);});
22+
}
23+
Object.defineProperty(exports,"__esModule",{value:true});
24+
exports.default=isLatestVersion;

‎lib/utils/fetch.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
"use strict";
2-
varfetch=function(url){
3-
returnnewPromise(function(resolve,reject){
4-
varlib=url.startsWith('https') ?require('https') :require('http');
5-
varrequest=lib.get(url,function(response){
6-
if(response.statusCode<200||response.statusCode>299){
7-
reject(newError('Failed to load page, status code: '+response.statusCode));
8-
}
9-
varbody=[];
10-
response.on('data',function(chunk){returnbody.push(chunk);});
11-
response.on('end',function(){returnresolve(body.join(''));});
12-
});
13-
request.on('error',function(err){returnreject(err);});
14-
});
15-
};
16-
Object.defineProperty(exports,"__esModule",{value:true});
17-
exports.default=fetch;
2+
functionstatus(response){
3+
if(response.status>=200&&response.status<300){
4+
returnPromise.resolve(response);
5+
}
6+
else{
7+
returnPromise.reject(newError(response.statusText));
8+
}
9+
}
10+
exports.status=status;
11+
functionjson(response){
12+
returnresponse.json();
13+
}
14+
exports.json=json;

‎src/components/Tutorials/UpdateTutorial/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class UpdateTutorial extends React.Component<{
2727
color={pink500}
2828
onTouchTap={tutorialUpdate.bind(this,tutorial.name)}
2929
/>
30+
<span>{tutorial.latest}</span>
3031
</span>
3132
);
3233
}

‎src/components/Tutorials/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Tutorials extends React.Component<{
4848
<TableRowColumn>
4949
{tutorial.version}
5050

51-
{!tutorial.latest
51+
{!tutorial.isLatest
5252
?<UpdateTutorialtutorial={tutorial}/>
5353
:null
5454
}

‎src/modules/tutorials/actions.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import{alertOpen}from'../alert/actions';
2-
import{TUTORIALS_FIND,TUTORIALS_UPDATE,TUTORIAL_UPDATE}from'./types';
2+
import{TUTORIALS_FIND,TUTORIALS_UPDATE,TUTORIAL_UPDATE,TUTORIAL_VERSION}from'./types';
33
export{tutorialSet}from'../tutorial/actions';
44

55
exportfunctiontutorialUpdate(title:string):
66
Redux.ThunkAction<any,{},{}>{
77
return(dispatch)=>{
8+
dispatch({type:TUTORIAL_UPDATE,payload:{ title}});
9+
10+
// alert instructions
811
constalert={
912
message:`run \`npm install --save-dev${title}\``,
1013
action:'note',
1114
duration:3000,
1215
};
13-
dispatch({type:TUTORIAL_UPDATE,payload:{ title}});
1416
dispatch(alertOpen(alert));
1517
};
1618
}
@@ -26,3 +28,7 @@ export function tutorialsFind(): Redux.ThunkAction<any, {dir: string}, {}> {
2628
dispatch(tutorialsUpdate());
2729
};
2830
}
31+
32+
exportfunctiontutorialVersion({name, latest}):Action{
33+
return{type:TUTORIAL_VERSION,payload:{ name, latest}};
34+
}

‎src/modules/tutorials/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// import {tutorialUpdate} from './utils/update';
2-
importisLatestVersionfrom'./latestVersion';
3-
import{TUTORIALS_FIND,TUTORIALS_UPDATE}from'./types';
2+
import{TUTORIALS_FIND,TUTORIALS_UPDATE,TUTORIAL_VERSION}from'./types';
3+
importisLatestVersionfrom'./utils/latestVersion';
44
import{tutorials}from'coderoad-cli';
55

66
/**
@@ -31,9 +31,16 @@ export default function tutorialsReducer(
3131
caseTUTORIALS_UPDATE:
3232
returnt.map((tutorial:Tutorial.Info)=>{
3333
const{ name, version}=tutorial;
34-
if(version){
35-
isLatestVersion({name, version})
36-
.then(x=>tutorial.latest=x);
34+
isLatestVersion({name, version});
35+
returntutorial;
36+
});
37+
38+
caseTUTORIAL_VERSION:
39+
const{ name, latest}=action.payload;
40+
t.map((tutorial:Tutorial.Info)=>{
41+
if(tutorial.name===name){
42+
tutorial.isLatest=false;
43+
tutorial.latest=latest;
3744
}
3845
returntutorial;
3946
});

‎src/modules/tutorials/latestVersion.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎src/modules/tutorials/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
exportconstTUTORIALS_FIND='TUTORIALS_FIND';
22
exportconstTUTORIALS_UPDATE='TUTORIALS_UPDATE';
33
exportconstTUTORIAL_UPDATE='TUTORIAL_UPDATE';
4+
exportconstTUTORIAL_VERSION='TUTORIAL_VERSION';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
importstorefrom'../../../store';
2+
import{isAboveVersion}from'../../../utils/compareVersions';
3+
import{json,status}from'../../../utils/fetch';
4+
import{tutorialVersion}from'./actions';
5+
6+
constnpmApiCall=name=>`https://registry.npmjs.org/${name}`;
7+
8+
functiongetLatest(version,data:Object):boolean{
9+
returndata['dist-tags'].latest;
10+
}
11+
12+
/**
13+
* Checks that current tutorial version is >= latest version
14+
* via the NPM registry
15+
*
16+
* triggers an update by dispatching "tutorialVersion"
17+
*
18+
*@param {} {name
19+
*@param {} version}
20+
*@param {string} current
21+
*@returns Promise
22+
*/
23+
functionisLatestVersion({name, version}):void{
24+
window.fetch(npmApiCall(name))
25+
.then(status)
26+
.then(json)
27+
.then(getLatest.bind(this,version))
28+
.then(latest=>{
29+
if(!isAboveVersion(version,latest)){
30+
store.dispatch(tutorialVersion({name, latest}))
31+
}
32+
})
33+
.catch((err)=>console.log(`Error fetching tutorial "${name}":${err}`));
34+
}
35+
36+
exportdefaultisLatestVersion;

‎src/typings/tutorial/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ declare namespace Tutorial {
1010
description?:string;
1111
keywords?:string[];
1212
version?:string;
13-
latest?:boolean;
13+
latest?:string;
14+
isLatest:boolean;
1415
}
1516

1617
exportinterfaceConfig{

‎src/utils/fetch.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
/**
2-
* fetch data from url
3-
*@param {string} url
4-
*@returns Promise
5-
*/
6-
constfetch=(url:string)=>{
7-
returnnewPromise((resolve,reject)=>{
8-
constlib=url.startsWith('https') ?require('https') :require('http');
9-
constrequest=lib.get(url,(response)=>{
10-
// handle http errors
11-
if(response.statusCode<200||response.statusCode>299){
12-
reject(newError('Failed to load page, status code: '+response.statusCode));
13-
}
14-
constbody=[];
15-
// on every content chunk, push it to the data array
16-
response.on('data',(chunk:never)=>body.push(chunk));
17-
// we are done, resolve promise with those joined chunks
18-
response.on('end',()=>resolve(body.join('')));
19-
});
20-
// handle connection errors of the request
21-
request.on('error',(err)=>reject(err));
22-
});
23-
};
1+
exportfunctionstatus(response){
2+
if(response.status>=200&&response.status<300){
3+
returnPromise.resolve(response);
4+
}else{
5+
returnPromise.reject(newError(response.statusText));
6+
}
7+
}
248

25-
exportdefaultfetch;
9+
exportfunctionjson(response){
10+
returnresponse.json();
11+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp