-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathdevconfig.ts
More file actions
190 lines (173 loc) · 5.15 KB
/
devconfig.ts
File metadata and controls
190 lines (173 loc) · 5.15 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
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
/*
* devconfig.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { error, info } from "../deno_ral/log.ts";
import { join } from "../deno_ral/path.ts";
import { ensureDirSync, existsSync } from "../deno_ral/fs.ts";
import { md5HashSync } from "./hash.ts";
import { quartoConfig } from "./quarto.ts";
import { normalizePath } from "./path.ts";
import { isWindows } from "../deno_ral/platform.ts";
const kDevConfig = "dev-config";
export interface DevConfig {
deno: string;
deno_dom: string;
pandoc: string;
dartsass: string;
esbuild: string;
typst: string;
script: string;
importMap: string; // import map for most imports, which we need on dev version
bundleImportMap: string; // import map for dynamic imports which we need on bundled versions
}
export function createDevConfig(
deno: string,
deno_dom: string,
pandoc: string,
dartsass: string,
esbuild: string,
typst: string,
scriptDir: string,
): DevConfig {
const scriptPath = join(scriptDir, "quarto" + (isWindows ? ".cmd" : ""));
const srcDir = quartoConfig.srcPath();
return {
deno,
deno_dom,
pandoc,
dartsass,
esbuild,
typst,
script: md5HashSync(Deno.readTextFileSync(scriptPath)),
importMap: md5HashSync(
Deno.readTextFileSync(
join(srcDir, "import_map.json"),
),
),
bundleImportMap: md5HashSync(
Deno.readTextFileSync(
join(srcDir, "resources/vendor/import_map.json"),
),
),
};
}
export function writeDevConfig(config: DevConfig, binPath: string) {
const configPath = join(binPath, "..", "config");
ensureDirSync(configPath);
Deno.writeTextFileSync(
join(configPath, kDevConfig),
JSON.stringify(config, undefined, 2),
);
}
export function readInstalledDevConfig(): DevConfig | null {
const binPath = quartoConfig.binPath();
const configPath = join(binPath, "..", "config", kDevConfig);
if (existsSync(configPath)) {
return JSON.parse(Deno.readTextFileSync(configPath)) as DevConfig;
} else {
return null;
}
}
export function readSourceDevConfig(): DevConfig {
const rootDir = Deno.env.get("QUARTO_ROOT") ||
join(quartoConfig.sharePath(), "../..");
const configurationScript = join(
rootDir,
"configuration",
);
const configurationScriptSrc = Deno.readTextFileSync(configurationScript);
const readConfig = (key: string) => {
const regex = new RegExp(`${key}=(\\S+)`);
const match = configurationScriptSrc.match(regex);
if (match) {
return match[1];
} else {
return "";
}
};
return createDevConfig(
readConfig("DENO"),
readConfig("DENO_DOM"),
readConfig("PANDOC"),
readConfig("DARTSASS"),
readConfig("ESBUILD"),
readConfig("TYPST"),
quartoConfig.binPath(),
);
}
export function devConfigsEqual(a: DevConfig, b: DevConfig) {
return a.deno === b.deno &&
a.deno_dom === b.deno_dom &&
a.pandoc === b.pandoc &&
a.dartsass == b.dartsass &&
a.esbuild == b.esbuild &&
a.typst === b.typst &&
a.script == b.script &&
a.importMap === b.importMap &&
a.bundleImportMap === b.bundleImportMap;
}
export async function reconfigureQuarto(
installed: DevConfig | null,
source: DevConfig,
) {
// Running configuration from within quarto does not work on windows
// because deno.exe is running and this lock and prevent reinstallation
// So we fail and print comment to run
if (isWindows) {
error(
`Quarto requires reconfiguration to ${
reconfigureReason(installed, source)
}. Please run \`./configure.cmd\` command.\n`,
);
return;
}
// Leaving Windows here if we find a way to reconfigure
// to not forget how to call the script
const configureScript = isWindows
? ["cmd", "/c", ".\\configure.cmd"]
: ["./configure.sh"];
const quartoDir = normalizePath(
join(quartoConfig.sharePath(), "..", ".."),
);
const process = Deno.run({
cmd: configureScript,
cwd: quartoDir,
});
await process.status();
info("");
error(
`Quarto required reconfiguration to ${
reconfigureReason(installed, source)
}. Please try command again.\n`,
);
}
function reconfigureReason(
installed: DevConfig | null,
source: DevConfig,
) {
const versionMessage = (dep: string, version: string) => {
return `install ${dep} version ${version}`;
};
if (installed === null || installed.deno !== source.deno) {
return versionMessage("Deno", source.deno);
} else if (installed.deno_dom !== source.deno_dom) {
return versionMessage("Deno Dom", source.deno_dom);
} else if (installed.pandoc !== source.pandoc) {
return versionMessage("Pandoc", source.pandoc);
} else if (installed.dartsass !== source.dartsass) {
return versionMessage("Dart Sass", source.dartsass);
} else if (installed.esbuild !== source.esbuild) {
return versionMessage("Esbuild", source.esbuild);
} else if (installed.typst !== source.typst) {
return versionMessage("Typst", source.typst);
} else if (installed.script !== source.script) {
return "update Quarto wrapper script";
} else if (
installed.importMap !== source.importMap ||
installed.bundleImportMap !== source.importMap
) {
return "update dev import map";
}
}