This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
forked from ezwelty/opentrees-harvester
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.js
executable file
·85 lines (80 loc) · 1.93 KB
/
get.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
#!/usr/bin/env node
const commandLineUsage = require('command-line-usage')
const commandLineArgs = require('command-line-args')
const { DEFAULT_OPTIONS } = require('./common')
const { loadSources } = require('../lib/load')
const OPTIONS = [
...DEFAULT_OPTIONS,
{
name: 'force', alias: 'f', type: Boolean, defaultValue: false,
description: 'Overwrite input directory even if it is not empty.'
}
]
const USAGE = [
{
header: 'example/get.js',
content: 'Download remote files, unpack compressed or archive files, and execute shell commands to prepare source files for processing.'
},
{
header: 'Options',
optionList: OPTIONS
}
]
// Parse command line arguments
let options
try {
options = commandLineArgs(OPTIONS)
if (options.help) {
console.log(commandLineUsage(USAGE))
process.exit(0)
}
} catch (error) {
console.error(`${'[Error]'.red}`, error)
console.log(commandLineUsage(USAGE))
process.exit(1)
}
// Load sources
const sources = loadSources(
`${__dirname}/../sources`,
{ ids: options.ids, countries: options.countries },
options.dir
)
// Get sources
const success = []
const failure = []
const skip = []
async function getSource(source) {
try {
const paths = await source.get(options.force)
if (paths.length) {
success.push(source.props.id)
} else {
skip.push(source.props.id)
}
} catch (error) {
console.error(error.message)
failure.push(source.props.id)
}
}
async function get() {
await Promise.all(sources.map(source => getSource(source)))
if (success.length) {
console.log(
`${'[SUCCESS]'.green} Got ${success.length} sources:`,
success.join(', ')
)
}
if (failure.length) {
console.error(
`${'[ERROR]'.red} Failed to get ${failure.length} sources:`,
failure.join(', ')
)
}
if (skip.length) {
console.log(
`${'[SKIPPED]'.dim} Skipped ${skip.length} sources:`,
skip.join(', ')
)
}
}
get()