forked from timsayshey/cfml-cheatsheets
/
deploy.js
100 lines (92 loc) · 2.91 KB
/
deploy.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
100
var exec = require('child_process').exec,
fs = require('fs'),
del = require('del');
// Shell gitCommands
var gitCommands = {
currentBranch: "git branch | grep \\* | cut -d ' ' -f2",
clone:
'git clone -b gh-pages https://github.com/timsayshey/cheatSheets.git gh-pages',
configUser: 'git config user.name "Travis-CI"',
configEmail: 'git config user.email "timsayshey@gmail.com"',
add: 'git add --all',
commit: 'git commit -am "Automatic build from Travis-CI"',
push:
'git push "https://${GH_TOKEN}@github.com/timsayshey/cheatSheets.git" gh-pages:gh-pages'
};
var DEPLOY_BRANCH = 'master';
/**
* Executes a shell command and returns result in a promise.
* @return {Promise} the promise that will be resolved when command is executed
*/
var execCommand = function(cmd) {
var p = new Promise(function(resolve, reject) {
exec(cmd, function(err, stdout, stderr) {
if (!err) {
resolve(stdout);
} else {
reject(stderr);
}
});
});
return p;
};
/**
* Returns the current branch name.
* @return {String} the current branch name
*/
var getBranchName = function() {
if (process.env.TRAVIS_BRANCH) {
return Promise.resolve(process.env.TRAVIS_BRANCH);
} else {
return execCommand(gitCommands.currentBranch);
}
};
// Steps for deployment
// 1 - get Branch name
// 2 - clean gh-pages directory
// 2 - Copy 'dist' directory contents into gh-pages
// 3 - Add, commit and push changes to Github
// 1 - get Branch name
getBranchName()
.then(function(branch) {
branch = branch.trim();
console.log('DEPLOY_BRANCH', DEPLOY_BRANCH, DEPLOY_BRANCH.length);
console.log('banch', branch, branch.length);
if (branch === DEPLOY_BRANCH) {
return Promise.resolve();
} else {
return Promise.reject('WRONG_BRANCH');
}
})
// 2 - clean gh-pages directory
.then(() => {
console.info('gh-pages cleaned');
return del('gh-pages/**/*', { force: true, dot: true });
})
// 3 - Checkout gh-pages from github
.then(function() {
console.info('Checkout gh-pages');
return execCommand(gitCommands.clone);
})
// 4 - Copy 'dist' directory contents into gh-pages
.then(function() {
var path = 'gh-pages/';
console.info('Copy dist/* into ' + path);
return execCommand('cp -rf dist/* ' + path);
})
// 5- Add, commit and push changes to Github
.then(execCommand.bind(null, 'cd gh-pages && ' + gitCommands.configUser))
.then(execCommand.bind(null, 'cd gh-pages && ' + gitCommands.configEmail))
.then(execCommand.bind(null, 'cd gh-pages && ' + gitCommands.add))
.then(execCommand.bind(null, 'cd gh-pages && ' + gitCommands.commit))
.then(execCommand.bind(null, 'cd gh-pages && ' + gitCommands.push))
.catch(function(err) {
if (err === 'WRONG_BRANCH') {
console.log(
'deploy in only available on "' + DEPLOY_BRANCH + '" branch.'
);
} else {
console.error(err);
throw new Error(err);
}
});