@metamask/snaps-cli 0.18.0 → 0.19.1

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.
Files changed (51) hide show
  1. package/dist/builders.d.ts +0 -1
  2. package/dist/builders.js +0 -6
  3. package/dist/builders.js.map +1 -1
  4. package/dist/cmds/build/buildHandler.js +7 -6
  5. package/dist/cmds/build/buildHandler.js.map +1 -1
  6. package/dist/cmds/build/bundle.js +3 -2
  7. package/dist/cmds/build/bundle.js.map +1 -1
  8. package/dist/cmds/build/index.js +0 -1
  9. package/dist/cmds/build/index.js.map +1 -1
  10. package/dist/cmds/eval/evalHandler.d.ts +1 -1
  11. package/dist/cmds/eval/evalHandler.js +8 -9
  12. package/dist/cmds/eval/evalHandler.js.map +1 -1
  13. package/dist/cmds/eval/index.js +1 -1
  14. package/dist/cmds/eval/index.js.map +1 -1
  15. package/dist/cmds/init/initHandler.js +8 -9
  16. package/dist/cmds/init/initHandler.js.map +1 -1
  17. package/dist/cmds/init/initUtils.d.ts +1 -1
  18. package/dist/cmds/init/initUtils.js +12 -12
  19. package/dist/cmds/init/initUtils.js.map +1 -1
  20. package/dist/cmds/manifest/manifestHandler.d.ts +1 -10
  21. package/dist/cmds/manifest/manifestHandler.js +23 -191
  22. package/dist/cmds/manifest/manifestHandler.js.map +1 -1
  23. package/dist/cmds/serve/serveHandler.js +6 -2
  24. package/dist/cmds/serve/serveHandler.js.map +1 -1
  25. package/dist/cmds/watch/index.js +0 -1
  26. package/dist/cmds/watch/index.js.map +1 -1
  27. package/dist/cmds/watch/watchHandler.js +6 -5
  28. package/dist/cmds/watch/watchHandler.js.map +1 -1
  29. package/dist/tsconfig.json +6 -1
  30. package/dist/utils/index.d.ts +0 -2
  31. package/dist/utils/index.js +0 -2
  32. package/dist/utils/index.js.map +1 -1
  33. package/dist/utils/misc.d.ts +0 -1
  34. package/dist/utils/misc.js +1 -3
  35. package/dist/utils/misc.js.map +1 -1
  36. package/package.json +4 -5
  37. package/dist/cmds/eval/eval-worker.d.ts +0 -1
  38. package/dist/cmds/eval/eval-worker.js +0 -33
  39. package/dist/cmds/eval/eval-worker.js.map +0 -1
  40. package/dist/cmds/eval/mock.d.ts +0 -14
  41. package/dist/cmds/eval/mock.js +0 -98
  42. package/dist/cmds/eval/mock.js.map +0 -1
  43. package/dist/cmds/eval/workerEval.d.ts +0 -8
  44. package/dist/cmds/eval/workerEval.js +0 -37
  45. package/dist/cmds/eval/workerEval.js.map +0 -1
  46. package/dist/utils/fs.d.ts +0 -24
  47. package/dist/utils/fs.js +0 -66
  48. package/dist/utils/fs.js.map +0 -1
  49. package/dist/utils/validate-fs.d.ts +0 -35
  50. package/dist/utils/validate-fs.js +0 -69
  51. package/dist/utils/validate-fs.js.map +0 -1
