This repository has been archived by the owner on Dec 2, 2019. It is now read-only.
forked from ember-cli-deploy/ember-cli-deploy-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (83 loc) · 2.79 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
/* jshint node: true */
'use strict';
var Promise = require('ember-cli/lib/ext/promise');
var glob = require('glob');
var DeployPluginBase = require('ember-cli-deploy-plugin');
var path = require('path');
var fs = require('fs');
module.exports = {
name: 'ember-cli-deploy-build',
createDeployPlugin: function(options) {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
environment: 'production',
outputPath: 'tmp' + path.sep + 'deploy-dist'
},
willBuild: function() {
var outputPath = this.readConfig('outputPath');
this._deleteDistFolder(outputPath);
},
build: function(context) {
var self = this;
var outputPath = this.readConfig('outputPath');
var buildEnv = this.readConfig('environment');
var Builder = this.project.require('ember-cli/lib/models/builder');
var builder = new Builder({
ui: this.ui,
outputPath: outputPath,
environment: buildEnv,
project: this.project
});
this.log('building app to `' + outputPath + '` using buildEnv `' + buildEnv + '`...', { verbose: true });
return builder.build()
.finally(function() {
return builder.cleanup();
})
.then(this._logSuccess.bind(this, outputPath))
.then(function(files) {
files = files || [];
return {
distDir: outputPath,
distFiles: files
};
})
.catch(function(error) {
self.log('build failed', { color: 'red' });
return Promise.reject(error);
});
},
_logSuccess: function(outputPath) {
var self = this;
var files = glob.sync('**/**/*', { nonull: false, nodir: true, cwd: outputPath });
if (files && files.length) {
files.forEach(function(path) {
self.log('✔ ' + path, { verbose: true });
});
}
self.log('build ok', { verbose: true });
return Promise.resolve(files);
},
_deleteDistFolder: function(outputPath) {
var self = this;
try {
if (fs.statSync(outputPath)) {
self.log('Deleting dist folder ' + outputPath, { verbose: true });
fs.readdirSync(outputPath).forEach(function(file /*, index*/) {
var currentPath = path.join(outputPath, file);
if (fs.lstatSync(currentPath).isDirectory()) {
self._deleteDistFolder(currentPath);
} else {
fs.unlinkSync(currentPath);
}
});
fs.rmdirSync(outputPath);
}
} catch (e) {
// no such file or directory - skip
}
}
});
return new DeployPlugin();
}
};