-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
104 lines (86 loc) · 2.45 KB
/
api.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
"use strict";
var _ = require('lodash');
var reader = require('./lib/reader');
var parser = require('./lib/parser');
var runner = require('./lib/runner');
var CONSTANTS = require('./lib/constants');
function readNdCommandFromFile(filePath, options) {
try {
return _.flow([
reader.readFile,
parser.parseNdCommand,
function (commands) {
return runner(commands, options);
}
])(filePath);
} catch (error) {
throw error;
}
}
function readNdJsonFromFile(filePath, options) {
try {
return _.flow([
reader.readFile,
parser.parseNdJson,
function (commands) {
return runner(commands, options);
}
])(filePath);
} catch (error) {
throw error;
}
}
function readJsonFromFile(filePath, options) {
try {
return _.flow([
reader.readFile,
parser.parseJson,
function (commands) {
return runner(commands, options);
}
])(filePath);
} catch (error) {
throw error;
}
}
function readFromArguments(args, options) {
try {
function mapHandler(params) {
var handler;
switch (params.format) {
case CONSTANTS.FORMAT_TYPE.NDJSON:
handler = parser.parseNdJson;
break;
case CONSTANTS.FORMAT_TYPE.JSON:
handler = parser.parseJson;
break;
case CONSTANTS.FORMAT_TYPE.NDCOMMAND:
handler = parser.parseNdCommand;
break;
default:
throw new Error(`'${params.format}' is not a supported formating`);
}
var content = reader.readFile(params.file);
return handler(content);
}
return _.flow([
reader.readArguments,
mapHandler,
function (commands) {
return runner(commands, options);
}
])(args);
} catch (error) {
throw error;
}
}
// *** Exports API ****************************
function defaultExport (filePath, options) {
return readNdCommandsFromFile(filePath, options);
}
defaultExport.fromNdCommandFile = readNdCommandFromFile;
defaultExport.fromNdJsonFile = readNdJsonFromFile;
defaultExport.fromJsonFile = readJsonFromFile;
defaultExport.fromArguments = readFromArguments;
// ********
module.exports = defaultExport;