-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
140 lines (121 loc) · 3.66 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
/* eslint-env node */
'use strict';
var RSVP = require('rsvp');
var glob = require('glob');
var DeployPluginBase = require('ember-cli-deploy-plugin');
var path = require('path');
var Funnel = require('broccoli-funnel');
var stew = require('broccoli-stew');
var fs = require('fs');
var chalk = require('chalk');
function cleanupRobotsTxt(outputPath) {
var files = glob.sync(outputPath + path.sep + 'robots-*.txt');
if (files && files.length) {
files.forEach(function(path) {
fs.unlink(path, function(){});
});
}
}
module.exports = {
name: 'ember-cli-deploy-build-plus',
// Cleanup env specific robots.txt
postBuild: function(result) {
cleanupRobotsTxt(result.directory);
},
// Pick env specific robots.txt
treeForPublic: function() {
var appEnv = this.app.env;
if (process.env.DEPLOY_TARGET) {
appEnv = process.env.DEPLOY_TARGET;
}
var publicFiles = new Funnel(this.app.trees.public);
this._requireBuildPackages();
fs.stat(
path.join(this.project.root, 'public', 'robots.txt'),
function(err, stats) {
if (stats && stats.isFile()) {
console.log(chalk.yellow('There is a robots.txt in /public and ENV specific robots.txt are ignored!'));
}
}
);
publicFiles = stew.rename(
publicFiles,
'robots-' + appEnv + '.txt',
'robots.txt'
);
return new Funnel(publicFiles, {
srcDir: '/',
destDir: '/'
});
},
_requireBuildPackages() {
if (this._didRequiredBuildPackages === true) {
return;
} else {
this._didRequiredBuildPackages = true;
}
},
createDeployPlugin: function(options) {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
environment: 'production',
outputPath: 'tmp' + path.sep + 'deploy-dist',
distDir: function(context) {
return context.distDir;
}
},
setup: function() {
var outputPath = this.readConfig('outputPath');
return {
distDir: outputPath
};
},
build: function(/* context */) {
var self = this;
var distDir = this.readConfig('distDir');
var buildEnv = this.readConfig('environment');
var Builder = this.project.require('ember-cli/lib/models/builder');
var builder = new Builder({
ui: this.ui,
outputPath: distDir,
environment: buildEnv,
project: this.project
});
this.log('building app to `' + distDir + '` using buildEnv `' + buildEnv + '`...', { verbose: true });
return builder.build()
.finally(function() {
return builder.cleanup();
})
.then(this._cleanupRobotsTxt.bind(this, distDir))
.then(this._logSuccess.bind(this, distDir))
.then(function(files) {
files = files || [];
return {
distFiles: files
};
})
.catch(function(error) {
self.log('build failed', { color: 'red' });
return RSVP.reject(error);
});
},
_cleanupRobotsTxt: function(outputPath) {
cleanupRobotsTxt(outputPath);
return outputPath;
},
_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 RSVP.resolve(files);
}
});
return new DeployPlugin();
}
};