forked from rtfeldman/elm-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
251 lines (212 loc) · 6.92 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
var fs = require("fs");
var tmp = require("tmp");
var path = require("path");
var compile = require("node-elm-compiler").compile;
var jsEmitterFilename = "emitter.js";
var KNOWN_MODULES =
[
"Native",
"fullscreen",
"embed",
"worker",
"Basics",
"Maybe",
"List",
"Array",
"Char",
"Color",
"Transform2D",
"Text",
"Graphics",
"Debug",
"Result",
"Task",
"Signal",
"String",
"Dict",
"Json",
"Regex",
"VirtualDom",
"Html",
"Css"
];
// elmModuleName is optional, and is by default inferred based on the filename.
module.exports = function(projectDir, stylesheetsPath, outputDir, stylesheetsModule, stylesheetsPort, pathToMake) {
var originalWorkingDir = process.cwd();
process.chdir(projectDir);
return createTmpDir()
.then(function (tmpDirPath) {
return generateCssFiles(
stylesheetsPath,
path.join(tmpDirPath, jsEmitterFilename),
outputDir,
stylesheetsModule || "Stylesheets",
stylesheetsPort || "files",
pathToMake
);
})
.then(function(result) {
process.chdir(originalWorkingDir);
return result;
})
.catch(function(err) {
process.chdir(originalWorkingDir);
throw Error(err);
});
}
function createTmpDir() {
return new Promise(function (resolve, reject) {
tmp.dir(function (err, tmpDirPath) {
if (err) {
reject(err);
} else {
resolve(tmpDirPath);
}
});
});
}
function generateCssFiles(stylesheetsPath, emitterDest, outputDir, stylesheetsModule, stylesheetsPort, pathToMake) {
return emit(stylesheetsPath, emitterDest, stylesheetsModule, stylesheetsPort, pathToMake)
.then(writeResults(outputDir));
}
function emit(src, dest, stylesheetsModule, stylesheetsPort, pathToMake) {
// Compile the temporary file.
return compileEmitter(src, {output: dest, yes: true, pathToMake: pathToMake})
.then(extractCssResults(dest, stylesheetsModule, stylesheetsPort));
}
function suggestModulesNames(Elm){
return Object.keys(Elm).filter(function(key){
return KNOWN_MODULES.indexOf(key) === -1;
})
}
function missingEntryModuleMessage(stylesheetsModule, Elm){
var errorMessage = "I couldn't find the entry module " + stylesheetsModule + ".\n";
var suggestions = suggestModulesNames(Elm);
if (suggestions.length > 1){
errorMessage += "\nMaybe you meant one of these: " + suggestions.join(",");
} else if (suggestions.length === 1){
errorMessage += "\nMaybe you meant: " + suggestions;
}
errorMessage += "\nYou can pass me a different module to use with --module=<moduleName>";
return errorMessage;
}
function noPortsMessage(stylesheetsModule, stylesheetsPort){
var errorMessage = "The module " + stylesheetsModule + " doesn't expose any ports!\n";
errorMessage += "\nI was looking for a port called `" + stylesheetsPort + "` but couldn't find it!";
errorMessage += "\n\nTry adding something like";
errorMessage += `
port ${stylesheetsPort} : CssFileStructure
port ${stylesheetsPort} =
toFileStructure
[]
to ${stylesheetsModule}!
`;
return errorMessage.trim();
}
function noMatchingPortMessage(stylesheetsModule, stylesheetsPort, ports){
var errorMessage = `The module ${stylesheetsModule} has no port called ${stylesheetsPort}.\n`;
errorMessage += "\nI was looking for a port called `" + stylesheetsPort + "` but couldn't find it!";
var portKeys = Object.keys(ports);
if (portKeys.length === 1){
errorMessage += "\nHowever, I did find the port: " + portKeys[0];
errorMessage += "\nMaybe you meant that instead?";
} else {
errorMessage += "\nHowever, I did find the ports : " + Object.keys(ports).join(",");
}
errorMessage += "\n\nYou can specify which port to use by doing";
errorMessage += "\nelm-css -p <port-name>";
return errorMessage.trim();
}
/*
This is a poor man's type error. To get better error messages, might be worth
considered moving to how elm-static-site does it instead, and use Elm's compiler
for type errors!
*/
function portTypeErrorMessage(stylesheetsModule, stylesheetsPort, portValue){
var errorMessage = `
The type of the port ${stylesheetsPort} was not what I wanted!
I was expecting a List CssFileStructure but got ${typeof portValue}!
`;
return errorMessage.trim();
}
function extractCssResults(dest, stylesheetsModuleName, stylesheetsPort) {
return function () {
return new Promise(function (resolve, reject) {
var Elm = require(dest);
/*
If you have a nested stylesheet Elm module like "My.Nested.Stylesheet"
the resulting Elm object will be like that:
Elm = {
My: {
Nested: {
Stylesheet: { worker: function () {...} }
}
}
}
So we need to split the module name on each dot and iterate
over the nested objects to reach the targeted one,
starting with the Elm object itself
*/
var stylesheetsModule = Elm;
var parts = stylesheetsModuleName.split('.');
parts.forEach(function (part) {
if (!stylesheetsModule || !stylesheetsModule[part]){
return reject(missingEntryModuleMessage(stylesheetsModuleName, Elm));
}
stylesheetsModule = stylesheetsModule[part];
})
var worker = stylesheetsModule.worker();
if (Object.keys(worker.ports).length === 0){
return reject(noPortsMessage(stylesheetsModuleName, stylesheetsPort));
}
if (!(stylesheetsPort in worker.ports)){
return reject(noMatchingPortMessage(stylesheetsModuleName, stylesheetsPort, worker.ports));
}
worker.ports[stylesheetsPort].subscribe(function (stylesheets) {
if (!Array.isArray(stylesheets)){
return reject(portTypeErrorMessage(stylesheetsModuleName, stylesheetsPort, stylesheets));
}
var failures = stylesheets.filter(function(result) {
return !result.success;
});
return failures.length > 0
? reject(reportFailures(failures))
: resolve(stylesheets);
});
});
};
}
function writeResults(outputDir) {
return function(results) {
return Promise.all(
results.map(writeResult(outputDir))
);
};
}
function writeResult(outputDir) {
return function (result) {
return new Promise(function(resolve, reject) {
fs.writeFile(path.join(outputDir, result.filename), result.content + "\n", function(err, file) {
return err ? reject(err) : resolve(result);
});
});
}
}
function reportFailures(failures) {
return "The following errors occurred during compilation:\n\n" +
failures.map(function(result) {
return result.filename + ": " + result.content;
}).join("\n\n");
}
function compileEmitter(src, options) {
return new Promise(function(resolve, reject) {
compile(src, options)
.on("close", function(exitCode) {
if (exitCode === 0) {
resolve();
} else {
reject("Errored with exit code " + exitCode);
}
});
});
}