UNPKG

8.89 kBMarkdownView Raw
1
2# gh-pages
3
4Publish files to a `gh-pages` branch on GitHub (or any other branch anywhere else).
5
6## Getting Started
7
8```shell
9npm install gh-pages --save-dev
10```
11
12This module requires Git `>=1.9`.
13
14## Basic Usage
15
16```js
17var ghpages = require('gh-pages');
18
19ghpages.publish('dist', function(err) {});
20```
21
22
23## `publish`
24
25```js
26ghpages.publish(dir, callback);
27// or...
28ghpages.publish(dir, options, callback);
29```
30
31Calling this function will create a temporary clone of the current repository, create a `gh-pages` branch if one doesn't already exist, copy over all files from the base path, or only those that match patterns from the optional `src` configuration, commit all changes, and push to the `origin` remote.
32
33If a `gh-pages` branch already exists, it will be updated with all commits from the remote before adding any commits from the provided `src` files.
34
35**Note** that any files in the `gh-pages` branch that are *not* in the `src` files **will be removed**. See the [`add` option](#optionsadd) if you don't want any of the existing files removed.
36
37
38### <a id="dir">`dir`</a>
39* type: `string`
40
41The base directory for all source files (those listed in the `src` config property).
42
43Example use:
44
45```js
46/**
47 * Given the following directory structure:
48 *
49 * dist/
50 * index.html
51 * js/
52 * site.js
53 *
54 * The usage below will create a `gh-pages` branch that looks like this:
55 *
56 * index.html
57 * js/
58 * site.js
59 *
60 */
61ghpages.publish('dist', callback);
62```
63
64
65### Options
66
67The default options work for simple cases. The options described below let you push to alternate branches, customize your commit messages, and more.
68
69
70#### <a id="optionssrc">options.src</a>
71 * type: `string|Array<string>`
72 * default: `'**/*'`
73
74The [minimatch](https://github.com/isaacs/minimatch) pattern or array of patterns used to select which files should be published.
75
76
77#### <a id="optionsbranch">options.branch</a>
78 * type: `string`
79 * default: `'gh-pages'`
80
81The name of the branch you'll be pushing to. The default uses GitHub's `gh-pages` branch, but this can be configured to push to any branch on any remote.
82
83Example use of the `branch` option:
84
85```js
86/**
87 * This task pushes to the `master` branch of the configured `repo`.
88 */
89ghpages.publish('dist', {
90 branch: 'master',
91 repo: 'https://example.com/other/repo.git'
92}, callback);
93```
94
95
96#### <a id="optionsdest">options.dest</a>
97 * type: `string`
98 * default: `'.'`
99
100The destination folder within the destination branch. By default, all files are published to the root of the repository.
101
102Example use of the `dest` option:
103
104```js
105/**
106 * Place content in the static/project subdirectory of the target
107 * branch.
108 */
109ghpages.publish('dist', {
110 dest: 'static/project'
111}, callback);
112```
113
114#### <a id="optionsdotfiles">options.dotfiles</a>
115 * type: `boolean`
116 * default: `false`
117
118Include dotfiles. By default, files starting with `.` are ignored unless they are explicitly provided in the `src` array. If you want to also include dotfiles that otherwise match your `src` patterns, set `dotfiles: true` in your options.
119
120Example use of the `dotfiles` option:
121
122```js
123/**
124 * The usage below will push dotfiles (directories and files)
125 * that otherwise match the `src` pattern.
126 */
127ghpages.publish('dist', {dotfiles: true}, callback);
128```
129
130
131#### <a id="optionsadd">options.add</a>
132 * type: `boolean`
133 * default: `false`
134
135Only add, and never remove existing files. By default, existing files in the target branch are removed before adding the ones from your `src` config. If you want the task to add new `src` files but leave existing ones untouched, set `add: true` in your options.
136
137Example use of the `add` option:
138
139```js
140/**
141 * The usage below will only add files to the `gh-pages` branch, never removing
142 * any existing files (even if they don't exist in the `src` config).
143 */
144ghpages.publish('dist', {add: true}, callback);
145```
146
147
148#### <a id="optionsrepo">options.repo</a>
149 * type: `string`
150 * default: url for the origin remote of the current dir (assumes a git repository)
151
152By default, `gh-pages` assumes that the current working directory is a git repository, and that you want to push changes to the `origin` remote.
153
154If instead your script is not in a git repository, or if you want to push to another repository, you can provide the repository URL in the `repo` option.
155
156Example use of the `repo` option:
157
158```js
159/**
160 * If the current directory is not a clone of the repository you want to work
161 * with, set the URL for the repository in the `repo` option. This usage will
162 * push all files in the `src` config to the `gh-pages` branch of the `repo`.
163 */
164ghpages.publish('dist', {
165 repo: 'https://example.com/other/repo.git'
166}, callback);
167```
168
169
170#### <a id="optionsremote">options.remote</a>
171 * type: `string`
172 * default: `'origin'`
173
174The name of the remote you'll be pushing to. The default is your `'origin'` remote, but this can be configured to push to any remote.
175
176Example use of the `remote` option:
177
178```js
179/**
180 * This task pushes to the `gh-pages` branch of of your `upstream` remote.
181 */
182ghpages.publish('dist', {
183 remote: 'upstream'
184}, callback);
185```
186
187
188#### <a id="optionstag">options.tag</a>
189 * type: `string`
190 * default: `''`
191
192Create a tag after committing changes on the target branch. By default, no tag is created. To create a tag, provide the tag name as the option value.
193
194
195#### <a id="optionsmessage">options.message</a>
196 * type: `string`
197 * default: `'Updates'`
198
199The commit message for all commits.
200
201Example use of the `message` option:
202
203```js
204/**
205 * This adds commits with a custom message.
206 */
207ghpages.publish('dist', {
208 message: 'Auto-generated commit'
209}, callback);
210```
211
212
213#### <a id="optionsuser">options.user</a>
214 * type: `Object`
215 * default: `null`
216
217If you are running the `gh-pages` task in a repository without a `user.name` or `user.email` git config properties (or on a machine without these global config properties), you must provide user info before git allows you to commit. The `options.user` object accepts `name` and `email` string values to identify the committer.
218
219Example use of the `user` option:
220
221```js
222ghpages.publish('dist', {
223 user: {
224 name: 'Joe Code',
225 email: 'coder@example.com'
226 }
227}, callback);
228```
229
230
231#### <a id="optionspush">options.push</a>
232 * type: `boolean`
233 * default: `true`
234
235Push branch to remote. To commit only (with no push) set to `false`.
236
237Example use of the `push` option:
238
239```js
240ghpages.publish('dist', {push: false}, callback);
241```
242
243
244#### <a id="optionssilent">options.silent</a>
245 * type: `boolean`
246 * default: `false`
247
248Avoid showing repository URLs or other information in errors.
249
250Example use of the `silent` option:
251
252```js
253/**
254 * This configuration will avoid logging the GH_TOKEN if there is an error.
255 */
256ghpages.publish('dist', {
257 repo: 'https://' + process.env.GH_TOKEN + '@github.com/user/private-repo.git',
258 silent: true
259}, callback);
260```
261
262
263#### <a id="optionsgit">options.git</a>
264 * type: `string`
265 * default: `'git'`
266
267Your `git` executable.
268
269Example use of the `git` option:
270
271```js
272/**
273 * If `git` is not on your path, provide the path as shown below.
274 */
275ghpages.publish('dist', {
276 git: '/path/to/git'
277}, callback);
278```
279
280## Command Line Utility
281
282Installing the package creates a `gh-pages` command line utility. Run `gh-pages --help` to see a list of supported options.
283
284With a local install of `gh-pages`, you can set up a package script with something like the following:
285
286```shell
287"scripts": {
288 "deploy": "gh-pages -d dist"
289}
290```
291
292And then to publish everything from your `dist` folder to your `gh-pages` branch, you'd run this:
293
294```shell
295npm run deploy
296```
297
298## Debugging
299
300To get additional output from the `gh-pages` script, set `NODE_DEBUG=gh-pages`. For example:
301
302```shell
303NODE_DEBUG=gh-pages npm run deploy
304```
305
306## Dependencies
307
308Note that this plugin requires Git 1.9 or higher (because it uses the `--exit-code` option for `git ls-remote`). If you'd like to see this working with earlier versions of Git, please [open an issue](https://github.com/tschaub/gh-pages/issues).
309
310[![Current Status](https://secure.travis-ci.org/tschaub/gh-pages.svg?branch=master)](https://travis-ci.org/tschaub/gh-pages)
311
312## Tips
313
314### when get error `branch already exists`
315```
316{ ProcessError: fatal: A branch named 'gh-pages' already exists.
317
318 at ChildProcess.<anonymous> (~/node_modules/gh-pages/lib/git.js:42:16)
319 at ChildProcess.emit (events.js:180:13)
320 at maybeClose (internal/child_process.js:936:16)
321 at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
322 code: 128,
323 message: 'fatal: A branch named \'gh-pages\' already exists.\n',
324 name: 'ProcessError' }
325 ```
326
327When processing `gh-pages` module generate file in`.cache/` and if stuck some reason like wrong password
328it will not automatically cleanup
329
330Run `~node_modules/gh-pages/bin/gh-pages-clean`
331or remove `~node_modules/gh-pages/.cache`
332
\No newline at end of file