-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·63 lines (54 loc) · 1.58 KB
/
Copy pathcli.js
File metadata and controls
executable file
·63 lines (54 loc) · 1.58 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
#!/usr/bin/env node
/* eslint-disable no-console */
import { readFileSync } from 'fs'
import { fileURLToPath } from 'url'
import path from 'path'
import { program } from 'commander'
import { CategoryLoader, MwSources } from './index.js'
const packageJsonPath = path.join(fileURLToPath(import.meta.url), '../package.json')
const { version } = JSON.parse(readFileSync(packageJsonPath))
/**
* @param {string} source
* @return {boolean}
*/
function isTemplateSource(source) {
return !source.startsWith('http')
}
program
.version(version)
.arguments('<source> <title>')
.option( '-c, --csv', 'Make output csv compatible')
.action( async (source, title, env) => {
let members
let loader
if (isTemplateSource(source)) {
const sourceParts = source.split(':')
const mwSourceId = sourceParts.shift()
const languageCode = sourceParts.shift() || 'en'
const url = MwSources[mwSourceId]
if (!url) {
console.error('error', `could not find a template for '${mwSourceId}'`)
return
}
loader = CategoryLoader.createFromTemplate(url, languageCode)
} else {
loader = CategoryLoader.createFromUrl(source)
}
try {
members = await loader.loadMembers(title)
} catch (error) {
console.error('error', 'program was unable to load category members')
return
}
members.forEach(
(item) => {
// add quotation marks in csv mode
if (env.csv) {
console.log(`"${item.title}"`)
return
}
console.log(item.title)
}
)
})
.parse(process.argv)