-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploy.js
80 lines (69 loc) · 2.41 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
import path from 'path';
import npmRunScript from 'npm-run-script';
import fetch from 'node-fetch';
import { spawn } from './lib/cp';
import { makeDir } from './lib/fs';
// Heroku
const remote = {
name: 'heroku',
url: 'https://git.heroku.com/polskifrontend-back.git',
branch: 'master',
website: 'https://polskifrontend-back.herokuapp.com/',
};
const options = {
cwd: path.resolve(__dirname, './dist'),
stdio: ['ignore', 'inherit', 'inherit'],
};
/**
* Deploy the contents of the `/build` folder to a remote server via Git.
*/
async function deploy() {
// Initialize a new repository
await makeDir('dist');
await spawn('git', ['init', '--quiet'], options);
// Changing a remote's URL
let isRemoteExists = false;
try {
await spawn('git', ['config', '--get', `remote.${remote.name}.url`], options);
isRemoteExists = true;
} catch (error) {
/* skip */
}
await spawn('git', ['remote', isRemoteExists ? 'set-url' : 'add', remote.name, remote.url], options);
// Fetch the remote repository if it exists
let isRefExists = false;
try {
await spawn('git', ['ls-remote', '--quiet', '--exit-code', remote.url, remote.branch], options);
isRefExists = true;
} catch (error) {
await spawn('git', ['update-ref', '-d', 'HEAD'], options);
}
if (isRefExists) {
await spawn('git', ['fetch', remote.name], options);
await spawn('git', ['reset', `${remote.name}/${remote.branch}`, '--hard'], options);
await spawn('git', ['clean', '--force'], options);
}
// Build the project in RELEASE mode which
// generates optimized and minimized bundles
await runScript();
// Push the contents of the build folder to the remote server via Git
await spawn('git', ['add', '.', '--all'], options);
try {
await spawn('git', ['diff', '--cached', '--exit-code', '--quiet'], options);
} catch (error) {
await spawn('git', ['commit', '--message', `Update ${new Date().toISOString()}`], options);
}
await spawn('git', ['push', remote.name, `master:${remote.branch}`, '--set-upstream'], options);
// Check if the site was successfully deployed
const response = await fetch(remote.website);
console.log(`${remote.website} => ${response.status} ${response.statusText}`);;
}
async function runScript() {
return new Promise(resolve => {
const child = npmRunScript('npm run build && npm run copy');
child.once('exit', () => {
resolve();
});
});
}
export default deploy;