Skip to content

Commit 3e3d29a

Browse files
committed
change to inline parse loaders
1 parent 9a8b781 commit 3e3d29a

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

lib/reducers/run-tests/parse-loaders.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,23 @@ function parseLoaders(data, fileType) {
1414
console.log("File type \"" + fileType + "\" not supported.");
1515
return '';
1616
}
17-
var imports = '';
1817
var i = -1;
1918
var lines = data.split('\n');
19+
var filesLoaded = [];
2020
while (i < lines.length - 1) {
2121
i += 1;
2222
var loader = lines[i].match(loaderMatch[fileType]);
2323
if (loader) {
2424
var fileToLoad = loader[1];
25+
if (filesLoaded.indexOf(fileToLoad) > -1) {
26+
console.log("\"" + fileToLoad + "\" already loaded.");
27+
continue;
28+
}
2529
var pathToFile = path.normalize(path.join(window.coderoad.dir, fileToLoad));
26-
var file = fs.readFileSync(pathToFile, 'utf8');
27-
imports += file;
30+
lines[i] = fs.readFileSync(pathToFile, 'utf8');
2831
}
2932
}
30-
var output = null;
31-
if (imports.length > 0) {
32-
output = imports + '\n'.concat(data);
33-
}
34-
else {
35-
output = data;
36-
}
37-
return output;
33+
return lines.join('\n');
3834
}
3935
Object.defineProperty(exports, "__esModule", { value: true });
4036
exports.default = parseLoaders;

lib/services/write.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
var fs = require('fs');
3+
var path = require('path');
4+
function getPath(filePath) {
5+
return path.normalize(path.join(window.coderoad.dir, filePath));
6+
}
7+
function write(filePath, text) {
8+
try {
9+
fs.writeFileSync(getPath(filePath), text, 'utf8');
10+
}
11+
catch (e) {
12+
if (e) {
13+
console.log('Error writing to filePath', filePath);
14+
}
15+
}
16+
}
17+
exports.write = write;
18+
function readWrite(writePath, readPath) {
19+
try {
20+
write(writePath, fs.readFileSync(getPath(readPath)));
21+
}
22+
catch (e) {
23+
if (e) {
24+
console.log('Error reading from filePath', readPath);
25+
}
26+
}
27+
}
28+
exports.readWrite = readWrite;

src/reducers/run-tests/parse-loaders.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as path from 'path';
22
import * as fs from 'fs';
33

44
const supportedFileTypes = ['js', 'jsx', 'ts', 'py'];
5-
const js = /^\/\/\s?load\(['"`](.+)['"`]\)$/m;
5+
const js = /^\/\/\s?load\(['"`](.+)['"`]\)$/m; // // load('file'),
66
const loaderMatch = {
7-
js, // // load('file'),
7+
js,
88
ts: js,
99
jsx: js,
1010
py: /^#\s?load\(['"`](.+)['"`]\)$/m // # load('file')
@@ -17,29 +17,28 @@ export default function parseLoaders(data: string, fileType: string) {
1717
return '';
1818
}
1919

20-
let imports = '';
2120
// loop over lines and add editor files
2221
let i = -1;
2322
let lines = data.split('\n');
2423

24+
let filesLoaded = [];
25+
2526
while (i < lines.length - 1) {
2627
i += 1;
2728
let loader: string[] = lines[i].match(loaderMatch[fileType]);
2829

2930
if (loader) {
3031
// loader found
3132
let fileToLoad: string = loader[1];
33+
34+
if (filesLoaded.indexOf(fileToLoad) > -1) {
35+
console.log(`"${fileToLoad}" already loaded.`);
36+
continue;
37+
}
38+
3239
let pathToFile: string = path.normalize(path.join(window.coderoad.dir, fileToLoad));
33-
let file: string = fs.readFileSync(pathToFile, 'utf8');
34-
imports += file;
40+
lines[i] = fs.readFileSync(pathToFile, 'utf8');
3541
}
3642
}
37-
38-
let output: string = null;
39-
if (imports.length > 0) {
40-
output = imports + '\n'.concat(data);
41-
} else {
42-
output = data;
43-
}
44-
return output;
43+
return lines.join('\n');
4544
}

0 commit comments

Comments
 (0)