-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublishPages
executable file
·56 lines (49 loc) · 1.46 KB
/
publishPages
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
#!/usr/bin/env node
/* eslint-disable no-console */
const { execSync } = require('child_process');
const { writeFileSync, existsSync, readFileSync } = require('fs');
const { resolve } = require('path');
const BranchName = require('branch-name');
const tmp = require('tmp');
if (!process.argv[2]) {
console.error('You must provide a path to publish');
}
const input = process.argv[2];
const output = process.argv[3] || input;
const branch = process.argv[4] || 'gh-pages';
const ignorePath = resolve(output, '.gitignore');
const GitIgnore = existsSync(ignorePath)
? readFileSync(ignorePath)
: `
node_modules
`;
const cnamePath = resolve(output, 'CNAME');
const CNAME = existsSync(cnamePath) && readFileSync(cnamePath);
async function runPublish() {
const branchName = await BranchName.get();
try {
process.chdir(input);
const { name: path } = tmp.dirSync();
execSync(`mv ${process.cwd()}/dist/* ${path}`);
process.chdir(output);
execSync(`git checkout ${branch}`);
execSync('git rm "*" -rf');
writeFileSync(ignorePath, GitIgnore);
if (CNAME) {
writeFileSync(cnamePath, CNAME);
}
execSync(`mv ${path}/* ${output}`);
execSync('git add .');
execSync('git commit -m "Automated publish"');
try {
execSync(`git push origin ${branch}`);
} catch (e) {
console.error(e);
}
} catch (e) {
console.error(e);
} finally {
execSync(`git checkout ${branchName}`);
}
}
runPublish().catch(console.error);