-
Notifications
You must be signed in to change notification settings - Fork 2
/
inout
executable file
·116 lines (96 loc) · 2.44 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
var
fs=require("fs"),
rw=require("rw"),
JSONstat=require("jsonstat"),
dataset=function(contents){
try{
var 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){
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
input=argv._[0],
out=function(output, content){
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.");
}
if(fs.writeFileSync(output, content, "utf8")===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);
}
var result=(isString) ?
callback( fs.readFileSync(input, "utf8") )
:
JSON.stringify( callback( fs.readFileSync(input, "utf8") ) )
;
out( argv._[1], result);
}else{ //streams
if(argv._.length>1){
console.error("Error: With --stream enabled no input/output filename can be specified.");
process.exit(1);
}
rw.readFile("/dev/stdin", "utf8", function(error, contents){
rw.writeFile("/dev/stdout",
(isString) ?
callback(contents)
:
JSON.stringify( callback(contents) ),
"utf8",
function(err){
process.exit( +(err===null) );
}
);
});
}
}
;
module.exports.dataset=dataset;
module.exports.main=main;