-
Notifications
You must be signed in to change notification settings - Fork 1
/
inout
executable file
·146 lines (122 loc) · 3.26 KB
/
inout
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var
fs=require("fs"),
rw=require("rw"),
JSONstat=require("jsonstat-toolkit"),
dataset=function(contents){
var json;
try{
json=JSON.parse(contents);
}catch (e){
console.error("Error: The input does not containt JSON.");
process.exit(1);
}
var j=JSONstat(json);
if(j.length===0 ||
(
j.class!=="dataset" &&
j.class!=="collection" &&
j.class!=="bundle"
)
){
console.error("Error: The input does not containt JSON-stat.");
process.exit(1);
}
var
ds=(j.class==="dataset") ? j : j.Dataset(0),
i=ds.length,
len=1
;
for(; i--;){
len*=ds.Dimension(i).length;
}
if(len!==ds.n){
console.error("Error: The input does not containt valid JSON-stat.");
process.exit(1);
}
return ds;
},
main=function(argv, callback, isString, outBinary, inBinary){
var
outcoding=outBinary ? null : "utf8",
incoding=inBinary ? null : "utf8"
;
if(!argv.stream){
if(argv._.length<2){
console.error("Error: When --stream is not enabled you must specify two filenames.");
process.exit(1);
}
//arguments
var
result,
input=argv._[0],
out=function(output, content, big){
if(fs.existsSync(output)){
console.log("Warning: " + output + " already exists.");
var d=new Date();
output+="-" + d.toJSON().replace(/-|:|\.|T|Z/g, "");
if(fs.existsSync(output)){
console.error("Error: Tried" + output + " but it already exists too. Run again.");
process.exit(1);
}
console.log("Warning: Using " + output + " instead.");
}
//So far, only in the non-stream interface...
if(big && Array.isArray(content)){
var
len=content.length,
half=Math.ceil(len/2),
first=content.slice(0, half),
second=content.slice(half-len)
;
fs.writeFileSync(output, first.join("\n")+"\n", { flag: "a+" });
fs.writeFileSync(output, second.join("\n")+"\n", { flag: "a+" });
}else{
if(fs.writeFileSync(output, content, outcoding)===false){
console.error("Error: Couldn't create " + output + ".");
process.exit(1);
}
}
console.log("Success: " + output + " has been created.");
process.exit(0);
}
;
if(!fs.existsSync(input)){
console.error("Error: " + input + " does not exist.");
process.exit(1);
}
if(outBinary){//2arrow
result=callback( fs.readFileSync(input, incoding) );
out(argv._[1], result);
}else{
result=(isString) ?
callback( fs.readFileSync(input, incoding) )
:
JSON.stringify( callback( fs.readFileSync(input, incoding) ) )
;
out(argv._[1], result, !argv.stream && argv.big);
}
}else{ //streams
if(argv._.length>1){
console.error("Error: With --stream enabled no input/output filename can be specified.");
process.exit(1);
}
if(outBinary){
isString=true; //Is not really a string but it's not an object that should be stringified either
}
rw.readFile("/dev/stdin", incoding, function(error, contents){
rw.writeFile("/dev/stdout",
(isString) ?
callback(contents)
:
JSON.stringify( callback(contents) ),
outcoding,
function(err){
process.exit( +(err===null) );
}
);
});
}
}
;
module.exports.dataset=dataset;
module.exports.main=main;