-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathinstall.ts
More file actions
621 lines (561 loc) · 17.6 KB
/
install.ts
File metadata and controls
621 lines (561 loc) · 17.6 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
* install.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { ensureDirSync, existsSync, safeRemoveSync } from "../deno_ral/fs.ts";
import { Confirm } from "cliffy/prompt/mod.ts";
import { Table } from "cliffy/table/mod.ts";
import { basename, dirname, join, relative } from "../deno_ral/path.ts";
import { projectContext } from "../project/project-context.ts";
import { TempContext } from "../core/temp-types.ts";
import { unzip } from "../core/zip.ts";
import { copyTo } from "../core/copy.ts";
import { Extension } from "./types.ts";
import { kExtensionDir } from "./constants.ts";
import { withSpinner } from "../core/console.ts";
import { downloadWithProgress } from "../core/download.ts";
import { createExtensionContext, readExtensions } from "./extension.ts";
import { info } from "../deno_ral/log.ts";
import { ExtensionSource, extensionSource } from "./extension-host.ts";
import { safeExistsSync } from "../core/path.ts";
import { InternalError } from "../core/lib/error.ts";
import { notebookContext } from "../render/notebook/notebook-context.ts";
import { openUrl } from "../core/shell.ts";
const kUnversionedFrom = " (?)";
const kUnversionedTo = "(?) ";
// Core Installation
export async function installExtension(
target: string,
temp: TempContext,
allowPrompt: boolean,
embed?: string,
): Promise<boolean> {
// Is this local or remote?
const source = await extensionSource(target);
// Is this source valid?
if (!source) {
info(
`Extension not found in local or remote sources`,
);
return false;
}
// Does the user trust the extension?
const trusted = await isTrusted(source, allowPrompt);
if (!trusted) {
// Not trusted, cancel
cancelInstallation();
return false;
}
// Compute the installation directory
const currentDir = Deno.cwd();
const installDir = await determineInstallDir(
currentDir,
allowPrompt,
embed,
);
// Stage the extension locally
const extensionDir = await stageExtension(source, temp.createDir());
// Validate the extension in in the staging dir
const stagedExtensions = await validateExtension(extensionDir);
// Confirm that the user would like to take this action
const confirmed = await confirmInstallation(
stagedExtensions,
installDir,
{ allowPrompt },
);
if (!confirmed) {
// Not confirmed, cancel the installation
cancelInstallation();
}
// Complete the installation
await completeInstallation(extensionDir, installDir);
await withSpinner(
{ message: "Extension installation complete" },
() => {
return Promise.resolve();
},
);
if (source.learnMoreUrl) {
info("");
if (allowPrompt) {
const open = await Confirm.prompt({
message: "View documentation using default browser?",
default: true,
});
if (open) {
await openUrl(source.learnMoreUrl);
}
} else {
info(
`\nLearn more about this extension at:\n${source.learnMoreUrl}\n`,
);
}
}
return true;
}
// Cancels the installation, providing user feedback that the installation is canceled
function cancelInstallation() {
info("Installation canceled\n");
}
// Determines whether the user trusts the extension
async function isTrusted(
source: ExtensionSource,
allowPrompt: boolean,
): Promise<boolean> {
if (allowPrompt && source.type === "remote") {
// Write the preamble
const preamble =
`\nQuarto extensions may execute code when documents are rendered. If you do not \ntrust the authors of the extension, we recommend that you do not install or \nuse the extension.`;
info(preamble);
// Ask for trust
const question = "Do you trust the authors of this extension";
const confirmed: boolean = await Confirm.prompt({
message: question,
default: true,
});
return confirmed;
} else {
return true;
}
}
// If the installation is happening in a project
// we should offer to install the extension into the project
async function determineInstallDir(
dir: string,
allowPrompt: boolean,
embed?: string,
) {
if (embed) {
// We're embeddeding this within an extension
const extensionName = embed;
const context = createExtensionContext();
// Load the extension to be sure it exists and then
// use its path as the target for installation
const extension = await context.extension(extensionName, dir);
if (extension) {
if (Object.keys(extension?.contributes.formats || {}).length > 0) {
return extension?.path;
} else {
throw new Error(
`The extension ${embed} does not contribute a format.\nYou can only embed extensions within an extension which itself contributes a format.`,
);
}
} else {
throw new Error(
`Unable to locate the extension '${embed}' that you'd like to embed this within.`,
);
}
} else {
// We're not embeddeding, check if we're in a project
// and offer to use that directory if we are
const nbContext = notebookContext();
const project = await projectContext(dir, nbContext);
if (project && project.dir !== dir) {
const question = "Install extension into project?";
if (allowPrompt) {
const useProject = await Confirm.prompt(question);
if (useProject) {
return project.dir;
} else {
return dir;
}
} else {
return dir;
}
} else {
return dir;
}
}
}
// This downloads or copies the extension files into a temporary working
// directory that we can use to enumerate, compare, etc... when deciding
// whether to proceed with installation
//
// Currently supports
// - Remote Paths
// - Local files (tarballs / zips)
// - Local folders (either the path to the _extensions directory or its parent)
async function stageExtension(
source: ExtensionSource,
workingDir: string,
) {
if (source.type === "remote") {
// Stages a remote file by downloading and unzipping it
const archiveDir = join(workingDir, "archive");
ensureDirSync(archiveDir);
// The filename
const filename = (typeof (source.resolvedTarget) === "string"
? source.resolvedTarget
: source.resolvedFile) || "extension.zip";
// The tarball path
const toFile = join(archiveDir, filename);
// Download the file
await downloadWithProgress(source.resolvedTarget, `Downloading`, toFile);
return unzipAndStage(toFile, source);
} else {
if (typeof source.resolvedTarget !== "string") {
throw new InternalError(
"local resolved extension should always have a string target.",
);
}
if (Deno.statSync(source.resolvedTarget).isDirectory) {
// Copy the extension dir only
const srcDir = extensionDir(source.resolvedTarget);
if (srcDir) {
const destDir = join(workingDir, kExtensionDir);
// If there is something to stage, go for it, otherwise
// just leave the directory empty
await readAndCopyExtensions(srcDir, destDir);
}
return workingDir;
} else {
const filename = basename(source.resolvedTarget);
// A local copy of a zip file
const toFile = join(workingDir, filename);
copyTo(source.resolvedTarget, toFile);
return unzipAndStage(toFile, source);
}
}
}
// Unpack and stage a zipped file
async function unzipAndStage(
zipFile: string,
source: ExtensionSource,
) {
// Unzip the file
await withSpinner(
{ message: "Unzipping" },
async () => {
// Unzip the archive
const result = await unzip(zipFile);
if (!result.success) {
throw new Error("Failed to unzip extension.\n" + result.stderr);
}
// Remove the tar ball itself
await Deno.remove(zipFile);
return Promise.resolve();
},
);
// Use any subdirectory inside, if appropriate
const archiveDir = dirname(zipFile);
const findExtensionDir = () => {
if (source.targetSubdir) {
// If the source provides a subdirectory, just use that
const subDirPath = join(archiveDir, source.targetSubdir);
if (existsSync(subDirPath)) {
return subDirPath;
}
}
// Otherwise, we should inspect the directory either:
// - use the directory itself it has an _extensions dir
// - use a subdirectory if there is a single subdirectory and it has an
// _extensions dir
if (safeExistsSync(join(archiveDir, kExtensionDir))) {
return archiveDir;
} else {
const dirEntries = Deno.readDirSync(archiveDir);
let count = 0;
let name;
for (const dirEntry of dirEntries) {
// ignore any files
if (dirEntry.isDirectory) {
name = dirEntry.name;
count++;
}
}
if (count === 1 && name && name !== kExtensionDir) {
if (safeExistsSync(join(archiveDir, name, kExtensionDir))) {
return join(archiveDir, name);
} else {
return archiveDir;
}
} else {
return archiveDir;
}
}
};
// Use a subdirectory if the source provides one
const extensionsDir = join(findExtensionDir(), kExtensionDir);
// Make the final directory we're staging into
const finalDir = join(archiveDir, "staged");
await copyExtensions(source, extensionsDir, finalDir);
return finalDir;
}
export async function copyExtensions(
source: ExtensionSource,
srcDir: string,
targetDir: string,
) {
const finalExtensionsDir = join(targetDir, kExtensionDir);
const finalExtensionTargetDir = source.owner
? join(finalExtensionsDir, source.owner)
: finalExtensionsDir;
ensureDirSync(finalExtensionTargetDir);
// Move extensions into the target directory (root or owner)
await readAndCopyExtensions(srcDir, finalExtensionTargetDir);
}
// Reads the extensions from an extensions directory and copies
// them to a destination directory
async function readAndCopyExtensions(
extensionsDir: string,
targetDir: string,
) {
const extensions = await readExtensions(extensionsDir);
info(
` Found ${extensions.length} ${
extensions.length === 1 ? "extension" : "extensions"
}.`,
);
for (const extension of extensions) {
copyTo(
extension.path,
join(targetDir, extension.id.name),
);
}
}
// Validates that a path on disk is a valid path to extensions
// Currently just ensures there is an _extensions directory
// and that the directory contains readable extensions
async function validateExtension(path: string) {
const extensionsFolder = extensionDir(path);
if (!extensionsFolder) {
throw new Error(
`Invalid extension\nThe extension staged at ${path} is missing an '_extensions' folder.`,
);
}
const extensions = await readExtensions(extensionsFolder);
if (extensions.length === 0) {
throw new Error(
`Invalid extension\nThe extension staged at ${path} does not provide any valid extensions.`,
);
}
return extensions;
}
export interface ConfirmationOptions {
allowPrompt: boolean;
throw?: boolean;
message?: string;
}
// Confirm that the user would like to proceed with the installation
export async function confirmInstallation(
extensions: Extension[],
installDir: string,
options: ConfirmationOptions,
) {
const readExisting = async () => {
try {
const existingExtensions = await readExtensions(
join(installDir, kExtensionDir),
);
return existingExtensions;
} catch {
return [];
}
};
const name = (extension: Extension) => {
const idStr = extension.id.organization
? `${extension.id.organization}/${extension.id.name}`
: extension.id.name;
return extension.title || idStr;
};
const existingExtensions = await readExisting();
const existing = (extension: Extension) => {
return existingExtensions.find((existing) => {
return existing.id.name === extension.id.name &&
existing.id.organization === extension.id.organization;
});
};
if (existingExtensions.length > 0 && !options.allowPrompt && options.throw) {
throw new Error(
`There are extensions installed which would be overwritten. Aborting installation.\n${
existingExtensions.map((ext) => {
return ext.title;
}).join("\n - ")
}`,
);
}
const typeStr = (to: Extension) => {
const contributes = to.contributes;
const extTypes: string[] = [];
if (
contributes.formats &&
Object.keys(contributes.formats).length > 0
) {
Object.keys(contributes.formats).length === 1
? extTypes.push("format")
: extTypes.push("formats");
}
if (
contributes.shortcodes &&
contributes.shortcodes.length > 0
) {
contributes.shortcodes.length === 1
? extTypes.push("shortcode")
: extTypes.push("shortcodes");
}
if (contributes.filters && contributes.filters.length > 0) {
contributes.filters.length === 1
? extTypes.push("filter")
: extTypes.push("filters");
}
if (extTypes.length > 0) {
return `(${extTypes.join(",")})`;
} else {
return "";
}
};
const versionMessage = (to: Extension, from?: Extension) => {
if (to && !from) {
const versionStr = to.version?.format();
// New Install
return {
action: "Install",
from: "",
to: versionStr,
};
} else {
if (to.version && from?.version) {
// From version to version
const comparison = to.version.compare(from.version);
if (comparison === 0) {
return {
action: "No Change",
from: "",
to: "",
};
} else if (comparison > 0) {
return {
action: "Update",
from: from.version.format(),
to: to.version.format(),
};
} else {
return {
action: "Revert",
from: from.version.format(),
to: to.version.format(),
};
}
} else if (to.version && !from?.version) {
// From unversioned to versioned
return {
action: "Update",
from: kUnversionedFrom,
to: to.version.format(),
};
} else if (!to.version && from?.version) {
// From versioned to unversioned
return {
action: "Update",
from: from.version.format(),
to: kUnversionedTo,
};
} else {
// Both unversioned
return {
action: "Update",
from: kUnversionedFrom,
to: kUnversionedTo,
};
}
}
};
const extensionRows: string[][] = [];
for (const stagedExtension of extensions) {
const installedExtension = existing(stagedExtension);
const message = versionMessage(
stagedExtension,
installedExtension,
);
const types = typeStr(stagedExtension);
if (message) {
extensionRows.push([
name(stagedExtension) + " ",
`[${message.action}]`,
message.from || "",
message.to && message.from ? "->" : "",
message.to || "",
types,
]);
}
}
if (extensionRows.length > 0) {
const table = new Table(...extensionRows);
info(
`\n${
options.message || "The following changes will be made:"
}\n${table.toString()}`,
);
const question = "Would you like to continue";
return !options.allowPrompt ||
await Confirm.prompt({
message: question,
default: true,
});
} else {
info(`\nNo changes required - extensions already installed.`);
return true;
}
}
// Copy the extension files into place
export async function completeInstallation(
downloadDir: string,
installDir: string,
) {
info("");
await withSpinner({
message: `Copying`,
}, async () => {
// Determine a staging location in the installDir
// (to ensure we can use move without fear of spanning volumes)
const stagingDir = join(installDir, "._extensions.staging");
try {
// For each 'extension' in the install dir, perform a move
const downloadedExtDir = join(downloadDir, kExtensionDir);
// We'll stage the extension in a directory within the install dir
// then move it to the install dir when ready
const stagingExtDir = join(stagingDir, kExtensionDir);
ensureDirSync(stagingExtDir);
// The final installation target
const installExtDir = join(installDir, kExtensionDir);
ensureDirSync(installExtDir);
// Read the extensions that have been downloaded and install them
// one by bone
const extensions = await readExtensions(downloadedExtDir);
extensions.forEach((extension) => {
const extensionRelativeDir = relative(downloadedExtDir, extension.path);
// Copy to the staging path
const stagingPath = join(stagingExtDir, extensionRelativeDir);
copyTo(extension.path, stagingPath);
// Move from the staging path to the install dir
const installPath = join(installExtDir, extensionRelativeDir);
if (existsSync(installPath)) {
safeRemoveSync(installPath, { recursive: true });
}
// Ensure the parent directory exists
ensureDirSync(dirname(installPath));
Deno.renameSync(stagingPath, installPath);
});
} finally {
// Clean up the staging directory
safeRemoveSync(stagingDir, { recursive: true });
}
return Promise.resolve();
});
}
// Is this _extensions or does this contain _extensions?
const extensionDir = (path: string) => {
if (basename(path) === kExtensionDir) {
// If this is pointing to an _extensions dir, use that
return path;
} else {
// Otherwise, add _extensions to this and use that
const extDir = join(path, kExtensionDir);
if (existsSync(extDir) && Deno.statSync(extDir).isDirectory) {
return extDir;
} else {
return path;
}
}
};