c479c05b73441aba83ce6c3851444319f655b095
[platform/framework/web/crosswalk-tizen.git] /
1 "use strict";
2
3 //
4 // helpers used on the specs
5 //
6
7
8 var _fs = require('fs');
9 var _path = require('path');
10 var stripJsonComments = require('strip-json-comments');
11
12
13 // ---
14
15
16 exports.CACHE = {};
17 exports.COMPARE_FOLDER = _path.join(__dirname, 'compare');
18
19
20 // ---
21
22
23 exports.readIn = function(id) {
24   return exports.readFile(_path.join(exports.COMPARE_FOLDER, id + '-in.js'));
25 };
26
27
28 exports.readOut = function(id) {
29   return exports.readFile(_path.join(exports.COMPARE_FOLDER, id + '-out.js'));
30 };
31
32
33 exports.readConfig = function(id) {
34   var filePath = _path.join(exports.COMPARE_FOLDER, id + '-config.json');
35   return JSON.parse(stripJsonComments(exports.readFile(filePath) + '\n'));
36 };
37
38
39 exports.readFile = function(path) {
40   // we cache the results to avoid redundant I/O
41   if (!(path in exports.CACHE)) {
42     exports.CACHE[path] = exports.lineFeed(_fs.readFileSync(path).toString());
43   }
44   return exports.CACHE[path];
45 };
46
47
48 exports.purge = function(dir) {
49   if (!exports.SHOULD_PURGE) return;
50   _fs.readdirSync(dir).forEach(function(relPath) {
51     var path = _path.join(dir, relPath);
52     if (_fs.statSync(path).isDirectory()) {
53       exports.purge(path);
54     } else {
55       _fs.unlinkSync(path);
56     }
57   });
58   _fs.rmdirSync(dir);
59 };
60
61
62 exports.mkdir = function(dir) {
63   if (!_fs.existsSync(dir)) {
64     _fs.mkdirSync(dir);
65   }
66 };
67
68 exports.lineFeed = function(text) {
69   return text.replace(/\r\n?|[\n\u2028\u2029]/g, "\n");
70 };