├── .verb.md ├── .travis.yml ├── .gitattributes ├── .editorconfig ├── .gitignore ├── example.js ├── appveyor.yml ├── gulpfile.js ├── LICENSE ├── package.json ├── .eslintrc.json ├── README.md ├── index.js └── test.js /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | var githubContent = require('{%= name %}'); 5 | ``` 6 | 7 | ## API 8 | {%= apidocs("index.js") %} 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '8' 9 | - '7' 10 | - '6' 11 | - '5' 12 | - '4' 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | package-lock.json -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var GithubContent = require('./'); 4 | var gc = new GithubContent({ 5 | owner: 'doowb', 6 | repo: 'handlebars-helpers', 7 | branch: 'docs' 8 | }); 9 | 10 | gc.files(['scaffolds.json', 'package.json'], function(err, results) { 11 | if (err) return console.error(err); 12 | results.forEach(function(file) { 13 | console.log('----', file.path, '----'); 14 | console.log(file.contents.toString()); 15 | console.log(); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Test against this version of Node.js 2 | environment: 3 | matrix: 4 | # node.js 5 | - nodejs_version: "8.0" 6 | - nodejs_version: "7.0" 7 | - nodejs_version: "6.0" 8 | - nodejs_version: "5.0" 9 | - nodejs_version: "4.0" 10 | 11 | # Install scripts. (runs after repo cloning) 12 | install: 13 | # Get the latest stable version of Node.js or io.js 14 | - ps: Install-Product node $env:nodejs_version 15 | # install modules 16 | - npm install 17 | 18 | # Post-install test scripts. 19 | test_script: 20 | # Output useful info for debugging. 21 | - node --version 22 | - npm --version 23 | # run tests 24 | - npm test 25 | 26 | # Don't actually build. 27 | build: off 28 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var mocha = require('gulp-mocha'); 3 | var eslint = require('gulp-eslint'); 4 | var istanbul = require('gulp-istanbul'); 5 | var formatter = require('eslint-friendly-formatter'); 6 | 7 | var lint = ['gulpfile.js', 'index.js', 'utils.js']; 8 | 9 | gulp.task('coverage', function() { 10 | return gulp.src(lint.concat(['!gulpfile.js'])) 11 | .pipe(istanbul({ 12 | includeUntested: true 13 | })) 14 | .pipe(istanbul.hookRequire()); 15 | }); 16 | 17 | gulp.task('test', ['coverage'], function() { 18 | return gulp.src('test.js') 19 | .pipe(mocha({ 20 | reporter: 'spec' 21 | })) 22 | .pipe(istanbul.writeReports()) 23 | .pipe(istanbul.writeReports({ 24 | reporters: [ 'text' ], 25 | reportOpts: {dir: 'coverage', file: 'summary.txt'} 26 | })); 27 | }); 28 | 29 | gulp.task('lint', function() { 30 | return gulp.src(lint.concat(['test.js'])) 31 | .pipe(eslint()) 32 | .pipe(eslint.format(formatter)); 33 | }); 34 | 35 | gulp.task('default', ['test']); 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2017, Brian Woodward. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-content", 3 | "description": "Easily download files from github raw user content.", 4 | "version": "1.0.1", 5 | "homepage": "https://github.com/doowb/github-content", 6 | "author": "Brian Woodward (https://github.com/doowb)", 7 | "repository": "doowb/github-content", 8 | "bugs": { 9 | "url": "https://github.com/doowb/github-content/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=4.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "each-parallel-async": "^1.0.0", 24 | "extend-shallow": "^2.0.1", 25 | "github-base": "^0.5.4" 26 | }, 27 | "devDependencies": { 28 | "eslint-friendly-formatter": "^1.2.2", 29 | "gulp": "^3.9.0", 30 | "gulp-eslint": "^1.1.0", 31 | "gulp-format-md": "^1.0.0", 32 | "gulp-istanbul": "^0.10.2", 33 | "gulp-mocha": "^2.1.3", 34 | "mocha": "*", 35 | "should": "*", 36 | "sinon": "^1.17.2" 37 | }, 38 | "keywords": [ 39 | "base", 40 | "download", 41 | "files", 42 | "github", 43 | "github-base", 44 | "raw", 45 | "usercontent" 46 | ], 47 | "verb": { 48 | "related": { 49 | "list": [ 50 | "github-base" 51 | ] 52 | }, 53 | "toc": false, 54 | "layout": "default", 55 | "tasks": [ 56 | "readme" 57 | ], 58 | "plugins": [ 59 | "gulp-format-md" 60 | ], 61 | "lint": { 62 | "reflinks": true 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": false, 4 | "es6": true, 5 | "node": true, 6 | "mocha": true 7 | }, 8 | 9 | "globals": { 10 | "document": false, 11 | "navigator": false, 12 | "window": false 13 | }, 14 | 15 | "rules": { 16 | "accessor-pairs": 2, 17 | "arrow-spacing": [2, { "before": true, "after": true }], 18 | "block-spacing": [2, "always"], 19 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 20 | "comma-dangle": [2, "never"], 21 | "comma-spacing": [2, { "before": false, "after": true }], 22 | "comma-style": [2, "last"], 23 | "constructor-super": 2, 24 | "curly": [2, "multi-line"], 25 | "dot-location": [2, "property"], 26 | "eol-last": 2, 27 | "eqeqeq": [2, "allow-null"], 28 | "generator-star-spacing": [2, { "before": true, "after": true }], 29 | "handle-callback-err": [2, "^(err|error)$" ], 30 | "indent": [2, 2, { "SwitchCase": 1 }], 31 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 32 | "keyword-spacing": [2, { "before": true, "after": true }], 33 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 34 | "new-parens": 2, 35 | "no-array-constructor": 2, 36 | "no-caller": 2, 37 | "no-class-assign": 2, 38 | "no-cond-assign": 2, 39 | "no-const-assign": 2, 40 | "no-control-regex": 2, 41 | "no-debugger": 2, 42 | "no-delete-var": 2, 43 | "no-dupe-args": 2, 44 | "no-dupe-class-members": 2, 45 | "no-dupe-keys": 2, 46 | "no-duplicate-case": 2, 47 | "no-empty-character-class": 2, 48 | "no-eval": 2, 49 | "no-ex-assign": 2, 50 | "no-extend-native": 2, 51 | "no-extra-bind": 2, 52 | "no-extra-boolean-cast": 2, 53 | "no-extra-parens": [2, "functions"], 54 | "no-fallthrough": 2, 55 | "no-floating-decimal": 2, 56 | "no-func-assign": 2, 57 | "no-implied-eval": 2, 58 | "no-inner-declarations": [2, "functions"], 59 | "no-invalid-regexp": 2, 60 | "no-irregular-whitespace": 2, 61 | "no-iterator": 2, 62 | "no-label-var": 2, 63 | "no-labels": 2, 64 | "no-lone-blocks": 2, 65 | "no-mixed-spaces-and-tabs": 2, 66 | "no-multi-spaces": 2, 67 | "no-multi-str": 2, 68 | "no-multiple-empty-lines": [2, { "max": 1 }], 69 | "no-native-reassign": 0, 70 | "no-negated-in-lhs": 2, 71 | "no-new": 2, 72 | "no-new-func": 2, 73 | "no-new-object": 2, 74 | "no-new-require": 2, 75 | "no-new-wrappers": 2, 76 | "no-obj-calls": 2, 77 | "no-octal": 2, 78 | "no-octal-escape": 2, 79 | "no-proto": 0, 80 | "no-redeclare": 2, 81 | "no-regex-spaces": 2, 82 | "no-return-assign": 2, 83 | "no-self-compare": 2, 84 | "no-sequences": 2, 85 | "no-shadow-restricted-names": 2, 86 | "no-spaced-func": 2, 87 | "no-sparse-arrays": 2, 88 | "no-this-before-super": 2, 89 | "no-throw-literal": 2, 90 | "no-trailing-spaces": 0, 91 | "no-undef": 2, 92 | "no-undef-init": 2, 93 | "no-unexpected-multiline": 2, 94 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 95 | "no-unreachable": 2, 96 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 97 | "no-useless-call": 0, 98 | "no-with": 2, 99 | "one-var": [0, { "initialized": "never" }], 100 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 101 | "padded-blocks": [0, "never"], 102 | "quotes": [2, "single", "avoid-escape"], 103 | "radix": 2, 104 | "semi": [2, "always"], 105 | "semi-spacing": [2, { "before": false, "after": true }], 106 | "space-before-blocks": [2, "always"], 107 | "space-before-function-paren": [2, "never"], 108 | "space-in-parens": [2, "never"], 109 | "space-infix-ops": 2, 110 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 111 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 112 | "use-isnan": 2, 113 | "valid-typeof": 2, 114 | "wrap-iife": [2, "any"], 115 | "yoda": [2, "never"] 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # github-content [![NPM version](https://img.shields.io/npm/v/github-content.svg?style=flat)](https://www.npmjs.com/package/github-content) [![NPM monthly downloads](https://img.shields.io/npm/dm/github-content.svg?style=flat)](https://npmjs.org/package/github-content) [![NPM total downloads](https://img.shields.io/npm/dt/github-content.svg?style=flat)](https://npmjs.org/package/github-content) [![Linux Build Status](https://img.shields.io/travis/doowb/github-content.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/github-content) [![Windows Build Status](https://img.shields.io/appveyor/ci/doowb/github-content.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/doowb/github-content) 2 | 3 | > Easily download files from github raw user content. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save github-content 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | var githubContent = require('github-content'); 17 | ``` 18 | 19 | ## API 20 | 21 | ### [GithubContent](index.js#L33) 22 | 23 | Create an instance of GithubContent to setup downloading of files. 24 | 25 | **Params** 26 | 27 | * `options` **{Object}**: Options to set on instance. Additional options passed to [github-base](https://github.com/jonschlinkert/github-base) 28 | * `options.owner` **{String}**: Set the owner to be used for each file. 29 | * `options.repo` **{String}**: Set the repository to be used for each file. 30 | * `options.branch` **{String}**: Set the branch to be used for each file. Defaults to `master` 31 | 32 | **Example** 33 | 34 | ```js 35 | var options = { 36 | owner: 'doowb', 37 | repo: 'github-content', 38 | branch: 'master' // defaults to master 39 | }; 40 | 41 | var gc = new GithubContent(options); 42 | ``` 43 | 44 | ### [.file](index.js#L73) 45 | 46 | Download a single file given the file name and repository options. File object returned will contain a `.path` property with the file path passed in and a `.contents` property with the contents of the downloaded file. 47 | 48 | **Params** 49 | 50 | * `path` **{String}**: file path to download 51 | * `options` **{Object}**: Additional options to base to [github-base](https://github.com/jonschlinkert/github-base) `.get` method. 52 | * `cb` **{Function}**: Callback function taking `err` and `file` 53 | * `returns` **{Object}** `this`: to enable chaining 54 | 55 | **Example** 56 | 57 | ```js 58 | gc.file('package.json', function(err, file) { 59 | if (err) return console.log(err); 60 | console.log(file.path); 61 | console.log(file.contents); 62 | }); 63 | //=> package.json 64 | //=> { 65 | //=> "name": "github-content" 66 | //=> ... 67 | //=> } 68 | ``` 69 | 70 | ### [.files](index.js#L129) 71 | 72 | Download an array of files using the previous settings and the passed in file names. File objects returned will contain a `.path` property with the file path passed in and a `.contents` property with the contents of the downloaded file. 73 | 74 | **Params** 75 | 76 | * `files` **{String|Array}**: files to download 77 | * `options` **{Object}**: Additional options to base to [github-base](https://github.com/jonschlinkert/github-base) `.get` method. 78 | * `cb` **{Function}**: Callback function taking `err` and `files` 79 | * `returns` **{Object}** `this`: to enable chaining 80 | 81 | **Example** 82 | 83 | ```js 84 | gc.files(['package.json', 'index.js'], function(err, files) { 85 | if (err) return console.log(err); 86 | console.log(files.length); 87 | console.log(files[0].path); 88 | console.log(files[0].contents); 89 | }); 90 | //=> 2 91 | //=> package.json 92 | //=> { 93 | //=> "name": "github-content" 94 | //=> ... 95 | //=> } 96 | ``` 97 | 98 | ## About 99 | 100 | ### Related projects 101 | 102 | [github-base](https://www.npmjs.com/package/github-base): JavaScript wrapper that greatly simplifies working with GitHub's API. | [homepage](https://github.com/jonschlinkert/github-base "JavaScript wrapper that greatly simplifies working with GitHub's API.") 103 | 104 | ### Contributing 105 | 106 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 107 | 108 | ### Building docs 109 | 110 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 111 | 112 | To generate the readme, run the following command: 113 | 114 | ```sh 115 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 116 | ``` 117 | 118 | ### Running tests 119 | 120 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 121 | 122 | ```sh 123 | $ npm install && npm test 124 | ``` 125 | 126 | ### Author 127 | 128 | **Brian Woodward** 129 | 130 | * [github/doowb](https://github.com/doowb) 131 | * [twitter/doowb](https://twitter.com/doowb) 132 | 133 | ### License 134 | 135 | Copyright © 2017, [Brian Woodward](https://github.com/doowb). 136 | Released under the [MIT License](LICENSE). 137 | 138 | *** 139 | 140 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 11, 2017._ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * github-content 3 | * 4 | * Copyright (c) 2015-2017, Brian Woodward. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var GithubBase = require('github-base'); 11 | var extend = require('extend-shallow'); 12 | var each = require('each-parallel-async'); 13 | 14 | /** 15 | * Create an instance of GithubContent to setup downloading of files. 16 | * 17 | * ```js 18 | * var options = { 19 | * owner: 'doowb', 20 | * repo: 'github-content', 21 | * branch: 'master' // defaults to master 22 | * }; 23 | * 24 | * var gc = new GithubContent(options); 25 | * ``` 26 | * @param {Object} `options` Options to set on instance. Additional options passed to [github-base] 27 | * @param {String} `options.owner` Set the owner to be used for each file. 28 | * @param {String} `options.repo` Set the repository to be used for each file. 29 | * @param {String} `options.branch` Set the branch to be used for each file. Defaults to `master` 30 | * @api public 31 | */ 32 | 33 | function GithubContent(options) { 34 | if (!(this instanceof GithubContent)) { 35 | return new GithubContent(options); 36 | } 37 | 38 | // setup our specific options 39 | var opts = extend({branch: 'master'}, options); 40 | opts.json = false; 41 | opts.apiurl = 'https://raw.githubusercontent.com'; 42 | GithubBase.call(this, opts); 43 | this.options = extend({}, opts, this.options); 44 | } 45 | 46 | GithubBase.extend(GithubContent); 47 | 48 | /** 49 | * Download a single file given the file name and repository options. 50 | * File object returned will contain a `.path` property with the file path passed in 51 | * and a `.contents` property with the contents of the downloaded file. 52 | * 53 | * ```js 54 | * gc.file('package.json', function(err, file) { 55 | * if (err) return console.log(err); 56 | * console.log(file.path); 57 | * console.log(file.contents); 58 | * }); 59 | * //=> package.json 60 | * //=> { 61 | * //=> "name": "github-content" 62 | * //=> ... 63 | * //=> } 64 | * ``` 65 | * 66 | * @param {String} `path` file path to download 67 | * @param {Object} `options` Additional options to base to [github-base] `.get` method. 68 | * @param {Function} `cb` Callback function taking `err` and `file` 69 | * @return {Object} `this` to enable chaining 70 | * @api public 71 | */ 72 | 73 | GithubContent.prototype.file = function(path, options, cb) { 74 | if (typeof options === 'function') { 75 | cb = options; 76 | options = {}; 77 | } 78 | 79 | if (typeof cb !== 'function') { 80 | throw new TypeError('expected callback to be a function'); 81 | } 82 | 83 | var opts = extend({branch: 'master', path: path}, this.options, options); 84 | if (!opts.repo) { 85 | cb(new Error('expected "options.repo" to be specified')); 86 | return; 87 | } 88 | 89 | var segs = opts.repo.split('/'); 90 | if (segs.length > 1) { 91 | opts.owner = segs[0]; 92 | opts.repo = segs[1]; 93 | } 94 | 95 | this.get('/:owner/:repo/:branch/:path', opts, function(err, contents) { 96 | if (err) return cb(err); 97 | cb(null, {path: path, contents: contents}); 98 | }); 99 | return this; 100 | }; 101 | 102 | /** 103 | * Download an array of files using the previous settings and the passed in file names. 104 | * File objects returned will contain a `.path` property with the file path passed in 105 | * and a `.contents` property with the contents of the downloaded file. 106 | * 107 | * ```js 108 | * gc.files(['package.json', 'index.js'], function(err, files) { 109 | * if (err) return console.log(err); 110 | * console.log(files.length); 111 | * console.log(files[0].path); 112 | * console.log(files[0].contents); 113 | * }); 114 | * //=> 2 115 | * //=> package.json 116 | * //=> { 117 | * //=> "name": "github-content" 118 | * //=> ... 119 | * //=> } 120 | * ``` 121 | * 122 | * @param {String|Array} `files` files to download 123 | * @param {Object} `options` Additional options to base to [github-base] `.get` method. 124 | * @param {Function} `cb` Callback function taking `err` and `files` 125 | * @return {Object} `this` to enable chaining 126 | * @api public 127 | */ 128 | 129 | GithubContent.prototype.files = function(files, options, cb) { 130 | if (typeof options === 'function') { 131 | cb = options; 132 | options = {}; 133 | } 134 | var opts = extend({}, options); 135 | files = arrayify(files); 136 | 137 | map(files, function(file, next) { 138 | this.file(file, opts, next); 139 | }.bind(this), cb); 140 | 141 | return this; 142 | }; 143 | 144 | function arrayify(val) { 145 | if (val === null || typeof val === 'undefined') { 146 | return []; 147 | } 148 | return Array.isArray(val) ? val : [val]; 149 | } 150 | 151 | function map(arr, iterator, cb) { 152 | var i = 0; 153 | var res = new Array(arr.length); 154 | each(arr, function(ele, next) { 155 | iterator(ele, function(err, val) { 156 | if (err) { 157 | next(err); 158 | return err; 159 | } 160 | res[i++] = val; 161 | next(); 162 | }); 163 | }, function(err) { 164 | if (err) { 165 | cb(err); 166 | return; 167 | } 168 | cb(null, res); 169 | }); 170 | } 171 | 172 | /** 173 | * Exposes `GithubContent` 174 | */ 175 | 176 | module.exports = GithubContent; 177 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * github-content 3 | * 4 | * Copyright (c) 2015 . 5 | * Licensed under the MIT license. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | /* deps:mocha */ 11 | var assert = require('assert'); 12 | var sinon = require('sinon'); 13 | var GithubContent = require('./'); 14 | 15 | describe('github-content', function() { 16 | it('should create an instance with default options', function() { 17 | var gc = new GithubContent(); 18 | assert(gc instanceof GithubContent); 19 | assert.equal(gc.options.json, false); 20 | assert.equal(gc.options.apiurl, 'https://raw.githubusercontent.com'); 21 | }); 22 | 23 | it('should create an instance with default options without using new', function() { 24 | var gc = GithubContent(); 25 | assert(gc instanceof GithubContent); 26 | assert.equal(gc.options.json, false); 27 | assert.equal(gc.options.apiurl, 'https://raw.githubusercontent.com'); 28 | }); 29 | 30 | it('should create an instance with specified option values', function() { 31 | var gc = new GithubContent({ 32 | owner: 'doowb', 33 | repo: 'github-content', 34 | branch: 'master' 35 | }); 36 | 37 | assert.equal(gc.options.owner, 'doowb'); 38 | assert.equal(gc.options.repo, 'github-content'); 39 | assert.equal(gc.options.branch, 'master'); 40 | }); 41 | 42 | it('should throw an error when callback is not specified', function() { 43 | var gc = new GithubContent({ 44 | owner: 'doowb', 45 | repo: 'github-content', 46 | branch: 'master' 47 | }); 48 | 49 | assert.throws(function() { 50 | gc.file('package.json'); 51 | }); 52 | }); 53 | 54 | it('should return an error when the repo is not specified', function(cb) { 55 | var gc = new GithubContent({ 56 | owner: 'doowb', 57 | branch: 'master' 58 | }); 59 | 60 | gc.file('package.json', function(err, file) { 61 | if (!err) { 62 | cb(new Error('expected an error to be returned')); 63 | return; 64 | } 65 | assert.equal(err.message, 'expected "options.repo" to be specified'); 66 | cb(); 67 | }) 68 | }); 69 | 70 | it('should download a file', function(cb) { 71 | var gc = new GithubContent({ 72 | owner: 'doowb', 73 | repo: 'github-content', 74 | branch: 'master' 75 | }); 76 | 77 | gc.file('package.json', function(err, file) { 78 | if (err) { 79 | cb(err); 80 | return; 81 | } 82 | assert.equal(file.path, 'package.json'); 83 | cb(); 84 | }); 85 | }); 86 | 87 | it('should allow passing repo in :owner/:name format', function(cb) { 88 | var gc = new GithubContent({repo: 'doowb/github-content'}); 89 | 90 | gc.file('package.json', function(err, file) { 91 | if (err) { 92 | cb(err); 93 | return; 94 | } 95 | assert.equal(file.path, 'package.json'); 96 | cb(); 97 | }); 98 | }); 99 | 100 | it('should not download a file', function(cb) { 101 | var gc = new GithubContent({ 102 | owner: 'doowb', 103 | repo: 'github-content', 104 | branch: 'master' 105 | }); 106 | 107 | // false is just to get code coverage 108 | gc.file('notfound.json', false, function(err, file) { 109 | if (err) { 110 | cb(err); 111 | return; 112 | } 113 | assert.equal(file.path, 'notfound.json'); 114 | assert.equal(file.contents, '404: Not Found\n'); 115 | cb(); 116 | }); 117 | }); 118 | 119 | it('should download a single file as an array of files', function(cb) { 120 | var gc = new GithubContent({ 121 | owner: 'doowb', 122 | repo: 'github-content', 123 | branch: 'master' 124 | }); 125 | 126 | // false is just to get code coverage 127 | gc.files('package.json', false, function(err, files) { 128 | if (err) { 129 | cb(err); 130 | return; 131 | } 132 | assert.equal(files.length, 1); 133 | assert.equal(files[0].path, 'package.json'); 134 | cb(); 135 | }); 136 | }); 137 | 138 | it('should download an array of files', function(cb) { 139 | var gc = new GithubContent({ 140 | owner: 'doowb', 141 | repo: 'github-content', 142 | branch: 'master' 143 | }); 144 | 145 | gc.files(['package.json', 'index.js'], function(err, files) { 146 | if (err) { 147 | cb(err); 148 | return; 149 | } 150 | assert.equal(files.length, 2); 151 | files.forEach(function(file) { 152 | assert(file.path === 'package.json' || file.path === 'index.js'); 153 | }); 154 | cb(); 155 | }); 156 | }); 157 | 158 | it('should not download null files', function(cb) { 159 | var gc = new GithubContent(); 160 | gc.files(null, function(err, files) { 161 | if (err) { 162 | cb(err); 163 | return; 164 | } 165 | assert.equal(files.length, 0); 166 | cb(); 167 | }); 168 | }); 169 | 170 | it('should not download undefined files', function(cb) { 171 | var gc = new GithubContent(); 172 | gc.files(undefined, function(err, files) { 173 | if (err) { 174 | cb(err); 175 | return; 176 | } 177 | assert.equal(files.length, 0); 178 | cb(); 179 | }); 180 | }); 181 | 182 | describe('errors', function() { 183 | var gc = null; 184 | before(function(){ 185 | gc = new GithubContent({owner: 'doowb', repo: 'github-content'}); 186 | sinon 187 | .stub(gc, 'get') 188 | .yields(new Error('Fake Error'), null); 189 | }); 190 | 191 | after(function(){ 192 | gc.get.restore(); 193 | }); 194 | 195 | it('should error on invalid owner and repo', function(cb) { 196 | gc.files(['package.json'], function(err, files) { 197 | assert(err); 198 | cb(); 199 | }); 200 | }); 201 | }); 202 | }); 203 | --------------------------------------------------------------------------------