Skip to content

Commit 00a6941

Browse files
committed
init
0 parents  commit 00a6941

37 files changed

+3376
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
This project adheres to [Semantic Versioning](http://semver.org/).
4+
5+
## [0.1.0] - 2016-08-14
6+
- created

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# JS CodeRoad
2+
3+
JavaScript features bundled for [CodeRoad](https://coderoad.github.io) test runners.
4+
5+
## Features
6+
- handles `import`'s and `export`'s
7+
- overrides `console.log` to return correct types
8+
- `exists` function for testing folders and files
9+
- `BASE` paths get replaced with user directory file paths
10+
- provides easy testing of globals

lib/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
exports.signal = '@@@CodeRoad Results@@@';

lib/helpers/babelModules.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.default = "require('babel-core/register')({plugins: [['transform-es2015-modules-commonjs',{loose:true}]]});";

lib/helpers/fileExists.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
function fileExistsPolyfill(dir) {
3+
return "function _fileExists(e,r){void 0===r&&(r=!0);try{accessSync(e,F_OK)}catch(c){if(c)return r||console.log(c),!1}return!0}var _require=require(\"fs\"),accessSync=_require.accessSync,F_OK=_require.F_OK,_require2=require(\"path\"),resolve=_require2.resolve;function exists(p){return _fileExists(resolve('" + dir + "',p))}";
4+
}
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.default = fileExistsPolyfill;

lib/helpers/import-paths.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var importPathRegex = /require\(["'](BASE.+)["']\)([a-zA-Z0-9\-\_]+)?|^import.+?\s?["'](BASE.+)["'];?$/m;
4+
var relativePathRegex = /^BASE/;
5+
function fixImportPaths(dir, str) {
6+
var entries = new Set([]);
7+
return str.split('\n').map(function (line) {
8+
var isMatch = line.match(importPathRegex);
9+
if (!isMatch) {
10+
return line;
11+
}
12+
var importPath = isMatch[1] || isMatch[2];
13+
if (importPath.match(relativePathRegex)) {
14+
var newPath = path_1.join(dir, importPath.replace('BASE', ''));
15+
var newLine = line.replace(importPath, newPath);
16+
if (!entries.has(newLine)) {
17+
entries.add(newLine);
18+
return newLine;
19+
}
20+
return '';
21+
}
22+
return line;
23+
}).join('\n');
24+
}
25+
Object.defineProperty(exports, "__esModule", { value: true });
26+
exports.default = fixImportPaths;

lib/helpers/importPaths.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var importPathRegex = /require\(["'](BASE.+)["']\)([a-zA-Z0-9\-\_]+)?|^import.+?\s?["'](BASE.+)["'];?$/m;
4+
var relativePathRegex = /^BASE/;
5+
function fixImportPaths(_a) {
6+
var dir = _a.dir, content = _a.content;
7+
var entries = new Set([]);
8+
return content.split('\n').map(function (line) {
9+
var isMatch = line.match(importPathRegex);
10+
if (!isMatch) {
11+
return line;
12+
}
13+
var importPath = isMatch[1] || isMatch[2];
14+
if (importPath.match(relativePathRegex)) {
15+
var newPath = path_1.join(dir, importPath.replace('BASE', ''));
16+
var newLine = line.replace(importPath, newPath);
17+
if (!entries.has(newLine)) {
18+
entries.add(newLine);
19+
return newLine;
20+
}
21+
return '';
22+
}
23+
return line;
24+
}).join('\n');
25+
}
26+
Object.defineProperty(exports, "__esModule", { value: true });
27+
exports.default = fixImportPaths;

lib/helpers/overrideRequire.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.default = "require = require('rewire');";

lib/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
var babelModules_1 = require('./helpers/babelModules');
3+
var process_console_log_1 = require('process-console-log');
4+
var fileExists_1 = require('./helpers/fileExists');
5+
var overrideRequire_1 = require('./helpers/overrideRequire');
6+
var importPaths_1 = require('./helpers/importPaths');
7+
var jsCodeRoad = function (_a) {
8+
var dir = _a.dir, content = _a.content;
9+
return ('(function(){\n'
10+
+ process_console_log_1.logger
11+
+ babelModules_1.default
12+
+ fileExists_1.default(dir)
13+
+ overrideRequire_1.default
14+
+ importPaths_1.default({ dir: dir, content: content })
15+
+ '\n}());');
16+
};
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
exports.default = jsCodeRoad;

lib/runner/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
var start_runner_1 = require('./start-runner');
3+
var runner_process_1 = require('./runner-process');
4+
function runner(_a) {
5+
var dir = _a.dir, taskPosition = _a.taskPosition, handleResult = _a.handleResult, testPath = _a.testPath;
6+
var runner = runner_process_1.default({ dir: dir, taskPosition: taskPosition, testPath: testPath });
7+
return start_runner_1.default({ runner: runner, handleResult: handleResult, taskPosition: taskPosition });
8+
}
9+
Object.defineProperty(exports, "__esModule", { value: true });
10+
exports.default = runner;

lib/runner/paths/mocha.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
var node_file_exists_1 = require('node-file-exists');
3+
var path_1 = require('path');
4+
function getMocha() {
5+
var mocha = path_1.join(__dirname, '..', '..', '..', '..', 'mocha', 'bin', 'mocha');
6+
if (!node_file_exists_1.default(mocha)) {
7+
mocha = path_1.join(__dirname, '..', '..', '..', 'node_modules', 'mocha', 'bin', 'mocha');
8+
if (!node_file_exists_1.default(mocha)) {
9+
var error = 'Error finding mocha';
10+
throw (error);
11+
}
12+
}
13+
return mocha;
14+
}
15+
Object.defineProperty(exports, "__esModule", { value: true });
16+
exports.default = getMocha;

lib/runner/paths/node.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
function getNode() {
4+
if (process.platform === 'darwin' && process.resourcesPath) {
5+
return path_1.resolve(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper');
6+
}
7+
else if (process.platform.match(/win/)) {
8+
return 'node';
9+
}
10+
return process.execPath;
11+
}
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.default = getNode;

lib/runner/runner-process.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var child_process_1 = require('child_process');
4+
var mocha_1 = require('./paths/mocha');
5+
var node_1 = require('./paths/node');
6+
var reporterPath = path_1.join(__dirname, '..', 'reporter', 'index.js');
7+
var node = node_1.default();
8+
var mocha = mocha_1.default();
9+
function spawnRunnerProcess(_a) {
10+
var dir = _a.dir, taskPosition = _a.taskPosition, testPath = _a.testPath;
11+
var options = {
12+
cwd: dir
13+
};
14+
if (options.env == null) {
15+
options.env = Object.create(process.env);
16+
}
17+
Object.assign(options.env, {
18+
ATOM_SHELL_INTERNAL_RUN_AS_NODE: 1,
19+
DIR: dir,
20+
TASK_POSITION: taskPosition,
21+
NODE_PATH: path_1.join(dir, 'node_modules'),
22+
});
23+
return child_process_1.spawn(node, [
24+
mocha,
25+
'--bail',
26+
'--harmony',
27+
'--no-colors',
28+
'--timeout=3000',
29+
'--compilers js:babel-core/register',
30+
("--reporter=" + reporterPath),
31+
testPath,
32+
], options);
33+
}
34+
Object.defineProperty(exports, "__esModule", { value: true });
35+
exports.default = spawnRunnerProcess;

lib/runner/start-runner.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
var process_console_log_1 = require('process-console-log');
3+
var constants_1 = require('../constants');
4+
var signalMatch = new RegExp(constants_1.signal);
5+
function startRunner(_a) {
6+
var runner = _a.runner, handleResult = _a.handleResult, taskPosition = _a.taskPosition;
7+
var final = null;
8+
new Promise(function run(resolve, reject) {
9+
runner.stdout.on('data', function onData(data) {
10+
data = data.toString();
11+
var match = signalMatch.exec(data);
12+
if (!match) {
13+
try {
14+
process_console_log_1.parseLog(data);
15+
}
16+
catch (e) {
17+
process_console_log_1.parseLog(data);
18+
}
19+
return;
20+
}
21+
var resultString = data.substring(match.index + constants_1.signal.length);
22+
var result = JSON.parse(JSON.stringify(resultString));
23+
if (typeof result === 'string') {
24+
result = JSON.parse(result);
25+
}
26+
switch (result.pass) {
27+
case true:
28+
final = result.passes[result.passes.length - 1];
29+
break;
30+
case false:
31+
final = result.failures[0];
32+
break;
33+
default:
34+
console.log('error processing result: ', result);
35+
}
36+
final.change = final.taskPosition - taskPosition;
37+
final.pass = final.change > 0;
38+
final.completed = result.pass;
39+
handleResult(final);
40+
});
41+
runner.stderr.on('data', function onError(data) {
42+
console.log('test error', data.toString());
43+
});
44+
runner.on('close', function onClose(code) {
45+
resolve(final);
46+
});
47+
});
48+
}
49+
Object.defineProperty(exports, "__esModule", { value: true });
50+
exports.default = startRunner;

lib/testPath.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
function tmpTestName(testFile) {
4+
return path_1.resolve(__dirname, '..', '.tmp', testFile + '.js');
5+
}
6+
Object.defineProperty(exports, "__esModule", { value: true });
7+
exports.default = tmpTestName;

lib/writeTests/helpers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
function helpers(projectDir) {
3+
return "\n// run time compiler\nrequire('babel-core/register')({plugins: [['transform-es2015-modules-commonjs',{\nloose:true}]]});\n\n// fileExists test helper\nfunction fileExists(e,r){void 0===r&&(r=!0);try{accessSync(e,F_OK)}catch(c){if(c)return r||console.log(c),!1}return!0}var _require=require(\"fs\"),accessSync=_require.accessSync,F_OK=_require.F_OK,_require2=require(\"path\"),resolve=_require2.resolve;function exists(p){return fileExists(resolve('" + projectDir + "',p))}\n\n// overwrite require to catch globals in tests\nrequire = require('rewire');\n\n";
4+
}
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.default = helpers;

lib/writeTests/import-paths.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var importPathRegex = /require\(["'](BASE.+)["']\)([a-zA-Z0-9\-\_]+)?|^import.+?\s?["'](BASE.+)["'];?$/m;
4+
var relativePathRegex = /^BASE/;
5+
function fixImportPaths(dir, str) {
6+
var entries = new Set([]);
7+
return str.split('\n').map(function (line) {
8+
var isMatch = line.match(importPathRegex);
9+
if (!isMatch) {
10+
return line;
11+
}
12+
var importPath = isMatch[1] || isMatch[2];
13+
if (importPath.match(relativePathRegex)) {
14+
var newPath = path_1.join(dir, importPath.replace('BASE', ''));
15+
var newLine = line.replace(importPath, newPath);
16+
if (!entries.has(newLine)) {
17+
entries.add(newLine);
18+
return newLine;
19+
}
20+
return '';
21+
}
22+
return line;
23+
}).join('\n');
24+
}
25+
Object.defineProperty(exports, "__esModule", { value: true });
26+
exports.default = fixImportPaths;

lib/writeTests/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
var fs = require('fs');
3+
var path_1 = require('path');
4+
var process_console_log_1 = require('process-console-log');
5+
var import_paths_1 = require('./import-paths');
6+
var helpers_1 = require('./helpers');
7+
var tmpPath = path_1.join(__dirname, '..', '..', '.tmp');
8+
function writeTest(_a) {
9+
var dir = _a.dir, tests = _a.tests, testPath = _a.testPath;
10+
var output = "(function(){\n"
11+
.concat(process_console_log_1.logger)
12+
.concat(helpers_1.default(dir))
13+
.concat(import_paths_1.default(dir, tests))
14+
.concat('\n}());');
15+
writeTestFile(testPath, output);
16+
}
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
exports.default = writeTest;
19+
function writeTestFile(testPath, output) {
20+
if (!fs.existsSync(tmpPath)) {
21+
fs.mkdirSync(tmpPath);
22+
}
23+
return new Promise(function (resolve, reject) {
24+
fs.writeFile(testPath, output, function (err) {
25+
if (err) {
26+
reject(err);
27+
}
28+
resolve();
29+
});
30+
});
31+
}

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "js-coderoad",
3+
"version": "0.1.0",
4+
"description": "bundled javascript features for coderoad test runners",
5+
"keywords": [
6+
"coderoad",
7+
"mocha",
8+
"javascript",
9+
"js"
10+
],
11+
"homepage": "https://github.com/coderoad/js-coderoad#readme",
12+
"bugs": {
13+
"url": "https://github.com/coderoad/js-coderoad/issues"
14+
},
15+
"license": "ISC",
16+
"author": "Shawn McKay <[email protected]>",
17+
"files": [
18+
"lib",
19+
"package.json",
20+
"*.md"
21+
],
22+
"main": "lib/index.js",
23+
"repository": {
24+
"type": "git",
25+
"url": "git+https://github.com/coderoad/js-coderoad.git"
26+
},
27+
"scripts": {
28+
"test": "echo \"Error: no test specified\" && exit 1"
29+
},
30+
"dependencies": {
31+
"babel-plugin-transform-es2015-modules-commonjs": "6.11.5",
32+
"babel-register": "6.11.6",
33+
"node-file-exists": "1.1.0",
34+
"process-console-log": "0.2.3",
35+
"rewire-coderoad": "3.1.1"
36+
}
37+
}

src/helpers/babelModules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// babel with es2015 import/export
2+
export default `require('babel-core/register')({plugins: [['transform-es2015-modules-commonjs',{loose:true}]]});`;

src/helpers/fileExists.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// exists(path/to/file) - relative to project directory
2+
export default function fileExistsPolyfill(dir: string): string {
3+
return `function _fileExists(e,r){void 0===r&&(r=!0);try{accessSync(e,F_OK)}catch(c){if(c)return r||console.log(c),!1}return!0}var _require=require("fs"),accessSync=_require.accessSync,F_OK=_require.F_OK,_require2=require("path"),resolve=_require2.resolve;function exists(p){return _fileExists(resolve('${dir}',p))}`;
4+
}

0 commit comments

Comments
 (0)