-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
85 lines (68 loc) · 1.99 KB
/
Copy pathdeploy.js
File metadata and controls
85 lines (68 loc) · 1.99 KB
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
#!/usr/bin/env node
const fs = require('node:fs');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const rootDir = __dirname;
const distDir = path.join(rootDir, 'docs', '.vuepress', 'dist');
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
function run(command, args, options = {}) {
const result = spawnSync(command, args, {
cwd: options.cwd || rootDir,
stdio: options.stdio || 'inherit',
encoding: 'utf8',
shell: options.shell || false,
});
if (result.error) {
console.error(`Failed to run ${command}: ${result.error.message}`);
process.exit(1);
}
if (result.status !== 0) {
if (options.stdio === 'pipe') {
if (result.stdout) {
process.stdout.write(result.stdout);
}
if (result.stderr) {
process.stderr.write(result.stderr);
}
}
process.exit(result.status || 1);
}
return result.stdout ? result.stdout.trim() : '';
}
function getRepoUrl() {
const remoteUrl = run('git', ['remote', 'get-url', 'origin'], {
cwd: rootDir,
stdio: 'pipe',
});
if (!remoteUrl) {
console.error('Git remote "origin" not found.');
process.exit(1);
}
return remoteUrl;
}
const repoUrl = getRepoUrl();
console.log('Building site...');
run(npmCommand, ['run', 'build'], {
shell: process.platform === 'win32',
});
if (!fs.existsSync(distDir)) {
console.error(`Build output not found: ${distDir}`);
process.exit(1);
}
console.log('Preparing deployment branch...');
run('git', ['init'], { cwd: distDir });
run('git', ['add', '-A'], { cwd: distDir });
const status = run('git', ['status', '--porcelain'], {
cwd: distDir,
stdio: 'pipe',
});
if (!status) {
console.log('No changes to deploy.');
process.exit(0);
}
run('git', ['commit', '-m', 'deploy'], { cwd: distDir });
console.log('Pushing to remote branch gh-pages...');
run('git', ['push', '-f', repoUrl, 'HEAD:gh-pages'], {
cwd: distDir,
});
console.log('Deploy finished. Published branch: gh-pages');