-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathdart-sass.ts
More file actions
97 lines (85 loc) · 2.67 KB
/
dart-sass.ts
File metadata and controls
97 lines (85 loc) · 2.67 KB
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
/*
* dart-sass.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { join } from "../deno_ral/path.ts";
import { architectureToolsPath } from "./resources.ts";
import { execProcess } from "./process.ts";
import { TempContext } from "./temp.ts";
import { lines } from "./text.ts";
import { debug, info } from "../deno_ral/log.ts";
import { existsSync } from "fs/mod.ts";
import { warnOnce } from "./log.ts";
export function dartSassInstallDir() {
return architectureToolsPath("dart-sass");
}
export async function dartSassVersion() {
return await dartCommand(["--version"]);
}
export async function dartCompile(
input: string,
outputFilePath: string,
temp: TempContext,
loadPaths?: string[],
compressed?: boolean,
): Promise<string | undefined> {
// Write the scss to a file
// We were previously passing it via stdin, but that can be overflowed
const inputFilePath = temp.createFile({ suffix: ".scss" });
// Write the css itself to a file
Deno.writeTextFileSync(inputFilePath, input);
const args = [
inputFilePath,
outputFilePath,
"--style",
compressed ? "compressed" : "expanded",
"--quiet", // Remove this flag to get depedency warnings from SASS
];
if (loadPaths) {
loadPaths.forEach((loadPath) => {
args.push(`--load-path=${loadPath}`);
});
}
await dartCommand(args);
return outputFilePath;
}
export async function dartCommand(args: string[]) {
const resolvePath = () => {
const dartOverrideCmd = Deno.env.get("QUARTO_DART_SASS");
if (dartOverrideCmd) {
if (!existsSync(dartOverrideCmd)) {
warnOnce(
`Specified QUARTO_DART_SASS does not exist, using built in dart sass.`,
);
} else {
return dartOverrideCmd;
}
}
const command = Deno.build.os === "windows" ? "sass.bat" : "sass";
return architectureToolsPath(join("dart-sass", command));
};
const sass = resolvePath();
// Run the sass compiler
const result = await execProcess(sass, {
args,
stdout: "piped",
stderr: "piped",
});
if (result.success) {
if (result.stderr) {
info(result.stderr);
}
return result.stdout;
} else {
debug(`[DART path] : ${sass}`);
debug(`[DART args] : ${args.join(" ")}`);
debug(`[DART stdout] : ${result.stdout}`);
debug(`[DART stderr] : ${result.stderr}`);
const errLines = lines(result.stderr || "");
// truncate the last 2 lines (they include a pointer to the temp file containing
// all of the concatenated sass, which is more or less incomprehensible for users.
const errMsg = errLines.slice(0, errLines.length - 2).join("\n");
throw new Error("Theme file compilation failed:\n\n" + errMsg);
}
}