-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathconfigure.ts
More file actions
199 lines (178 loc) · 6.05 KB
/
configure.ts
File metadata and controls
199 lines (178 loc) · 6.05 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
191
192
193
194
195
196
197
198
199
/*
* dependencies.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { dirname, join, SEP } from "../../../src/deno_ral/path.ts";
import { existsSync, ensureDirSync } from "../../../src/deno_ral/fs.ts";
import { info, warning } from "../../../src/deno_ral/log.ts";
import { expandPath } from "../../../src/core/path.ts";
import {
createDevConfig,
writeDevConfig,
} from "../../../src/core/devconfig.ts";
import { Configuration } from "./config.ts";
import {
configureDependency,
kDependencies,
} from "./dependencies/dependencies.ts";
import { suggestUserBinPaths, which } from "../../../src/core/path.ts";
import { buildQuartoPreviewJs } from "./previewjs.ts";
import { isWindows } from "../../../src/deno_ral/platform.ts";
export async function configure(
config: Configuration,
) {
// npm is required later for building quarto-preview.js (used by `quarto
// preview` live-reload). Check upfront to fail fast before downloading
// hundreds of MB of dependencies when npm is missing.
if ((await which("npm")) === undefined) {
throw new Error(
"npm not found on PATH. Please install Node.js (which provides npm) before running configure.",
);
}
// Download dependencies
for (const dependency of kDependencies) {
const targetDir = join(
config.directoryInfo.bin,
"tools",
);
await configureDependency(dependency, targetDir, config);
}
info("Building quarto-preview.js...");
const result = buildQuartoPreviewJs(config.directoryInfo.src);
if (!result.success) {
throw new Error();
}
info("Build completed.");
// Move the quarto script into place
info("Placing Quarto script");
copyQuartoScript(config, config.directoryInfo.bin);
info("Creating architecture specific Pandoc link");
const vendor = Deno.env.get("QUARTO_VENDOR_BINARIES");
if (vendor === undefined || vendor === "true") {
// Quarto tools may look right in the bin/tools directory for Pandoc
// so make a symlink that points to the architecture specific version.
// Note that if we are being instructed not to vendor binaries,
// Pandoc won't be present in the architecture specific directory, so
// just skip this step.
copyPandocScript(config, join(config.directoryInfo.bin, "tools"));
}
// record dev config. These are versions as defined in the root configuration file.
const devConfig = createDevConfig(
Deno.env.get("DENO") || "",
Deno.env.get("DENO_DOM") || "",
Deno.env.get("PANDOC") || "",
Deno.env.get("DARTSASS") || "",
Deno.env.get("ESBUILD") || "",
Deno.env.get("TYPST") || "",
config.directoryInfo.bin,
);
writeDevConfig(devConfig, config.directoryInfo.bin);
info("Wrote dev config to bin directory");
if (
config.os !== "windows" &&
Deno.env.get("QUARTO_NO_SYMLINK") === undefined
) {
info("Creating Quarto Symlink");
// Set up a symlink (if appropriate)
const possibleBinPaths = suggestUserBinPaths();
const symlinksFiltered = possibleBinPaths.map((path) =>
join(path, "quarto")
);
info(`Found ${symlinksFiltered.length} paths to try.`);
if (symlinksFiltered.length > 0) {
for (let i = 0; i < symlinksFiltered.length; i++) {
info(`> Trying ${symlinksFiltered[i]}`);
const symlinkPath = expandPath(symlinksFiltered[i]);
// Remove existing symlink
try {
if (existsSync(symlinkPath)) {
Deno.removeSync(symlinkPath);
}
} catch (error) {
info(error);
warning(
"\n> Failed to remove existing symlink.\n> Did you previously install with sudo? Run 'which quarto' to test which version will be used.",
);
}
// Create new symlink
try {
ensureDirSync(dirname(symlinkPath) + SEP);
Deno.symlinkSync(
join(config.directoryInfo.bin, "quarto"),
symlinkPath,
);
info(`> Symlink created at ${symlinkPath}`);
info("> Success");
// it worked, just move on
break;
} catch (_error) {
info(`> Didn't create symlink at ${symlinkPath}`);
if (i === symlinksFiltered.length - 1) {
warning(
`\n> Please ensure that ${
join(config.directoryInfo.bin, "quarto")
} is in your path.`,
);
}
}
}
} else {
// Just warn the user and create a symlink in our last resort
warning(
`\n> Please ensure that ${
join(config.directoryInfo.bin, "quarto")
} is in your path.`,
);
}
}
}
export function copyQuartoScript(config: Configuration, targetDir: string) {
// Move the quarto script into place
if (config.os === "windows") {
Deno.copyFileSync(
join(config.directoryInfo.pkg, "scripts", "windows", "quarto.cmd"),
join(targetDir, "quarto.cmd"),
);
} else {
const out = join(targetDir, "quarto");
Deno.copyFileSync(
join(config.directoryInfo.pkg, "scripts", "common", "quarto"),
out,
);
Deno.chmodSync(out, 0o755);
}
}
export function copyPandocScript(config: Configuration, targetDir: string) {
const linkTarget = join(config.arch, "pandoc");
const pandocFile = join(targetDir, "pandoc");
if (existsSync(pandocFile)) {
info("> removing existing pandoc link");
Deno.removeSync(pandocFile);
}
if (!isWindows) {
info("> creating pandoc symlink");
Deno.run({
cwd: targetDir,
cmd: ["ln", "-s", linkTarget, "pandoc"]
});
}
}
export function copyPandocAliasScript(config: Configuration, toolsDir: string) {
// Move the quarto script into place
if (config.os === "darwin") {
const out = join(toolsDir, "pandoc");
Deno.copyFileSync(
join(config.directoryInfo.pkg, "scripts", "macos", "pandoc"),
out,
);
Deno.chmodSync(out, 0o755);
} else if (config.os === "linux") {
const out = join(toolsDir, "pandoc");
Deno.copyFileSync(
join(config.directoryInfo.pkg, "scripts", "linux", "pandoc"),
out,
);
Deno.chmodSync(out, 0o755);
}
}