-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy-to-gh-pages.js
100 lines (84 loc) · 2.97 KB
/
deploy-to-gh-pages.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
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
/**
* Promisify exec.
*
* @param {string} commandString -
* @return {Promise<string>} commandString -
*/
const exec2 = (commandString) => {
return new Promise((resolve, reject) => {
exec(commandString, (err, stdout, stderr) => {
if (err) { reject(err); return; }
console.log(stdout + stderr);
resolve(stdout + stderr);
});
});
};
/**
* Copy file stopping js execution until is completed.
*
* @param {string} source -
* @param {string} target -
*/
const copyFileSync = (source, target) => {
var targetFile = target;
if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory())
targetFile = path.join(target, path.basename(source));
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
};
/**
* Copy folder and it's content stopping js execution until is completed.
*
* @param {string} source -
* @param {string} target -
*/
const copyFolderRecursiveSync = (source, target) => {
var files = [];
var targetFolder = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach((file) => {
var curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory())
copyFolderRecursiveSync(curSource, targetFolder);
else
copyFileSync(curSource, targetFolder);
});
}
};
console.log('Deleting folder static-build if exists. Close other programs if they\'re locked.');
rimraf('static-build', async () => {
if (!fs.existsSync('static-build'))
fs.mkdirSync('static-build');
await exec2('npm run build')
.catch((_err) => { console.log(_err); process.exit(0); });
await exec2('git clone -b gh-pages --single-branch https://github.com/patopitaluga/umm.git static-build')
.catch((_err) => { console.log(_err); process.exit(0); });
files = fs.readdirSync('public');
files.forEach(function(file) {
if (!fs.lstatSync(path.join('public', file)).isDirectory())
copyFileSync(path.join('public', file), 'static-build');
});
copyFolderRecursiveSync('public/dist', 'static-build');
copyFolderRecursiveSync('public/icons', 'static-build');
copyFolderRecursiveSync('public/memes', 'static-build');
await exec2('git -C static-build add .')
.catch((_err) => { console.log(_err); process.exit(0); });
await exec2('git -C static-build commit -am "Update"')
.catch((_err) => { console.log(_err); process.exit(0); });
await exec2('git -C static-build push origin gh-pages')
.catch((_err) => { console.log(_err); process.exit(0); });
rimraf('static-build', async () => {
console.log('gh-pages updated. See https://patopitaluga.github.io/umm/ ');
await exec2('start https://patopitaluga.github.io/umm/')
.catch((_err) => { console.log(_err); process.exit(0); });
});
});