forked from kiwiupover/ember-cli-deploy-asset-sizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (71 loc) · 2.42 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
/* jshint node: true */
'use strict';
var RSVP = require('rsvp');
var DeployPluginBase = require('ember-cli-deploy-plugin');
var path = require('path');
var sendDeployData = function(assets, options, keen) {
keen = keen.configure({
projectId: options.KEEN_PROJECT_ID,
writeKey: options.KEEN_WRITE_KEY
});
var pushedAssets = assets.map(function(asset) {
// Get rid of the fingerprint
asset.name = asset.name.split(/-[a-f0-9]{32}/ig).join('');
// And only keep filename
asset.name = path.basename(asset.name);
return asset;
})
return new RSVP.Promise(function(resolve, reject) {
var keenPayload = JSON.stringify({ deploy: pushedAssets });
keen.addEvents(keenPayload, function(err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
module.exports = {
name: 'ember-cli-deploy-asset-sizes',
createDeployPlugin: function(options) {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
requiredConfig: ['KEEN_PROJECT_ID', 'KEEN_WRITE_KEY'],
defaultConfig: {
projectRoot: function(context) {
return context.project.root;
},
distDir: function(context) {
return context.distDir || 'tmp/deploy-dist';
},
keen: function(context) {
return context.keen;
}
},
willUpload: function(context) {
var root = this.readConfig('projectRoot');
var distDir = this.readConfig('distDir');
var keen = this.readConfig('keen') || require('keen.io');
var outputPath = root + '/' + distDir;
var options = {
KEEN_PROJECT_ID: this.readConfig('KEEN_PROJECT_ID'),
KEEN_WRITE_KEY: this.readConfig('KEEN_WRITE_KEY')
};
var AssetSizePrinter = require('ember-cli/lib/models/asset-size-printer');
var sizePrinter = new AssetSizePrinter({
ui: this.ui,
outputPath: outputPath
});
var makeAssetSizesObject = sizePrinter.makeAssetSizesObject();
// this.readConfig('sendDeployData') will automatically call the function
// Instead, we take it from the pluginConfig
var sendDeployDataFunction = this.pluginConfig.sendDeployData || sendDeployData;
return makeAssetSizesObject.then(function(assets) {
return sendDeployDataFunction(assets, options, keen);
});
}
});
return new DeployPlugin();
}
};