-
Notifications
You must be signed in to change notification settings - Fork 12
/
deploy.js
97 lines (80 loc) · 2.5 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
const { execSync } = require('child_process');
const { existsSync } = require('fs');
const { argv, env, exit, cwd, chdir } = process;
const simpleGit = require('simple-git');
const ErrorCode = {
MISSING_PARAMETERS: -1,
PATH_NOT_FOUND: -2,
REMOTE_NOT_FOUND: -3,
CANNOT_DEPLOY_TO_ORIGIN: -4,
NO_NPM_EXECPATH: -5,
EXEC_FAILED: -6,
GIT_FAILED: -7,
GIT_NOT_CLEAN: -8,
UNKNOWN: -254
};
if (!env.npm_execpath) {
console.error('❌ use yarn deploy or npm run deploy');
exit(ErrorCode.NO_NPM_EXECPATH);
}
const npm = env.npm_execpath.match('yarn') ? 'yarn' : 'npm run';
const printUsage = () => {
const command = env.npm_execpath.match('yarn') ? `${npm} deploy` : `${npm} run deploy`;
console.error(`usage: ${command} git-remote`);
}
const run = async () => {
const git = simpleGit();
const [,, remote] = argv;
if (!remote) {
printUsage();
exit(ErrorCode.MISSING_PARAMETERS);
}
if (remote.toLowerCase() === 'origin') {
console.error('❌ cannot deploy to origin');
exit(ErrorCode.CANNOT_DEPLOY_TO_ORIGIN);
}
const remotes = await git.getRemotes();
if (!remotes.find(({name}) => name === remote)) {
console.error('❌ remote not found:', remote);
exit(ErrorCode.REMOTE_NOT_FOUND);
}
const branchTime = new Date();
const currentDir = cwd();
const branchName = `deploy-${branchTime.getTime()}`;
const status = await git.status();
if (!status.isClean()) {
console.error('❌ your branch is not clean. please ensure that your changes are committed, your working directory is clean, and there are no conflicts or untracked files.');
exit(ErrorCode.GIT_NOT_CLEAN);
}
try {
// 1. build (yarn build)
console.error('Generating build…');
execSync(`${npm} build`);
} catch(e) {
console.error(e);
exit(ErrorCode.EXEC_FAILED);
}
try {
// 3. create branch (git checkout; git add; git commit;
// 4. git push remote main --force)
// delete branch
console.error('Deploying…')
await git.checkoutLocalBranch(branchName);
await git.add('build/*');
await git.commit(`Deploy ${branchTime.toUTCString()}`);
await git.push([remote, `${branchName}:master`, '--force']);
await git.checkout('main');
await git.deleteLocalBranch(branchName, true);
console.error('✅ Done');
exit();
} catch(e) {
console.error(e);
await git.checkout('main');
await git.deleteLocalBranch(branchName, true);
chdir(currentDir);
exit(ErrorCode.GIT_FAILED);
}
}
(async () => {
await run();
})();