Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/dart-sass.ts
3557 views
1
/*
2
* dart-sass.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
import { join } from "../deno_ral/path.ts";
7
8
import { architectureToolsPath } from "./resources.ts";
9
import { execProcess } from "./process.ts";
10
import { TempContext } from "./temp.ts";
11
import { lines } from "./text.ts";
12
import { debug, info } from "../deno_ral/log.ts";
13
import { existsSync } from "../deno_ral/fs.ts";
14
import { warnOnce } from "./log.ts";
15
import { isWindows } from "../deno_ral/platform.ts";
16
17
export function dartSassInstallDir() {
18
return architectureToolsPath("dart-sass");
19
}
20
21
export async function dartSassVersion() {
22
return await dartCommand(["--version"]);
23
}
24
25
export async function dartCompile(
26
input: string,
27
outputFilePath: string,
28
temp: TempContext,
29
loadPaths?: string[],
30
compressed?: boolean,
31
): Promise<string | undefined> {
32
// Write the scss to a file
33
// We were previously passing it via stdin, but that can be overflowed
34
const inputFilePath = temp.createFile({ suffix: ".scss" });
35
36
// Write the css itself to a file
37
Deno.writeTextFileSync(inputFilePath, input);
38
const args = [
39
inputFilePath,
40
outputFilePath,
41
"--style",
42
compressed ? "compressed" : "expanded",
43
"--quiet", // Remove this flag to get depedency warnings from SASS
44
];
45
46
if (loadPaths) {
47
loadPaths.forEach((loadPath) => {
48
args.push(`--load-path=${loadPath}`);
49
});
50
}
51
52
await dartCommand(args);
53
return outputFilePath;
54
}
55
56
export async function dartCommand(args: string[]) {
57
const resolvePath = () => {
58
const dartOverrideCmd = Deno.env.get("QUARTO_DART_SASS");
59
if (dartOverrideCmd) {
60
if (!existsSync(dartOverrideCmd)) {
61
warnOnce(
62
`Specified QUARTO_DART_SASS does not exist, using built in dart sass.`,
63
);
64
} else {
65
return dartOverrideCmd;
66
}
67
}
68
69
const command = isWindows ? "sass.bat" : "sass";
70
return architectureToolsPath(join("dart-sass", command));
71
};
72
const sass = resolvePath();
73
74
const cmd = sass;
75
// Run the sass compiler
76
const result = await execProcess(
77
{
78
cmd,
79
args,
80
stdout: "piped",
81
stderr: "piped",
82
},
83
);
84
85
if (result.success) {
86
if (result.stderr) {
87
info(result.stderr);
88
}
89
return result.stdout;
90
} else {
91
debug(`[DART path] : ${sass}`);
92
debug(`[DART args] : ${args.join(" ")}`);
93
debug(`[DART stdout] : ${result.stdout}`);
94
debug(`[DART stderr] : ${result.stderr}`);
95
96
const errLines = lines(result.stderr || "");
97
// truncate the last 2 lines (they include a pointer to the temp file containing
98
// all of the concatenated sass, which is more or less incomprehensible for users.
99
const errMsg = errLines.slice(0, errLines.length - 2).join("\n");
100
throw new Error("Theme file compilation failed:\n\n" + errMsg);
101
}
102
}
103
104