forked from rtfeldman/elm-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (167 loc) · 5.16 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
//@flow
const _ = require("lodash"),
path = require("path"),
glob = require("glob"),
mkdirp = require("mkdirp"),
findExposedValues = require("./js/find-exposed-values").findExposedValues,
writeGeneratedElmPackage = require("./js/generate-elm-package"),
writeMain = require("./js/generate-main").writeMain,
writeFile = require("./js/generate-class-modules").writeFile,
findElmFiles = require("./js/find-elm-files"),
compileAll = require("./js/compile-all"),
fs = require("fs-extra"),
compile = require("node-elm-compiler").compile,
extractCssResults = require("./js/extract-css-results.js"),
hackMain = require("./js/hack-main.js");
const binaryExtension = process.platform === "win32" ? ".exe" : "";
const readElmiPath =
path.join(__dirname, "bin", "elm-interface-to-json") + binaryExtension;
const jsEmitterFilename = "emitter.js";
module.exports = function(
projectDir /*: string*/,
outputDir /*: string */,
pathToMake /*: ?string */
) {
const cssSourceDir = path.join(projectDir, "css");
const cssElmPackageJson = path.join(cssSourceDir, "elm-package.json");
if (!fs.existsSync(cssElmPackageJson)) {
mkdirp.sync(cssSourceDir);
// TODO do an init here
}
const elmFilePaths = findElmFiles(cssSourceDir);
const generatedDir = path.join(
projectDir,
"elm-stuff",
"generated-code",
"rtfeldman",
"elm-css"
);
// Symlink our existing elm-stuff into the generated code,
// to avoid re-downloading and recompiling things we already
// just downloaded and compiled.
var generatedElmStuff = path.join(generatedDir, "elm-stuff");
mkdirp.sync(generatedDir);
if (!fs.existsSync(generatedElmStuff)) {
try {
fs.symlinkSync(
path.join(cssSourceDir, "elm-stuff"),
generatedElmStuff,
"junction" // Only affects Windows, but necessary for this to work there. See https://github.com/gulpjs/vinyl-fs/issues/210
);
} catch (err) {
// This will blow up on macOS because !fs.existsSync(generatedElmStuff)
// fails on macOS for symlinks. Ignore it; if it legit fails, that's
// fine; this doesn't *need* to be symlinked. It's just a performance optimization.
}
}
const generatedSrc = path.join(generatedDir, "src");
const mainFilename = path.join(generatedSrc, "Main.elm");
const makeGeneratedSrcDir = new Promise(function(resolve, reject) {
mkdirp(generatedSrc, function(error) {
if (error) reject(error);
resolve();
});
});
return Promise.all([
writeGeneratedElmPackage(generatedDir, generatedSrc, cssSourceDir),
makeGeneratedSrcDir,
compileAll(pathToMake, cssSourceDir, elmFilePaths)
]).then(function(promiseOutputs) {
const repository /*: string */ = promiseOutputs[0];
return findExposedValues(
[
"DEPRECATED.Css.File.UniqueClass",
"DEPRECATED.Css.File.UniqueSvgClass",
"Css.Global.Snippet"
],
readElmiPath,
generatedDir,
elmFilePaths,
[cssSourceDir],
true
).then(function(modules) {
return Promise.all(
[writeMain(mainFilename, modules)].concat(
modules.map(function(modul) {
return writeFile(path.join(generatedDir, "styles"), modul);
})
)
).then(function() {
return emit(
mainFilename,
repository,
path.join(generatedDir, jsEmitterFilename),
generatedDir,
pathToMake
).then(writeResults(outputDir));
});
});
});
};
function emit(
src /*: string */,
repository /*: string */,
dest /*: string */,
cwd /*: string */,
pathToMake /*: ?string */
) {
// Compile the js file.
return compileEmitter(src, {
output: dest,
yes: true,
cwd: cwd,
pathToMake: pathToMake
})
.then(function() {
return hackMain(repository, dest);
})
.then(function() {
return extractCssResults(dest);
});
}
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) {
const filename = path.join(outputDir, result.filename);
// It's important to call path.dirname explicitly,
// because result.filename can have directories in it!
const directory = path.dirname(filename);
mkdirp(directory, function(dirError) {
if (dirError) return reject(dirError);
fs.writeFile(filename, result.content + "\n", function(
fileError,
file
) {
if (fileError) return reject(fileError);
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);
}
});
});
}