UNPKG

1.8 kBJavaScriptView Raw
1/**
2 * Deploy GitHub wiki.
3 * @memberof ape-deploying/lib
4 * @function deployWiki
5 * @param {string} src - Source filename pattern.
6 * @param {string} url - Wiki git url.
7 * @param {object} [options] - Optional settings.
8 * @param {string} [options.tmpDir] - Template directory path.
9 * @param {boolean} [options.clean] - Should clean existing wiki.
10 * @param {string} [options.commitMsg] - Message to commit.
11 * @returns {Promise}
12 */
13
14'use strict'
15
16const argx = require('argx')
17const path = require('path')
18const colorprint = require('colorprint')
19const co = require('co')
20const gitHelper = require('./helpers/git_helper')
21const fsHelper = require('./helpers/fs_helper')
22
23/** @lends deployWiki */
24function deployWiki (src, url, options) {
25 let args = argx(arguments)
26 src = args.shift('string')
27 url = args.shift('string')
28 if (args.pop('function')) {
29 throw new Error('[ape-deploying] Callback is no longer supported. Use promise instead.')
30 }
31 options = args.pop('object') || {}
32
33 let clean = !!options.clean
34 let commitMsg = options.commitMsg || 'Update wiki.'
35 let tmpDir = options.tmpDir || 'tmp'
36
37 let localRepo = path.resolve(tmpDir, `deploy-wiki-${new Date().getTime()}`)
38 return co(function * () {
39 let exists = yield fsHelper.existsAsync(src)
40 if (!exists) {
41 throw new Error(`Directory not exists: ${src}`)
42 }
43
44 yield gitHelper.cloneTo(url, localRepo)
45
46 if (clean) {
47 yield fsHelper.cleanDir(localRepo, {
48 ignore: '.git'
49 })
50 }
51
52 yield fsHelper.copyDir(src, localRepo)
53 let changed = yield gitHelper.hasChange(localRepo)
54 if (changed) {
55 yield gitHelper.pushGit(localRepo, commitMsg)
56 } else {
57 colorprint.warn('Nothing to push.')
58 }
59 yield fsHelper.rimrafAsync(localRepo)
60 })
61}
62
63module.exports = deployWiki