This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
97 lines (79 loc) · 2.66 KB
/
index.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
// Publish task
var exec = require('child_process').exec
, path = require('path')
, rimraf = require('rimraf')
, fs = require('fs')
;
function buildBranch(options, callback) {
var curBranch = 'master'
, execOptions = {}
, command = ''
;
// Checking options
options.folder = options.folder || 'www';
options.branch = options.branch || 'gh-pages';
options.remote = options.remote || 'origin';
options.ignore = options.ignore || [];
options.ignore.push('.git', 'node_modules', options.folder);
options.cname = options.cname || 'CNAME';
options.commit = options.commit || 'Build '+(new Date());
options.cwd = options.cwd || process.cwd();
execOptions.cwd = options.cwd;
// Remember the current branch
command = 'git rev-parse --abbrev-ref HEAD'
exec(command, execOptions, function(err, stdout, stderr) {
if(err) {
callback(err); return;
}
curBranch = stdout.trim();
// Switch to ghpages branch
command = 'git branch -D ' + options.branch + ';'
+' git checkout --orphan ' + options.branch + ';'
+' git rm -r --cached .';
exec(command, execOptions, function(err) {
var ignore;
if(err) {
callback(err); return;
}
// delete all files except the untracked ones
ignore = options.ignore.slice(0);
fs.readdirSync(options.cwd).forEach(function(file) {
if(-1 === ignore.indexOf(file)) {
rimraf.sync(path.join(options.cwd, file));
}
});
fs.readdirSync(path.join(options.cwd, options.folder))
.forEach(function(file) {
fs.renameSync(path.join(options.cwd, options.folder, file),
path.join(options.cwd, file));
});
fs.rmdirSync(path.join(options.cwd, options.folder));
// Add the domain cname field
if(options.domain) {
fs.writeFileSync(path.join(options.cwd, options.cname), options.domain);
}
// Add a new ignore file
ignore.push('.gitignore');
fs.writeFileSync(path.join(options.cwd, '.gitignore'), ignore.join('\n'));
// Commit files
command = 'git add .;'
+ ' git commit -m "' + options.commit.replace('"', '\\"') + '"';
exec(command, execOptions, function(err) {
if(err) {
callback(err); return;
}
// Pushing commit
command = 'git push -f ' + options.remote + ' ' + options.branch + ';'
+ ' git checkout ' + curBranch + ' ;'
+ ' git checkout .';
exec(command, execOptions, function(err) {
if(err) {
callback(err); return;
}
callback();
});
});
});
});
}
module.exports = buildBranch;