Skip to content

Commit 6889d06

Browse files
committed
loadEditor, loadGlobal functions
1 parent a67b2ca commit 6889d06

9 files changed

+67
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.5.3] - 2016-03-13
6+
- `loadEditor` for loading editor files
7+
- `loadGlobal` for loading global data
8+
59
## [0.5.1] - 2016-03-11
610
- optimizations
711
- handle older NPM versions

lib/create-runner.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
var path = require('path');
33
var exists_1 = require('./exists');
4+
require('./loaders');
45
var spawn = require('child_process').spawn;
56
var node = null;
67
if (process.platform === 'darwin' && process.resourcesPath) {
@@ -37,6 +38,7 @@ function createRunner(config, testFile) {
3738
'--harmony',
3839
'--no-colors',
3940
("--reporter=" + path.join(__dirname, 'reporter.js')),
41+
path.join(__dirname, 'loaders'),
4042
testFile
4143
], options);
4244
}

lib/loaders.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
var vm = require('vm');
3+
var fs = require('fs');
4+
var path = require('path');
5+
function loadEditor(pathToContext) {
6+
var absPath = path.join(process.env.DIR, getCrossPlatformPath(pathToContext));
7+
var context = fs.readFileSync(absPath, 'utf8');
8+
vm.runInThisContext(context);
9+
}
10+
function loadGlobal(name, pathToData) {
11+
pathToData = path.join(process.env.TUTORIAL_DIR, getCrossPlatformPath(pathToData));
12+
global[name] = require(pathToData);
13+
}
14+
function getCrossPlatformPath(pathTo) {
15+
pathTo = path.normalize(pathTo);
16+
if (process.platform.match(/win/)) {
17+
pathTo = pathTo.replace(/\\/g, '/');
18+
}
19+
else {
20+
pathTo = pathTo.replace(/\//g, '\\');
21+
}
22+
return pathTo;
23+
}
24+
global.loadEditor = loadEditor;
25+
global.loadGlobal = loadGlobal;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mocha-coderoad",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"description": "mocha test runner & reporter for atom-coderoad",
55
"main": "lib/runner.js",
66
"scripts": {

src/create-runner.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from 'path';
22
import {fileExists} from './exists';
3+
import './loaders';
34
const spawn = require('child_process').spawn;
45

56
// get absolute path to node exec
@@ -45,6 +46,7 @@ export function createRunner(config: CR.Config, testFile: string) {
4546
'--harmony',
4647
'--no-colors',
4748
`--reporter=${path.join(__dirname, 'reporter.js') }`,
49+
path.join(__dirname, 'loaders'),
4850
testFile
4951
], options);
5052
// .concat(runnerOptions)

src/loaders.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as vm from 'vm';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
5+
function loadEditor(pathToContext: string): void {
6+
let absPath = path.join(process.env.DIR, getCrossPlatformPath(pathToContext));
7+
let context = fs.readFileSync(absPath, 'utf8');
8+
vm.runInThisContext(context);
9+
}
10+
11+
function loadGlobal(name: string, pathToData: string): void {
12+
pathToData = path.join(process.env.TUTORIAL_DIR, getCrossPlatformPath(pathToData));
13+
global[name] = require(pathToData);
14+
}
15+
16+
function getCrossPlatformPath(pathTo: string): string {
17+
pathTo = path.normalize(pathTo);
18+
if (process.platform.match(/win/)) {
19+
pathTo = pathTo.replace(/\\/g, '/');
20+
} else {
21+
pathTo = pathTo.replace(/\//g, '\\');
22+
}
23+
return pathTo;
24+
}
25+
26+
global.loadEditor = loadEditor;
27+
global.loadGlobal = loadGlobal;

src/typings/cr/globals.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Global {
2+
load: (pathToContext: string) => void;
3+
}

src/typings/tsd.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/// <reference path="es6-promise/es6-promise.d.ts" />
33
/// <reference path="mocha/mocha.d.ts" />
44
/// <reference path="node/node.d.ts" />
5+
/// <reference path="cr/globals.d.ts" />
56
/// <reference path="cr/cr.d.ts" />

tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
"files": [
1818
"src/create-runner.ts",
1919
"src/exists.ts",
20+
"src/loaders.ts",
2021
"src/reporter.ts",
2122
"src/runner.ts",
2223
"src/utils.ts",
2324
"src/typings/chai/chai.d.ts",
2425
"src/typings/cr/cr.d.ts",
26+
"src/typings/cr/globals.d.ts",
2527
"src/typings/es6-promise/es6-promise.d.ts",
2628
"src/typings/mocha/mocha.d.ts",
2729
"src/typings/node/node.d.ts",

0 commit comments

Comments
 (0)