-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
83 lines (66 loc) · 2.2 KB
/
Copy pathmain.js
File metadata and controls
83 lines (66 loc) · 2.2 KB
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
#!/usr/bin/env node
/* Download collimated data packages.
*/
const fs = require('fs'); // node:fs
const URL = require('url').URL;
const path = require('path');
var loader = require('arrayloader');
var beam = require('./lib/beam.js');
/* exits if there's an error or if object is null
if object is null the message is shown
if err is not null that's shown
*/
function handleError(err, obj, message){
if(!obj){
console.log(message);
console.log('\t' + (err.message ? err.message : err));
process.exit(1);
} else if(err) {
console.log(err);
process.exit(1);
}
}
if(require.main === module){
// yes, parse command line args and show something
var argv = require('yargs')
.usage('Download collimated data packages\nUsage: $0 [options] <url>')
.demand(1)
.boolean('v').describe('v', 'show more info')
.help('h').alias('h', 'help')
.argv
var url = argv._[0];
// verbose
if(argv.v){}
var file = "index.json";
var index_path;
if(!url.endsWith("/")) url += "/";
index_path = url + file;
loader.load(index_path, function(err, index){
handleError(err, "Couldn't load index from: " + url);
return load(url, index, function(err, results){
handleError(err, "Couldn't download data from: " + url);
/// write columns and index.json to files
// extract package name from end of url
const parsed_url = new URL(url);
var path_part = parsed_url.pathname;
var parts = path_part.split("/");
//console.log(parts);
var package_name = parts[parts.length-1];
if(package_name == "") package_name = parts[parts.length-2];
var package_dir = "./" + package_name;
try {
// create directory with package name
fs.mkdirSync(package_dir);
// write index.json
fs.writeFileSync(path.join(package_dir, 'index.json'), JSON.stringify(index, null, 4));
// iterate columns and write
for(const [name, filename] of Object.entries(index)){
fs.writeFileSync(path.join(package_dir, filename), results.columns[name]);
}
console.log("Wrote "+ Object.entries(index).length +" files to '"+package_name+"' directory.");
} catch (err) {
console.error(err);
}
});
});
}