-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
65 lines (50 loc) · 2.02 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
const {execSync} = require("child_process");
const fs = require("fs");
const chalk = require("chalk");
// github branch pages name
const PAGES_BRANCH_NAME = 'gh-pages';
const execSyncVerbose = command => {
console.log(command);
execSync(command);
};
// get current branch name
let currentBranch = execSync("git branch |grep \\* |sed 's/\\* //g'");
currentBranch = currentBranch.toString().trim();
const endProcess = (folderName) => {
console.log('\n' + chalk.cyan(`Return to ${currentBranch} branch`));
if (folderName) {
execSyncVerbose(`rm -rf ${folderName}`);
} else {
execSyncVerbose('rm -rf dist');
execSyncVerbose('rm -rf build');
}
execSyncVerbose(`git checkout -f ${currentBranch}`);
execSyncVerbose(`git branch -D ${PAGES_BRANCH_NAME}`);
};
try {
console.log(chalk.cyan('Deploy started from branch') + ' ' + chalk.bgBlue(chalk.yellowBright(currentBranch)));
try {
console.log('\n' + chalk.cyan('Committing uncommitted changes'));
execSyncVerbose('git add .');
execSyncVerbose('git commit -m "Develop"');
execSyncVerbose('git pull');
execSyncVerbose('git push');
} catch (err) {
console.error(chalk.yellow('Nothing to commit'));
}
console.log('\n' + chalk.cyan('Build project'));
execSyncVerbose(`git checkout --orphan ${PAGES_BRANCH_NAME}`);
execSyncVerbose('npm run build');
const folderName = fs.existsSync('dist') ? 'dist' : 'build';
console.log('\n' + chalk.cyan(`Commit ${folderName} directory`));
execSyncVerbose(`git --work-tree ${folderName} add --all`);
execSyncVerbose(`git --work-tree ${folderName} commit -m "GitHub pages deploy"`);
console.log('\n' + chalk.cyan(`Pushing to ${PAGES_BRANCH_NAME}`));
execSyncVerbose(`git push origin HEAD:${PAGES_BRANCH_NAME} --force`);
endProcess(folderName);
console.log('\n' + chalk.green('SUCCESSFULLY DEPLOYED'));
} catch (err) {
console.error(chalk.red(err.message));
endProcess(false);
process.exit(1);
}