forked from bustle/ember-cli-amp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
169 lines (139 loc) · 4.5 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
/*jshint node:true*/
'use strict';
var fs = require('fs');
var path = require('path');
var CSSInjector = require('./lib/css_injector');
var CSSValidator = require('./lib/css_validator');
var VersionChecker = require('ember-cli-version-checker');
var TEMPLATE_PATH = path.join(__dirname, './ext/amp-index-template.html');
var PLACEHOLDER = '<!-- EMBER_CLI_AMP_CSS_PLACEHOLDER -->';
var MINIMUM_FASTBOOT_VERSION = '1.0.0-beta.8';
var MINIMUM_EMBER_HEAD_VERSION = '0.1.0';
module.exports = {
name: 'ember-cli-amp',
after: [
'ember-cli-sass',
'ember-cli-fastboot'
],
init() {
this._super.apply(this, arguments);
this._checkDeps();
},
/**
* addon hook
* Runs when the addon is initially included by ember-cli
* @see https://ember-cli.com/api/classes/Addon.html#method_included
*/
included(app) {
var options = app.options.amp || {};
this.cssPath = options.css;
this.ampIndexPath = options.index;
this.buildCount = 0;
this.alwaysRebuildIndex = options.alwaysRebuildIndex === undefined ? false : options.alwaysRebuildIndex;
if (!this.cssPath) {
throw new Error('You must specify amp.css option in ember-cli-build.js');
}
if (!this.ampIndexPath) {
throw new Error('You must specify amp.index option in ember-cli-build.js');
}
},
/**
* addon hook
* Runs after ember-cli builds the app, but before syncing the built output
* to the outputPath. Changes made to the results directory will be synced to
* the built output.
* @see https://ember-cli.com/api/classes/Addon.html#method_postBuild
*/
postBuild(results) {
if (this._shouldRebuildIndex()) {
return this._rebuildIndex(results).catch(err => {
this.ui.writeError('[amp-addon] Error building amp index: ' + err);
});
}
},
/**
* inject the CSS into the amp index template and write the index to the
* results directory
*/
_injectCSS(css) {
var template = this._readFile(TEMPLATE_PATH);
var outputPath = this._pathFor(this.ampIndexPath);
var cssInjector = new CSSInjector({
css: css,
template: template,
placeholder: PLACEHOLDER
});
cssInjector.write(outputPath);
},
_readFile(absolutePath) {
try {
return fs.readFileSync(absolutePath, 'utf8');
} catch(e) {
return '';
}
},
_readBuiltFile(relativePath) {
return this._readFile(this._pathFor(relativePath));
},
_pathFor(relativePath) {
return path.join(this.resultsDir, relativePath);
},
_shouldRebuildIndex() {
if (this.alwaysRebuildIndex) {
return true;
} else {
return this.buildCount === 0;
}
},
_rebuildIndex(results) {
this.buildCount++;
this.resultsDir = results.directory;
var css = this._readBuiltFile(this.cssPath);
return this._validateCSS(css)
.then(() => this._injectCSS(css));
},
_validateCSS(css) {
var validator = new CSSValidator();
return validator.validate(css)
.then(results => this._logCSSResults(results));
},
_logCSSResults(results) {
var ui = this.ui;
if (results.errors.length) {
ui.writeError('AMP CSS failed validation: ');
results.errors.forEach(error => {
var code = error.code,
severity = error.severity,
detail = error.detail,
extraDetail = error.extraDetail,
specUrl = error.specUrl;
var msg = [];
if (detail) {
msg.push('Details: ' + detail);
if (extraDetail) {
msg.push('Other Details: ' + extraDetail);
}
} else if (extraDetail) {
msg.push('Details: ' + extraDetail);
}
msg.push('Spec URL: ' + specUrl);
msg.push('Code: ' + code);
msg.push('Severity: ' + severity);
ui.writeError(' ' + msg.join('. '));
});
} else {
ui.writeInfoLine('AMP CSS validated');
}
},
_checkDeps() {
var checker = new VersionChecker(this);
var fastboot = checker.for('ember-cli-fastboot', 'npm');
var head = checker.for('ember-cli-head', 'npm');
if (!fastboot.satisfies('> ' + MINIMUM_FASTBOOT_VERSION)) {
this.ui.writeWarnLine('[ember-cli-amp]: ember-cli-fastboot should be > ' + MINIMUM_FASTBOOT_VERSION + '. Your version ('+fastboot.version+') may not work properly.');
}
if (!head.satisfies('>= ' + MINIMUM_EMBER_HEAD_VERSION)) {
this.ui.writeWarnLine('[ember-cli-amp]: ember-cli-head should be >= ' + MINIMUM_EMBER_HEAD_VERSION + '. Your version: ' + head.version);
}
}
};