Skip to content

Commit 2a99028

Browse files
committed
create loader
1 parent d0cc4b0 commit 2a99028

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+
var fs = require('fs');
3+
var path = require('path');
4+
var loaderMatch = {
5+
js: /^\/\/\s?load\(['"`](.+)['"`]\)$/,
6+
py: /\#\s?load\(['"`](.+)['"`]\)$/
7+
};
8+
function unlink(targetFile) {
9+
return new Promise(function (resolve) {
10+
if (fs.existsSync(targetFile)) {
11+
fs.unlink(targetFile);
12+
}
13+
resolve();
14+
});
15+
}
16+
function parseLoaders(data, fileType) {
17+
var lines = data.split('\n');
18+
var imports = '';
19+
var i = 0;
20+
while (i < lines.length - 1) {
21+
i += 1;
22+
var loader = lines[i].match(loaderMatch[fileType]);
23+
if (loader) {
24+
var fileToLoad = loader[1];
25+
var pathToFile = path.normalize(path.join(window.coderoad.dir, fileToLoad));
26+
imports.concat(fs.readFileSync(pathToFile, 'utf8'));
27+
}
28+
}
29+
var output = null;
30+
if (imports.length > 0) {
31+
output = imports + '\n'.concat(data);
32+
}
33+
else {
34+
output = data;
35+
}
36+
return output;
37+
}
38+
function readAppend(targetFile, file) {
39+
try {
40+
var data = fs.readFileSync(file, 'utf8');
41+
var fileType = targetFile.substr(targetFile.lastIndexOf('.') + 1, targetFile.length);
42+
var output = parseLoaders(data, fileType);
43+
fs.appendFileSync(targetFile, output, 'utf8');
44+
return true;
45+
}
46+
catch (e) {
47+
console.log('Error reading test file', e);
48+
}
49+
}
50+
function concatTests(targetFile, files) {
51+
unlink(targetFile).then(function () {
52+
files.forEach(function (testPath) {
53+
return new Promise(function (resolve) {
54+
resolve(readAppend(targetFile, testPath));
55+
});
56+
});
57+
});
58+
return;
59+
}
60+
function load(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
var path = require('path');
33
var Type = require('../../actions/actionTypes');
4-
var concat_tests_1 = require('./concat-tests');
4+
var loader_1 = require('./loader');
55
function taskTestsReducer(taskTests, action) {
66
if (taskTests === void 0) { taskTests = ''; }
77
switch (action.type) {
88
case Type.SET_PAGE:
99
var target = path.join(window.coderoad.tutorialDir || window.coderoad.dir, "_tmp" + window.coderoad.suffix);
10-
return concat_tests_1.concatTests(target, action.payload.taskTests);
10+
loader_1.default(target, action.payload.taskTests);
11+
return target;
1112
default:
1213
return taskTests;
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 * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
const loaderMatch = {
5+
js: /^\/\/\s?load\(['"`](.+)['"`]\)$/,
6+
py: /\#\s?load\(['"`](.+)['"`]\)$/
7+
};
8+
9+
function unlink(targetFile: string) {
10+
return new Promise((resolve) => {
11+
if (fs.existsSync(targetFile)) {
12+
fs.unlink(targetFile);
13+
}
14+
resolve();
15+
});
16+
}
17+
18+
function parseLoaders(data: string, fileType: string) {
19+
let lines = data.split('\n');
20+
let imports = '';
21+
// loop over lines and add editor files
22+
let i = 0;
23+
while (i < lines.length - 1) {
24+
i += 1;
25+
let loader: string[] = lines[i].match(loaderMatch[fileType]);
26+
27+
if (loader) {
28+
// loader found
29+
let fileToLoad: string = loader[1];
30+
let pathToFile: string = path.normalize(path.join(window.coderoad.dir, fileToLoad));
31+
imports.concat(fs.readFileSync(pathToFile, 'utf8'));
32+
}
33+
}
34+
35+
let output: string = null;
36+
if (imports.length > 0) {
37+
output = imports + '\n'.concat(data);
38+
} else {
39+
output = data;
40+
}
41+
return output;
42+
}
43+
44+
function readAppend(targetFile: string, file: string): boolean {
45+
try {
46+
let data = fs.readFileSync(file, 'utf8');
47+
let fileType = targetFile.substr(targetFile.lastIndexOf('.') + 1, targetFile.length);
48+
let output = parseLoaders(data, fileType);
49+
fs.appendFileSync(targetFile, output, 'utf8');
50+
return true;
51+
} catch (e) {
52+
console.log('Error reading test file', e);
53+
}
54+
}
55+
56+
function concatTests(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+
return new Promise((resolve) => {
63+
resolve(readAppend(targetFile, testPath));
64+
});
65+
});
66+
});
67+
return;
68+
}
69+
70+
export default function load(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 * as path from 'path';
33
import * as Type from '../../actions/actionTypes';
4-
import {concatTests} from './concat-tests';
4+
import load from './loader';
55

66
export default function taskTestsReducer(taskTests = '', action: CR.Action): string {
77
switch (action.type) {
88
case Type.SET_PAGE:
99
let target = path.join(window.coderoad.tutorialDir || window.coderoad.dir, `_tmp${window.coderoad.suffix}`);
10-
return concatTests(target, action.payload.taskTests);
10+
load(target, action.payload.taskTests);
11+
return target;
1112
default:
1213
return taskTests;
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)