@metamask/snaps-cli 0.38.1-flask.1 → 1.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.
Files changed (253) hide show
  1. package/CHANGELOG.md +377 -19
  2. package/README.md +67 -542
  3. package/dist/builders.d.ts +27 -0
  4. package/dist/{esm/builders.js → builders.js} +51 -48
  5. package/dist/builders.js.map +1 -0
  6. package/dist/{types/cli.d.ts → cli.d.ts} +1 -1
  7. package/dist/cli.js +51 -0
  8. package/dist/cli.js.map +1 -0
  9. package/dist/cmds/build/buildHandler.d.ts +13 -0
  10. package/dist/cmds/build/buildHandler.js +37 -0
  11. package/dist/cmds/build/buildHandler.js.map +1 -0
  12. package/dist/cmds/build/bundle.d.ts +15 -0
  13. package/dist/cmds/build/bundle.js +80 -0
  14. package/dist/cmds/build/bundle.js.map +1 -0
  15. package/dist/{types/commands/serve → cmds/build}/index.d.ts +4 -4
  16. package/dist/cmds/build/index.js +29 -0
  17. package/dist/cmds/build/index.js.map +1 -0
  18. package/dist/cmds/build/utils.d.ts +56 -0
  19. package/dist/cmds/build/utils.js +100 -0
  20. package/dist/cmds/build/utils.js.map +1 -0
  21. package/dist/cmds/eval/evalHandler.d.ts +9 -0
  22. package/dist/cmds/eval/evalHandler.js +25 -0
  23. package/dist/cmds/eval/evalHandler.js.map +1 -0
  24. package/dist/cmds/eval/index.d.ts +9 -0
  25. package/dist/cmds/eval/index.js +15 -0
  26. package/dist/cmds/eval/index.js.map +1 -0
  27. package/dist/cmds/index.js +14 -0
  28. package/dist/cmds/index.js.map +1 -0
  29. package/dist/cmds/init/index.d.ts +9 -0
  30. package/dist/cmds/init/index.js +34 -0
  31. package/dist/cmds/init/index.js.map +1 -0
  32. package/dist/cmds/init/initHandler.d.ts +26 -0
  33. package/dist/cmds/init/initHandler.js +69 -0
  34. package/dist/cmds/init/initHandler.js.map +1 -0
  35. package/dist/cmds/init/initUtils.d.ts +39 -0
  36. package/dist/cmds/init/initUtils.js +122 -0
  37. package/dist/cmds/init/initUtils.js.map +1 -0
  38. package/dist/cmds/manifest/index.d.ts +9 -0
  39. package/dist/cmds/manifest/index.js +24 -0
  40. package/dist/cmds/manifest/index.js.map +1 -0
  41. package/dist/cmds/manifest/manifestHandler.d.ts +10 -0
  42. package/dist/cmds/manifest/manifestHandler.js +50 -0
  43. package/dist/cmds/manifest/manifestHandler.js.map +1 -0
  44. package/dist/cmds/serve/index.d.ts +9 -0
  45. package/dist/cmds/serve/index.js +15 -0
  46. package/dist/cmds/serve/index.js.map +1 -0
  47. package/dist/cmds/serve/serveHandler.d.ts +10 -0
  48. package/dist/cmds/serve/serveHandler.js +59 -0
  49. package/dist/cmds/serve/serveHandler.js.map +1 -0
  50. package/dist/cmds/serve/serveUtils.d.ts +22 -0
  51. package/dist/cmds/serve/serveUtils.js +39 -0
  52. package/dist/cmds/serve/serveUtils.js.map +1 -0
  53. package/dist/cmds/watch/index.d.ts +9 -0
  54. package/dist/cmds/watch/index.js +32 -0
  55. package/dist/cmds/watch/index.js.map +1 -0
  56. package/dist/cmds/watch/watchHandler.d.ts +14 -0
  57. package/dist/cmds/watch/watchHandler.js +100 -0
  58. package/dist/cmds/watch/watchHandler.js.map +1 -0
  59. package/dist/main.js +15 -0
  60. package/dist/main.js.map +1 -0
  61. package/dist/utils/index.d.ts +2 -0
  62. package/dist/utils/index.js +19 -0
  63. package/dist/utils/index.js.map +1 -0
  64. package/dist/utils/misc.d.ts +53 -0
  65. package/dist/utils/misc.js +156 -0
  66. package/dist/utils/misc.js.map +1 -0
  67. package/dist/utils/snap-config.d.ts +48 -0
  68. package/dist/utils/snap-config.js +117 -0
  69. package/dist/utils/snap-config.js.map +1 -0
  70. package/package.json +35 -54
  71. package/.browserslistrc +0 -3
  72. package/dist/cjs/builders.js +0 -148
  73. package/dist/cjs/builders.js.map +0 -1
  74. package/dist/cjs/cli.js +0 -35
  75. package/dist/cjs/cli.js.map +0 -1
  76. package/dist/cjs/commands/build/build.js +0 -54
  77. package/dist/cjs/commands/build/build.js.map +0 -1
  78. package/dist/cjs/commands/build/implementation.js +0 -31
  79. package/dist/cjs/commands/build/implementation.js.map +0 -1
  80. package/dist/cjs/commands/build/index.js +0 -45
  81. package/dist/cjs/commands/build/index.js.map +0 -1
  82. package/dist/cjs/commands/eval/eval.js +0 -57
  83. package/dist/cjs/commands/eval/eval.js.map +0 -1
  84. package/dist/cjs/commands/eval/implementation.js +0 -27
  85. package/dist/cjs/commands/eval/implementation.js.map +0 -1
  86. package/dist/cjs/commands/eval/index.js +0 -50
  87. package/dist/cjs/commands/eval/index.js.map +0 -1
  88. package/dist/cjs/commands/index.js +0 -30
  89. package/dist/cjs/commands/index.js.map +0 -1
  90. package/dist/cjs/commands/manifest/implementation.js +0 -34
  91. package/dist/cjs/commands/manifest/implementation.js.map +0 -1
  92. package/dist/cjs/commands/manifest/index.js +0 -50
  93. package/dist/cjs/commands/manifest/index.js.map +0 -1
  94. package/dist/cjs/commands/manifest/manifest.js +0 -55
  95. package/dist/cjs/commands/manifest/manifest.js.map +0 -1
  96. package/dist/cjs/commands/serve/index.js +0 -33
  97. package/dist/cjs/commands/serve/index.js.map +0 -1
  98. package/dist/cjs/commands/serve/serve.js +0 -21
  99. package/dist/cjs/commands/serve/serve.js.map +0 -1
  100. package/dist/cjs/commands/watch/implementation.js +0 -35
  101. package/dist/cjs/commands/watch/implementation.js.map +0 -1
  102. package/dist/cjs/commands/watch/index.js +0 -47
  103. package/dist/cjs/commands/watch/index.js.map +0 -1
  104. package/dist/cjs/commands/watch/watch.js +0 -51
  105. package/dist/cjs/commands/watch/watch.js.map +0 -1
  106. package/dist/cjs/config.js +0 -290
  107. package/dist/cjs/config.js.map +0 -1
  108. package/dist/cjs/errors.js +0 -37
  109. package/dist/cjs/errors.js.map +0 -1
  110. package/dist/cjs/index.js +0 -77
  111. package/dist/cjs/index.js.map +0 -1
  112. package/dist/cjs/main.js +0 -19
  113. package/dist/cjs/main.js.map +0 -1
  114. package/dist/cjs/utils/cli.js +0 -61
  115. package/dist/cjs/utils/cli.js.map +0 -1
  116. package/dist/cjs/utils/errors.js +0 -42
  117. package/dist/cjs/utils/errors.js.map +0 -1
  118. package/dist/cjs/utils/index.js +0 -25
  119. package/dist/cjs/utils/index.js.map +0 -1
  120. package/dist/cjs/utils/legacy.js +0 -48
  121. package/dist/cjs/utils/legacy.js.map +0 -1
  122. package/dist/cjs/utils/logging.js +0 -46
  123. package/dist/cjs/utils/logging.js.map +0 -1
  124. package/dist/cjs/utils/path.js +0 -16
  125. package/dist/cjs/utils/path.js.map +0 -1
  126. package/dist/cjs/utils/steps.js +0 -52
  127. package/dist/cjs/utils/steps.js.map +0 -1
  128. package/dist/cjs/webpack/compiler.js +0 -96
  129. package/dist/cjs/webpack/compiler.js.map +0 -1
  130. package/dist/cjs/webpack/config.js +0 -234
  131. package/dist/cjs/webpack/config.js.map +0 -1
  132. package/dist/cjs/webpack/index.js +0 -22
  133. package/dist/cjs/webpack/index.js.map +0 -1
  134. package/dist/cjs/webpack/loaders/browserify.js +0 -97
  135. package/dist/cjs/webpack/loaders/browserify.js.map +0 -1
  136. package/dist/cjs/webpack/loaders/wasm.js +0 -36
  137. package/dist/cjs/webpack/loaders/wasm.js.map +0 -1
  138. package/dist/cjs/webpack/plugins.js +0 -340
  139. package/dist/cjs/webpack/plugins.js.map +0 -1
  140. package/dist/cjs/webpack/utils.js +0 -133
  141. package/dist/cjs/webpack/utils.js.map +0 -1
  142. package/dist/esm/builders.js.map +0 -1
  143. package/dist/esm/cli.js +0 -26
  144. package/dist/esm/cli.js.map +0 -1
  145. package/dist/esm/commands/build/build.js +0 -51
  146. package/dist/esm/commands/build/build.js.map +0 -1
  147. package/dist/esm/commands/build/implementation.js +0 -27
  148. package/dist/esm/commands/build/implementation.js.map +0 -1
  149. package/dist/esm/commands/build/index.js +0 -17
  150. package/dist/esm/commands/build/index.js.map +0 -1
  151. package/dist/esm/commands/eval/eval.js +0 -54
  152. package/dist/esm/commands/eval/eval.js.map +0 -1
  153. package/dist/esm/commands/eval/implementation.js +0 -24
  154. package/dist/esm/commands/eval/implementation.js.map +0 -1
  155. package/dist/esm/commands/eval/index.js +0 -22
  156. package/dist/esm/commands/eval/index.js.map +0 -1
  157. package/dist/esm/commands/index.js +0 -15
  158. package/dist/esm/commands/index.js.map +0 -1
  159. package/dist/esm/commands/manifest/implementation.js +0 -33
  160. package/dist/esm/commands/manifest/implementation.js.map +0 -1
  161. package/dist/esm/commands/manifest/index.js +0 -22
  162. package/dist/esm/commands/manifest/index.js.map +0 -1
  163. package/dist/esm/commands/manifest/manifest.js +0 -52
  164. package/dist/esm/commands/manifest/manifest.js.map +0 -1
  165. package/dist/esm/commands/serve/index.js +0 -18
  166. package/dist/esm/commands/serve/index.js.map +0 -1
  167. package/dist/esm/commands/serve/serve.js +0 -17
  168. package/dist/esm/commands/serve/serve.js.map +0 -1
  169. package/dist/esm/commands/watch/implementation.js +0 -34
  170. package/dist/esm/commands/watch/implementation.js.map +0 -1
  171. package/dist/esm/commands/watch/index.js +0 -19
  172. package/dist/esm/commands/watch/index.js.map +0 -1
  173. package/dist/esm/commands/watch/watch.js +0 -50
  174. package/dist/esm/commands/watch/watch.js.map +0 -1
  175. package/dist/esm/config.js +0 -294
  176. package/dist/esm/config.js.map +0 -1
  177. package/dist/esm/errors.js +0 -30
  178. package/dist/esm/errors.js.map +0 -1
  179. package/dist/esm/index.js +0 -6
  180. package/dist/esm/index.js.map +0 -1
  181. package/dist/esm/main.js +0 -10
  182. package/dist/esm/main.js.map +0 -1
  183. package/dist/esm/utils/cli.js +0 -43
  184. package/dist/esm/utils/cli.js.map +0 -1
  185. package/dist/esm/utils/errors.js +0 -43
  186. package/dist/esm/utils/errors.js.map +0 -1
  187. package/dist/esm/utils/index.js +0 -8
  188. package/dist/esm/utils/index.js.map +0 -1
  189. package/dist/esm/utils/legacy.js +0 -47
  190. package/dist/esm/utils/legacy.js.map +0 -1
  191. package/dist/esm/utils/logging.js +0 -40
  192. package/dist/esm/utils/logging.js.map +0 -1
  193. package/dist/esm/utils/path.js +0 -13
  194. package/dist/esm/utils/path.js.map +0 -1
  195. package/dist/esm/utils/steps.js +0 -43
  196. package/dist/esm/utils/steps.js.map +0 -1
  197. package/dist/esm/webpack/compiler.js +0 -89
  198. package/dist/esm/webpack/compiler.js.map +0 -1
  199. package/dist/esm/webpack/config.js +0 -236
  200. package/dist/esm/webpack/config.js.map +0 -1
  201. package/dist/esm/webpack/index.js +0 -5
  202. package/dist/esm/webpack/index.js.map +0 -1
  203. package/dist/esm/webpack/loaders/browserify.js +0 -82
  204. package/dist/esm/webpack/loaders/browserify.js.map +0 -1
  205. package/dist/esm/webpack/loaders/wasm.js +0 -26
  206. package/dist/esm/webpack/loaders/wasm.js.map +0 -1
  207. package/dist/esm/webpack/plugins.js +0 -343
  208. package/dist/esm/webpack/plugins.js.map +0 -1
  209. package/dist/esm/webpack/utils.js +0 -160
  210. package/dist/esm/webpack/utils.js.map +0 -1
  211. package/dist/types/__fixtures__/configs/cjs.d.ts +0 -1
  212. package/dist/types/__fixtures__/configs/esm.d.ts +0 -3
  213. package/dist/types/__fixtures__/configs/invalid.d.ts +0 -3
  214. package/dist/types/__fixtures__/configs/javascript/snap.config.d.ts +0 -0
  215. package/dist/types/__fixtures__/configs/typescript/snap.config.d.ts +0 -3
  216. package/dist/types/builders.d.ts +0 -8
  217. package/dist/types/commands/build/build.d.ts +0 -10
  218. package/dist/types/commands/build/implementation.d.ts +0 -10
  219. package/dist/types/commands/build/index.d.ts +0 -10
  220. package/dist/types/commands/eval/__test__/browserify/bad/snap.config.d.ts +0 -3
  221. package/dist/types/commands/eval/__test__/browserify/good/snap.config.d.ts +0 -3
  222. package/dist/types/commands/eval/__test__/webpack/bad/snap.config.d.ts +0 -3
  223. package/dist/types/commands/eval/__test__/webpack/good/snap.config.d.ts +0 -3
  224. package/dist/types/commands/eval/__test__/webpack/snap.config.d.ts +0 -3
  225. package/dist/types/commands/eval/eval.d.ts +0 -14
  226. package/dist/types/commands/eval/implementation.d.ts +0 -9
  227. package/dist/types/commands/eval/index.d.ts +0 -10
  228. package/dist/types/commands/manifest/implementation.d.ts +0 -12
  229. package/dist/types/commands/manifest/index.d.ts +0 -10
  230. package/dist/types/commands/manifest/manifest.d.ts +0 -14
  231. package/dist/types/commands/serve/serve.d.ts +0 -16
  232. package/dist/types/commands/watch/implementation.d.ts +0 -14
  233. package/dist/types/commands/watch/index.d.ts +0 -10
  234. package/dist/types/commands/watch/watch.d.ts +0 -19
  235. package/dist/types/config.d.ts +0 -626
  236. package/dist/types/errors.d.ts +0 -25
  237. package/dist/types/index.d.ts +0 -5
  238. package/dist/types/utils/cli.d.ts +0 -17
  239. package/dist/types/utils/errors.d.ts +0 -23
  240. package/dist/types/utils/index.d.ts +0 -6
  241. package/dist/types/utils/legacy.d.ts +0 -27
  242. package/dist/types/utils/logging.d.ts +0 -22
  243. package/dist/types/utils/path.d.ts +0 -9
  244. package/dist/types/utils/steps.d.ts +0 -17
  245. package/dist/types/webpack/compiler.d.ts +0 -29
  246. package/dist/types/webpack/config.d.ts +0 -37
  247. package/dist/types/webpack/index.d.ts +0 -3
  248. package/dist/types/webpack/loaders/browserify.d.ts +0 -16
  249. package/dist/types/webpack/loaders/wasm.d.ts +0 -20
  250. package/dist/types/webpack/plugins.d.ts +0 -147
  251. package/dist/types/webpack/utils.d.ts +0 -156
  252. /package/dist/{types/commands → cmds}/index.d.ts +0 -0
  253. /package/dist/{types/main.d.ts → main.d.ts} +0 -0
