项目作者: cantidio

项目描述 :
Tool for publishing gh-pages
高级语言: JavaScript
项目地址: git://github.com/cantidio/node-github-pages.git
创建时间: 2016-03-23T04:43:44Z
项目社区:https://github.com/cantidio/node-github-pages

开源协议:MIT License

下载


NPM

Build Status
Code Climate
Test Coverage
Dependencies
devDependencies Status

Github Pages

Tool for publishing gh-pages the pro way.
WIP

Install

  1. npm install --save-dev github-pages

Usage

CLI Usage

  1. Publishes your github pages using the github API
  2. Usage
  3. $ github-pages [options] [src]
  4. Options
  5. -r, --repo
  6. -t, --token
  7. -m --commit-message
  8. -a --commit-author
  9. --remote-ref
  10. --api-version
  11. --api-protocol
  12. --api-host
  13. --api-path
  14. --api-timeout
  15. Examples
  16. $ github-pages -r user/repo -t $GH_TOKEN ./data
  17. > github-pages commit
  18. > github-pages push to user/repo

Not that the CLI arguments will always overwrite your github-pages configuration in your package.json file.

You only need to provide the repository, the token data and the src. The other arguments are optional.

WARNING
The GithubPages will replace all the content of the given ref (heads/gh-pages by default) with the provided content.
EVERYTHING that is not present in the src folder will be deleted in your branch.
Have you made any mistake? Don’t panic, GithubPages does not mess up with you git story. Just revert the commit. ;)

Configuration

All of the CLI options can be configured in the GithubPages section of your package.json. This allows you to modify the default behavior of the GithubPages command.

  1. {
  2. "github-pages": {
  3. "api": {
  4. "version": "3.0.0",
  5. "protocol": "https",
  6. "host": "api.github.com",
  7. "pathPrefix": "",
  8. "timeout": 5000
  9. },
  10. "auth": {
  11. "type": "token",
  12. "token": "GH_TOKEN"
  13. },
  14. "remote": {
  15. "user": "user",
  16. "repo": "repo",
  17. "ref": "heads/gh-pages"
  18. },
  19. "commit": {
  20. "message": "commit made by me",
  21. "author": {
  22. "name": "author-name",
  23. "email": "author-email"
  24. }
  25. },
  26. "src": "./data"
  27. }
  28. }

Arguments passed to the CLI will always take precedence over the configuration in package.json.

$GH_TOKEN: By default the token value is read from the environment var $GH_TOKEN. So if you don’t want to set it in the configurations/cli, it will be read from $GH_TOKEN.

API Usage

  1. const GithubPages = require('github-pages');
  2. const config = require('package.json')['github-pages'];
  3. const pages = new GithubPages(config);
  4. pages.publish().then((res)=> {
  5. console.log('published');
  6. console.log(JSON.stringify(res, null, 2));
  7. }).catch((err)=> {
  8. console.error('error while publishing');
  9. console.error(JSON.stringify(err, null, 2));
  10. });

The GithubPages needs the complete configuration to be used (same file structure as described in package.json). If you don’t want to provide all the configuration you can use the support file parse-config, passing the same arguments as the cli (using camelCase).

  1. const GithubPages = require('github-pages');
  2. const parseConfig = require('github-pages').parseConfig;
  3. const config = parseConfig({
  4. repo: 'user/repo',
  5. token: 'GH-TOKEN',
  6. remoteRef: 'heads/gh-pages',
  7. commitMessage: 'publishing from API.',
  8. commitAuthor: 'author-name <author-email>',
  9. apiVersion: '3.0.0',
  10. apiProtocol: 'https',
  11. apiHost: 'api.github.com',
  12. apiPath: '',
  13. apiTimeout: 5000
  14. }, './dist');
  15. const pages = new GithubPages(config);

The parseConfig method receives as a input the same structure used in the cli, which is a plain object with the cli params using camelCase, different from the one used in the package.json. Besides that, parseConfig method will try to preload the configuration from your package.json.

The parseConfig function will return null if the final configuration is invalid.

If you want to get the default configuration and change it yourself, then use if directly:

  1. const config = require('github-pages').parseConfig.default;
  2. /* => {
  3. api: {
  4. version: '3.0.0',
  5. protocol: 'https',
  6. host: 'api.github.com',
  7. pathPrefix: '',
  8. timeout: 5000
  9. },
  10. auth: { type: 'token', token: 'GH_TOKEN' },
  11. remote: { ref: 'heads/gh-pages' },
  12. commit: { message: 'github-pages publish.' }
  13. } */

If you want to validate your config object, you can use the helper function from parseConfig called isValid:

  1. const isValid = require('github-pages').parseConfig.isValid;
  2. isValid(config);
  3. // true|false

Integrating with Travis CI

To use GithubPages on your CI server, just install your lib and set your configuration.
For a less intrusive aproach, just create a script in your package.json for GithubPages:

  1. {
  2. "script": {
  3. "gh-pages": "github-pages -r user/repo ./dist"
  4. }
  5. }

add the following to your .travis.yml:

  1. after_success: npm run gh-pages

Remember that you need to provide the environment var GH_TOKEN.
To learn how to provide this var in a secure and safe way, look at travis-encrypted-environment-variables or travis-repository-variables (recommended).

stack