|
| 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 --> |
0 commit comments