-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
242 lines (224 loc) · 7.52 KB
/
index.ts
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
import * as path from "path";
import chalk from "chalk";
import klaw from "klaw";
import { filterFiles } from "./lib/pipes/filterFiles";
import { PostcssPipeResult } from "./lib/pipes/processPostcss";
import { getErrorCollector } from "./lib/util/error-collector";
import { compileFile, compilerPipe } from "./lib/compilerPipe";
import { LegacyOptions as SassOptions } from "sass";
import { writeFile } from "./lib/util/fs-util";
type OutputStyle = "compressed" | "expanded";
/**
* The paramets to pass in from the command
*/
export interface Params {
context: string;
dir: string[];
outputStyle: OutputStyle;
sourceMap: boolean;
watch: boolean;
debug: boolean;
}
/**
* The configuration for a single entry
*/
export interface EntryConfig {
srcRelative: string;
outRelative: string;
src: string;
out: string;
postsassConfig: {
postcss: any;
sass: SassOptions<'sync'>;
};
}
// A map of which file is a dependency of another file
const relationships: { [key: string]: string[] } = {};
const debugDir = path.resolve(process.cwd(), "_postsassDebug");
/**
* Compile scss files
* @param params
*/
export async function compile(params: Params) {
const { context, outputStyle, sourceMap, dir, watch } = params;
console.info(chalk.blue.bold("Using output style"), chalk.green(outputStyle));
console.info(chalk.blue.bold("Source Map"), chalk.green(sourceMap));
let postsassConfig = await loadConfig(params);
// create a configuration for each entry directory
const entries: EntryConfig[] = dir.map((d) => {
// split at semicolon
const split = d.split(":", 2);
const src = split[0];
// no output path specified = use src dir for output
const out = split[1] ?? split[0];
// create a configuration for the entry dir
const conf: EntryConfig = {
srcRelative: src,
outRelative: out,
src: path.resolve(context, src),
out: path.resolve(context, out),
postsassConfig,
};
return conf;
});
// create promises for each entry
const cbs = entries.map(
async (entry) =>
new Promise((resolve, reject) => {
const { srcRelative, outRelative, src } = entry;
console.info(
chalk.blue.bold("Source dir"),
chalk.magenta(srcRelative),
chalk.yellow(" => "),
chalk.blue.bold("Output dir"),
chalk.magenta(outRelative)
);
// Use klaw to recursively walk the directories
klaw(src + "/")
.pipe(filterFiles()) // ony use scss files
.pipe(compilerPipe(entry)) // make a few transformations with postcss
.on("data", dataListener(params, entry)) // need to process data to trigger the "end" event to resolve the promise
.on("end", resolve); // resolve promise
})
);
try {
// Wait until all files are processed
await Promise.all(cbs);
} catch(e : any) {
console.error(chalk.red(chalk.bold("Error occured:"), e.message));
console.error(e.stack);
process.exitCode = 1;
return;
}
// Check if any errors occured while compiling the sass files
const errors = getErrorCollector();
if (errors.hasErrors()) {
console.error(chalk.bold.red("Erorrs occured while compiling:"));
errors.forEach((e) => console.error(e.message));
process.exitCode = 2;
return;
}
// Write debug info if enabled
if (params.debug) {
await writeFile(path.resolve(debugDir, "relationships.json"), JSON.stringify(relationships, null, 2));
}
console.info(chalk.bold.green("All files compiled successfully!"));
// Are we done yet?
if (watch) {
await enableWatchMode(params, entries);
console.info("Graceful Shutdown");
process.exitCode = 0;
return;
}
}
/**
* Show a bit of info what should
*/
function dataListener(params: Params, entry: EntryConfig) {
return async (d: PostcssPipeResult) => {
console.info(
chalk.bold(
chalk.blue(d.from.replace(entry.src, entry.srcRelative)),
chalk.yellow("=>"),
chalk.blue(d.to.replace(entry.out, entry.outRelative))
)
);
// write debug info: which parts are a part of the file
// FIXME: Use a better ouput filename to avoid collisions
if (params.debug) {
await writeFile(
path.resolve(debugDir, "relations", path.basename(d.from) + ".json"),
JSON.stringify(d.sassResult.stats.includedFiles, null, 2)
);
}
if (params.watch || params.debug) {
relationTracker(d);
}
};
}
// track the dependency relations between files
// when a file is changed, all its dependants should be updated
// includedFiles includes the entry file as well
function relationTracker(d: PostcssPipeResult) {
d.sassResult.stats.includedFiles.forEach((f: string) => {
if (!(f in relationships)) {
relationships[f] = [];
relationships[f].push(d.from);
} else if (relationships[f].indexOf(d.from) === -1) {
relationships[f].push(d.from);
}
});
}
async function enableWatchMode(params: Params, entries: EntryConfig[]) {
try {
// Chokidar is a nice tool for watching directories for changes.
const chokidar = await import("chokidar");
console.info(chalk.bold.cyan("Starting Watch Mode"));
// Start watcher for every source set
const promises = entries.map((entry) => {
return new Promise<void>((resolve, reject) => {
// create a pattern to watch all scss files
const watchPattern = path.resolve(params.context, entry.srcRelative, "**/*.scss");
const watcher = chokidar.watch(watchPattern);
watcher
// Show a little info when the watcher is ready to roll
.on("ready", () =>
console.info(chalk.bold(chalk.blue("Watching changes for"), chalk.magenta(entry.srcRelative)))
)
// Show a little info when a file has been removed
.on("unlink", (path) => console.info(chalk.red(`File ${path} has been removed`)))
// register the change handler
.on("change", changeHandler(entry));
process.on("SIGINT", () => {
watcher.close().then(() => resolve());
});
});
});
return Promise.all(promises);
} catch(e : any) {
console.error(e);
return;
}
}
// The change listener for watchmode
function changeHandler(entry: EntryConfig) {
return async (path: string) => {
console.info(chalk.bold(chalk.blue("File changed:", chalk.magenta(path))));
// Recompile all files that depend on the changed file
if (path in relationships) {
for (const p of Object.values(relationships[path])) {
try {
const write = await compileFile(p, entry);
console.info(
chalk.bold(chalk.blue("Updated file", chalk.magenta(write.from.replace(entry.src, entry.srcRelative))))
);
} catch(e : any) {
console.error(chalk.bold.red("Error when compiling", p), e.message);
}
}
}
};
}
async function loadConfig(params: Params): Promise<EntryConfig["postsassConfig"]> {
const sassCliConfig = { outputStyle: params.outputStyle, sourceMap: params.sourceMap } as SassOptions<'sync'>;
let fileConfig;
const builder = {
postcss: null,
sass: sassCliConfig,
};
try {
fileConfig = await import(path.resolve(process.cwd(), "postsass.config.js"));
if (typeof fileConfig.postcss === "object") {
builder.postcss = fileConfig.postcss;
}
if (typeof fileConfig.sass === "object") {
builder.sass = { ...fileConfig.sass, ...sassCliConfig };
}
} catch(e : any) {
console.warn("No postsass config file found");
if (!e.code) {
console.error(e);
}
}
return builder;
}