/
engine.ts
233 lines (206 loc) · 5.95 KB
/
engine.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { logging } from '@angular-devkit/core';
import * as fse from 'fs-extra';
import * as path from 'path';
import { Schema } from '../deploy/schema';
import { GHPages } from '../interfaces';
import { defaults } from './defaults';
export async function run(
dir: string,
options: Schema,
logger: logging.LoggerApi
) {
options = prepareOptions(options, logger);
// this has to occur _after_ the monkeypatch of util.debuglog:
const ghpages = require('gh-pages');
// always clean the cache directory.
// avoids "Error: Remote url mismatch."
if (options.dryRun) {
logger.info('Dry-run / SKIPPED: cleaning of the cache directory');
} else {
ghpages.clean();
}
await checkIfDistFolderExists(dir);
await createNotFoundPage(dir, options, logger);
await createCnameFile(dir, options, logger);
await publishViaGhPages(ghpages, dir, options, logger);
logger.info(
'🚀 Successfully published via angular-cli-ghpages! Have a nice day!'
);
}
export function prepareOptions(origOptions: Schema, logger: logging.LoggerApi) {
const options = {
...defaults,
...origOptions
};
if (origOptions.noSilent) {
options.silent = !origOptions.noSilent;
// monkeypatch util.debuglog to get all the extra information
// see https://stackoverflow.com/a/39129886
const util = require('util');
let debuglog = util.debuglog;
util.debuglog = set => {
if (set === 'gh-pages') {
return function() {
let message = util.format.apply(util, arguments);
logger.info(message);
};
}
return debuglog(set);
};
}
if (origOptions.noDotfiles) {
options.dotfiles = !origOptions.noDotfiles;
}
if (options.dryRun) {
logger.info('Dry-run: No changes are applied at all.');
}
if (options.name && options.email) {
options['user'] = {
name: options.name,
email: options.email
};
}
if (process.env.TRAVIS) {
options.message +=
' -- ' +
process.env.TRAVIS_COMMIT_MESSAGE +
' \n\n' +
'Triggered by commit: https://github.com/' +
process.env.TRAVIS_REPO_SLUG +
'/commit/' +
process.env.TRAVIS_COMMIT +
'\n' +
'Travis CI build: https://travis-ci.org/' +
process.env.TRAVIS_REPO_SLUG +
'/builds/' +
process.env.TRAVIS_BUILD_ID;
}
if (process.env.CIRCLECI) {
options.message +=
'\n\n' +
'Triggered by commit: https://github.com/' +
process.env.CIRCLE_PROJECT_USERNAME +
'/' +
process.env.CIRCLE_PROJECT_REPONAME +
'/commit/' +
process.env.CIRCLE_SHA1 +
'\n' +
'CircleCI build: ' +
process.env.CIRCLE_BUILD_URL;
}
if (process.env.GH_TOKEN && options.repo) {
options.repo = options.repo.replace(
'http://github.com/',
'http://GH_TOKEN@github.com/'
);
options.repo = options.repo.replace(
'https://github.com/',
'https://GH_TOKEN@github.com/'
);
options.repo = options.repo.replace('GH_TOKEN', process.env.GH_TOKEN);
}
return options;
}
async function checkIfDistFolderExists(dir: string) {
if (await !fse.pathExists(dir)) {
throw new Error(
'Dist folder does not exist. Check the dir --dir parameter or build the project first!'
);
}
}
async function createNotFoundPage(
dir: string,
options: Schema,
logger: logging.LoggerApi
) {
if (options.dryRun) {
logger.info('Dry-run / SKIPPED: copying of index.html to 404.html');
return;
}
// Note:
// There is no guarantee that there will be an index.html file,
// as we may may specify a custom index file.
// TODO: respect setting in angular.json
const indexHtml = path.join(dir, 'index.html');
const notFoundPage = path.join(dir, '404.html');
try {
return await fse.copy(indexHtml, notFoundPage);
} catch (err) {
logger.info(
'index.html could not be copied to 404.html. This does not look like an angular-cli project?!'
);
logger.info(
'(Hint: are you sure that you have setup the directory correctly?)'
);
logger.debug('Diagnostic info: ' + err.message);
return;
}
}
async function createCnameFile(
dir: string,
options: Schema,
logger: logging.LoggerApi
) {
if (!options.cname) {
return;
}
const cnameFile = path.join(dir, 'CNAME');
if (options.dryRun) {
logger.info(
'Dry-run / SKIPPED: creating of CNAME file with content: ' + options.cname
);
return;
}
try {
await fse.writeFile(cnameFile, options.cname);
logger.info('CNAME file created');
} catch (err) {
throw new Error('CNAME file could not be created. ' + err.message);
}
}
async function publishViaGhPages(
ghPages: GHPages,
dir: string,
options: Schema,
logger: logging.LoggerApi
) {
if (options.dryRun) {
logger.info(
`Dry-run / SKIPPED: publishing folder "${dir}" with the following options: ` +
JSON.stringify(
{
dir: dir,
repo:
options.repo ||
'falsy: current working directory (which must be a git repo in this case) will be used to commit & push',
message: options.message,
branch: options.branch,
user:
options.user ||
'falsy: local or global git username & email properties will be taken',
silent:
options.silent || 'falsy: logging is in silent mode by default',
dotfiles:
options.dotfiles || 'falsy: dotfiles are included by default',
cname: options.cname || 'falsy: no CNAME file will be created'
},
null,
' '
)
);
return;
}
logger.info(
'👨🚀 Uploading via git, please wait...'
);
// do NOT (!!) await ghPages.publish,
// the promise is implemented in such a way that it always succeeds – even on errors!
return new Promise((resolve, reject) => {
ghPages.publish(dir, options, error => {
if (error) {
return reject(error);
}
resolve();
});
});
}