-
Notifications
You must be signed in to change notification settings - Fork 8
/
convert.js
executable file
路110 lines (76 loc) 路 3.54 KB
/
convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
//core
const fs = require('fs');
const path = require('path');
//npm
const builtinModules = require('builtin-modules');
//project
const _suman = global.__suman = (global.__suman || {});
module.exports = function convertSrcToDest(source,dest){
};
const file = fs.readFileSync(path.resolve(__dirname + '/../../test/mocha-conversion-tests/mocha-test0.js'), 'utf8');
const lines = String(file).split('\n'); //replace all lines but new line chars
console.log(lines);
const result = [];
//const coreModuleMatches = /'^(var|const|,)[a-z\$\_]{1,}=require\((\'|")(assert|fs|path)(\'|")\)[;|,]{0,1}$/;
//const coreModuleMatches = '^\s*(?:var|const|,)\s*([a-z$_]+\s*=\s*require\((\'|")(?:(' + builtinModules.join('|') + '))\2\),?[\n\r\t\s]*)*;$';
//const coreModuleMatches = `^\s*(var|const|,)\s*[a-zA-Z\_\$]+\s*=\s*require\(('|")(${builtinModules.join('|')})('|")\)`;
//const coreModuleMatches = new RegExp(`require\(('|")(${builtinModules.join('|')})('|")\)`);
const coreModuleMatches = new RegExp(`("|')(${ builtinModules.join('|') })\\1`);
//let rgxStr = 'require\(("|\')' + builtinModules.join('|') + '("|\')\)\\1';
console.log('rgxStr:', coreModuleMatches);
//let coreModuleMatches = new RegExp(rgxStr);
const coreModules = [];
let firstDescribeMatch = false;
const indexes = {
'index_of_top_level_describe': null
};
lines.forEach(function (line, index) {
const matchesDescribe = line.match(/^\s*describe\(/);
const matchesIt = line.match(/^\s*it\(/);
const matchesFn = line.match(/function\(\){/);
const matchesFnWithDone = line.match(/function\([a-z\$\_]{1,}\){/);
const coreModuleMatch = line.match(coreModuleMatches);
if (coreModuleMatch) {
coreModuleMatch.forEach(function (m) {
console.log('core module match:', m);
coreModules.push(m);
});
}
if (matchesDescribe && !firstDescribeMatch) {
firstDescribeMatch = true;
indexes.index_of_top_level_describe = index;
result.push(line.replace(/^\s*describe\(/, 'Test.describe(').replace('function(){', 'function(){'));
return;
}
if (matchesDescribe) {
firstDescribeMatch = true;
result.push(line.replace(/^\s*describe\(/, 'this.describe(').replace('function(){', 'function(){'));
return;
}
if (matchesIt && matchesIt.length > 0) {
if (matchesFn && matchesFn.length === 1) {
result.push(line.replace('it(', 'this.it(').replace('function(){', 'function(t){'));
return;
}
else if (matchesFnWithDone && matchesFnWithDone.length === 1) {
result.push(line.replace('it(', 'this.it(').replace(/function\([a-z]{1,}\){/, 'function(t,done){'));
return;
}
else if (matchesFn && matchesFn.length > 1) {
throw new Error('File cannot be converted.');
}
}
result.push(line);
});
let data = result[indexes.index_of_top_level_describe];
data = data.replace('function(){', 'function(' + coreModules.filter(function(item){
return !String(item).match(/('|")/g);
}).join(',') + '){');
result.splice(indexes.index_of_top_level_describe, 1, data);
result.splice(indexes.index_of_top_level_describe, 0, '');
result.splice(indexes.index_of_top_level_describe, 0, 'const Test = suman.init(module);');
result.splice(indexes.index_of_top_level_describe, 0, 'const suman = require(\'suman\');');
result.splice(indexes.index_of_top_level_describe, 0, '');
fs.writeFileSync(path.resolve(__dirname + '/../../test/mocha-conversion-tests/write-to.js'), result.join('\n'));
process.exit();