@@ -1,98 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.generateMockEndowments = exports.isConstructor = exports.ALL_APIS = void 0;
7
- const events_1 = __importDefault(require("events"));
8
- const crypto_1 = __importDefault(require("crypto"));
9
- const snap_controllers_1 = require("@metamask/snap-controllers");
10
- const NETWORK_APIS = ['fetch', 'WebSocket'];
11
- exports.ALL_APIS = [...snap_controllers_1.DEFAULT_ENDOWMENTS, ...NETWORK_APIS];
12
- /**
13
- * Get a mock snap provider, that always returns `true` for requests.
14
- *
15
- * @returns A mocked snap provider.
16
- */
17
- function getMockSnapProvider() {
18
- const mockProvider = new events_1.default();
19
- mockProvider.request = async () => true;
20
- return mockProvider;
21
- }
22
- /**
23
- * Check if a value is a constructor.
24
- *
25
- * @param value - The value to check.
26
- * @returns `true` if the value is a constructor, or `false` otherwise.
27
- */
28
- const isConstructor = (value) => { var _a, _b; return Boolean(typeof ((_b = (_a = value === null || value === void 0 ? void 0 : value.prototype) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name) === 'string'); };
29
- exports.isConstructor = isConstructor;
30
- /**
31
- * A function that always returns `true`.
32
- *
33
- * @returns `true`.
34
- */
35
- const mockFunction = () => true;
36
- class MockClass {
37
- }
38
- const handler = {
39
- construct(Target, args) {
40
- return new Proxy(new Target(...args), handler);
41
- },
42
- get(_target, _prop) {
43
- return mockFunction;
44
- },
45
- };
46
- /**
47
- * Generate a mock class for a given value. The value is wrapped in a Proxy, and
48
- * all methods are replaced with a mock function.
49
- *
50
- * @param value - The value to mock.
51
- * @returns A mock class.
52
- */
53
- const generateMockClass = (value) => {
54
- return new Proxy(value, handler);
55
- };
56
- // Things not currently auto-mocked because of NodeJS, by adding them here we have types for them and can use that to generate mocks if needed
57
- const mockWindow = {
58
- WebSocket: MockClass,
59
- crypto: crypto_1.default,
60
- SubtleCrypto: MockClass,
61
- };
62
- /**
63
- * Generate a mock endowment for a certain class or function on the `globalThis`
64
- * object.
65
- *
66
- * @param key - The key to generate the mock endowment for.
67
- * @returns A mocked class or function. If the key is part of the default
68
- * endowments, the original value is returned.
69
- */
70
- const generateMockEndowment = (key) => {
71
- const globalValue = globalThis[key];
72
- // Default exposed APIs don't need to be mocked
73
- if (globalValue && snap_controllers_1.DEFAULT_ENDOWMENTS.includes(key)) {
74
- return globalValue;
75
- }
76
- // Fall back to mockWindow for certain APIs not exposed in global in Node.JS
77
- const globalOrMocked = globalValue !== null && globalValue !== void 0 ? globalValue : mockWindow[key];
78
- const type = typeof globalOrMocked;
79
- const isFunction = type === 'function';
80
- if (isFunction && (0, exports.isConstructor)(globalOrMocked)) {
81
- return generateMockClass(globalOrMocked);
82
- }
83
- else if (isFunction || !globalOrMocked) {
84
- // Fall back to function mock for now
85
- return mockFunction;
86
- }
87
- return globalOrMocked;
88
- };
89
- /**
90
- * Generate mock endowments for all the APIs as defined in {@link ALL_APIS}.
91
- *
92
- * @returns A map of endowments.
93
- */
94
- const generateMockEndowments = () => {
95
- return exports.ALL_APIS.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), { [cur]: generateMockEndowment(cur) })), { wallet: getMockSnapProvider() });
96
- };
97
- exports.generateMockEndowments = generateMockEndowments;
98
- //# sourceMappingURL=mock.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mock.js","sourceRoot":"","sources":["../../../src/cmds/eval/mock.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAkC;AAClC,oDAA4B;AAC5B,iEAAgE;AAEhE,MAAM,YAAY,GAAG,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;AAE/B,QAAA,QAAQ,GAAa,CAAC,GAAG,qCAAkB,EAAE,GAAG,YAAY,CAAC,CAAC;AAM3E;;;;GAIG;AACH,SAAS,mBAAmB;IAC1B,MAAM,YAAY,GAAG,IAAI,gBAAY,EAA+B,CAAC;IACrE,YAAY,CAAC,OAAO,GAAG,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC;IACxC,OAAO,YAAgC,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACI,MAAM,aAAa,GAAG,CAAC,KAAU,EAAE,EAAE,eAC1C,OAAA,OAAO,CAAC,OAAO,CAAA,MAAA,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,SAAS,0CAAE,WAAW,0CAAE,IAAI,CAAA,KAAK,QAAQ,CAAC,CAAA,EAAA,CAAC;AADtD,QAAA,aAAa,iBACyC;AAEnE;;;;GAIG;AACH,MAAM,YAAY,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC;AAChC,MAAM,SAAS;CAAG;AAElB,MAAM,OAAO,GAAG;IACd,SAAS,CAAC,MAAW,EAAE,IAAW;QAChC,OAAO,IAAI,KAAK,CAAC,IAAI,MAAM,CAAC,GAAG,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;IACD,GAAG,CAAC,OAAY,EAAE,KAAU;QAC1B,OAAO,YAAY,CAAC;IACtB,CAAC;CACF,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,iBAAiB,GAAG,CAAC,KAAU,EAAE,EAAE;IACvC,OAAO,IAAI,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,8IAA8I;AAC9I,MAAM,UAAU,GAAG;IACjB,SAAS,EAAE,SAAS;IACpB,MAAM,EAAN,gBAAM;IACN,YAAY,EAAE,SAAS;CACxB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,qBAAqB,GAAG,CAAC,GAAW,EAAE,EAAE;IAC5C,MAAM,WAAW,GAAI,UAAkB,CAAC,GAAG,CAAC,CAAC;IAE7C,+CAA+C;IAC/C,IAAI,WAAW,IAAI,qCAAkB,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QACnD,OAAO,WAAW,CAAC;KACpB;IAED,4EAA4E;IAC5E,MAAM,cAAc,GAAG,WAAW,aAAX,WAAW,cAAX,WAAW,GAAK,UAAkB,CAAC,GAAG,CAAC,CAAC;IAE/D,MAAM,IAAI,GAAG,OAAO,cAAc,CAAC;IACnC,MAAM,UAAU,GAAG,IAAI,KAAK,UAAU,CAAC;IACvC,IAAI,UAAU,IAAI,IAAA,qBAAa,EAAC,cAAc,CAAC,EAAE;QAC/C,OAAO,iBAAiB,CAAC,cAAc,CAAC,CAAC;KAC1C;SAAM,IAAI,UAAU,IAAI,CAAC,cAAc,EAAE;QACxC,qCAAqC;QACrC,OAAO,YAAY,CAAC;KACrB;IACD,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAEF;;;;GAIG;AACI,MAAM,sBAAsB,GAAG,GAAG,EAAE;IACzC,OAAO,gBAAQ,CAAC,MAAM,CACpB,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,iCAAM,GAAG,KAAE,CAAC,GAAG,CAAC,EAAE,qBAAqB,CAAC,GAAG,CAAC,IAAG,EAC7D,EAAE,MAAM,EAAE,mBAAmB,EAAE,EAAE,CAClC,CAAC;AACJ,CAAC,CAAC;AALW,QAAA,sBAAsB,0BAKjC","sourcesContent":["import EventEmitter from 'events';\nimport crypto from 'crypto';\nimport { DEFAULT_ENDOWMENTS } from '@metamask/snap-controllers';\n\nconst NETWORK_APIS = ['fetch', 'WebSocket'];\n\nexport const ALL_APIS: string[] = [...DEFAULT_ENDOWMENTS, ...NETWORK_APIS];\n\ntype MockSnapProvider = EventEmitter & {\n request: () => Promise<any>;\n};\n\n/**\n * Get a mock snap provider, that always returns `true` for requests.\n *\n * @returns A mocked snap provider.\n */\nfunction getMockSnapProvider(): MockSnapProvider {\n const mockProvider = new EventEmitter() as Partial<MockSnapProvider>;\n mockProvider.request = async () => true;\n return mockProvider as MockSnapProvider;\n}\n\n/**\n * Check if a value is a constructor.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nexport const isConstructor = (value: any) =>\n Boolean(typeof value?.prototype?.constructor?.name === 'string');\n\n/**\n * A function that always returns `true`.\n *\n * @returns `true`.\n */\nconst mockFunction = () => true;\nclass MockClass {}\n\nconst handler = {\n construct(Target: any, args: any[]): any {\n return new Proxy(new Target(...args), handler);\n },\n get(_target: any, _prop: any) {\n return mockFunction;\n },\n};\n\n/**\n * Generate a mock class for a given value. The value is wrapped in a Proxy, and\n * all methods are replaced with a mock function.\n *\n * @param value - The value to mock.\n * @returns A mock class.\n */\nconst generateMockClass = (value: any) => {\n return new Proxy(value, handler);\n};\n\n// Things not currently auto-mocked because of NodeJS, by adding them here we have types for them and can use that to generate mocks if needed\nconst mockWindow = {\n WebSocket: MockClass,\n crypto,\n SubtleCrypto: MockClass,\n};\n\n/**\n * Generate a mock endowment for a certain class or function on the `globalThis`\n * object.\n *\n * @param key - The key to generate the mock endowment for.\n * @returns A mocked class or function. If the key is part of the default\n * endowments, the original value is returned.\n */\nconst generateMockEndowment = (key: string) => {\n const globalValue = (globalThis as any)[key];\n\n // Default exposed APIs don't need to be mocked\n if (globalValue && DEFAULT_ENDOWMENTS.includes(key)) {\n return globalValue;\n }\n\n // Fall back to mockWindow for certain APIs not exposed in global in Node.JS\n const globalOrMocked = globalValue ?? (mockWindow as any)[key];\n\n const type = typeof globalOrMocked;\n const isFunction = type === 'function';\n if (isFunction && isConstructor(globalOrMocked)) {\n return generateMockClass(globalOrMocked);\n } else if (isFunction || !globalOrMocked) {\n // Fall back to function mock for now\n return mockFunction;\n }\n return globalOrMocked;\n};\n\n/**\n * Generate mock endowments for all the APIs as defined in {@link ALL_APIS}.\n *\n * @returns A map of endowments.\n */\nexport const generateMockEndowments = () => {\n return ALL_APIS.reduce<Record<string, any>>(\n (acc, cur) => ({ ...acc, [cur]: generateMockEndowment(cur) }),\n { wallet: getMockSnapProvider() },\n );\n};\n"]}
@@ -1,8 +0,0 @@
1
- /**
2
- * Spawn a new worker thread to run the provided bundle in.
3
- *
4
- * @param bundlePath - The path to the bundle to run.
5
- * @returns `null` if the worker ran successfully.
6
- * @throws If the worker failed to run successfully.
7
- */
8
- export declare function workerEval(bundlePath: string): Promise<null>;
@@ -1,37 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.workerEval = void 0;
7
- const child_process_1 = require("child_process");
8
- const path_1 = __importDefault(require("path"));
9
- /**
10
- * Spawn a new worker thread to run the provided bundle in.
11
- *
12
- * @param bundlePath - The path to the bundle to run.
13
- * @returns `null` if the worker ran successfully.
14
- * @throws If the worker failed to run successfully.
15
- */
16
- function workerEval(bundlePath) {
17
- return new Promise((resolve) => {
18
- (0, child_process_1.fork)(getEvalWorkerPath(), [bundlePath]).on('exit', (exitCode) => {
19
- if (exitCode === 0) {
20
- resolve(null);
21
- }
22
- else {
23
- throw new Error(`Worker exited abnormally! Code: ${exitCode}`);
24
- }
25
- });
26
- });
27
- }
28
- exports.workerEval = workerEval;
29
- /**
30
- * Get the path to the eval worker file.
31
- *
32
- * @returns The path to the eval worker file.
33
- */
34
- function getEvalWorkerPath() {
35
- return path_1.default.join(__dirname, 'eval-worker.js');
36
- }
37
- //# sourceMappingURL=workerEval.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"workerEval.js","sourceRoot":"","sources":["../../../src/cmds/eval/workerEval.ts"],"names":[],"mappings":";;;;;;AAAA,iDAAqC;AACrC,gDAA6B;AAE7B;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,UAAkB;IAC3C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,IAAA,oBAAI,EAAC,iBAAiB,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,QAAgB,EAAE,EAAE;YACtE,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,OAAO,CAAC,IAAI,CAAC,CAAC;aACf;iBAAM;gBACL,MAAM,IAAI,KAAK,CAAC,mCAAmC,QAAQ,EAAE,CAAC,CAAC;aAChE;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAVD,gCAUC;AAED;;;;GAIG;AACH,SAAS,iBAAiB;IACxB,OAAO,cAAS,CAAC,IAAI,CAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import { fork } from 'child_process';\nimport pathUtils from 'path';\n\n/**\n * Spawn a new worker thread to run the provided bundle in.\n *\n * @param bundlePath - The path to the bundle to run.\n * @returns `null` if the worker ran successfully.\n * @throws If the worker failed to run successfully.\n */\nexport function workerEval(bundlePath: string): Promise<null> {\n return new Promise((resolve) => {\n fork(getEvalWorkerPath(), [bundlePath]).on('exit', (exitCode: number) => {\n if (exitCode === 0) {\n resolve(null);\n } else {\n throw new Error(`Worker exited abnormally! Code: ${exitCode}`);\n }\n });\n });\n}\n\n/**\n * Get the path to the eval worker file.\n *\n * @returns The path to the eval worker file.\n */\nfunction getEvalWorkerPath(): string {\n return pathUtils.join(__dirname, 'eval-worker.js');\n}\n"]}
@@ -1,24 +0,0 @@
1
- import type { Json } from '@metamask/snap-controllers';
2
- /**
3
- * Checks whether the given path string resolves to an existing directory, and
4
- * optionally creates the directory if it doesn't exist.
5
- *
6
- * @param pathString - The path string to check.
7
- * @param createDir - Whether to create the directory if it doesn't exist.
8
- * @returns Whether the given path is an existing directory.
9
- */
10
- export declare function isDirectory(pathString: string, createDir: boolean): Promise<boolean>;
11
- /**
12
- * Checks whether the given path string resolves to an existing file.
13
- *
14
- * @param pathString - The path string to check.
15
- * @returns Whether the given path is an existing file.
16
- */
17
- export declare function isFile(pathString: string): Promise<boolean>;
18
- /**
19
- * Reads a `.json` file, parses its contents, and returns them.
20
- *
21
- * @param pathString - The path to the JSON file.
22
- * @returns The parsed contents of the JSON file.
23
- */
24
- export declare function readJsonFile(pathString: string): Promise<Json>;
package/dist/utils/fs.js DELETED
@@ -1,66 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readJsonFile = exports.isFile = exports.isDirectory = void 0;
4
- const fs_1 = require("fs");
5
- const misc_1 = require("./misc");
6
- /**
7
- * Checks whether the given path string resolves to an existing directory, and
8
- * optionally creates the directory if it doesn't exist.
9
- *
10
- * @param pathString - The path string to check.
11
- * @param createDir - Whether to create the directory if it doesn't exist.
12
- * @returns Whether the given path is an existing directory.
13
- */
14
- async function isDirectory(pathString, createDir) {
15
- try {
16
- const stats = await fs_1.promises.stat(pathString);
17
- return stats.isDirectory();
18
- }
19
- catch (error) {
20
- if (error.code === 'ENOENT') {
21
- if (!createDir) {
22
- return false;
23
- }
24
- try {
25
- await fs_1.promises.mkdir(pathString);
26
- return true;
27
- }
28
- catch (mkdirError) {
29
- (0, misc_1.logError)(`Directory '${pathString}' could not be created.`, mkdirError);
30
- throw mkdirError;
31
- }
32
- }
33
- return false;
34
- }
35
- }
36
- exports.isDirectory = isDirectory;
37
- /**
38
- * Checks whether the given path string resolves to an existing file.
39
- *
40
- * @param pathString - The path string to check.
41
- * @returns Whether the given path is an existing file.
42
- */
43
- async function isFile(pathString) {
44
- try {
45
- const stats = await fs_1.promises.stat(pathString);
46
- return stats.isFile();
47
- }
48
- catch (error) {
49
- return false;
50
- }
51
- }
52
- exports.isFile = isFile;
53
- /**
54
- * Reads a `.json` file, parses its contents, and returns them.
55
- *
56
- * @param pathString - The path to the JSON file.
57
- * @returns The parsed contents of the JSON file.
58
- */
59
- async function readJsonFile(pathString) {
60
- if (!pathString.endsWith('.json')) {
61
- throw new Error('The specified file must be a ".json" file.');
62
- }
63
- return JSON.parse(await fs_1.promises.readFile(pathString, 'utf8'));
64
- }
65
- exports.readJsonFile = readJsonFile;
66
- //# sourceMappingURL=fs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fs.js","sourceRoot":"","sources":["../../src/utils/fs.ts"],"names":[],"mappings":";;;AAAA,2BAAoC;AAEpC,iCAAkC;AAElC;;;;;;;GAOG;AACI,KAAK,UAAU,WAAW,CAC/B,UAAkB,EAClB,SAAkB;IAElB,IAAI;QACF,MAAM,KAAK,GAAG,MAAM,aAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC;KAC5B;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE;YAC3B,IAAI,CAAC,SAAS,EAAE;gBACd,OAAO,KAAK,CAAC;aACd;YAED,IAAI;gBACF,MAAM,aAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;gBAC3B,OAAO,IAAI,CAAC;aACb;YAAC,OAAO,UAAU,EAAE;gBACnB,IAAA,eAAQ,EAAC,cAAc,UAAU,yBAAyB,EAAE,UAAU,CAAC,CAAC;gBACxE,MAAM,UAAU,CAAC;aAClB;SACF;QACD,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAvBD,kCAuBC;AAED;;;;;GAKG;AACI,KAAK,UAAU,MAAM,CAAC,UAAkB;IAC7C,IAAI;QACF,MAAM,KAAK,GAAG,MAAM,aAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC,MAAM,EAAE,CAAC;KACvB;IAAC,OAAO,KAAK,EAAE;QACd,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAPD,wBAOC;AAED;;;;;GAKG;AACI,KAAK,UAAU,YAAY,CAAC,UAAkB;IACnD,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;QACjC,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;KAC/D;IAED,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,aAAE,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC;AAC3D,CAAC;AAND,oCAMC","sourcesContent":["import { promises as fs } from 'fs';\nimport type { Json } from '@metamask/snap-controllers';\nimport { logError } from './misc';\n\n/**\n * Checks whether the given path string resolves to an existing directory, and\n * optionally creates the directory if it doesn't exist.\n *\n * @param pathString - The path string to check.\n * @param createDir - Whether to create the directory if it doesn't exist.\n * @returns Whether the given path is an existing directory.\n */\nexport async function isDirectory(\n pathString: string,\n createDir: boolean,\n): Promise<boolean> {\n try {\n const stats = await fs.stat(pathString);\n return stats.isDirectory();\n } catch (error) {\n if (error.code === 'ENOENT') {\n if (!createDir) {\n return false;\n }\n\n try {\n await fs.mkdir(pathString);\n return true;\n } catch (mkdirError) {\n logError(`Directory '${pathString}' could not be created.`, mkdirError);\n throw mkdirError;\n }\n }\n return false;\n }\n}\n\n/**\n * Checks whether the given path string resolves to an existing file.\n *\n * @param pathString - The path string to check.\n * @returns Whether the given path is an existing file.\n */\nexport async function isFile(pathString: string): Promise<boolean> {\n try {\n const stats = await fs.stat(pathString);\n return stats.isFile();\n } catch (error) {\n return false;\n }\n}\n\n/**\n * Reads a `.json` file, parses its contents, and returns them.\n *\n * @param pathString - The path to the JSON file.\n * @returns The parsed contents of the JSON file.\n */\nexport async function readJsonFile(pathString: string): Promise<Json> {\n if (!pathString.endsWith('.json')) {\n throw new Error('The specified file must be a \".json\" file.');\n }\n\n return JSON.parse(await fs.readFile(pathString, 'utf8'));\n}\n"]}
@@ -1,35 +0,0 @@
1
- /**
2
- * Gets the complete out file path from an output file name and parent
3
- * directory path.
4
- *
5
- * @param outDir - The path to the out file's parent directory.
6
- * @param outFileName - The out file's name.
7
- * @returns The complete path to the out file.
8
- */
9
- export declare function getOutfilePath(outDir: string, outFileName: string): string;
10
- /**
11
- * Ensures that the outfile name is just a `.js` file name.
12
- * Throws on validation failure.
13
- *
14
- * @param filename - The file name to validate.
15
- * @returns `true` if validation succeeded.
16
- * @throws If the file name is invalid.
17
- */
18
- export declare function validateOutfileName(filename: string): boolean;
19
- /**
20
- * Validates a file path. Throws on validation failure.
21
- *
22
- * @param filePath - The file path to validate.
23
- * @returns `true` if validation succeeded.
24
- * @throws If the path does not resolve to a file.
25
- */
26
- export declare function validateFilePath(filePath: string): Promise<boolean>;
27
- /**
28
- * Validates a directory path. Throws on validation failure.
29
- *
30
- * @param dirPath - The directory path to validate.
31
- * @param createDir - Whether to create the directory if it doesn't exist.
32
- * @returns `true` if validation succeeded or the directory was created.
33
- * @throws If the directory does not exist or could not be created.
34
- */
35
- export declare function validateDirPath(dirPath: string, createDir: boolean): Promise<boolean>;
@@ -1,69 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.validateDirPath = exports.validateFilePath = exports.validateOutfileName = exports.getOutfilePath = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const fs_1 = require("./fs");
9
- /**
10
- * Gets the complete out file path from an output file name and parent
11
- * directory path.
12
- *
13
- * @param outDir - The path to the out file's parent directory.
14
- * @param outFileName - The out file's name.
15
- * @returns The complete path to the out file.
16
- */
17
- function getOutfilePath(outDir, outFileName) {
18
- return path_1.default.join(outDir, outFileName || 'bundle.js');
19
- }
20
- exports.getOutfilePath = getOutfilePath;
21
- /**
22
- * Ensures that the outfile name is just a `.js` file name.
23
- * Throws on validation failure.
24
- *
25
- * @param filename - The file name to validate.
26
- * @returns `true` if validation succeeded.
27
- * @throws If the file name is invalid.
28
- */
29
- function validateOutfileName(filename) {
30
- if (!filename.endsWith('.js') ||
31
- filename === '.js' ||
32
- filename.indexOf('/') !== -1) {
33
- throw new Error(`Invalid outfile name: ${filename}. Must be a .js file`);
34
- }
35
- return true;
36
- }
37
- exports.validateOutfileName = validateOutfileName;
38
- /**
39
- * Validates a file path. Throws on validation failure.
40
- *
41
- * @param filePath - The file path to validate.
42
- * @returns `true` if validation succeeded.
43
- * @throws If the path does not resolve to a file.
44
- */
45
- async function validateFilePath(filePath) {
46
- const exists = await (0, fs_1.isFile)(filePath);
47
- if (!exists) {
48
- throw new Error(`Invalid params: '${filePath}' is not a file or does not exist.`);
49
- }
50
- return true;
51
- }
52
- exports.validateFilePath = validateFilePath;
53
- /**
54
- * Validates a directory path. Throws on validation failure.
55
- *
56
- * @param dirPath - The directory path to validate.
57
- * @param createDir - Whether to create the directory if it doesn't exist.
58
- * @returns `true` if validation succeeded or the directory was created.
59
- * @throws If the directory does not exist or could not be created.
60
- */
61
- async function validateDirPath(dirPath, createDir) {
62
- const exists = await (0, fs_1.isDirectory)(dirPath, createDir);
63
- if (!exists) {
64
- throw new Error(`Invalid params: '${dirPath}' is not a directory or could not be created.`);
65
- }
66
- return true;
67
- }
68
- exports.validateDirPath = validateDirPath;
69
- //# sourceMappingURL=validate-fs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"validate-fs.js","sourceRoot":"","sources":["../../src/utils/validate-fs.ts"],"names":[],"mappings":";;;;;;AAAA,gDAA6B;AAC7B,6BAA2C;AAE3C;;;;;;;GAOG;AACH,SAAgB,cAAc,CAAC,MAAc,EAAE,WAAmB;IAChE,OAAO,cAAS,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,IAAI,WAAW,CAAC,CAAC;AAC5D,CAAC;AAFD,wCAEC;AAED;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CAAC,QAAgB;IAClD,IACE,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC;QACzB,QAAQ,KAAK,KAAK;QAClB,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAC5B;QACA,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,sBAAsB,CAAC,CAAC;KAC1E;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AATD,kDASC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,gBAAgB,CAAC,QAAgB;IACrD,MAAM,MAAM,GAAG,MAAM,IAAA,WAAM,EAAC,QAAQ,CAAC,CAAC;IACtC,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,oBAAoB,QAAQ,oCAAoC,CACjE,CAAC;KACH;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AARD,4CAQC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,eAAe,CACnC,OAAe,EACf,SAAkB;IAElB,MAAM,MAAM,GAAG,MAAM,IAAA,gBAAW,EAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACrD,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,oBAAoB,OAAO,+CAA+C,CAC3E,CAAC;KACH;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAXD,0CAWC","sourcesContent":["import pathUtils from 'path';\nimport { isFile, isDirectory } from './fs';\n\n/**\n * Gets the complete out file path from an output file name and parent\n * directory path.\n *\n * @param outDir - The path to the out file's parent directory.\n * @param outFileName - The out file's name.\n * @returns The complete path to the out file.\n */\nexport function getOutfilePath(outDir: string, outFileName: string): string {\n return pathUtils.join(outDir, outFileName || 'bundle.js');\n}\n\n/**\n * Ensures that the outfile name is just a `.js` file name.\n * Throws on validation failure.\n *\n * @param filename - The file name to validate.\n * @returns `true` if validation succeeded.\n * @throws If the file name is invalid.\n */\nexport function validateOutfileName(filename: string): boolean {\n if (\n !filename.endsWith('.js') ||\n filename === '.js' ||\n filename.indexOf('/') !== -1\n ) {\n throw new Error(`Invalid outfile name: ${filename}. Must be a .js file`);\n }\n return true;\n}\n\n/**\n * Validates a file path. Throws on validation failure.\n *\n * @param filePath - The file path to validate.\n * @returns `true` if validation succeeded.\n * @throws If the path does not resolve to a file.\n */\nexport async function validateFilePath(filePath: string): Promise<boolean> {\n const exists = await isFile(filePath);\n if (!exists) {\n throw new Error(\n `Invalid params: '${filePath}' is not a file or does not exist.`,\n );\n }\n return true;\n}\n\n/**\n * Validates a directory path. Throws on validation failure.\n *\n * @param dirPath - The directory path to validate.\n * @param createDir - Whether to create the directory if it doesn't exist.\n * @returns `true` if validation succeeded or the directory was created.\n * @throws If the directory does not exist or could not be created.\n */\nexport async function validateDirPath(\n dirPath: string,\n createDir: boolean,\n): Promise<boolean> {\n const exists = await isDirectory(dirPath, createDir);\n if (!exists) {\n throw new Error(\n `Invalid params: '${dirPath}' is not a directory or could not be created.`,\n );\n }\n return true;\n}\n"]}