This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
373 lines (325 loc) · 10.4 KB
/
index.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
'use strict';
var denodeify = require('denodeify');
var fs = require('fs-extra');
var ensureDir = denodeify(fs.ensureDir);
var copy = denodeify(fs.copy);
var path = require('path');
var git = require('gift');
var RSVP = require('rsvp');
var Promise = RSVP.Promise;
var DeployPluginBase = require('ember-cli-deploy-plugin');
module.exports = {
name: 'ember-cli-deploy-ghpages',
createDeployPlugin(options) {
/**
* Private function for creating an orphan branch that does not have any
* parents.
*
* @params {Repo} repository
* @params {String} branch
* @return {Promise}
*/
function createOrphanBranch(repository, branch) {
return new Promise((resolve, reject) => {
repository.git('checkout', { orphan: true }, branch, (error, b) => {
if (error) {
return reject(error);
}
return resolve(b);
});
});
}
/**
* Private function to delete a branch.
*
* @params {Repo} repository
* @params {String} branch
* @return {Promise}
*/
function deleteBranch(repository, branch) {
return new Promise((resolve, reject) => {
repository.git('branch', { D: true }, branch, error => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
/**
* Private function for checking whether a branch exists.
*
* @params {Repo} repository
* @params {String} branch
* @return {Promise}
*/
function branchExists(repository, branch) {
return new Promise((resolve, reject) => {
repository.branch(branch, (error) => {
if (error) {
return reject();
}
return resolve();
});
});
}
/**
* Private function for deleting all files.
*
* @params {Repo} repository
* @return {Promise}
*/
function removeAll(repository) {
return new Promise((resolve, reject) => {
repository.remove('.', { r: true, f: true }, (error, output) => {
if (error) {
return reject(error);
}
return resolve(output);
});
});
}
/**
* Private function for copying dist files to tmp directory.
*
* @params {String} srcDir
* @params {Array} srcFiles
* @params {String} dstDir
* @return {Promise}
*/
function copyDistFiles(srcDir, srcFiles, dstDir) {
return RSVP.all(srcFiles.map(file => copy(
path.resolve(srcDir, file),
path.resolve(dstDir, file)
)));
}
/**
* Private function for staging all files to the repo.
*
* @params {Repo} repository
* @returns {Promise}
*/
function stageAllFiles(repository) {
return new Promise((resolve, reject) => {
repository.add('.', error => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
/**
* Private function for commiting.
*
* @params {Repo} repository
* @return {Promise}
*/
function commit(repository, message) {
return new Promise((resolve, reject) => {
repository.commit(message, error => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
/**
* Private function for adding remote repo.
*
* @params {Repo} repository
* @params {String} remoteUrl
* @return {Promise}
*/
function addRemote(repository, remoteName, remoteUrl) {
return new Promise((resolve, reject) => {
repository.remote_add(remoteName, remoteUrl, error => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
/**
* Private function for setting upstream for a branch.
*
* @params {Repo} repository
* @params {String} branch
* @params {String} remoteName
* @return {Promise}
*/
function setUpstream(repository, branch, remoteName) {
return new Promise((resolve, reject) => {
let remoteBranch = `${remoteName}/${branch}`;
repository.git('branch', { u: true }, [remoteBranch, branch], error => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
/**
* Private function for pushing to upstream branch.
*
* @params {Repo} repository
* @params {String} branch
* @params {String} remote
* @params {Boolean} setUpstream
* @return {Promise}
*/
function pushUpstream(repository, branch, remote, setUpstream) {
return new Promise((resolve, reject) => {
repository.git('push', {
u: setUpstream,
f: true
}, [remote, branch], error => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
var GHPagesPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
projectTmpPath: 'tmp',
repoTmpPath: 'gh-pages',
commitMessage: 'Deploy dist files by ember-cli-deploy-ghpages',
gitBranch: 'gh-pages',
gitRemoteName: 'ember-cli-deploy',
domain: undefined
},
requiredConfig: ['gitRemoteUrl'],
setup: function (context) {
let repo;
let self = this;
let branch = this.readConfig('gitBranch');
let tmp = this.readConfig('projectTmpPath');
let relativeRepoTmpPath = this.readConfig('repoTmpPath');
let projectTmpPath = path.resolve(context.project.root, tmp);
let repoTmpPath = path.resolve(projectTmpPath, relativeRepoTmpPath);
let domain = this.readConfig('domain');
return ensureDir(path.resolve(context.project.root, repoTmpPath))
.then(function () {
self.log('cloning project repository to tmp');
// TODO: Replace with streams for faster copy.
return copy(context.project.root, repoTmpPath, {
// If you recursively copy the tmp directory, you'll have a bad
// time.
filter: path => (
path !== projectTmpPath &&
path.indexOf('node_modules') < 0 &&
path.indexOf('bower_components') < 0
)
});
})
.then(function () {
self.log('cloned project repository to tmp', { color: 'green' });
repo = context.gitRepo = git(repoTmpPath);
return branchExists(repo, branch);
})
.then(function () {
self.log(`branch '${branch}' already exists`, { color: 'yellow' });
return deleteBranch(repo, branch)
.then(function () {
return createOrphanBranch(repo, branch);
});
}, function () {
return createOrphanBranch(repo, branch);
})
.then(function () {
self.log(`successfully checked out '${branch}' branch`, {
color: 'green'
});
self.log('removing all files');
return removeAll(repo);
})
.then(function () {
self.log(`all files removed on '${branch}' branch`, {
color: 'green'
});
if (domain) {
self.log(`setting up custom domain`);
let cname = path.resolve(repoTmpPath, 'CNAME');
fs.writeFile(cname, domain, () => {
self.log(`custom domain set up at: ${domain}`);
});
}
})
.catch(function (error) {
this.log(error, { color: 'red' });
});
},
didBuild: function (context) {
let self = this;
let tmp = this.readConfig('projectTmpPath');
let repoTmpPath = path.join(tmp, this.readConfig('repoTmpPath'));
repoTmpPath = path.resolve(context.project.root, repoTmpPath);
this.log('copying dist files to tmp');
return copyDistFiles(context.distDir, context.distFiles, repoTmpPath)
.then(function () {
self.log('copied dist files to tmp', { color: 'green' });
self.log('staging all files for commit');
return stageAllFiles(context.gitRepo);
})
.then(function () {
self.log('committing staged files');
return commit(context.gitRepo, self.readConfig('commitMessage'));
})
.then(function () {
self.log('committed all staged files', { color: 'green' });
})
.catch(error => self.log(error));
},
willUpload: function (context) {
let remoteName = this.readConfig('gitRemoteName');
let remoteUrl = this.readConfig('gitRemoteUrl');
let branch = this.readConfig('gitBranch');
return addRemote(context.gitRepo, remoteName, remoteUrl)
.then(() => {
this.log('remote repository added', { color: 'green' });
return setUpstream(context.gitRepo, branch, remoteName);
})
.catch(() => {
// Upstream branch does not exist. Mark it so we know during
// upload stage.
this.log('remote branch does not exist', { color: 'yellow' });
context.setUpstreamOnPush = true;
});
},
upload: function (context) {
let repo = context.gitRepo;
let branch = this.readConfig('gitBranch');
let remoteName = this.readConfig('gitRemoteName');
let setUpstream = context.setUpstreamOnPush;
this.log('pushing to remote repository');
return pushUpstream(repo, branch, remoteName, setUpstream)
.then(() => {
this.log('push success', { color: 'green' });
})
.catch(error => this.log(error, { color: 'red' }));
},
teardown: function (context) {
let tmp = this.readConfig('projectTmpPath');
let repoTmpPath = path.join(tmp, this.readConfig('repoTmpPath'));
repoTmpPath = path.resolve(context.project.root, repoTmpPath);
return new Promise((resolve, reject) => {
this.log('cleaning up');
fs.remove(repoTmpPath, error => {
if (error) {
this.log(error, { color: 'red' });
reject(error);
}
this.log('cleanup complete', { color: 'green' });
resolve();
});
});
}
});
return new GHPagesPlugin();
}
};