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

Commit2a99028

Browse files
committed
create loader
1 parentd0cc4b0 commit2a99028

File tree

7 files changed

+143
-70
lines changed

7 files changed

+143
-70
lines changed

‎lib/reducers/task-tests/concat-tests.js

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

‎lib/reducers/task-tests/loader.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"use strict";
2+
varfs=require('fs');
3+
varpath=require('path');
4+
varloaderMatch={
5+
js:/^\/\/\s?load\(['"`](.+)['"`]\)$/,
6+
py:/\#\s?load\(['"`](.+)['"`]\)$/
7+
};
8+
functionunlink(targetFile){
9+
returnnewPromise(function(resolve){
10+
if(fs.existsSync(targetFile)){
11+
fs.unlink(targetFile);
12+
}
13+
resolve();
14+
});
15+
}
16+
functionparseLoaders(data,fileType){
17+
varlines=data.split('\n');
18+
varimports='';
19+
vari=0;
20+
while(i<lines.length-1){
21+
i+=1;
22+
varloader=lines[i].match(loaderMatch[fileType]);
23+
if(loader){
24+
varfileToLoad=loader[1];
25+
varpathToFile=path.normalize(path.join(window.coderoad.dir,fileToLoad));
26+
imports.concat(fs.readFileSync(pathToFile,'utf8'));
27+
}
28+
}
29+
varoutput=null;
30+
if(imports.length>0){
31+
output=imports+'\n'.concat(data);
32+
}
33+
else{
34+
output=data;
35+
}
36+
returnoutput;
37+
}
38+
functionreadAppend(targetFile,file){
39+
try{
40+
vardata=fs.readFileSync(file,'utf8');
41+
varfileType=targetFile.substr(targetFile.lastIndexOf('.')+1,targetFile.length);
42+
varoutput=parseLoaders(data,fileType);
43+
fs.appendFileSync(targetFile,output,'utf8');
44+
returntrue;
45+
}
46+
catch(e){
47+
console.log('Error reading test file',e);
48+
}
49+
}
50+
functionconcatTests(targetFile,files){
51+
unlink(targetFile).then(function(){
52+
files.forEach(function(testPath){
53+
returnnewPromise(function(resolve){
54+
resolve(readAppend(targetFile,testPath));
55+
});
56+
});
57+
});
58+
return;
59+
}
60+
functionload(targetFile,files){
61+
concatTests(targetFile,files);
62+
}
63+
Object.defineProperty(exports,"__esModule",{value:true});
64+
exports.default=load;

‎lib/reducers/task-tests/task-tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22
varpath=require('path');
33
varType=require('../../actions/actionTypes');
4-
varconcat_tests_1=require('./concat-tests');
4+
varloader_1=require('./loader');
55
functiontaskTestsReducer(taskTests,action){
66
if(taskTests===void0){taskTests='';}
77
switch(action.type){
88
caseType.SET_PAGE:
99
vartarget=path.join(window.coderoad.tutorialDir||window.coderoad.dir,"_tmp"+window.coderoad.suffix);
10-
returnconcat_tests_1.concatTests(target,action.payload.taskTests);
10+
loader_1.default(target,action.payload.taskTests);
11+
returntarget;
1112
default:
1213
returntaskTests;
1314
}

‎src/reducers/task-tests/concat-tests.ts

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

‎src/reducers/task-tests/loader.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import*asfsfrom'fs';
2+
import*aspathfrom'path';
3+
4+
constloaderMatch={
5+
js:/^\/\/\s?load\(['"`](.+)['"`]\)$/,
6+
py:/\#\s?load\(['"`](.+)['"`]\)$/
7+
};
8+
9+
functionunlink(targetFile:string){
10+
returnnewPromise((resolve)=>{
11+
if(fs.existsSync(targetFile)){
12+
fs.unlink(targetFile);
13+
}
14+
resolve();
15+
});
16+
}
17+
18+
functionparseLoaders(data:string,fileType:string){
19+
letlines=data.split('\n');
20+
letimports='';
21+
// loop over lines and add editor files
22+
leti=0;
23+
while(i<lines.length-1){
24+
i+=1;
25+
letloader:string[]=lines[i].match(loaderMatch[fileType]);
26+
27+
if(loader){
28+
// loader found
29+
letfileToLoad:string=loader[1];
30+
letpathToFile:string=path.normalize(path.join(window.coderoad.dir,fileToLoad));
31+
imports.concat(fs.readFileSync(pathToFile,'utf8'));
32+
}
33+
}
34+
35+
letoutput:string=null;
36+
if(imports.length>0){
37+
output=imports+'\n'.concat(data);
38+
}else{
39+
output=data;
40+
}
41+
returnoutput;
42+
}
43+
44+
functionreadAppend(targetFile:string,file:string):boolean{
45+
try{
46+
letdata=fs.readFileSync(file,'utf8');
47+
letfileType=targetFile.substr(targetFile.lastIndexOf('.')+1,targetFile.length);
48+
letoutput=parseLoaders(data,fileType);
49+
fs.appendFileSync(targetFile,output,'utf8');
50+
returntrue;
51+
}catch(e){
52+
console.log('Error reading test file',e);
53+
}
54+
}
55+
56+
functionconcatTests(targetFile:string,files:any):string{
57+
// delete previous file
58+
unlink(targetFile).then(()=>{
59+
// load tests in order
60+
files.forEach((testPath:string)=>{
61+
// ensure loaded synchronously
62+
returnnewPromise((resolve)=>{
63+
resolve(readAppend(targetFile,testPath));
64+
});
65+
});
66+
});
67+
return;
68+
}
69+
70+
exportdefaultfunctionload(targetFile:string,files:any){
71+
concatTests(targetFile,files);
72+
}

‎src/reducers/task-tests/task-tests.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22
import*aspathfrom'path';
33
import*asTypefrom'../../actions/actionTypes';
4-
import{concatTests}from'./concat-tests';
4+
importloadfrom'./loader';
55

66
exportdefaultfunctiontaskTestsReducer(taskTests='',action:CR.Action):string{
77
switch(action.type){
88
caseType.SET_PAGE:
99
lettarget=path.join(window.coderoad.tutorialDir||window.coderoad.dir,`_tmp${window.coderoad.suffix}`);
10-
returnconcatTests(target,action.payload.taskTests);
10+
load(target,action.payload.taskTests);
11+
returntarget;
1112
default:
1213
returntaskTests;
1314
}

‎tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"src/reducers/run-tests/run.ts",
8484
"src/reducers/run-tests/test-result.ts",
8585
"src/reducers/task-position/task-position.ts",
86-
"src/reducers/task-tests/concat-tests.ts",
86+
"src/reducers/task-tests/loader.ts",
8787
"src/reducers/task-tests/task-tests.ts",
8888
"src/reducers/tasks/tasks.ts",
8989
"src/reducers/tutorials/tutorials.ts",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp