-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
99 lines (91 loc) · 3.55 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const fs = require('fs-extra');
const Git = require('gift');
const gulp = require('gulp');
const log = require('fancy-log');
const path = require('path');
const util = require('util');
const version = require('./package.json').version;
/**
* when version No is specified, check for be not development status
*/
function checkIsStableVersion () {
return version.indexOf('dev') === -1;
}
/**
* move 'dist' directory (that is compiled by the before webpack process) to the distribution directory
* @param baseDir webroot (/var/www/html/htmlanno) or gh-pages branch root ('.publish')
* @param tag application root (latest | the version No)
*/
function publish (baseDir, tag) {
const distDir = path.join(baseDir, tag);
log.info('replacing ' + distDir);
fs.removeSync(distDir);
fs.copySync('dist', distDir);
}
/**
* clone a repository from GitHub and checkout opts.branch branch.
* @param opts option set for git
* @param opts.cacheDir gh-pages branch root ('.publish')
* @param opts.branch branch name ('gh-pages')
* @return gift Repo object (https://github.com/notatestuser/gift#repo)
*/
async function cloneAndCheckout (opts) {
const git = Git(process.cwd());
const gitConf = await util.promisify(git.config.bind(git))();
const repository = await util.promisify(Git.clone.bind(this))(gitConf.items['remote.origin.url'], opts.cacheDir);
await util.promisify(repository.checkout.bind(repository))(opts.branch);
return repository;
}
gulp.task('prepare', () => {
fs.removeSync('dist');
['index.html', 'index.css', 'jats-preview.css', 'sample'].forEach((target) => {
fs.copySync(path.join('src', 'preset', target), path.join('dist', target));
});
});
gulp.task('publish', async () => {
const readmeFile = 'README.md';
if (version !== undefined && version !== null) {
const target = checkIsStableVersion() ? version: 'latest';
log.info(`start delpoying for ${target}`);
// Compatible with gulp-gh-pages lib.
const opts = {
branch: 'gh-pages',
message: `updated ${new Date().toISOString()}`,
cacheDir: '.publish',
push : true
};
log.info(`checking out ${opts.branch} branch as ${opts.cacheDir}`);
if (fs.existsSync(opts.cacheDir)) {
fs.removeSync(opts.cacheDir);
}
// 1. clone and checkout
const repo = await cloneAndCheckout(opts);
// 2, remove current content if exists it
await util.promisify(repo.remove.bind(repo))(target, {r: true, 'ignore-unmatch': true});
// 3. local deploy
publish(opts.cacheDir, target);
fs.copySync(readmeFile, path.join(opts.cacheDir, readmeFile));
// 4. add new content to repository
await util.promisify(repo.add.bind(repo))(target);
await util.promisify(repo.add.bind(repo))(readmeFile);
// 5. commit
const gitStatus = await util.promisify(repo.status.bind(repo))();
if (gitStatus.clean) {
log.info('commit are skipped because does not find any difference');
} else {
await util.promisify(repo.commit.bind(repo))(opts.message);
if (opts.push) {
// 6. push to GitHub (real deploy)
await util.promisify(repo.remote_push.bind(repo))('origin', opts.branch);
} else {
log.info('dry-run');
}
}
} else {
log.error('Need `version` property in package.json');
}
});
gulp.task('publishLocal', () => {
const baseDir = process.env.BASE_DIR;
publish(baseDir, 'latest');
});