@metamask/snaps-utils 11.7.1 → 12.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -1
- package/dist/localization.d.cts +1 -0
- package/dist/localization.d.cts.map +1 -1
- package/dist/localization.d.mts +1 -0
- package/dist/localization.d.mts.map +1 -1
- package/dist/manifest/manifest.cjs +99 -27
- package/dist/manifest/manifest.cjs.map +1 -1
- package/dist/manifest/manifest.d.cts +18 -5
- package/dist/manifest/manifest.d.cts.map +1 -1
- package/dist/manifest/manifest.d.mts +18 -5
- package/dist/manifest/manifest.d.mts.map +1 -1
- package/dist/manifest/manifest.mjs +106 -28
- package/dist/manifest/manifest.mjs.map +1 -1
- package/dist/manifest/validation.cjs +1 -0
- package/dist/manifest/validation.cjs.map +1 -1
- package/dist/manifest/validation.d.cts +2 -0
- package/dist/manifest/validation.d.cts.map +1 -1
- package/dist/manifest/validation.d.mts +2 -0
- package/dist/manifest/validation.d.mts.map +1 -1
- package/dist/manifest/validation.mjs +1 -0
- package/dist/manifest/validation.mjs.map +1 -1
- package/dist/manifest/validator-types.cjs.map +1 -1
- package/dist/manifest/validator-types.d.cts +4 -5
- package/dist/manifest/validator-types.d.cts.map +1 -1
- package/dist/manifest/validator-types.d.mts +4 -5
- package/dist/manifest/validator-types.d.mts.map +1 -1
- package/dist/manifest/validator-types.mjs.map +1 -1
- package/dist/manifest/validator.cjs +1 -1
- package/dist/manifest/validator.cjs.map +1 -1
- package/dist/manifest/validator.d.cts +3 -3
- package/dist/manifest/validator.d.cts.map +1 -1
- package/dist/manifest/validator.d.mts +3 -3
- package/dist/manifest/validator.d.mts.map +1 -1
- package/dist/manifest/validator.mjs +1 -1
- package/dist/manifest/validator.mjs.map +1 -1
- package/dist/manifest/validators/checksum.cjs +11 -3
- package/dist/manifest/validators/checksum.cjs.map +1 -1
- package/dist/manifest/validators/checksum.d.cts.map +1 -1
- package/dist/manifest/validators/checksum.d.mts.map +1 -1
- package/dist/manifest/validators/checksum.mjs +11 -3
- package/dist/manifest/validators/checksum.mjs.map +1 -1
- package/dist/manifest/validators/icon-declared.cjs +1 -1
- package/dist/manifest/validators/icon-declared.cjs.map +1 -1
- package/dist/manifest/validators/icon-declared.mjs +1 -1
- package/dist/manifest/validators/icon-declared.mjs.map +1 -1
- package/dist/manifest/validators/icon-missing.cjs +1 -1
- package/dist/manifest/validators/icon-missing.cjs.map +1 -1
- package/dist/manifest/validators/icon-missing.d.cts.map +1 -1
- package/dist/manifest/validators/icon-missing.d.mts.map +1 -1
- package/dist/manifest/validators/icon-missing.mjs +1 -1
- package/dist/manifest/validators/icon-missing.mjs.map +1 -1
- package/dist/manifest/validators/is-snap-manifest.cjs +1 -1
- package/dist/manifest/validators/is-snap-manifest.cjs.map +1 -1
- package/dist/manifest/validators/is-snap-manifest.mjs +1 -1
- package/dist/manifest/validators/is-snap-manifest.mjs.map +1 -1
- package/dist/manifest/validators/manifest-localization.cjs +1 -1
- package/dist/manifest/validators/manifest-localization.cjs.map +1 -1
- package/dist/manifest/validators/manifest-localization.mjs +1 -1
- package/dist/manifest/validators/manifest-localization.mjs.map +1 -1
- package/dist/manifest/validators/package-name-match.cjs +6 -2
- package/dist/manifest/validators/package-name-match.cjs.map +1 -1
- package/dist/manifest/validators/package-name-match.d.cts.map +1 -1
- package/dist/manifest/validators/package-name-match.d.mts.map +1 -1
- package/dist/manifest/validators/package-name-match.mjs +6 -2
- package/dist/manifest/validators/package-name-match.mjs.map +1 -1
- package/dist/manifest/validators/platform-version.cjs +4 -4
- package/dist/manifest/validators/platform-version.cjs.map +1 -1
- package/dist/manifest/validators/platform-version.d.cts.map +1 -1
- package/dist/manifest/validators/platform-version.d.mts.map +1 -1
- package/dist/manifest/validators/platform-version.mjs +4 -4
- package/dist/manifest/validators/platform-version.mjs.map +1 -1
- package/dist/manifest/validators/production-platform-version.cjs +1 -1
- package/dist/manifest/validators/production-platform-version.cjs.map +1 -1
- package/dist/manifest/validators/production-platform-version.d.cts.map +1 -1
- package/dist/manifest/validators/production-platform-version.d.mts.map +1 -1
- package/dist/manifest/validators/production-platform-version.mjs +1 -1
- package/dist/manifest/validators/production-platform-version.mjs.map +1 -1
- package/dist/manifest/validators/repository-match.cjs +2 -2
- package/dist/manifest/validators/repository-match.cjs.map +1 -1
- package/dist/manifest/validators/repository-match.d.cts.map +1 -1
- package/dist/manifest/validators/repository-match.d.mts.map +1 -1
- package/dist/manifest/validators/repository-match.mjs +2 -2
- package/dist/manifest/validators/repository-match.mjs.map +1 -1
- package/dist/manifest/validators/unused-exports.cjs +3 -3
- package/dist/manifest/validators/unused-exports.cjs.map +1 -1
- package/dist/manifest/validators/unused-exports.mjs +3 -3
- package/dist/manifest/validators/unused-exports.mjs.map +1 -1
- package/dist/manifest/validators/version-match.cjs +2 -2
- package/dist/manifest/validators/version-match.cjs.map +1 -1
- package/dist/manifest/validators/version-match.mjs +2 -2
- package/dist/manifest/validators/version-match.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +66 -1
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +66 -1
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [12.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add `loadManifest` function to load extendable manifests ([#3793](https://github.com/MetaMask/snaps/pull/3793))
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- **BREAKING:** Allow manifest to extend another manifest ([#3802](https://github.com/MetaMask/snaps/pull/3802))
|
|
19
|
+
- `checkManifest` now returns `ExtendableSnapFiles`, containing a manifest
|
|
20
|
+
that may extend another manifest.
|
|
21
|
+
- **BREAKING:** Require path to manifest instead of directory in `checkManifest` function ([#3793](https://github.com/MetaMask/snaps/pull/3793))
|
|
22
|
+
|
|
10
23
|
## [11.7.1]
|
|
11
24
|
|
|
12
25
|
### Changed
|
|
@@ -754,7 +767,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
754
767
|
- The version of the package no longer needs to match the version of all other
|
|
755
768
|
MetaMask Snaps packages.
|
|
756
769
|
|
|
757
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@
|
|
770
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@12.0.0...HEAD
|
|
771
|
+
[12.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.7.1...@metamask/snaps-utils@12.0.0
|
|
758
772
|
[11.7.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.7.0...@metamask/snaps-utils@11.7.1
|
|
759
773
|
[11.7.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.6.3...@metamask/snaps-utils@11.7.0
|
|
760
774
|
[11.6.3]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-utils@11.6.2...@metamask/snaps-utils@11.6.3
|
package/dist/localization.d.cts
CHANGED
|
@@ -131,6 +131,7 @@ export declare function getLocalizedSnapManifest(snapManifest: SnapManifest, loc
|
|
|
131
131
|
wallet_snap: Record<string, import("@metamask/snaps-sdk").RequestedSnap>;
|
|
132
132
|
}>;
|
|
133
133
|
manifestVersion: "0.1";
|
|
134
|
+
extends?: string | undefined;
|
|
134
135
|
repository?: {
|
|
135
136
|
type: string;
|
|
136
137
|
url: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localization.d.cts","sourceRoot":"","sources":["../src/localization.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAWnD,OAAO,KAAK,EAAE,YAAY,EAAE,6BAAmB;AAC/C,OAA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BAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,gBAAgB,EAAE;;;;;;cAatC;AAED,eAAO,MAAM,iBAAiB,QAAuC,CAAC;AAEtE;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,GAAG,SAAS,UAsB1E;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,gBAAgB,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAWtC;AAED;;;;;;GAMG;AACH,wBAAgB,iCAAiC,CAC/C,YAAY,EAAE,YAAY,EAC1B,iBAAiB,EAAE,gBAAgB,EAAE,QAkBtC"}
|
package/dist/localization.d.mts
CHANGED
|
@@ -131,6 +131,7 @@ export declare function getLocalizedSnapManifest(snapManifest: SnapManifest, loc
|
|
|
131
131
|
wallet_snap: Record<string, import("@metamask/snaps-sdk").RequestedSnap>;
|
|
132
132
|
}>;
|
|
133
133
|
manifestVersion: "0.1";
|
|
134
|
+
extends?: string | undefined;
|
|
134
135
|
repository?: {
|
|
135
136
|
type: string;
|
|
136
137
|
url: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localization.d.mts","sourceRoot":"","sources":["../src/localization.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAWnD,OAAO,KAAK,EAAE,YAAY,EAAE,6BAAmB;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,iCAAuB;AAElD,eAAO,MAAM,kBAAkB,0CAA2C,CAAC;AAE3E,eAAO,MAAM,sBAAsB;;;;;;;;;;;;EASjC,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,KAAK,CAAC,OAAO,sBAAsB,CAAC,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AE,IAAI,EAAE,gBAAgB,GAAG,SAAS,UAsB1E;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,MAAM,EACd,iBAAiB,EAAE,gBAAgB,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAWtC;AAED;;;;;;GAMG;AACH,wBAAgB,iCAAiC,CAC/C,YAAY,EAAE,YAAY,EAC1B,iBAAiB,EAAE,gBAAgB,EAAE,QAkBtC"}
|
|
@@ -26,11 +26,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.getWritableManifest = exports.getSnapFiles = exports.getSnapFilePaths = exports.getSnapIcon = exports.getSnapSourceCode = exports.runFixes = exports.checkManifest = void 0;
|
|
29
|
+
exports.getWritableManifest = exports.getSnapFiles = exports.getSnapFilePaths = exports.getSnapIcon = exports.getSnapSourceCode = exports.runFixes = exports.checkManifest = exports.loadManifest = void 0;
|
|
30
30
|
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
31
31
|
const utils_1 = require("@metamask/utils");
|
|
32
|
+
const deepmerge_1 = __importDefault(require("deepmerge"));
|
|
32
33
|
const fs_1 = require("fs");
|
|
33
|
-
const path_1 =
|
|
34
|
+
const path_1 = __importStar(require("path"));
|
|
34
35
|
const validator_1 = require("./validator.cjs");
|
|
35
36
|
const defaultValidators = __importStar(require("./validators/index.cjs"));
|
|
36
37
|
const deep_clone_1 = require("../deep-clone.cjs");
|
|
@@ -40,22 +41,85 @@ const types_1 = require("../types.cjs");
|
|
|
40
41
|
const node_1 = require("../virtual-file/node.cjs");
|
|
41
42
|
const MANIFEST_SORT_ORDER = {
|
|
42
43
|
$schema: 1,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
44
|
+
extends: 2,
|
|
45
|
+
version: 3,
|
|
46
|
+
description: 4,
|
|
47
|
+
proposedName: 5,
|
|
48
|
+
repository: 6,
|
|
49
|
+
source: 7,
|
|
50
|
+
initialConnections: 8,
|
|
51
|
+
initialPermissions: 9,
|
|
52
|
+
platformVersion: 10,
|
|
53
|
+
manifestVersion: 11,
|
|
52
54
|
};
|
|
55
|
+
/**
|
|
56
|
+
* Merge two Snap manifests, with the extended manifest taking precedence
|
|
57
|
+
* over the base manifest.
|
|
58
|
+
*
|
|
59
|
+
* @param mainManifest - The main manifest.
|
|
60
|
+
* @param extendedManifest - The extended manifest.
|
|
61
|
+
* @returns The merged manifest.
|
|
62
|
+
*/
|
|
63
|
+
function mergeManifests(mainManifest, extendedManifest) {
|
|
64
|
+
if (!extendedManifest) {
|
|
65
|
+
return mainManifest;
|
|
66
|
+
}
|
|
67
|
+
(0, utils_1.assert)((0, utils_1.isObject)(mainManifest));
|
|
68
|
+
(0, utils_1.assert)((0, utils_1.isObject)(extendedManifest));
|
|
69
|
+
const mergedManifest = (0, deepmerge_1.default)(extendedManifest, mainManifest);
|
|
70
|
+
delete mergedManifest.extends;
|
|
71
|
+
return getWritableManifest(mergedManifest);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Load a manifest and its extended manifest if it has one.
|
|
75
|
+
*
|
|
76
|
+
* Note: This function does not validate the manifests.
|
|
77
|
+
*
|
|
78
|
+
* @param manifestPath - The path to the manifest file.
|
|
79
|
+
* @param files - A set of already loaded manifest file paths to prevent
|
|
80
|
+
* circular dependencies.
|
|
81
|
+
* @param root - Whether this is the root manifest being loaded. Used for
|
|
82
|
+
* recursive calls, and should not be set by callers.
|
|
83
|
+
* @returns The base and extended manifests.
|
|
84
|
+
*/
|
|
85
|
+
async function loadManifest(manifestPath, files = new Set(), root = true) {
|
|
86
|
+
(0, utils_1.assert)(path_1.default.isAbsolute(manifestPath), 'The `loadManifest` function must be called with an absolute path.');
|
|
87
|
+
if (files.has(manifestPath)) {
|
|
88
|
+
throw new Error(`Failed to load Snap manifest: Circular dependency detected when loading "${manifestPath}".`);
|
|
89
|
+
}
|
|
90
|
+
const mainManifest = await (0, fs_2.readJsonFile)(manifestPath);
|
|
91
|
+
files.add(manifestPath);
|
|
92
|
+
if (!(0, utils_1.isObject)(mainManifest.result)) {
|
|
93
|
+
throw new Error(`Failed to load Snap manifest: The Snap manifest file at "${manifestPath}" must contain a JSON object.`);
|
|
94
|
+
}
|
|
95
|
+
if (mainManifest.result.extends &&
|
|
96
|
+
typeof mainManifest.result.extends === 'string') {
|
|
97
|
+
const fileName = path_1.default.basename(manifestPath);
|
|
98
|
+
if (root && fileName === 'snap.manifest.json') {
|
|
99
|
+
throw new Error(`Failed to load Snap manifest: The Snap manifest file at "snap.manifest.json" cannot extend another manifest.`);
|
|
100
|
+
}
|
|
101
|
+
const extendedManifestPath = path_1.default.resolve(path_1.default.dirname(manifestPath), mainManifest.result.extends);
|
|
102
|
+
const extendedManifest = await loadManifest(extendedManifestPath, files, false);
|
|
103
|
+
return {
|
|
104
|
+
mainManifest,
|
|
105
|
+
extendedManifest: extendedManifest.mergedManifest,
|
|
106
|
+
mergedManifest: mergeManifests(mainManifest.result, extendedManifest.mergedManifest),
|
|
107
|
+
files,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
mainManifest,
|
|
112
|
+
mergedManifest: mainManifest.result,
|
|
113
|
+
files,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
exports.loadManifest = loadManifest;
|
|
53
117
|
/**
|
|
54
118
|
* Validates a snap.manifest.json file. Attempts to fix the manifest and write
|
|
55
119
|
* the fixed version to disk if `writeManifest` is true. Throws if validation
|
|
56
120
|
* fails.
|
|
57
121
|
*
|
|
58
|
-
* @param
|
|
122
|
+
* @param manifestPath - The path to the manifest file.
|
|
59
123
|
* @param options - Additional options for the function.
|
|
60
124
|
* @param options.sourceCode - The source code of the Snap.
|
|
61
125
|
* @param options.writeFileFn - The function to use to write the manifest to
|
|
@@ -73,10 +137,10 @@ const MANIFEST_SORT_ORDER = {
|
|
|
73
137
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
74
138
|
* were encountered during processing of the manifest files.
|
|
75
139
|
*/
|
|
76
|
-
async function checkManifest(
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const unvalidatedManifest =
|
|
140
|
+
async function checkManifest(manifestPath, { updateAndWriteManifest = true, sourceCode, writeFileFn = fs_1.promises.writeFile, exports, handlerEndowments, watchMode = false, } = {}) {
|
|
141
|
+
const basePath = (0, path_1.dirname)(manifestPath);
|
|
142
|
+
const extendableManifest = await loadManifest(manifestPath);
|
|
143
|
+
const unvalidatedManifest = extendableManifest.mergedManifest;
|
|
80
144
|
const packageFile = await (0, fs_2.readJsonFile)(path_1.default.join(basePath, types_1.NpmSnapFileNames.PackageJson));
|
|
81
145
|
const auxiliaryFilePaths = getSnapFilePaths(unvalidatedManifest, (manifest) => manifest?.source?.files);
|
|
82
146
|
const localizationFilePaths = getSnapFilePaths(unvalidatedManifest, (manifest) => manifest?.source?.locales);
|
|
@@ -91,7 +155,7 @@ async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCo
|
|
|
91
155
|
}
|
|
92
156
|
}
|
|
93
157
|
const snapFiles = {
|
|
94
|
-
manifest:
|
|
158
|
+
manifest: extendableManifest,
|
|
95
159
|
packageJson: packageFile,
|
|
96
160
|
sourceCode: await getSnapSourceCode(basePath, unvalidatedManifest, sourceCode),
|
|
97
161
|
svgIcon: await getSnapIcon(basePath, unvalidatedManifest),
|
|
@@ -111,7 +175,7 @@ async function checkManifest(basePath, { updateAndWriteManifest = true, sourceCo
|
|
|
111
175
|
manifestResults = fixedResults;
|
|
112
176
|
(0, utils_1.assert)(manifestResults.files);
|
|
113
177
|
try {
|
|
114
|
-
await writeFileFn(
|
|
178
|
+
await writeFileFn(manifestPath, manifestResults.files.manifest.mainManifest.toString());
|
|
115
179
|
}
|
|
116
180
|
catch (error) {
|
|
117
181
|
// Note: This error isn't pushed to the errors array, because it's not an
|
|
@@ -143,18 +207,27 @@ async function runFixes(results, rules, errorsOnly = false) {
|
|
|
143
207
|
(0, utils_1.assert)(results.files);
|
|
144
208
|
let fixResults = results;
|
|
145
209
|
(0, utils_1.assert)(fixResults.files);
|
|
146
|
-
fixResults.files.manifest =
|
|
210
|
+
fixResults.files.manifest.mainManifest =
|
|
211
|
+
fixResults.files.manifest.mainManifest.clone();
|
|
147
212
|
const mergedReports = (0, deep_clone_1.deepClone)(fixResults.reports);
|
|
148
213
|
for (let attempts = 1; shouldRunFixes && attempts <= MAX_ATTEMPTS; attempts++) {
|
|
149
214
|
(0, utils_1.assert)(fixResults.files);
|
|
150
|
-
let manifest = fixResults.files.manifest.result;
|
|
151
215
|
const fixable = fixResults.reports.filter((report) => (0, validator_1.isReportFixable)(report, errorsOnly));
|
|
216
|
+
let { manifest } = fixResults.files;
|
|
152
217
|
for (const report of fixable) {
|
|
153
218
|
(0, utils_1.assert)(report.fix);
|
|
154
219
|
({ manifest } = await report.fix({ manifest }));
|
|
220
|
+
manifest.mergedManifest = mergeManifests(manifest.mainManifest.result, manifest.extendedManifest);
|
|
155
221
|
}
|
|
156
|
-
|
|
157
|
-
|
|
222
|
+
// The `mainManifest` is always the first manifest loaded, and is the one
|
|
223
|
+
// that should be updated with fixes. Any manifests that the main manifest
|
|
224
|
+
// extends will not be updated.
|
|
225
|
+
// We can revisit this in the future if we want to support fixing extended
|
|
226
|
+
// manifests as well, but it adds complexity, as we'd need to track which
|
|
227
|
+
// fixes apply to which manifest.
|
|
228
|
+
fixResults.files.manifest.mainManifest.value = `${JSON.stringify(getWritableManifest(manifest.mainManifest.result), null, 2)}\n`;
|
|
229
|
+
fixResults.files.manifest = manifest;
|
|
230
|
+
fixResults.files.manifest.mergedManifest = mergeManifests(fixResults.files.manifest.mainManifest.result, fixResults.files.manifest.extendedManifest);
|
|
158
231
|
fixResults = await (0, validator_1.runValidators)(fixResults.files, rules);
|
|
159
232
|
shouldRunFixes = (0, validator_1.hasFixes)(fixResults, errorsOnly);
|
|
160
233
|
fixResults.reports
|
|
@@ -196,7 +269,7 @@ exports.runFixes = runFixes;
|
|
|
196
269
|
* @returns The contents of the bundle file, if any.
|
|
197
270
|
*/
|
|
198
271
|
async function getSnapSourceCode(basePath, manifest, sourceCode) {
|
|
199
|
-
if (!(0, utils_1.
|
|
272
|
+
if (!(0, utils_1.isObject)(manifest)) {
|
|
200
273
|
return undefined;
|
|
201
274
|
}
|
|
202
275
|
const sourceFilePath = manifest.source?.location
|
|
@@ -228,7 +301,7 @@ exports.getSnapSourceCode = getSnapSourceCode;
|
|
|
228
301
|
* @returns The contents of the icon, if any.
|
|
229
302
|
*/
|
|
230
303
|
async function getSnapIcon(basePath, manifest) {
|
|
231
|
-
if (!(0, utils_1.
|
|
304
|
+
if (!(0, utils_1.isObject)(manifest)) {
|
|
232
305
|
return undefined;
|
|
233
306
|
}
|
|
234
307
|
const iconPath = manifest.source?.location?.npm
|
|
@@ -253,7 +326,7 @@ exports.getSnapIcon = getSnapIcon;
|
|
|
253
326
|
* @returns The paths to the files, if any.
|
|
254
327
|
*/
|
|
255
328
|
function getSnapFilePaths(manifest, selector) {
|
|
256
|
-
if (!(0, utils_1.
|
|
329
|
+
if (!(0, utils_1.isObject)(manifest)) {
|
|
257
330
|
return undefined;
|
|
258
331
|
}
|
|
259
332
|
const snapManifest = manifest;
|
|
@@ -295,13 +368,12 @@ exports.getSnapFiles = getSnapFiles;
|
|
|
295
368
|
function getWritableManifest(manifest) {
|
|
296
369
|
const { repository, ...remaining } = manifest;
|
|
297
370
|
const keys = Object.keys(repository ? { ...remaining, repository } : remaining);
|
|
298
|
-
|
|
371
|
+
return keys
|
|
299
372
|
.sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])
|
|
300
373
|
.reduce((result, key) => ({
|
|
301
374
|
...result,
|
|
302
375
|
[key]: manifest[key],
|
|
303
376
|
}), {});
|
|
304
|
-
return writableManifest;
|
|
305
377
|
}
|
|
306
378
|
exports.getWritableManifest = getWritableManifest;
|
|
307
379
|
//# sourceMappingURL=manifest.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAsD;AAEtD,2CAAwD;AACxD,2BAAoC;AACpC,gDAA6B;AAI7B,+CAAuE;AAEvE,0EAAkD;AAClD,kDAA0C;AAC1C,kCAAqC;AACrC,sCAAoC;AAEpC,wCAA4C;AAC5C,mDAAoE;AAEpE,MAAM,mBAAmB,GAAuC;IAC9D,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,CAAC;IACb,MAAM,EAAE,CAAC;IACT,kBAAkB,EAAE,CAAC;IACrB,kBAAkB,EAAE,CAAC;IACrB,eAAe,EAAE,CAAC;IAClB,eAAe,EAAE,EAAE;CACpB,CAAC;AA4DF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACI,KAAK,UAAU,aAAa,CACjC,QAAgB,EAChB,EACE,sBAAsB,GAAG,IAAI,EAC7B,UAAU,EACV,WAAW,GAAG,aAAE,CAAC,SAAS,EAC1B,OAAO,EACP,iBAAiB,EACjB,SAAS,GAAG,KAAK,MACO,EAAE;IAE5B,MAAM,YAAY,GAAG,cAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,wBAAgB,CAAC,QAAQ,CAAC,CAAC;IACzE,MAAM,YAAY,GAAG,MAAM,IAAA,iBAAY,EAAC,YAAY,CAAC,CAAC;IACtD,MAAM,mBAAmB,GAAG,YAAY,CAAC,MAAM,CAA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gBAAgB,GAAG,IAAI;SAC1B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAAC;SAC/D,MAAM,CACL,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QAChB,GAAG,MAAM;QACT,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;KACrB,CAAC,EACF,EAAE,CACH,CAAC;IAEJ,OAAO,gBAAgC,CAAC;AAC1C,CAAC;AAlBD,kDAkBC","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\nimport { assert, isPlainObject } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport type { SnapManifest } from './validation';\nimport type { ValidatorResults } from './validator';\nimport { isReportFixable, hasFixes, runValidators } from './validator';\nimport type { ValidatorMeta, ValidatorReport } from './validator-types';\nimport * as defaultValidators from './validators';\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { parseJson } from '../json';\nimport type { SnapFiles, UnvalidatedSnapFiles } from '../types';\nimport { NpmSnapFileNames } from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file/node';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n version: 2,\n description: 3,\n proposedName: 4,\n repository: 5,\n source: 6,\n initialConnections: 7,\n initialPermissions: 8,\n platformVersion: 9,\n manifestVersion: 10,\n};\n\nexport type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {\n wasFixed?: boolean;\n};\n\n/**\n * The options for the `checkManifest` function.\n */\nexport type CheckManifestOptions = {\n /**\n * Whether to auto-magically try to fix errors and then write the manifest to\n * disk.\n */\n updateAndWriteManifest?: boolean;\n\n /**\n * The source code of the Snap.\n */\n sourceCode?: string;\n\n /**\n * The function to use to write the manifest to disk.\n */\n writeFileFn?: WriteFileFunction;\n\n /**\n * The exports detected by evaluating the bundle. This may be used by one or\n * more validators to determine whether the Snap is valid.\n */\n exports?: string[];\n\n /**\n * An object containing the names of the handlers and their respective\n * permission name. This must be provided to avoid circular dependencies\n * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.\n */\n handlerEndowments?: Record<string, string | null>;\n\n /**\n * Whether the compiler is running in watch mode. This is used to determine\n * whether to fix warnings or errors only.\n */\n watchMode?: boolean;\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was written and updated.\n */\nexport type CheckManifestResult = {\n files?: SnapFiles;\n updated: boolean;\n reports: CheckManifestReport[];\n};\n\nexport type WriteFileFunction = (path: string, data: string) => Promise<void>;\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param options - Additional options for the function.\n * @param options.sourceCode - The source code of the Snap.\n * @param options.writeFileFn - The function to use to write the manifest to\n * disk.\n * @param options.updateAndWriteManifest - Whether to auto-magically try to fix\n * errors and then write the manifest to disk.\n * @param options.exports - The exports detected by evaluating the bundle. This\n * may be used by one or more validators to determine whether the Snap is valid.\n * @param options.handlerEndowments - An object containing the names of the\n * handlers and their respective permission name. This must be provided to avoid\n * circular dependencies between `@metamask/snaps-utils` and\n * `@metamask/snaps-rpc-methods`.\n * @param options.watchMode - Whether the compiler is running in watch mode.\n * This is used to determine whether to fix warnings or errors only.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n {\n updateAndWriteManifest = true,\n sourceCode,\n writeFileFn = fs.writeFile,\n exports,\n handlerEndowments,\n watchMode = false,\n }: CheckManifestOptions = {},\n): Promise<CheckManifestResult> {\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const auxiliaryFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.files,\n );\n\n const localizationFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.locales,\n );\n const localizationFiles =\n (await getSnapFiles(basePath, localizationFilePaths)) ?? [];\n for (const localization of localizationFiles) {\n try {\n localization.result = parseJson(localization.toString());\n } catch (error) {\n assert(error instanceof SyntaxError, error);\n throw new Error(\n `Failed to parse localization file \"${localization.path}\" as JSON.`,\n );\n }\n }\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n // Intentionally pass null as the encoding here since the files may be binary\n auxiliaryFiles:\n (await getSnapFiles(basePath, auxiliaryFilePaths, null)) ?? [],\n localizationFiles,\n };\n\n const validatorResults = await runValidators(\n snapFiles,\n Object.values(defaultValidators),\n { exports, handlerEndowments },\n );\n\n let manifestResults: CheckManifestResult = {\n updated: false,\n files: validatorResults.files,\n reports: validatorResults.reports,\n };\n\n if (updateAndWriteManifest && hasFixes(manifestResults, watchMode)) {\n const fixedResults = await runFixes(validatorResults, undefined, watchMode);\n\n if (fixedResults.updated) {\n manifestResults = fixedResults;\n\n assert(manifestResults.files);\n\n try {\n await writeFileFn(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n manifestResults.files.manifest.toString(),\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(\n `Failed to update \"snap.manifest.json\": ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n return manifestResults;\n}\n\n/**\n * Run the algorithm for automatically fixing errors in manifest.\n *\n * The algorithm updates the manifest by fixing all fixable problems,\n * and then run validation again to check if the new manifest is now correct.\n * If not correct, the algorithm will use the manifest from previous iteration\n * and try again `MAX_ATTEMPTS` times to update it before bailing and\n * resulting in failure.\n *\n * @param results - Results of the initial run of validation.\n * @param rules - Optional list of rules to run the fixes with.\n * @param errorsOnly - Whether to only run fixes for errors, not warnings.\n * @returns The updated manifest and whether it was updated.\n */\nexport async function runFixes(\n results: ValidatorResults,\n rules?: ValidatorMeta[],\n errorsOnly = false,\n): Promise<CheckManifestResult> {\n let shouldRunFixes = true;\n const MAX_ATTEMPTS = 10;\n\n assert(results.files);\n\n let fixResults: ValidatorResults = results;\n assert(fixResults.files);\n fixResults.files.manifest = fixResults.files.manifest.clone();\n\n const mergedReports: ValidatorReport[] = deepClone(fixResults.reports);\n\n for (\n let attempts = 1;\n shouldRunFixes && attempts <= MAX_ATTEMPTS;\n attempts++\n ) {\n assert(fixResults.files);\n\n let manifest = fixResults.files.manifest.result;\n\n const fixable = fixResults.reports.filter((report) =>\n isReportFixable(report, errorsOnly),\n );\n\n for (const report of fixable) {\n assert(report.fix);\n ({ manifest } = await report.fix({ manifest }));\n }\n\n fixResults.files.manifest.value = `${JSON.stringify(\n getWritableManifest(manifest),\n null,\n 2,\n )}\\n`;\n fixResults.files.manifest.result = manifest;\n\n fixResults = await runValidators(fixResults.files, rules);\n shouldRunFixes = hasFixes(fixResults, errorsOnly);\n\n fixResults.reports\n .filter(\n (report) =>\n !mergedReports.some((mergedReport) => mergedReport.id === report.id),\n )\n .forEach((report) => mergedReports.push(report));\n }\n\n const allReports: (CheckManifestReport & ValidatorReport)[] =\n deepClone(mergedReports);\n\n // Was fixed\n if (!shouldRunFixes) {\n for (const report of allReports) {\n if (report.fix) {\n report.wasFixed = true;\n delete report.fix;\n }\n }\n\n return {\n files: fixResults.files,\n updated: true,\n reports: allReports,\n };\n }\n\n for (const report of allReports) {\n delete report.fix;\n }\n\n return {\n files: results.files,\n updated: false,\n reports: allReports,\n };\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(\n `Failed to read snap bundle file: ${getErrorMessage(error)}`,\n );\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read snap icon file: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Get an array of paths from an unvalidated Snap manifest.\n *\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param selector - A function that returns the paths to the files.\n * @returns The paths to the files, if any.\n */\nexport function getSnapFilePaths(\n manifest: Json,\n selector: (manifest: Partial<SnapManifest>) => string[] | undefined,\n) {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const snapManifest = manifest as Partial<SnapManifest>;\n const paths = selector(snapManifest);\n\n if (!Array.isArray(paths)) {\n return undefined;\n }\n\n return paths;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the files with the\n * given paths and read them.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param paths - The paths to the files.\n * @param encoding - An optional encoding to pass down to readVirtualFile.\n * @returns A list of auxiliary files and their contents, if any.\n */\nexport async function getSnapFiles(\n basePath: string,\n paths: string[] | undefined,\n encoding: BufferEncoding | null = 'utf8',\n): Promise<VirtualFile[] | undefined> {\n if (!paths) {\n return undefined;\n }\n\n try {\n return await Promise.all(\n paths.map(async (filePath) =>\n readVirtualFile(pathUtils.join(basePath, filePath), encoding),\n ),\n );\n } catch (error) {\n throw new Error(`Failed to read snap files: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAsD;AAEtD,2CAAmD;AACnD,0DAAkC;AAClC,2BAAoC;AACpC,6CAA0C;AAI1C,+CAAuE;AAEvE,0EAAkD;AAClD,kDAA0C;AAC1C,kCAAqC;AACrC,sCAAoC;AAOpC,wCAA4C;AAC5C,mDAAoE;AAEpE,M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AQ,CAAC,CAC9D,CACF,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,8BAA8B,IAAA,2BAAe,EAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAC1E,CAAC;AACH,CAAC;AAlBD,oCAkBC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,QAAmC;IAEnC,MAAM,EAAE,UAAU,EAAE,GAAG,SAAS,EAAE,GAAG,QAAQ,CAAC;IAE9C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CACtB,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,SAAS,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,SAAS,CAC5B,CAAC;IAE5B,OAAO,IAAI;SACR,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,CAAC,CAAC,CAAC;SAC/D,MAAM,CACL,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QAChB,GAAG,MAAM;QACT,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;KACrB,CAAC,EACF,EAAE,CACH,CAAC;AACN,CAAC;AAlBD,kDAkBC","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\nimport { assert, isObject } from '@metamask/utils';\nimport deepmerge from 'deepmerge';\nimport { promises as fs } from 'fs';\nimport pathUtils, { dirname } from 'path';\n\nimport type { SnapManifest } from './validation';\nimport type { ValidatorResults } from './validator';\nimport { hasFixes, isReportFixable, runValidators } from './validator';\nimport type { ValidatorMeta, ValidatorReport } from './validator-types';\nimport * as defaultValidators from './validators';\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { parseJson } from '../json';\nimport type {\n DeepPartial,\n ExtendableSnapFiles,\n UnvalidatedExtendableManifest,\n UnvalidatedSnapFiles,\n} from '../types';\nimport { NpmSnapFileNames } from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file/node';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n $schema: 1,\n extends: 2,\n version: 3,\n description: 4,\n proposedName: 5,\n repository: 6,\n source: 7,\n initialConnections: 8,\n initialPermissions: 9,\n platformVersion: 10,\n manifestVersion: 11,\n};\n\ntype MergedManifest<Type> =\n Type extends DeepPartial<SnapManifest> ? SnapManifest : Json;\n\n/**\n * Merge two Snap manifests, with the extended manifest taking precedence\n * over the base manifest.\n *\n * @param mainManifest - The main manifest.\n * @param extendedManifest - The extended manifest.\n * @returns The merged manifest.\n */\nfunction mergeManifests<Type>(\n mainManifest: Type,\n extendedManifest?: Type,\n): MergedManifest<Type> {\n if (!extendedManifest) {\n return mainManifest as MergedManifest<Type>;\n }\n\n assert(isObject(mainManifest));\n assert(isObject(extendedManifest));\n\n const mergedManifest = deepmerge(extendedManifest, mainManifest);\n delete mergedManifest.extends;\n\n return getWritableManifest(mergedManifest) as MergedManifest<Type>;\n}\n\n/**\n * Load a manifest and its extended manifest if it has one.\n *\n * Note: This function does not validate the manifests.\n *\n * @param manifestPath - The path to the manifest file.\n * @param files - A set of already loaded manifest file paths to prevent\n * circular dependencies.\n * @param root - Whether this is the root manifest being loaded. Used for\n * recursive calls, and should not be set by callers.\n * @returns The base and extended manifests.\n */\nexport async function loadManifest(\n manifestPath: string,\n files = new Set<string>(),\n root = true,\n): Promise<UnvalidatedExtendableManifest> {\n assert(\n pathUtils.isAbsolute(manifestPath),\n 'The `loadManifest` function must be called with an absolute path.',\n );\n\n if (files.has(manifestPath)) {\n throw new Error(\n `Failed to load Snap manifest: Circular dependency detected when loading \"${manifestPath}\".`,\n );\n }\n\n const mainManifest = await readJsonFile(manifestPath);\n files.add(manifestPath);\n\n if (!isObject(mainManifest.result)) {\n throw new Error(\n `Failed to load Snap manifest: The Snap manifest file at \"${manifestPath}\" must contain a JSON object.`,\n );\n }\n\n if (\n mainManifest.result.extends &&\n typeof mainManifest.result.extends === 'string'\n ) {\n const fileName = pathUtils.basename(manifestPath);\n if (root && fileName === 'snap.manifest.json') {\n throw new Error(\n `Failed to load Snap manifest: The Snap manifest file at \"snap.manifest.json\" cannot extend another manifest.`,\n );\n }\n\n const extendedManifestPath = pathUtils.resolve(\n pathUtils.dirname(manifestPath),\n mainManifest.result.extends,\n );\n\n const extendedManifest = await loadManifest(\n extendedManifestPath,\n files,\n false,\n );\n\n return {\n mainManifest,\n extendedManifest: extendedManifest.mergedManifest,\n mergedManifest: mergeManifests(\n mainManifest.result,\n extendedManifest.mergedManifest,\n ),\n files,\n };\n }\n\n return {\n mainManifest,\n mergedManifest: mainManifest.result,\n files,\n };\n}\n\nexport type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {\n wasFixed?: boolean;\n};\n\n/**\n * The options for the `checkManifest` function.\n */\nexport type CheckManifestOptions = {\n /**\n * Whether to auto-magically try to fix errors and then write the manifest to\n * disk.\n */\n updateAndWriteManifest?: boolean;\n\n /**\n * The source code of the Snap.\n */\n sourceCode?: string;\n\n /**\n * The function to use to write the manifest to disk.\n */\n writeFileFn?: WriteFileFunction;\n\n /**\n * The exports detected by evaluating the bundle. This may be used by one or\n * more validators to determine whether the Snap is valid.\n */\n exports?: string[];\n\n /**\n * An object containing the names of the handlers and their respective\n * permission name. This must be provided to avoid circular dependencies\n * between `@metamask/snaps-utils` and `@metamask/snaps-rpc-methods`.\n */\n handlerEndowments?: Record<string, string | null>;\n\n /**\n * Whether the compiler is running in watch mode. This is used to determine\n * whether to fix warnings or errors only.\n */\n watchMode?: boolean;\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was written and updated.\n */\nexport type CheckManifestResult = {\n files?: ExtendableSnapFiles;\n updated: boolean;\n reports: CheckManifestReport[];\n};\n\nexport type WriteFileFunction = (path: string, data: string) => Promise<void>;\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param manifestPath - The path to the manifest file.\n * @param options - Additional options for the function.\n * @param options.sourceCode - The source code of the Snap.\n * @param options.writeFileFn - The function to use to write the manifest to\n * disk.\n * @param options.updateAndWriteManifest - Whether to auto-magically try to fix\n * errors and then write the manifest to disk.\n * @param options.exports - The exports detected by evaluating the bundle. This\n * may be used by one or more validators to determine whether the Snap is valid.\n * @param options.handlerEndowments - An object containing the names of the\n * handlers and their respective permission name. This must be provided to avoid\n * circular dependencies between `@metamask/snaps-utils` and\n * `@metamask/snaps-rpc-methods`.\n * @param options.watchMode - Whether the compiler is running in watch mode.\n * This is used to determine whether to fix warnings or errors only.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n manifestPath: string,\n {\n updateAndWriteManifest = true,\n sourceCode,\n writeFileFn = fs.writeFile,\n exports,\n handlerEndowments,\n watchMode = false,\n }: CheckManifestOptions = {},\n): Promise<CheckManifestResult> {\n const basePath = dirname(manifestPath);\n const extendableManifest = await loadManifest(manifestPath);\n const unvalidatedManifest = extendableManifest.mergedManifest;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const auxiliaryFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.files,\n );\n\n const localizationFilePaths = getSnapFilePaths(\n unvalidatedManifest,\n (manifest) => manifest?.source?.locales,\n );\n const localizationFiles =\n (await getSnapFiles(basePath, localizationFilePaths)) ?? [];\n for (const localization of localizationFiles) {\n try {\n localization.result = parseJson(localization.toString());\n } catch (error) {\n assert(error instanceof SyntaxError, error);\n throw new Error(\n `Failed to parse localization file \"${localization.path}\" as JSON.`,\n );\n }\n }\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: extendableManifest,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n // Intentionally pass null as the encoding here since the files may be binary\n auxiliaryFiles:\n (await getSnapFiles(basePath, auxiliaryFilePaths, null)) ?? [],\n localizationFiles,\n };\n\n const validatorResults = await runValidators(\n snapFiles,\n Object.values(defaultValidators),\n { exports, handlerEndowments },\n );\n\n let manifestResults: CheckManifestResult = {\n updated: false,\n files: validatorResults.files,\n reports: validatorResults.reports,\n };\n\n if (updateAndWriteManifest && hasFixes(manifestResults, watchMode)) {\n const fixedResults = await runFixes(validatorResults, undefined, watchMode);\n\n if (fixedResults.updated) {\n manifestResults = fixedResults;\n\n assert(manifestResults.files);\n\n try {\n await writeFileFn(\n manifestPath,\n manifestResults.files.manifest.mainManifest.toString(),\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(\n `Failed to update \"snap.manifest.json\": ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n return manifestResults;\n}\n\n/**\n * Run the algorithm for automatically fixing errors in manifest.\n *\n * The algorithm updates the manifest by fixing all fixable problems,\n * and then run validation again to check if the new manifest is now correct.\n * If not correct, the algorithm will use the manifest from previous iteration\n * and try again `MAX_ATTEMPTS` times to update it before bailing and\n * resulting in failure.\n *\n * @param results - Results of the initial run of validation.\n * @param rules - Optional list of rules to run the fixes with.\n * @param errorsOnly - Whether to only run fixes for errors, not warnings.\n * @returns The updated manifest and whether it was updated.\n */\nexport async function runFixes(\n results: ValidatorResults,\n rules?: ValidatorMeta[],\n errorsOnly = false,\n): Promise<CheckManifestResult> {\n let shouldRunFixes = true;\n const MAX_ATTEMPTS = 10;\n\n assert(results.files);\n\n let fixResults: ValidatorResults = results;\n assert(fixResults.files);\n\n fixResults.files.manifest.mainManifest =\n fixResults.files.manifest.mainManifest.clone();\n\n const mergedReports: ValidatorReport[] = deepClone(fixResults.reports);\n\n for (\n let attempts = 1;\n shouldRunFixes && attempts <= MAX_ATTEMPTS;\n attempts++\n ) {\n assert(fixResults.files);\n\n const fixable = fixResults.reports.filter((report) =>\n isReportFixable(report, errorsOnly),\n );\n\n let { manifest } = fixResults.files;\n for (const report of fixable) {\n assert(report.fix);\n ({ manifest } = await report.fix({ manifest }));\n\n manifest.mergedManifest = mergeManifests(\n manifest.mainManifest.result,\n manifest.extendedManifest,\n );\n }\n\n // The `mainManifest` is always the first manifest loaded, and is the one\n // that should be updated with fixes. Any manifests that the main manifest\n // extends will not be updated.\n // We can revisit this in the future if we want to support fixing extended\n // manifests as well, but it adds complexity, as we'd need to track which\n // fixes apply to which manifest.\n fixResults.files.manifest.mainManifest.value = `${JSON.stringify(\n getWritableManifest(manifest.mainManifest.result),\n null,\n 2,\n )}\\n`;\n fixResults.files.manifest = manifest;\n fixResults.files.manifest.mergedManifest = mergeManifests(\n fixResults.files.manifest.mainManifest.result,\n fixResults.files.manifest.extendedManifest,\n );\n\n fixResults = await runValidators(fixResults.files, rules);\n shouldRunFixes = hasFixes(fixResults, errorsOnly);\n\n fixResults.reports\n .filter(\n (report) =>\n !mergedReports.some((mergedReport) => mergedReport.id === report.id),\n )\n .forEach((report) => mergedReports.push(report));\n }\n\n const allReports: (CheckManifestReport & ValidatorReport)[] =\n deepClone(mergedReports);\n\n // Was fixed\n if (!shouldRunFixes) {\n for (const report of allReports) {\n if (report.fix) {\n report.wasFixed = true;\n delete report.fix;\n }\n }\n\n return {\n files: fixResults.files,\n updated: true,\n reports: allReports,\n };\n }\n\n for (const report of allReports) {\n delete report.fix;\n }\n\n return {\n files: results.files,\n updated: false,\n reports: allReports,\n };\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(\n `Failed to read snap bundle file: ${getErrorMessage(error)}`,\n );\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read snap icon file: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Get an array of paths from an unvalidated Snap manifest.\n *\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param selector - A function that returns the paths to the files.\n * @returns The paths to the files, if any.\n */\nexport function getSnapFilePaths(\n manifest: Json,\n selector: (manifest: Partial<SnapManifest>) => string[] | undefined,\n) {\n if (!isObject(manifest)) {\n return undefined;\n }\n\n const snapManifest = manifest as Partial<SnapManifest>;\n const paths = selector(snapManifest);\n\n if (!Array.isArray(paths)) {\n return undefined;\n }\n\n return paths;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the files with the\n * given paths and read them.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param paths - The paths to the files.\n * @param encoding - An optional encoding to pass down to readVirtualFile.\n * @returns A list of auxiliary files and their contents, if any.\n */\nexport async function getSnapFiles(\n basePath: string,\n paths: string[] | undefined,\n encoding: BufferEncoding | null = 'utf8',\n): Promise<VirtualFile[] | undefined> {\n if (!paths) {\n return undefined;\n }\n\n try {\n return await Promise.all(\n paths.map(async (filePath) =>\n readVirtualFile(pathUtils.join(basePath, filePath), encoding),\n ),\n );\n } catch (error) {\n throw new Error(`Failed to read snap files: ${getErrorMessage(error)}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(\n manifest: DeepPartial<SnapManifest>,\n): DeepPartial<SnapManifest> {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n return keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<DeepPartial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n}\n"]}
|
|
@@ -3,8 +3,21 @@ import type { Json } from "@metamask/utils";
|
|
|
3
3
|
import type { SnapManifest } from "./validation.cjs";
|
|
4
4
|
import type { ValidatorResults } from "./validator.cjs";
|
|
5
5
|
import type { ValidatorMeta, ValidatorReport } from "./validator-types.cjs";
|
|
6
|
-
import type {
|
|
6
|
+
import type { DeepPartial, ExtendableSnapFiles, UnvalidatedExtendableManifest } from "../types.cjs";
|
|
7
7
|
import { VirtualFile } from "../virtual-file/node.cjs";
|
|
8
|
+
/**
|
|
9
|
+
* Load a manifest and its extended manifest if it has one.
|
|
10
|
+
*
|
|
11
|
+
* Note: This function does not validate the manifests.
|
|
12
|
+
*
|
|
13
|
+
* @param manifestPath - The path to the manifest file.
|
|
14
|
+
* @param files - A set of already loaded manifest file paths to prevent
|
|
15
|
+
* circular dependencies.
|
|
16
|
+
* @param root - Whether this is the root manifest being loaded. Used for
|
|
17
|
+
* recursive calls, and should not be set by callers.
|
|
18
|
+
* @returns The base and extended manifests.
|
|
19
|
+
*/
|
|
20
|
+
export declare function loadManifest(manifestPath: string, files?: Set<string>, root?: boolean): Promise<UnvalidatedExtendableManifest>;
|
|
8
21
|
export type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {
|
|
9
22
|
wasFixed?: boolean;
|
|
10
23
|
};
|
|
@@ -49,7 +62,7 @@ export type CheckManifestOptions = {
|
|
|
49
62
|
* @property updated - Whether the manifest was written and updated.
|
|
50
63
|
*/
|
|
51
64
|
export type CheckManifestResult = {
|
|
52
|
-
files?:
|
|
65
|
+
files?: ExtendableSnapFiles;
|
|
53
66
|
updated: boolean;
|
|
54
67
|
reports: CheckManifestReport[];
|
|
55
68
|
};
|
|
@@ -59,7 +72,7 @@ export type WriteFileFunction = (path: string, data: string) => Promise<void>;
|
|
|
59
72
|
* the fixed version to disk if `writeManifest` is true. Throws if validation
|
|
60
73
|
* fails.
|
|
61
74
|
*
|
|
62
|
-
* @param
|
|
75
|
+
* @param manifestPath - The path to the manifest file.
|
|
63
76
|
* @param options - Additional options for the function.
|
|
64
77
|
* @param options.sourceCode - The source code of the Snap.
|
|
65
78
|
* @param options.writeFileFn - The function to use to write the manifest to
|
|
@@ -77,7 +90,7 @@ export type WriteFileFunction = (path: string, data: string) => Promise<void>;
|
|
|
77
90
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
78
91
|
* were encountered during processing of the manifest files.
|
|
79
92
|
*/
|
|
80
|
-
export declare function checkManifest(
|
|
93
|
+
export declare function checkManifest(manifestPath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, watchMode, }?: CheckManifestOptions): Promise<CheckManifestResult>;
|
|
81
94
|
/**
|
|
82
95
|
* Run the algorithm for automatically fixing errors in manifest.
|
|
83
96
|
*
|
|
@@ -137,5 +150,5 @@ export declare function getSnapFiles(basePath: string, paths: string[] | undefin
|
|
|
137
150
|
* @param manifest - The manifest to sort and modify.
|
|
138
151
|
* @returns The disk-ready manifest.
|
|
139
152
|
*/
|
|
140
|
-
export declare function getWritableManifest(manifest: SnapManifest): SnapManifest
|
|
153
|
+
export declare function getWritableManifest(manifest: DeepPartial<SnapManifest>): DeepPartial<SnapManifest>;
|
|
141
154
|
//# sourceMappingURL=manifest.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAM5C,OAAO,KAAK,EAAE,YAAY,EAAE,yBAAqB;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,wBAAoB;AAEpD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,8BAA0B;AAKxE,OAAO,KAAK,EACV,WAAW,EACX,mBAAmB,EACnB,6BAA6B,EAE9B,qBAAiB;AAElB,OAAO,EAAmB,WAAW,EAAE,iCAA6B;AA4CpE;;;;;;;;;;;GAWG;AACH,wBAAsB,YAAY,CAChC,YAAY,EAAE,MAAM,EACpB,KAAK,cAAoB,EACzB,IAAI,UAAO,GACV,OAAO,CAAC,6BAA6B,CAAC,CA2DxC;AAED,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,eAAe,EAAE,KAAK,CAAC,GAAG;IAC/D,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;OAGG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;IAEjC;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,WAAW,CAAC,EAAE,iBAAiB,CA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,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,IAAI,EACd,QAAQ,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,YAAY,CAAC,KAAK,MAAM,EAAE,GAAG,SAAS,wBAcpE;AAED;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAChC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EAAE,GAAG,SAAS,EAC3B,QAAQ,GAAE,cAAc,GAAG,IAAa,GACvC,OAAO,CAAC,WAAW,EAAE,GAAG,SAAS,CAAC,CAcpC;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,WAAW,CAAC,YAAY,CAAC,GAClC,WAAW,CAAC,YAAY,CAAC,CAgB3B"}
|
|
@@ -3,8 +3,21 @@ import type { Json } from "@metamask/utils";
|
|
|
3
3
|
import type { SnapManifest } from "./validation.mjs";
|
|
4
4
|
import type { ValidatorResults } from "./validator.mjs";
|
|
5
5
|
import type { ValidatorMeta, ValidatorReport } from "./validator-types.mjs";
|
|
6
|
-
import type {
|
|
6
|
+
import type { DeepPartial, ExtendableSnapFiles, UnvalidatedExtendableManifest } from "../types.mjs";
|
|
7
7
|
import { VirtualFile } from "../virtual-file/node.mjs";
|
|
8
|
+
/**
|
|
9
|
+
* Load a manifest and its extended manifest if it has one.
|
|
10
|
+
*
|
|
11
|
+
* Note: This function does not validate the manifests.
|
|
12
|
+
*
|
|
13
|
+
* @param manifestPath - The path to the manifest file.
|
|
14
|
+
* @param files - A set of already loaded manifest file paths to prevent
|
|
15
|
+
* circular dependencies.
|
|
16
|
+
* @param root - Whether this is the root manifest being loaded. Used for
|
|
17
|
+
* recursive calls, and should not be set by callers.
|
|
18
|
+
* @returns The base and extended manifests.
|
|
19
|
+
*/
|
|
20
|
+
export declare function loadManifest(manifestPath: string, files?: Set<string>, root?: boolean): Promise<UnvalidatedExtendableManifest>;
|
|
8
21
|
export type CheckManifestReport = Omit<ValidatorReport, 'fix'> & {
|
|
9
22
|
wasFixed?: boolean;
|
|
10
23
|
};
|
|
@@ -49,7 +62,7 @@ export type CheckManifestOptions = {
|
|
|
49
62
|
* @property updated - Whether the manifest was written and updated.
|
|
50
63
|
*/
|
|
51
64
|
export type CheckManifestResult = {
|
|
52
|
-
files?:
|
|
65
|
+
files?: ExtendableSnapFiles;
|
|
53
66
|
updated: boolean;
|
|
54
67
|
reports: CheckManifestReport[];
|
|
55
68
|
};
|
|
@@ -59,7 +72,7 @@ export type WriteFileFunction = (path: string, data: string) => Promise<void>;
|
|
|
59
72
|
* the fixed version to disk if `writeManifest` is true. Throws if validation
|
|
60
73
|
* fails.
|
|
61
74
|
*
|
|
62
|
-
* @param
|
|
75
|
+
* @param manifestPath - The path to the manifest file.
|
|
63
76
|
* @param options - Additional options for the function.
|
|
64
77
|
* @param options.sourceCode - The source code of the Snap.
|
|
65
78
|
* @param options.writeFileFn - The function to use to write the manifest to
|
|
@@ -77,7 +90,7 @@ export type WriteFileFunction = (path: string, data: string) => Promise<void>;
|
|
|
77
90
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
78
91
|
* were encountered during processing of the manifest files.
|
|
79
92
|
*/
|
|
80
|
-
export declare function checkManifest(
|
|
93
|
+
export declare function checkManifest(manifestPath: string, { updateAndWriteManifest, sourceCode, writeFileFn, exports, handlerEndowments, watchMode, }?: CheckManifestOptions): Promise<CheckManifestResult>;
|
|
81
94
|
/**
|
|
82
95
|
* Run the algorithm for automatically fixing errors in manifest.
|
|
83
96
|
*
|
|
@@ -137,5 +150,5 @@ export declare function getSnapFiles(basePath: string, paths: string[] | undefin
|
|
|
137
150
|
* @param manifest - The manifest to sort and modify.
|
|
138
151
|
* @returns The disk-ready manifest.
|
|
139
152
|
*/
|
|
140
|
-
export declare function getWritableManifest(manifest: SnapManifest): SnapManifest
|
|
153
|
+
export declare function getWritableManifest(manifest: DeepPartial<SnapManifest>): DeepPartial<SnapManifest>;
|
|
141
154
|
//# sourceMappingURL=manifest.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.mts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"manifest.d.mts","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAM5C,OAAO,KAAK,EAAE,YAAY,EAAE,yBAAqB;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,wBAAoB;AAEpD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,8BAA0B;AAKxE,OAAO,KAAK,EACV,WAAW,EACX,mBAAmB,EACnB,6BAA6B,EAE9B,qBAAiB;AAElB,OAAO,EAAmB,WAAW,EAAE,iCAA6B;AA4CpE;;;;;;;;;;;GAWG;AACH,wBAAsB,YAAY,CAChC,YAAY,EAAE,MAAM,EACpB,KAAK,cAAoB,EACzB,IAAI,UAAO,GACV,OAAO,CAAC,6BAA6B,CAAC,CA2DxC;AAED,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,eAAe,EAAE,KAAK,CAAC,GAAG;IAC/D,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;OAGG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;IAEjC;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAEhC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,CAAC;IAElD;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF;;;;;GAKG;A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