Skip to content

Commit c4293aa

Browse files
committed
improved comments, async version of writeFile
1 parent 40d5d67 commit c4293aa

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

lib/write-test.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ var process_console_log_1 = require('process-console-log');
44
var exists_1 = require('./exists');
55
var constants_1 = require('./constants');
66
function writeTest(config, testString) {
7-
var output = process_console_log_1.logger + exists_1.default(config.dir) + testString;
8-
fs_1.writeFileSync(constants_1.testPath, output, 'utf8');
7+
return new Promise(function (resolve, reject) {
8+
var output = process_console_log_1.logger + exists_1.default(config.dir) + testString;
9+
fs_1.writeFile(constants_1.testPath, output, function (err) {
10+
if (err) {
11+
reject(err);
12+
}
13+
resolve();
14+
});
15+
});
916
}
1017
Object.defineProperty(exports, "__esModule", { value: true });
1118
exports.default = writeTest;

src/constants.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import {join} from 'path';
22

3+
// test results are taken after this signal
4+
// this helps to avoid parsing console.logs before the output
35
export const signal = '@@@CodeRoad Results@@@';
6+
7+
// location for a temporary test file
8+
// which combines user code with helper functions
49
export const testPath = join(__dirname, '.tmp.js');

src/exists.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// checks if file exists from cwd
32
// appends this function to the top of the tests
43
// require('node-file-exists')(require('path').;

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export default function runner({testString, config, handleResult}) {
1010
const runner = runnerProcess(config);
1111

1212
var final = null;
13+
14+
// take code after the signal to avoid confusing console.log statements
15+
// with test output
1316
const signalMatch = new RegExp(signal);
1417

1518
return new Promise(function run(resolve, reject) {

src/write-test.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import {writeFileSync} from 'fs';
1+
import {writeFile} from 'fs';
22
import {logger} from 'process-console-log';
33
import exists from './exists';
44
import {testPath} from './constants';
55

66
export default function writeTest(config, testString: string) {
7-
8-
// append logger
9-
const output = logger + exists(config.dir) + testString;
10-
// write test file
11-
writeFileSync(testPath, output, 'utf8');
7+
return new Promise((resolve, reject) => {
8+
// append logger
9+
const output = logger + exists(config.dir) + testString;
10+
// write test file
11+
writeFile(testPath, output, (err) => {
12+
if (err) { reject(err); }
13+
resolve();
14+
});
15+
});
1216
}

0 commit comments

Comments
 (0)