-
Notifications
You must be signed in to change notification settings - Fork 1
/
manifest.ts
89 lines (84 loc) · 2.7 KB
/
manifest.ts
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
/*
* Copyright © 2023 Norman Fomferra
* Permissions are hereby granted under the terms of the MIT License:
* https://opensource.org/licenses/MIT.
*/
import type { ExtensionManifest, ExtensionPathResolver } from "@/core/types";
import { type JsonTypedSchema, type JsonValue, validateJson } from "@/util";
import { toTitle } from "@/util/to-title";
const manifestSchema: JsonTypedSchema<ExtensionManifest> = {
type: "object",
properties: {
name: { type: "string" },
provider: { type: "string" },
main: { type: "string", nullable: true },
version: { type: "string", nullable: true },
displayName: { type: "string", nullable: true },
description: { type: "string", nullable: true },
activationEvents: {
type: "array",
items: { type: "string" },
nullable: true,
},
extensionDependencies: {
type: "array",
items: { type: "string" },
nullable: true,
},
contributes: { type: "object", additionalProperties: true, nullable: true },
},
required: ["name", "provider"],
additionalProperties: true,
};
/**
* Reads and validates an extension manifest.
*
* @category Extension API
* @param manifestPath Path or URL to the extension's manifest, a file usually
* called `package.json`.
* @returns A tuple `[manifest, resolvePath]` comprising the validated manifest
* and a path resolver function.
*/
export async function readExtensionManifest(
manifestPath: string
): Promise<[ExtensionManifest, ExtensionPathResolver]> {
const manifestModule = (await import(
manifestPath /* @vite-ignore */
)) as unknown as Record<string, unknown>;
const manifestJson = manifestModule.default as JsonValue;
const manifest = validateJson(
manifestSchema,
manifestJson,
`extension manifest ${manifestPath}`
);
const components = manifestPath.split("/");
const extensionPath = components.slice(0, components.length - 1).join("/");
const pathResolver = (path: string) => {
if (path.startsWith("./")) {
path = path.slice(2);
}
while (path.startsWith("/")) {
path = path.slice(1);
}
return `${extensionPath}/${path}`;
};
return [manifest as ExtensionManifest, pathResolver];
}
/**
* Get the extension identifier from given manifest.
*
* @category Extension API
* @param manifest - the application manifest
*/
export function getExtensionId(manifest: ExtensionManifest): string {
return `${manifest.provider}.${manifest.name}`;
}
/**
* Get the human-readable extension name from given manifest.
*
* @category Extension API
* @param manifest - the application manifest
*/
export function getExtensionDisplayName(manifest: ExtensionManifest): string {
return manifest.displayName ?? toTitle(manifest.name);
}