@@ -0,0 +1,59 @@
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.serve = void 0;
7
+ const snaps_utils_1 = require("@metamask/snaps-utils");
8
+ const http_1 = __importDefault(require("http"));
9
+ const serve_handler_1 = __importDefault(require("serve-handler"));
10
+ const serveUtils_1 = require("./serveUtils");
11
+ /**
12
+ * Starts a local, static HTTP server on the given port with the given root
13
+ * directory.
14
+ *
15
+ * @param argv - Arguments as an object generated by Yargs.
16
+ * @param argv.root - The root directory path string.
17
+ * @param argv.port - The server port.
18
+ */
19
+ async function serve(argv) {
20
+ const { port, root: rootDir } = argv;
21
+ await (0, snaps_utils_1.validateDirPath)(rootDir, true);
22
+ (0, snaps_utils_1.logInfo)(`\nStarting server...`);
23
+ const server = http_1.default.createServer((req, res) => {
24
+ (0, serve_handler_1.default)(req, res, {
25
+ public: rootDir,
26
+ headers: [
27
+ {
28
+ source: '**/*',
29
+ headers: [
30
+ {
31
+ key: 'Cache-Control',
32
+ value: 'no-cache',
33
+ },
34
+ {
35
+ key: 'Access-Control-Allow-Origin',
36
+ value: '*',
37
+ },
38
+ ],
39
+ },
40
+ ],
41
+ })?.catch((error) => {
42
+ (0, serveUtils_1.logServerError)(error, argv.port);
43
+ res.statusCode = 500;
44
+ res.end();
45
+ });
46
+ });
47
+ server.listen({ port }, () => (0, serveUtils_1.logServerListening)(port));
48
+ server.on('request', (request) => (0, serveUtils_1.logRequest)(request));
49
+ server.on('error', (error) => {
50
+ (0, serveUtils_1.logServerError)(error, argv.port);
51
+ process.exitCode = 1;
52
+ });
53
+ server.on('close', () => {
54
+ (0, snaps_utils_1.logInfo)('Server closed');
55
+ process.exitCode = 1;
56
+ });
57
+ }
58
+ exports.serve = serve;
59
+ //# sourceMappingURL=serveHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serveHandler.js","sourceRoot":"","sources":["../../../src/cmds/serve/serveHandler.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAiE;AACjE,gDAAwB;AACxB,kEAAyC;AAGzC,6CAA8E;AAE9E;;;;;;;GAOG;AACI,KAAK,UAAU,KAAK,CAAC,IAAe;IACzC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IAErC,MAAM,IAAA,6BAAe,EAAC,OAAiB,EAAE,IAAI,CAAC,C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CAAC,CAAC;IACvB,CAAC,CAAC,CAAC;AACL,CAAC;AA7CD,sBA6CC","sourcesContent":["import { logInfo, validateDirPath } from '@metamask/snaps-utils';\nimport http from 'http';\nimport serveHandler from 'serve-handler';\n\nimport { YargsArgs } from '../../types/yargs';\nimport { logRequest, logServerError, logServerListening } from './serveUtils';\n\n/**\n * Starts a local, static HTTP server on the given port with the given root\n * directory.\n *\n * @param argv - Arguments as an object generated by Yargs.\n * @param argv.root - The root directory path string.\n * @param argv.port - The server port.\n */\nexport async function serve(argv: YargsArgs): Promise<void> {\n const { port, root: rootDir } = argv;\n\n await validateDirPath(rootDir as string, true);\n\n logInfo(`\\nStarting server...`);\n\n const server = http.createServer((req, res) => {\n serveHandler(req, res, {\n public: rootDir as string,\n headers: [\n {\n source: '**/*',\n headers: [\n {\n key: 'Cache-Control',\n value: 'no-cache',\n },\n {\n key: 'Access-Control-Allow-Origin',\n value: '*',\n },\n ],\n },\n ],\n })?.catch((error) => {\n logServerError(error, argv.port);\n res.statusCode = 500;\n res.end();\n });\n });\n\n server.listen({ port }, () => logServerListening(port));\n\n server.on('request', (request) => logRequest(request));\n\n server.on('error', (error) => {\n logServerError(error, argv.port);\n process.exitCode = 1;\n });\n\n server.on('close', () => {\n logInfo('Server closed');\n process.exitCode = 1;\n });\n}\n"]}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Log a message with the URL and port of the server.
3
+ *
4
+ * @param port - The port that the server is running on.
5
+ */
6
+ export declare function logServerListening(port: number): void;
7
+ /**
8
+ * Log a message with the request URL.
9
+ *
10
+ * @param request - The request object.
11
+ * @param request.url - The URL of the request.
12
+ */
13
+ export declare function logRequest(request: {
14
+ url?: string;
15
+ }): void;
16
+ /**
17
+ * Log an error message.
18
+ *
19
+ * @param error - The error to log.
20
+ * @param port - The port that the server is running on.
21
+ */
22
+ export declare function logServerError(error: Error, port: number): void;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.logServerError = exports.logRequest = exports.logServerListening = void 0;
4
+ const snaps_utils_1 = require("@metamask/snaps-utils");
5
+ /**
6
+ * Log a message with the URL and port of the server.
7
+ *
8
+ * @param port - The port that the server is running on.
9
+ */
10
+ function logServerListening(port) {
11
+ (0, snaps_utils_1.logInfo)(`Server listening on: http://localhost:${port}`);
12
+ }
13
+ exports.logServerListening = logServerListening;
14
+ /**
15
+ * Log a message with the request URL.
16
+ *
17
+ * @param request - The request object.
18
+ * @param request.url - The URL of the request.
19
+ */
20
+ function logRequest(request) {
21
+ (0, snaps_utils_1.logInfo)(`Handling incoming request for: ${request.url ?? 'unknown'}`);
22
+ }
23
+ exports.logRequest = logRequest;
24
+ /**
25
+ * Log an error message.
26
+ *
27
+ * @param error - The error to log.
28
+ * @param port - The port that the server is running on.
29
+ */
30
+ function logServerError(error, port) {
31
+ if (error.code === 'EADDRINUSE') {
32
+ (0, snaps_utils_1.logError)(`Server error: Port ${port} already in use.`);
33
+ }
34
+ else {
35
+ (0, snaps_utils_1.logError)(`Server error: ${error.message}`);
36
+ }
37
+ }
38
+ exports.logServerError = logServerError;
39
+ //# sourceMappingURL=serveUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serveUtils.js","sourceRoot":"","sources":["../../../src/cmds/serve/serveUtils.ts"],"names":[],"mappings":";;;AAAA,uDAA0D;AAE1D;;;;GAIG;AACH,SAAgB,kBAAkB,CAAC,IAAY;IAC7C,IAAA,qBAAO,EAAC,yCAAyC,IAAI,EAAE,CAAC,CAAC;AAC3D,CAAC;AAFD,gDAEC;AAED;;;;;GAKG;AACH,SAAgB,UAAU,CAAC,OAAyB;IAClD,IAAA,qBAAO,EAAC,kCAAkC,OAAO,CAAC,GAAG,IAAI,SAAS,EAAE,CAAC,CAAC;AACxE,CAAC;AAFD,gCAEC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAY,EAAE,IAAY;IACvD,IAAK,KAAa,CAAC,IAAI,KAAK,YAAY,EAAE;QACxC,IAAA,sBAAQ,EAAC,sBAAsB,IAAI,kBAAkB,CAAC,CAAC;KACxD;SAAM;QACL,IAAA,sBAAQ,EAAC,iBAAiB,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;KAC5C;AACH,CAAC;AAND,wCAMC","sourcesContent":["import { logError, logInfo } from '@metamask/snaps-utils';\n\n/**\n * Log a message with the URL and port of the server.\n *\n * @param port - The port that the server is running on.\n */\nexport function logServerListening(port: number) {\n logInfo(`Server listening on: http://localhost:${port}`);\n}\n\n/**\n * Log a message with the request URL.\n *\n * @param request - The request object.\n * @param request.url - The URL of the request.\n */\nexport function logRequest(request: { url?: string }) {\n logInfo(`Handling incoming request for: ${request.url ?? 'unknown'}`);\n}\n\n/**\n * Log an error message.\n *\n * @param error - The error to log.\n * @param port - The port that the server is running on.\n */\nexport function logServerError(error: Error, port: number) {\n if ((error as any).code === 'EADDRINUSE') {\n logError(`Server error: Port ${port} already in use.`);\n } else {\n logError(`Server error: ${error.message}`);\n }\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import yargs from 'yargs';
2
+ import { YargsArgs } from '../../types/yargs';
3
+ declare const _default: {
4
+ command: string[];
5
+ desc: string;
6
+ builder: (yarg: yargs.Argv) => void;
7
+ handler: (argv: YargsArgs) => Promise<void>;
8
+ };
9
+ export = _default;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const builders_1 = __importDefault(require("../../builders"));
6
+ const utils_1 = require("../build/utils");
7
+ const watchHandler_1 = require("./watchHandler");
8
+ module.exports = {
9
+ command: ['watch', 'w'],
10
+ desc: 'Build Snap on change',
11
+ builder: (yarg) => {
12
+ yarg
13
+ .option('src', builders_1.default.src)
14
+ .option('eval', builders_1.default.eval)
15
+ .option('dist', builders_1.default.dist)
16
+ .option('outfileName', builders_1.default.outfileName)
17
+ .option('sourceMaps', builders_1.default.sourceMaps)
18
+ .option('stripComments', builders_1.default.stripComments)
19
+ .option('transpilationMode', builders_1.default.transpilationMode)
20
+ .option('depsToTranspile', builders_1.default.depsToTranspile)
21
+ .option('manifest', builders_1.default.manifest)
22
+ .option('writeManifest', builders_1.default.writeManifest)
23
+ .option('serve', builders_1.default.serve)
24
+ .option('root', builders_1.default.root)
25
+ .option('port', builders_1.default.port)
26
+ .implies('writeManifest', 'manifest')
27
+ .implies('depsToTranspile', 'transpilationMode')
28
+ .middleware((argv) => (0, utils_1.processInvalidTranspilation)(argv));
29
+ },
30
+ handler: async (argv) => (0, watchHandler_1.watch)(argv),
31
+ };
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/cmds/watch/index.ts"],"names":[],"mappings":";;;;AAEA,8DAAsC;AAEtC,0CAA6D;AAC7D,iDAAuC;AAEvC,iBAAS;IACP,OAAO,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC;IACvB,IAAI,EAAE,sBAAsB;IAC5B,OAAO,EAAE,CAAC,IAAgB,EAAE,EAAE;QAC5B,IAAI;aACD,MAAM,CAAC,KAAK,EAAE,kBAAQ,CAAC,GAAG,CAAC;aAC3B,MAAM,CAAC,MAAM,EAAE,kBAAQ,CAAC,IAAI,CAAC;aAC7B,MAAM,CAAC,MAAM,EAAE,kBAAQ,CAAC,IAAI,CAAC;aAC7B,MAAM,CAAC,aAAa,EAAE,kBAAQ,CAAC,WAAW,CAAC;aAC3C,MAAM,CAAC,YAAY,EAAE,kBAAQ,CAAC,UAAU,CAAC;aACzC,MAAM,CAAC,eAAe,EAAE,kBAAQ,CAAC,aAAa,CAAC;aAC/C,MAAM,CAAC,mBAAmB,EAAE,kBAAQ,CAAC,iBAAiB,CAAC;aACvD,MAAM,CAAC,iBAAiB,EAAE,kBAAQ,CAAC,eAAe,CAAC;aACnD,MAAM,CAAC,UAAU,EAAE,kBAAQ,CAAC,QAAQ,CAAC;aACrC,MAAM,CAAC,eAAe,EAAE,kBAAQ,CAAC,aAAa,CAAC;aAC/C,MAAM,CAAC,OAAO,EAAE,kBAAQ,CAAC,KAAK,CAAC;aAC/B,MAAM,CAAC,MAAM,EAAE,kBAAQ,CAAC,IAAI,CAAC;aAC7B,MAAM,CAAC,MAAM,EAAE,kBAAQ,CAAC,IAAI,CAAC;aAC7B,OAAO,CAAC,eAAe,EAAE,UAAU,CAAC;aACpC,OAAO,CAAC,iBAAiB,EAAE,mBAAmB,CAAC;aAC/C,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAA,mCAA2B,EAAC,IAAW,CAAC,CAAC,CAAC;IACpE,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,IAAe,EAAE,EAAE,CAAC,IAAA,oBAAK,EAAC,IAAI,CAAC;CAChD,CAAC","sourcesContent":["import yargs from 'yargs';\n\nimport builders from '../../builders';\nimport { YargsArgs } from '../../types/yargs';\nimport { processInvalidTranspilation } from '../build/utils';\nimport { watch } from './watchHandler';\n\nexport = {\n command: ['watch', 'w'],\n desc: 'Build Snap on change',\n builder: (yarg: yargs.Argv) => {\n yarg\n .option('src', builders.src)\n .option('eval', builders.eval)\n .option('dist', builders.dist)\n .option('outfileName', builders.outfileName)\n .option('sourceMaps', builders.sourceMaps)\n .option('stripComments', builders.stripComments)\n .option('transpilationMode', builders.transpilationMode)\n .option('depsToTranspile', builders.depsToTranspile)\n .option('manifest', builders.manifest)\n .option('writeManifest', builders.writeManifest)\n .option('serve', builders.serve)\n .option('root', builders.root)\n .option('port', builders.port)\n .implies('writeManifest', 'manifest')\n .implies('depsToTranspile', 'transpilationMode')\n .middleware((argv) => processInvalidTranspilation(argv as any));\n },\n handler: async (argv: YargsArgs) => watch(argv),\n};\n"]}
@@ -0,0 +1,14 @@
1
+ import { YargsArgs } from '../../types/yargs';
2
+ /**
3
+ * Watch a directory and its subdirectories for changes, and build when files
4
+ * are added or changed.
5
+ *
6
+ * Ignores 'node_modules' and dotfiles.
7
+ * Creates destination directory if it doesn't exist.
8
+ *
9
+ * @param argv - Arguments as an object generated by Yargs.
10
+ * @param argv.src - The source file path.
11
+ * @param argv.dist - The output directory path.
12
+ * @param argv.'outfileName' - The output file name.
13
+ */
14
+ export declare function watch(argv: YargsArgs): Promise<void>;
@@ -0,0 +1,100 @@
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.watch = void 0;
7
+ const snaps_utils_1 = require("@metamask/snaps-utils");
8
+ const chokidar_1 = __importDefault(require("chokidar"));
9
+ const utils_1 = require("../../utils");
10
+ const bundle_1 = require("../build/bundle");
11
+ const evalHandler_1 = require("../eval/evalHandler");
12
+ const manifestHandler_1 = require("../manifest/manifestHandler");
13
+ const serveHandler_1 = require("../serve/serveHandler");
14
+ /**
15
+ * Watch a directory and its subdirectories for changes, and build when files
16
+ * are added or changed.
17
+ *
18
+ * Ignores 'node_modules' and dotfiles.
19
+ * Creates destination directory if it doesn't exist.
20
+ *
21
+ * @param argv - Arguments as an object generated by Yargs.
22
+ * @param argv.src - The source file path.
23
+ * @param argv.dist - The output directory path.
24
+ * @param argv.'outfileName' - The output file name.
25
+ */
26
+ async function watch(argv) {
27
+ const { dist, eval: shouldEval, manifest, outfileName, src, serve: shouldServe, } = argv;
28
+ if (outfileName) {
29
+ (0, snaps_utils_1.validateOutfileName)(outfileName);
30
+ }
31
+ await (0, snaps_utils_1.validateFilePath)(src);
32
+ await (0, snaps_utils_1.validateDirPath)(dist, true);
33
+ const rootDir = src.includes('/')
34
+ ? src.substring(0, src.lastIndexOf('/') + 1)
35
+ : '.';
36
+ const outfilePath = (0, snaps_utils_1.getOutfilePath)(dist, outfileName);
37
+ const buildSnap = async (path, logMessage) => {
38
+ if (logMessage !== undefined) {
39
+ (0, snaps_utils_1.logInfo)(logMessage);
40
+ }
41
+ try {
42
+ await (0, bundle_1.bundle)(src, outfilePath, argv, (0, utils_1.loadConfig)().bundlerCustomizer);
43
+ if (manifest) {
44
+ await (0, manifestHandler_1.manifestHandler)(argv);
45
+ }
46
+ if (shouldEval) {
47
+ await (0, evalHandler_1.evalHandler)({ ...argv, bundle: outfilePath });
48
+ }
49
+ }
50
+ catch (error) {
51
+ (0, snaps_utils_1.logError)(`Error ${path === undefined
52
+ ? 'during initial build'
53
+ : `while processing "${path}"`}.`, error);
54
+ }
55
+ };
56
+ chokidar_1.default
57
+ .watch(rootDir, {
58
+ ignoreInitial: true,
59
+ ignored: [
60
+ '**/node_modules/**',
61
+ `**/${dist}/**`,
62
+ `**/test/**`,
63
+ `**/tests/**`,
64
+ `**/*.test.js`,
65
+ `**/*.test.ts`,
66
+ /* istanbul ignore next */
67
+ (str) => str !== '.' && str.startsWith('.'),
68
+ ],
69
+ })
70
+ .on('ready', () => {
71
+ buildSnap()
72
+ .then(() => {
73
+ if (shouldServe) {
74
+ return (0, serveHandler_1.serve)(argv);
75
+ }
76
+ return undefined;
77
+ })
78
+ .catch((error) => {
79
+ (0, snaps_utils_1.logError)('Error during initial build.', error);
80
+ });
81
+ })
82
+ .on('add', (path) => {
83
+ buildSnap(path, `File added: ${path}`).catch((error) => {
84
+ (0, snaps_utils_1.logError)(`Error while processing "${path}".`, error);
85
+ });
86
+ })
87
+ .on('change', (path) => {
88
+ buildSnap(path, `File changed: ${path}`).catch((error) => {
89
+ (0, snaps_utils_1.logError)(`Error while processing "${path}".`, error);
90
+ });
91
+ })
92
+ .on('unlink', (path) => (0, snaps_utils_1.logInfo)(`File removed: ${path}`))
93
+ .on('error', (error) => {
94
+ (0, snaps_utils_1.logError)(`Watcher error: ${error.message}`, error);
95
+ })
96
+ .add(rootDir);
97
+ (0, snaps_utils_1.logInfo)(`Watching '${rootDir}' for changes...`);
98
+ }
99
+ exports.watch = watch;
100
+ //# sourceMappingURL=watchHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watchHandler.js","sourceRoot":"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,iBAAiB,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACvD,IAAA,sBAAQ,EAAC,2BAA2B,IAAI,IAAI,EAAE,KAAK,CAAC,CAAC;QACvD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;SACD,EAAE,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAA,qBAAO,EAAC,iBAAiB,IAAI,EAAE,CAAC,CAAC;SACxD,EAAE,CAAC,OAAO,EAAE,CAAC,KAAY,EAAE,EAAE;QAC5B,IAAA,sBAAQ,EAAC,kBAAkB,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,CAAC,CAAC;IACrD,CAAC,CAAC;SAED,GAAG,CAAC,OAAO,CAAC,CAAC;IAEhB,IAAA,qBAAO,EAAC,aAAa,OAAO,kBAAkB,CAAC,CAAC;AAClD,CAAC;AA5FD,sBA4FC","sourcesContent":["import {\n getOutfilePath,\n logError,\n logInfo,\n validateDirPath,\n validateFilePath,\n validateOutfileName,\n} from '@metamask/snaps-utils';\nimport chokidar from 'chokidar';\n\nimport { YargsArgs } from '../../types/yargs';\nimport { loadConfig } from '../../utils';\nimport { bundle } from '../build/bundle';\nimport { evalHandler } from '../eval/evalHandler';\nimport { manifestHandler } from '../manifest/manifestHandler';\nimport { serve } from '../serve/serveHandler';\n\n/**\n * Watch a directory and its subdirectories for changes, and build when files\n * are added or changed.\n *\n * Ignores 'node_modules' and dotfiles.\n * Creates destination directory if it doesn't exist.\n *\n * @param argv - Arguments as an object generated by Yargs.\n * @param argv.src - The source file path.\n * @param argv.dist - The output directory path.\n * @param argv.'outfileName' - The output file name.\n */\nexport async function watch(argv: YargsArgs): Promise<void> {\n const {\n dist,\n eval: shouldEval,\n manifest,\n outfileName,\n src,\n serve: shouldServe,\n } = argv;\n if (outfileName) {\n validateOutfileName(outfileName);\n }\n await validateFilePath(src);\n await validateDirPath(dist, true);\n const rootDir = src.includes('/')\n ? src.substring(0, src.lastIndexOf('/') + 1)\n : '.';\n const outfilePath = getOutfilePath(dist, outfileName);\n\n const buildSnap = async (path?: string, logMessage?: string) => {\n if (logMessage !== undefined) {\n logInfo(logMessage);\n }\n\n try {\n await bundle(src, outfilePath, argv, loadConfig().bundlerCustomizer);\n\n if (manifest) {\n await manifestHandler(argv);\n }\n\n if (shouldEval) {\n await evalHandler({ ...argv, bundle: outfilePath });\n }\n } catch (error) {\n logError(\n `Error ${\n path === undefined\n ? 'during initial build'\n : `while processing \"${path}\"`\n }.`,\n error,\n );\n }\n };\n\n chokidar\n .watch(rootDir, {\n ignoreInitial: true,\n ignored: [\n '**/node_modules/**',\n `**/${dist}/**`,\n `**/test/**`,\n `**/tests/**`,\n `**/*.test.js`,\n `**/*.test.ts`,\n /* istanbul ignore next */\n (str: string) => str !== '.' && str.startsWith('.'),\n ],\n })\n\n .on('ready', () => {\n buildSnap()\n .then(() => {\n if (shouldServe) {\n return serve(argv);\n }\n\n return undefined;\n })\n .catch((error) => {\n logError('Error during initial build.', error);\n });\n })\n .on('add', (path) => {\n buildSnap(path, `File added: ${path}`).catch((error) => {\n logError(`Error while processing \"${path}\".`, error);\n });\n })\n .on('change', (path) => {\n buildSnap(path, `File changed: ${path}`).catch((error) => {\n logError(`Error while processing \"${path}\".`, error);\n });\n })\n .on('unlink', (path) => logInfo(`File removed: ${path}`))\n .on('error', (error: Error) => {\n logError(`Watcher error: ${error.message}`, error);\n })\n\n .add(rootDir);\n\n logInfo(`Watching '${rootDir}' for changes...`);\n}\n"]}
package/dist/main.js ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const cli_1 = require("./cli");
8
+ const cmds_1 = __importDefault(require("./cmds"));
9
+ global.snaps = {
10
+ verboseErrors: false,
11
+ suppressWarnings: false,
12
+ isWatching: false,
13
+ };
14
+ (0, cli_1.cli)(process.argv, cmds_1.default);
15
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;;;AAEA,+BAA4B;AAC5B,kDAA8B;AAE9B,MAAM,CAAC,KAAK,GAAG;IACb,aAAa,EAAE,KAAK;IACpB,gBAAgB,EAAE,KAAK;IACvB,UAAU,EAAE,KAAK;CAClB,CAAC;AAEF,IAAA,SAAG,EAAC,OAAO,CAAC,IAAI,EAAE,cAAQ,CAAC,CAAC","sourcesContent":["#!/usr/bin/env node\n\nimport { cli } from './cli';\nimport commands from './cmds';\n\nglobal.snaps = {\n verboseErrors: false,\n suppressWarnings: false,\n isWatching: false,\n};\n\ncli(process.argv, commands);\n"]}
@@ -0,0 +1,2 @@
1
+ export * from './misc';
2
+ export * from './snap-config';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./misc"), exports);
18
+ __exportStar(require("./snap-config"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,gDAA8B","sourcesContent":["export * from './misc';\nexport * from './snap-config';\n"]}
@@ -0,0 +1,53 @@
1
+ import { Arguments } from 'yargs';
2
+ export declare const permRequestKeys: string[];
3
+ export declare const CONFIG_FILE = "snap.config.js";
4
+ /**
5
+ * Sets global variable snaps which tracks user settings:
6
+ * watch mode activation, verbose errors messages, and whether to suppress
7
+ * warnings.
8
+ *
9
+ * @param argv - Arguments as an object generated by `yargs`.
10
+ */
11
+ export declare function setSnapGlobals(argv: Arguments): void;
12
+ /**
13
+ * Attempts to convert a string to a boolean and throws if the value is invalid.
14
+ *
15
+ * @param value - The value to convert to a boolean.
16
+ * @returns `true` if the value is the string `"true"`, `false` if it is the
17
+ * string `"false"`, the value if it is already a boolean, or an error
18
+ * otherwise.
19
+ */
20
+ export declare function booleanStringToBoolean(value: unknown): boolean;
21
+ /**
22
+ * Sanitizes inputs. Currently normalizes "./" paths to ".".
23
+ * Yargs handles other path normalization as specified in builders.
24
+ *
25
+ * @param argv - Arguments as an object generated by yargs.
26
+ */
27
+ export declare function sanitizeInputs(argv: Arguments): void;
28
+ /**
29
+ * Logs an error message to console. Logs original error if it exists and
30
+ * the verboseErrors global is true.
31
+ *
32
+ * @param message - The error message.
33
+ * @param error - The original error.
34
+ */
35
+ export declare function logError(message: string | null, error?: Error): void;
36
+ /**
37
+ * Logs an error, attempts to unlink the destination file, and kills the
38
+ * process.
39
+ *
40
+ * @param prefix - The message prefix.
41
+ * @param message - The error message.
42
+ * @param error - The original error.
43
+ * @param destFilePath - The output file path.
44
+ */
45
+ export declare function writeError(prefix: string, message: string, error: Error, destFilePath?: string): Promise<void>;
46
+ /**
47
+ * Trims leading and trailing periods "." and forward slashes "/" from the
48
+ * given path string.
49
+ *
50
+ * @param pathString - The path string to trim.
51
+ * @returns The trimmed path string.
52
+ */
53
+ export declare function trimPathString(pathString: string): string;
@@ -0,0 +1,156 @@
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.trimPathString = exports.writeError = exports.logError = exports.sanitizeInputs = exports.booleanStringToBoolean = exports.setSnapGlobals = exports.CONFIG_FILE = exports.permRequestKeys = void 0;
7
+ const snaps_utils_1 = require("@metamask/snaps-utils");
8
+ const utils_1 = require("@metamask/utils");
9
+ const fs_1 = require("fs");
10
+ const path_1 = __importDefault(require("path"));
11
+ exports.permRequestKeys = [
12
+ '@context',
13
+ 'id',
14
+ 'parentCapability',
15
+ 'invoker',
16
+ 'date',
17
+ 'caveats',
18
+ 'proof',
19
+ ];
20
+ exports.CONFIG_FILE = 'snap.config.js';
21
+ // CLI arguments whose values are file paths
22
+ const pathArguments = new Set([
23
+ 'src',
24
+ 's',
25
+ 'dist',
26
+ 'd',
27
+ 'bundle',
28
+ 'b',
29
+ 'root',
30
+ 'r',
31
+ ]);
32
+ /**
33
+ * Sets global variable snaps which tracks user settings:
34
+ * watch mode activation, verbose errors messages, and whether to suppress
35
+ * warnings.
36
+ *
37
+ * @param argv - Arguments as an object generated by `yargs`.
38
+ */
39
+ function setSnapGlobals(argv) {
40
+ if (['w', 'watch'].includes(argv._[0])) {
41
+ global.snaps.isWatching = true;
42
+ }
43
+ else {
44
+ global.snaps.isWatching = false;
45
+ }
46
+ if ((0, utils_1.hasProperty)(argv, 'verboseErrors')) {
47
+ global.snaps.verboseErrors = booleanStringToBoolean(argv.verboseErrors);
48
+ }
49
+ if ((0, utils_1.hasProperty)(argv, 'suppressWarnings')) {
50
+ global.snaps.suppressWarnings = booleanStringToBoolean(argv.suppressWarnings);
51
+ }
52
+ }
53
+ exports.setSnapGlobals = setSnapGlobals;
54
+ /**
55
+ * Attempts to convert a string to a boolean and throws if the value is invalid.
56
+ *
57
+ * @param value - The value to convert to a boolean.
58
+ * @returns `true` if the value is the string `"true"`, `false` if it is the
59
+ * string `"false"`, the value if it is already a boolean, or an error
60
+ * otherwise.
61
+ */
62
+ function booleanStringToBoolean(value) {
63
+ if (typeof value === 'boolean') {
64
+ return value;
65
+ }
66
+ else if (value === 'true') {
67
+ return true;
68
+ }
69
+ else if (value === 'false') {
70
+ return false;
71
+ }
72
+ throw new Error(`Expected a boolean or the strings "true" or "false". Received: "${String(value)}"`);
73
+ }
74
+ exports.booleanStringToBoolean = booleanStringToBoolean;
75
+ /**
76
+ * Sanitizes inputs. Currently normalizes "./" paths to ".".
77
+ * Yargs handles other path normalization as specified in builders.
78
+ *
79
+ * @param argv - Arguments as an object generated by yargs.
80
+ */
81
+ function sanitizeInputs(argv) {
82
+ Object.keys(argv).forEach((key) => {
83
+ if (typeof argv[key] === 'string') {
84
+ // Node's path.normalize() does not do this
85
+ if (argv[key] === './') {
86
+ argv[key] = '.';
87
+ }
88
+ if (pathArguments.has(key)) {
89
+ argv[key] = path_1.default.normalize(argv[key]);
90
+ }
91
+ }
92
+ });
93
+ }
94
+ exports.sanitizeInputs = sanitizeInputs;
95
+ /**
96
+ * Logs an error message to console. Logs original error if it exists and
97
+ * the verboseErrors global is true.
98
+ *
99
+ * @param message - The error message.
100
+ * @param error - The original error.
101
+ */
102
+ function logError(message, error) {
103
+ if (message !== null) {
104
+ (0, snaps_utils_1.logError)(message);
105
+ }
106
+ if (error && global.snaps.verboseErrors) {
107
+ (0, snaps_utils_1.logError)(error);
108
+ }
109
+ if (message === null && (!error || (error && !global.snaps.verboseErrors))) {
110
+ (0, snaps_utils_1.logError)('Unknown error.');
111
+ }
112
+ }
113
+ exports.logError = logError;
114
+ /**
115
+ * Logs an error, attempts to unlink the destination file, and kills the
116
+ * process.
117
+ *
118
+ * @param prefix - The message prefix.
119
+ * @param message - The error message.
120
+ * @param error - The original error.
121
+ * @param destFilePath - The output file path.
122
+ */
123
+ async function writeError(prefix, message, error, destFilePath) {
124
+ let processedPrefix = prefix;
125
+ if (!prefix.endsWith(' ')) {
126
+ processedPrefix += ' ';
127
+ }
128
+ logError(processedPrefix + message, error);
129
+ try {
130
+ if (destFilePath) {
131
+ await fs_1.promises.unlink(destFilePath);
132
+ }
133
+ }
134
+ catch (unlinkError) {
135
+ logError(`${processedPrefix}Failed to unlink mangled file.`, unlinkError);
136
+ }
137
+ // unless the watcher is active, exit
138
+ if (!global.snaps.isWatching) {
139
+ // TODO(ritave): Remove process exit and change into collapse of functions
140
+ // https://github.com/MetaMask/snaps-monorepo/issues/81
141
+ process.exit(1);
142
+ }
143
+ }
144
+ exports.writeError = writeError;
145
+ /**
146
+ * Trims leading and trailing periods "." and forward slashes "/" from the
147
+ * given path string.
148
+ *
149
+ * @param pathString - The path string to trim.
150
+ * @returns The trimmed path string.
151
+ */
152
+ function trimPathString(pathString) {
153
+ return pathString.replace(/^[./]+|[./]+$/gu, '');
154
+ }
155
+ exports.trimPathString = trimPathString;
156
+ //# sourceMappingURL=misc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"misc.js","sourceRoot":"","sources":["../../src/utils/misc.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAiE;AACjE,2CAA8C;AAC9C,2BAA4C;AAC5C,gDAAwB;AAGX,QAAA,eAAe,GAAG;IAC7B,UAAU;IACV,IAAI;IACJ,kBAAkB;IAClB,SAAS;IACT,MAAM;IACN,SAAS;IACT,OAAO;CACR,CAAC;AAEW,QAAA,WAAW,GAAG,gBAAgB,CAAC;AAE5C,4CAA4C;AAC5C,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC;IAC5B,KAAK;IACL,GAAG;IACH,MAAM;IACN,GAAG;IACH,QAAQ;IACR,GAAG;IACH,MAAM;IACN,GAAG;CACJ,CAAC,CAAC;AAEH;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,IAAe;IAC5C,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAW,CAAC,EAAE;QAChD,MAAM,CAAC,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC;KACh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C/C,OAAO,UAAU,CAAC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC,CAAC;AACnD,CAAC;AAFD,wCAEC","sourcesContent":["import { logError as logErrorUtil } from '@metamask/snaps-utils';\nimport { hasProperty } from '@metamask/utils';\nimport { promises as filesystem } from 'fs';\nimport path from 'path';\nimport { Arguments } from 'yargs';\n\nexport const permRequestKeys = [\n '@context',\n 'id',\n 'parentCapability',\n 'invoker',\n 'date',\n 'caveats',\n 'proof',\n];\n\nexport const CONFIG_FILE = 'snap.config.js';\n\n// CLI arguments whose values are file paths\nconst pathArguments = new Set([\n 'src',\n 's',\n 'dist',\n 'd',\n 'bundle',\n 'b',\n 'root',\n 'r',\n]);\n\n/**\n * Sets global variable snaps which tracks user settings:\n * watch mode activation, verbose errors messages, and whether to suppress\n * warnings.\n *\n * @param argv - Arguments as an object generated by `yargs`.\n */\nexport function setSnapGlobals(argv: Arguments) {\n if (['w', 'watch'].includes(argv._[0] as string)) {\n global.snaps.isWatching = true;\n } else {\n global.snaps.isWatching = false;\n }\n\n if (hasProperty(argv, 'verboseErrors')) {\n global.snaps.verboseErrors = booleanStringToBoolean(argv.verboseErrors);\n }\n\n if (hasProperty(argv, 'suppressWarnings')) {\n global.snaps.suppressWarnings = booleanStringToBoolean(\n argv.suppressWarnings,\n );\n }\n}\n\n/**\n * Attempts to convert a string to a boolean and throws if the value is invalid.\n *\n * @param value - The value to convert to a boolean.\n * @returns `true` if the value is the string `\"true\"`, `false` if it is the\n * string `\"false\"`, the value if it is already a boolean, or an error\n * otherwise.\n */\nexport function booleanStringToBoolean(value: unknown): boolean {\n if (typeof value === 'boolean') {\n return value;\n } else if (value === 'true') {\n return true;\n } else if (value === 'false') {\n return false;\n }\n\n throw new Error(\n `Expected a boolean or the strings \"true\" or \"false\". Received: \"${String(\n value,\n )}\"`,\n );\n}\n\n/**\n * Sanitizes inputs. Currently normalizes \"./\" paths to \".\".\n * Yargs handles other path normalization as specified in builders.\n *\n * @param argv - Arguments as an object generated by yargs.\n */\nexport function sanitizeInputs(argv: Arguments) {\n Object.keys(argv).forEach((key) => {\n if (typeof argv[key] === 'string') {\n // Node's path.normalize() does not do this\n if (argv[key] === './') {\n argv[key] = '.';\n }\n\n if (pathArguments.has(key)) {\n argv[key] = path.normalize(argv[key] as string);\n }\n }\n });\n}\n\n/**\n * Logs an error message to console. Logs original error if it exists and\n * the verboseErrors global is true.\n *\n * @param message - The error message.\n * @param error - The original error.\n */\nexport function logError(message: string | null, error?: Error): void {\n if (message !== null) {\n logErrorUtil(message);\n }\n\n if (error && global.snaps.verboseErrors) {\n logErrorUtil(error);\n }\n\n if (message === null && (!error || (error && !global.snaps.verboseErrors))) {\n logErrorUtil('Unknown error.');\n }\n}\n\n/**\n * Logs an error, attempts to unlink the destination file, and kills the\n * process.\n *\n * @param prefix - The message prefix.\n * @param message - The error message.\n * @param error - The original error.\n * @param destFilePath - The output file path.\n */\nexport async function writeError(\n prefix: string,\n message: string,\n error: Error,\n destFilePath?: string,\n): Promise<void> {\n let processedPrefix = prefix;\n if (!prefix.endsWith(' ')) {\n processedPrefix += ' ';\n }\n\n logError(processedPrefix + message, error);\n try {\n if (destFilePath) {\n await filesystem.unlink(destFilePath);\n }\n } catch (unlinkError) {\n logError(`${processedPrefix}Failed to unlink mangled file.`, unlinkError);\n }\n\n // unless the watcher is active, exit\n if (!global.snaps.isWatching) {\n // TODO(ritave): Remove process exit and change into collapse of functions\n // https://github.com/MetaMask/snaps-monorepo/issues/81\n process.exit(1);\n }\n}\n\n/**\n * Trims leading and trailing periods \".\" and forward slashes \"/\" from the\n * given path string.\n *\n * @param pathString - The path string to trim.\n * @returns The trimmed path string.\n */\nexport function trimPathString(pathString: string): string {\n return pathString.replace(/^[./]+|[./]+$/gu, '');\n}\n"]}
@@ -0,0 +1,48 @@
1
+ import type browserify from 'browserify';
2
+ import { Infer } from 'superstruct';
3
+ import { Arguments } from 'yargs';
4
+ import yargs from 'yargs/yargs';
5
+ export declare type BundleCustomizer = (bundler: browserify.BrowserifyObject) => void;
6
+ export declare const SnapConfigStruct: import("superstruct").Struct<{
7
+ cliOptions?: Record<string, unknown> | undefined;
8
+ bundlerCustomizer?: Function | undefined;
9
+ }, {
10
+ cliOptions: import("superstruct").Struct<Record<string, unknown> | undefined, null>;
11
+ bundlerCustomizer: import("superstruct").Struct<Function | undefined, null>;
12
+ }>;
13
+ export declare type SnapConfig = Omit<Infer<typeof SnapConfigStruct>, 'bundlerCustomizer'> & {
14
+ bundlerCustomizer?: BundleCustomizer;
15
+ };
16
+ /**
17
+ * Check if the given value is a {@link SnapConfig} object. Note that this
18
+ * function does not check the validity of the `bundleCustomizer` property, as
19
+ * it is not possible to check the validity of a function in JavaScript.
20
+ *
21
+ * @param value - The value to check.
22
+ * @returns `true` if the value is a valid {@link SnapConfig} object, `false`
23
+ * otherwise.
24
+ */
25
+ export declare function isSnapConfig(value: unknown): value is SnapConfig;
26
+ /**
27
+ * Attempt to load the snap config file (`snap.config.js`). By default will use
28
+ * the cached config, if it was loaded before, and `cached` is `true`. If the
29
+ * config file is not found, or the config is invalid, this function will kill
30
+ * the process.
31
+ *
32
+ * @param cached - Whether to use the cached config. Defaults to `true`.
33
+ * @returns The snap config.
34
+ */
35
+ export declare function loadConfig(cached?: boolean): SnapConfig;
36
+ /**
37
+ * Attempts to read configuration options for package.json and the config file,
38
+ * and apply them to argv if they weren't already set.
39
+ *
40
+ * Arguments are only set per the snap-cli config file if they were not specified
41
+ * on the command line.
42
+ *
43
+ * @param snapConfig - The snap config.
44
+ * @param processArgv - The command line arguments, i.e., `process.argv`.
45
+ * @param yargsArgv - The processed `yargs` arguments.
46
+ * @param yargsInstance - An instance of `yargs`.
47
+ */
48
+ export declare function applyConfig(snapConfig: SnapConfig, processArgv: string[], yargsArgv: Arguments, yargsInstance: typeof yargs): void;