@@ -9,6 +9,8 @@ import { get } from '#lib/request'
9
9
import { isPositiveIntegerString } from '#lib/types'
10
10
import { getWbk } from '#lib/wbk'
11
11
12
+ program . acceptsArgsOnStdin = true
13
+
12
14
await program
13
15
. option ( '-s, --start <date>' , 'start date' )
14
16
. option ( '-e, --end <date>' , 'end date' )
@@ -20,49 +22,60 @@ exitOnMissingInstance(program.instance)
20
22
21
23
const { getRevisions } = getWbk ( program )
22
24
23
- // Not parsing the ids with ../lib/tolerant_id_parser as that would
24
- // remove prefixes which are required for entities out of the main namespace
25
- // Ex: Property:P570
26
- const ids = program . args
27
- if ( ! ( ids && ids . length > 0 ) ) program . helpAndExit ( 0 )
28
-
29
- ids . forEach ( id => {
30
- let [ prefix , entityId ] = id . split ( ':' )
31
- if ( entityId ) {
32
- if ( prefix !== 'Property' && prefix !== 'Item' ) {
33
- throw new Error ( `invalid entity prefix: ${ prefix } ` )
25
+ function fetchAndLogRevisions ( ids ) {
26
+ ids . forEach ( id => {
27
+ let [ prefix , entityId ] = id . split ( ':' )
28
+ if ( entityId ) {
29
+ if ( prefix !== 'Property' && prefix !== 'Item' ) {
30
+ throw new Error ( `invalid entity prefix: ${ prefix } ` )
31
+ }
32
+ } else {
33
+ entityId = prefix
34
34
}
35
- } else {
36
- entityId = prefix
37
- }
38
- if ( ! isEntityId ( entityId ) ) throw new Error ( `invalid entity id: ${ id } ` )
39
- } )
35
+ if ( ! isEntityId ( entityId ) ) throw new Error ( `invalid entity id: ${ id } ` )
36
+ } )
40
37
41
- const query = { }
42
- let { start, end, limit, props, verbose } = program
43
- if ( isPositiveIntegerString ( start ) ) start = parseInt ( start )
44
- if ( isPositiveIntegerString ( end ) ) end = parseInt ( end )
38
+ const query = { }
39
+ let { start, end, limit, props, verbose } = program
40
+ if ( isPositiveIntegerString ( start ) ) start = parseInt ( start )
41
+ if ( isPositiveIntegerString ( end ) ) end = parseInt ( end )
45
42
46
- query . start = start
47
- query . end = end
48
- query . limit = limit
49
- if ( props ) query . prop = props ?. split ( ',' )
43
+ query . start = start
44
+ query . end = end
45
+ query . limit = limit
46
+ if ( props ) query . prop = props ?. split ( / [ , | ] / )
50
47
51
- const getAndLogRevisions = id => {
52
- const url = getRevisions ( { ids : [ id ] , ...query } )
53
- if ( verbose ) console . log ( `revision query: ${ id } ` , url )
54
- return get ( url )
55
- . then ( body => values ( body . query . pages ) [ 0 ] )
48
+ const getAndLogRevisions = id => {
49
+ const url = getRevisions ( { ids : [ id ] , ...query } )
50
+ if ( verbose ) console . log ( `revision query: ${ id } ` , url )
51
+ return get ( url )
52
+ . then ( body => values ( body . query . pages ) [ 0 ] )
53
+ }
54
+
55
+ if ( ids . length === 1 ) {
56
+ getAndLogRevisions ( ids [ 0 ] )
57
+ . then ( data => console . log ( JSON . stringify ( data ) ) )
58
+ . catch ( errors_ . exit )
59
+ } else {
60
+ // Getting revisisions data individually to be able to pass parameters
61
+ // cf https://github.com/maxlath/wikibase-sdk/blob/master/docs/get_revisions.md
62
+ Promise . all ( ids . map ( getAndLogRevisions ) )
63
+ . then ( logNdjson )
64
+ . catch ( errors_ . exit )
65
+ }
56
66
}
57
67
58
- if ( ids . length === 1 ) {
59
- getAndLogRevisions ( ids [ 0 ] )
60
- . then ( data => console . log ( JSON . stringify ( data ) ) )
61
- . catch ( errors_ . exit )
68
+ // process.stdin.isTTY will be undefined if the process is receiving
69
+ // its stdin from another process
70
+ if ( program . args . length === 0 && process . stdin . isTTY ) {
71
+ program . helpAndExit ( 0 )
72
+ } else if ( program . args . length > 0 ) {
73
+ // Not parsing the ids with ../lib/tolerant_id_parser as that would
74
+ // remove prefixes which are required for entities out of the main namespace
75
+ // Ex: Property:P570
76
+ const ids = program . args
77
+ fetchAndLogRevisions ( ids )
62
78
} else {
63
- // Getting revisisions data individually to be able to pass parameters
64
- // cf https://github.com/maxlath/wikibase-sdk/blob/master/docs/get_revisions.md
65
- Promise . all ( ids . map ( getAndLogRevisions ) )
66
- . then ( logNdjson )
67
- . catch ( errors_ . exit )
79
+ const { readIdsFromStdin } = await import ( '#lib/read_ids_from_stdin' )
80
+ readIdsFromStdin ( fetchAndLogRevisions )
68
81
}
0 commit comments