1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const fs = require('fs-extra');
|
11 | const path = require('path');
|
12 | const shell = require('shelljs');
|
13 |
|
14 | if (!shell.which('git')) {
|
15 | shell.echo('Sorry, this script requires git');
|
16 | shell.exit(1);
|
17 | }
|
18 |
|
19 | const siteConfig = require(`${process.cwd()}/siteConfig.js`);
|
20 | const GIT_USER = process.env.GIT_USER;
|
21 | const CURRENT_BRANCH =
|
22 | process.env.CIRCLE_BRANCH ||
|
23 | shell.exec('git rev-parse --abbrev-ref HEAD').stdout.trim();
|
24 | const ORGANIZATION_NAME =
|
25 | process.env.ORGANIZATION_NAME ||
|
26 | process.env.CIRCLE_PROJECT_USERNAME ||
|
27 | siteConfig.organizationName;
|
28 | const PROJECT_NAME =
|
29 | process.env.PROJECT_NAME ||
|
30 | process.env.CIRCLE_PROJECT_REPONAME ||
|
31 | siteConfig.projectName;
|
32 | const IS_PULL_REQUEST =
|
33 | process.env.CI_PULL_REQUEST || process.env.CIRCLE_PULL_REQUEST;
|
34 | const USE_SSH = process.env.USE_SSH;
|
35 |
|
36 | const DEPLOYMENT_BRANCH =
|
37 | PROJECT_NAME.indexOf('.github.io') !== -1 ? 'master' : 'gh-pages';
|
38 | const GITHUB_DOMAIN = 'github.com';
|
39 |
|
40 | const GITHUB_HOST =
|
41 | process.env.GITHUB_HOST || siteConfig.githubHost || GITHUB_DOMAIN;
|
42 | const CUSTOM_COMMIT_MESSAGE = process.env.CUSTOM_COMMIT_MESSAGE;
|
43 |
|
44 | if (!ORGANIZATION_NAME) {
|
45 | shell.echo(
|
46 | "Missing project organization name. Did you forget to define 'organizationName' in siteConfig.js? You may also export it via the ORGANIZATION_NAME environment variable.",
|
47 | );
|
48 | shell.exit(0);
|
49 | }
|
50 |
|
51 | if (!PROJECT_NAME) {
|
52 | shell.echo(
|
53 | "Missing project name. Did you forget to define 'projectName' in siteConfig.js? You may also export it via the PROJECT_NAME environment variable.",
|
54 | );
|
55 | shell.exit(0);
|
56 | }
|
57 |
|
58 | if (USE_SSH !== 'true' && !GIT_USER) {
|
59 | shell.echo(
|
60 | "Missing git user. Did you forget to export the 'GIT_USER' environment variable?",
|
61 | );
|
62 | shell.exit(0);
|
63 | }
|
64 |
|
65 | let remoteBranch;
|
66 | if (USE_SSH === 'true') {
|
67 | remoteBranch = `git@${GITHUB_HOST}:${ORGANIZATION_NAME}/${PROJECT_NAME}.git`;
|
68 | } else {
|
69 | remoteBranch = `https://${GIT_USER}@${GITHUB_HOST}/${ORGANIZATION_NAME}/${PROJECT_NAME}.git`;
|
70 | }
|
71 |
|
72 | if (IS_PULL_REQUEST) {
|
73 | shell.echo('Skipping deploy on a pull request');
|
74 | shell.exit(0);
|
75 | }
|
76 |
|
77 |
|
78 | const currentRepoUrl = shell
|
79 | .exec('git config --get remote.origin.url')
|
80 | .stdout.trim();
|
81 | const crossRepoPublish = !currentRepoUrl.endsWith(
|
82 | `${ORGANIZATION_NAME}/${PROJECT_NAME}.git`,
|
83 | );
|
84 |
|
85 |
|
86 |
|
87 | if (CURRENT_BRANCH === DEPLOYMENT_BRANCH && !crossRepoPublish) {
|
88 | shell.echo(`Cannot deploy from a ${DEPLOYMENT_BRANCH} branch. Only to it`);
|
89 | shell.exit(1);
|
90 | }
|
91 |
|
92 | if (
|
93 | shell.exec(
|
94 | `node ${path.join(__dirname, 'build-files.js')} ${process.argv
|
95 | .slice(2)
|
96 | .join(' ')}`,
|
97 | ).code
|
98 | ) {
|
99 | shell.echo('Error: generating html failed');
|
100 | shell.exit(1);
|
101 | }
|
102 |
|
103 |
|
104 | const currentCommit = shell.exec('git rev-parse HEAD').stdout.trim();
|
105 |
|
106 | shell.cd(process.cwd());
|
107 | shell.cd('build');
|
108 |
|
109 | if (
|
110 | shell.exec(`git clone ${remoteBranch} ${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`)
|
111 | .code !== 0
|
112 | ) {
|
113 | shell.echo('Error: git clone failed');
|
114 | shell.exit(1);
|
115 | }
|
116 |
|
117 | shell.cd(`${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`);
|
118 |
|
119 |
|
120 |
|
121 | const defaultBranch = shell
|
122 | .exec('git rev-parse --abbrev-ref HEAD')
|
123 | .stdout.trim();
|
124 | if (defaultBranch !== DEPLOYMENT_BRANCH) {
|
125 | if (shell.exec(`git checkout origin/${DEPLOYMENT_BRANCH}`).code !== 0) {
|
126 | if (shell.exec(`git checkout --orphan ${DEPLOYMENT_BRANCH}`).code !== 0) {
|
127 | shell.echo(`Error: Git checkout ${DEPLOYMENT_BRANCH} failed`);
|
128 | shell.exit(1);
|
129 | }
|
130 | } else if (
|
131 | shell.exec(`git checkout -b ${DEPLOYMENT_BRANCH}`).code +
|
132 | shell.exec(`git branch --set-upstream-to=origin/${DEPLOYMENT_BRANCH}`)
|
133 | .code !==
|
134 | 0
|
135 | ) {
|
136 | shell.echo(`Error: Git checkout ${DEPLOYMENT_BRANCH} failed`);
|
137 | shell.exit(1);
|
138 | }
|
139 | }
|
140 |
|
141 | shell.exec('git rm -rf .');
|
142 |
|
143 | shell.cd('../..');
|
144 |
|
145 | const fromPath = path.join('build', `${PROJECT_NAME}`);
|
146 | const toPath = path.join('build', `${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`);
|
147 |
|
148 |
|
149 | const excludePath = `${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`;
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | fs.copy(
|
155 | fromPath,
|
156 | toPath,
|
157 | src => {
|
158 | if (src.indexOf('.DS_Store') !== -1) {
|
159 | return false;
|
160 | }
|
161 | if (src.indexOf(excludePath) !== -1) {
|
162 | return false;
|
163 | }
|
164 |
|
165 | return true;
|
166 | },
|
167 | error => {
|
168 | if (error) {
|
169 | shell.echo(`Error: Copying build assets failed with error '${error}'`);
|
170 | shell.exit(1);
|
171 | }
|
172 |
|
173 | shell.cd(path.join('build', `${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`));
|
174 | shell.exec('git add --all');
|
175 |
|
176 | const commitMessage = CUSTOM_COMMIT_MESSAGE || 'Deploy website';
|
177 | const commitResults = shell.exec(
|
178 | `git commit -m "${commitMessage}" -m "Deploy website version based on ${currentCommit}"`,
|
179 | );
|
180 | if (shell.exec(`git push origin ${DEPLOYMENT_BRANCH}`).code !== 0) {
|
181 | shell.echo('Error: Git push failed');
|
182 | shell.exit(1);
|
183 | } else if (commitResults.code === 0) {
|
184 |
|
185 | const websiteURL =
|
186 | GITHUB_HOST === GITHUB_DOMAIN
|
187 | ? `https://${ORGANIZATION_NAME}.github.io/${PROJECT_NAME}`
|
188 | : `https://${GITHUB_HOST}/pages/${ORGANIZATION_NAME}/${PROJECT_NAME}`;
|
189 | shell.echo(`Website is live at: ${websiteURL}`);
|
190 | shell.exit(0);
|
191 | }
|
192 | },
|
193 | );
|