Skip to content

Commit 9420606

Browse files
committed
Port package to fetch a file from kgryte/github-fetch-file
1 parent 7325a46 commit 9420606

26 files changed

+2391
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Fetch File
22+
23+
> Fetch a file from one or more public Github repositories.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var fetchFile = require( '@stdlib/_tools/github/fetch-file' );
41+
```
42+
43+
<a name="fetchfile"></a>
44+
45+
#### fetchFile( filepath, repos, clbk )
46+
47+
Fetches a `file` from one or more **public** Github repositories.
48+
49+
<!-- run-disable -->
50+
51+
```javascript
52+
// List of repository slugs (username|org/repo):
53+
var repos = [
54+
'kgryte/utils-copy',
55+
'dstructs/matrix',
56+
'math-io/gamma',
57+
'unknown_user/repo'
58+
];
59+
60+
// Fetch a top-level `README.md` file from each repo:
61+
fetchFile( 'README.md', repos, clbk );
62+
63+
function clbk( error, results ) {
64+
if ( error ) {
65+
throw new Error( error.message );
66+
}
67+
console.dir( results );
68+
/* =>
69+
{
70+
'meta': {
71+
'total': 4,
72+
'success': 3,
73+
'failure': 1
74+
},
75+
'data': {
76+
'kgryte/utils-copy': '...',
77+
'dstructs/matrix': '...',
78+
'math-io/gamma': '...'
79+
},
80+
'failures': {
81+
'unknown_user/repo': 'Not Found'
82+
}
83+
}
84+
*/
85+
}
86+
```
87+
88+
89+
#### fetchFile.factory( filepath, repos, clbk )
90+
91+
Creates a reusable `function`.
92+
93+
<!-- run-disable -->
94+
95+
```javascript
96+
var repos = [
97+
'kgryte/utils-copy',
98+
'dstructs/matrix',
99+
'math-io/gamma',
100+
'unknown_user/repo'
101+
];
102+
103+
var get = fetchFile.factory( 'README.md', repos, clbk );
104+
105+
get();
106+
get();
107+
get();
108+
// ...
109+
```
110+
111+
The factory method accepts the same `arguments` as [`fetchFile()`](#fetchFile).
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="notes">
120+
121+
## Notes
122+
123+
- If the module encounters an application-level `error` (e.g., no network connection, etc), the `error` is returned immediately to the provided `callback`.
124+
- If the module encounters a downstream `error` (e.g., timeout, reset connection, etc), the `error` is included in the returned results under the `failures` field.
125+
126+
</section>
127+
128+
<!-- /.notes -->
129+
130+
<!-- Package usage examples. -->
131+
132+
<section class="examples">
133+
134+
## Examples
135+
136+
```javascript
137+
var fetchFile = require( '@stdlib/_tools/github/fetch-file' );
138+
139+
var repos = [
140+
'kgryte/utils-copy',
141+
'math-io/gamma',
142+
'dstructs/matrix'
143+
];
144+
145+
fetchFile( 'README.md', repos, clbk );
146+
147+
function clbk( error, results ) {
148+
if ( error ) {
149+
throw new Error( error.message );
150+
}
151+
console.dir( results );
152+
}
153+
```
154+
155+
</section>
156+
157+
<!-- /.examples -->
158+
159+
<!-- Section for describing a command-line interface. -->
160+
161+
* * *
162+
163+
<section class="cli">
164+
165+
## CLI
166+
167+
<!-- CLI usage documentation. -->
168+
169+
<section class="usage">
170+
171+
### Usage
172+
173+
```bash
174+
Usage: ghfetchfile [options] file --repo slug1 --repo slug2 ...
175+
176+
Options:
177+
178+
-h, --help Print this message.
179+
-V, --version Print the package version.
180+
--repo slug Repository slug; e.g., kgryte/github-fetch-file.
181+
```
182+
183+
</section>
184+
185+
<!-- /.usage -->
186+
187+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
188+
189+
<section class="notes">
190+
191+
### Notes
192+
193+
- If a repository file is successfully resolved, the file content is written to `stdout`.
194+
- If a repository file cannot be resolved due to a downstream `error` (failure), the repo `slug` (and its associated `error`) is written to `sterr`.
195+
- Output order is **not** guaranteed to match input order.
196+
197+
</section>
198+
199+
<!-- /.notes -->
200+
201+
<!-- CLI usage examples. -->
202+
203+
<section class="examples">
204+
205+
### Examples
206+
207+
<!-- run-disable -->
208+
209+
```bash
210+
$ DEBUG=* ghfetchfile README.md --repo 'kgryte/utils-copy' --repo 'dstructs/matrix' --repo 'math-io/gamma'
211+
# => {...}
212+
```
213+
214+
</section>
215+
216+
<!-- /.examples -->
217+
218+
</section>
219+
220+
<!-- /.cli -->
221+
222+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
223+
224+
<section class="references">
225+
226+
</section>
227+
228+
<!-- /.references -->
229+
230+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
231+
232+
<section class="links">
233+
234+
</section>
235+
236+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2021 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var objectKeys = require( '@stdlib/utils/keys' );
29+
var CLI = require( '@stdlib/tools/cli' );
30+
var ENV = require( '@stdlib/process/env' );
31+
var get = require( './../lib' );
32+
33+
34+
// MAIN //
35+
36+
/**
37+
* Main execution sequence.
38+
*
39+
* @private
40+
* @returns {void}
41+
*/
42+
function main() {
43+
var token;
44+
var flags;
45+
var repos;
46+
var args;
47+
var opts;
48+
var cli;
49+
50+
// Create a command-line interface:
51+
cli = new CLI({
52+
'pkg': require( './../package.json' ),
53+
'options': require( './../etc/cli_opts.json' ),
54+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
55+
'encoding': 'utf8'
56+
})
57+
});
58+
59+
// Get any provided command-line options:
60+
flags = cli.flags();
61+
if ( flags.help || flags.version ) {
62+
return;
63+
}
64+
// Get any provided command-line arguments:
65+
args = cli.args();
66+
67+
opts = {};
68+
if ( flags.useragent ) {
69+
opts.useragent = flags.useragent;
70+
}
71+
if ( flags.username ) {
72+
opts.username = flags.username;
73+
}
74+
if ( flags.token ) {
75+
token = flags.token;
76+
}
77+
else if ( ENV.GITHUB_TOKEN ) {
78+
token = ENV.GITHUB_TOKEN;
79+
}
80+
if ( token ) {
81+
opts.token = token;
82+
}
83+
if ( isArray( flags.repo ) ) {
84+
repos = flags.repo;
85+
} else {
86+
repos = [ flags.repo ];
87+
}
88+
89+
get( args[ 0 ], repos, clbk );
90+
91+
/**
92+
* Callback invoked upon starring a repository.
93+
*
94+
* @private
95+
* @param {(Error|null)} error - error object
96+
* @param {Object} results - results
97+
* @returns {void}
98+
*/
99+
function clbk( error, results ) {
100+
/* eslint-disable no-console */
101+
var len;
102+
if ( error ) {
103+
if ( error instanceof Error ) {
104+
return cli.error( error );
105+
}
106+
return cli.error( new Error( error.message ) );
107+
}
108+
if ( !results.data && !results.failures ) {
109+
return console.log( JSON.stringify( results ) );
110+
}
111+
len = objectKeys( results.data ).length;
112+
if ( len ) {
113+
process.stdout.write( JSON.stringify( results.data ), 'utf8' );
114+
process.stdout.write( '\n', 'utf8' );
115+
}
116+
len = objectKeys( results.failures ).length;
117+
if ( len ) {
118+
process.stderr.write( JSON.stringify( results.failures ), 'utf8' );
119+
process.stderr.write( '\n', 'utf8' );
120+
}
121+
}
122+
}
123+
124+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
Usage: ghfetchfile [options] file --repo slug1 --repo slug2 ...
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
--repo slug Repository slug; e.g., kgryte/github-fetch-file.
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"string": [
3+
"repo"
4+
],
5+
"boolean": [
6+
"help",
7+
"version"
8+
],
9+
"alias": {
10+
"help": [
11+
"h"
12+
],
13+
"version": [
14+
"V"
15+
]
16+
}
17+
}

0 commit comments

Comments
 (0)