@metamask/snaps-cli 0.35.0-flask.1 → 1.0.0-prerelease.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 (153) hide show
  1. package/CHANGELOG.md +3 -26
  2. package/README.md +6 -5
  3. package/dist/{esm/builders.js → builders.js} +29 -27
  4. package/dist/{cjs/builders.js.map → builders.js.map} +1 -1
  5. package/dist/cli.js +51 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/cmds/build/buildHandler.js +37 -0
  8. package/dist/cmds/build/buildHandler.js.map +1 -0
  9. package/dist/cmds/build/bundle.js +80 -0
  10. package/dist/cmds/build/bundle.js.map +1 -0
  11. package/dist/{types/cmds/serve → cmds/build}/index.d.ts +2 -2
  12. package/dist/cmds/build/index.js +29 -0
  13. package/dist/cmds/build/index.js.map +1 -0
  14. package/dist/{esm/cmds → cmds}/build/utils.js +34 -22
  15. package/dist/cmds/build/utils.js.map +1 -0
  16. package/dist/cmds/eval/evalHandler.js +25 -0
  17. package/dist/cmds/eval/evalHandler.js.map +1 -0
  18. package/dist/{types/cmds/manifest → cmds/eval}/index.d.ts +2 -2
  19. package/dist/cmds/eval/index.js +15 -0
  20. package/dist/cmds/eval/index.js.map +1 -0
  21. package/dist/cmds/index.js +14 -0
  22. package/dist/cmds/index.js.map +1 -0
  23. package/dist/{types/cmds/build → cmds/init}/index.d.ts +2 -2
  24. package/dist/cmds/init/index.js +34 -0
  25. package/dist/cmds/init/index.js.map +1 -0
  26. package/dist/cmds/init/initHandler.d.ts +26 -0
  27. package/dist/cmds/init/initHandler.js +69 -0
  28. package/dist/cmds/init/initHandler.js.map +1 -0
  29. package/dist/cmds/init/initUtils.d.ts +39 -0
  30. package/dist/cmds/init/initUtils.js +122 -0
  31. package/dist/cmds/init/initUtils.js.map +1 -0
  32. package/dist/{types/cmds/eval → cmds/manifest}/index.d.ts +2 -2
  33. package/dist/cmds/manifest/index.js +24 -0
  34. package/dist/cmds/manifest/index.js.map +1 -0
  35. package/dist/cmds/manifest/manifestHandler.js +50 -0
  36. package/dist/cmds/manifest/manifestHandler.js.map +1 -0
  37. package/dist/cmds/serve/index.d.ts +9 -0
  38. package/dist/cmds/serve/index.js +15 -0
  39. package/dist/cmds/serve/index.js.map +1 -0
  40. package/dist/cmds/serve/serveHandler.js +59 -0
  41. package/dist/cmds/serve/serveHandler.js.map +1 -0
  42. package/dist/cmds/serve/serveUtils.js +39 -0
  43. package/dist/cmds/serve/serveUtils.js.map +1 -0
  44. package/dist/cmds/watch/index.d.ts +9 -0
  45. package/dist/cmds/watch/index.js +32 -0
  46. package/dist/cmds/watch/index.js.map +1 -0
  47. package/dist/cmds/watch/watchHandler.js +100 -0
  48. package/dist/cmds/watch/watchHandler.js.map +1 -0
  49. package/dist/main.js +15 -0
  50. package/dist/main.js.map +1 -0
  51. package/dist/utils/index.js +19 -0
  52. package/dist/utils/index.js.map +1 -0
  53. package/dist/{esm/utils → utils}/misc.js +50 -32
  54. package/dist/utils/misc.js.map +1 -0
  55. package/dist/{esm/utils → utils}/snap-config.js +39 -26
  56. package/dist/utils/snap-config.js.map +1 -0
  57. package/package.json +27 -65
  58. package/dist/cjs/builders.js +0 -149
  59. package/dist/cjs/cli.js +0 -35
  60. package/dist/cjs/cli.js.map +0 -1
  61. package/dist/cjs/cmds/build/buildHandler.js +0 -36
  62. package/dist/cjs/cmds/build/buildHandler.js.map +0 -1
  63. package/dist/cjs/cmds/build/bundle.js +0 -83
  64. package/dist/cjs/cmds/build/bundle.js.map +0 -1
  65. package/dist/cjs/cmds/build/index.js +0 -32
  66. package/dist/cjs/cmds/build/index.js.map +0 -1
  67. package/dist/cjs/cmds/build/utils.js +0 -79
  68. package/dist/cjs/cmds/build/utils.js.map +0 -1
  69. package/dist/cjs/cmds/eval/evalHandler.js +0 -24
  70. package/dist/cjs/cmds/eval/evalHandler.js.map +0 -1
  71. package/dist/cjs/cmds/eval/index.js +0 -31
  72. package/dist/cjs/cmds/eval/index.js.map +0 -1
  73. package/dist/cjs/cmds/index.js +0 -30
  74. package/dist/cjs/cmds/index.js.map +0 -1
  75. package/dist/cjs/cmds/manifest/index.js +0 -44
  76. package/dist/cjs/cmds/manifest/index.js.map +0 -1
  77. package/dist/cjs/cmds/manifest/manifestHandler.js +0 -46
  78. package/dist/cjs/cmds/manifest/manifestHandler.js.map +0 -1
  79. package/dist/cjs/cmds/serve/index.js +0 -31
  80. package/dist/cjs/cmds/serve/index.js.map +0 -1
  81. package/dist/cjs/cmds/serve/serveHandler.js +0 -62
  82. package/dist/cjs/cmds/serve/serveHandler.js.map +0 -1
  83. package/dist/cjs/cmds/serve/serveUtils.js +0 -37
  84. package/dist/cjs/cmds/serve/serveUtils.js.map +0 -1
  85. package/dist/cjs/cmds/watch/index.js +0 -32
  86. package/dist/cjs/cmds/watch/index.js.map +0 -1
  87. package/dist/cjs/cmds/watch/watchHandler.js +0 -91
  88. package/dist/cjs/cmds/watch/watchHandler.js.map +0 -1
  89. package/dist/cjs/index.js +0 -52
  90. package/dist/cjs/index.js.map +0 -1
  91. package/dist/cjs/main.js +0 -20
  92. package/dist/cjs/main.js.map +0 -1
  93. package/dist/cjs/utils/index.js +0 -21
  94. package/dist/cjs/utils/index.js.map +0 -1
  95. package/dist/cjs/utils/misc.js +0 -141
  96. package/dist/cjs/utils/misc.js.map +0 -1
  97. package/dist/cjs/utils/snap-config.js +0 -101
  98. package/dist/cjs/utils/snap-config.js.map +0 -1
  99. package/dist/esm/builders.js.map +0 -1
  100. package/dist/esm/cli.js +0 -26
  101. package/dist/esm/cli.js.map +0 -1
  102. package/dist/esm/cmds/build/buildHandler.js +0 -36
  103. package/dist/esm/cmds/build/buildHandler.js.map +0 -1
  104. package/dist/esm/cmds/build/bundle.js +0 -81
  105. package/dist/esm/cmds/build/bundle.js.map +0 -1
  106. package/dist/esm/cmds/build/index.js +0 -17
  107. package/dist/esm/cmds/build/index.js.map +0 -1
  108. package/dist/esm/cmds/build/utils.js.map +0 -1
  109. package/dist/esm/cmds/eval/evalHandler.js +0 -20
  110. package/dist/esm/cmds/eval/evalHandler.js.map +0 -1
  111. package/dist/esm/cmds/eval/index.js +0 -16
  112. package/dist/esm/cmds/eval/index.js.map +0 -1
  113. package/dist/esm/cmds/index.js +0 -15
  114. package/dist/esm/cmds/index.js.map +0 -1
  115. package/dist/esm/cmds/manifest/index.js +0 -29
  116. package/dist/esm/cmds/manifest/index.js.map +0 -1
  117. package/dist/esm/cmds/manifest/manifestHandler.js +0 -43
  118. package/dist/esm/cmds/manifest/manifestHandler.js.map +0 -1
  119. package/dist/esm/cmds/serve/index.js +0 -16
  120. package/dist/esm/cmds/serve/index.js.map +0 -1
  121. package/dist/esm/cmds/serve/serveHandler.js +0 -54
  122. package/dist/esm/cmds/serve/serveHandler.js.map +0 -1
  123. package/dist/esm/cmds/serve/serveUtils.js +0 -30
  124. package/dist/esm/cmds/serve/serveUtils.js.map +0 -1
  125. package/dist/esm/cmds/watch/index.js +0 -17
  126. package/dist/esm/cmds/watch/index.js.map +0 -1
  127. package/dist/esm/cmds/watch/watchHandler.js +0 -87
  128. package/dist/esm/cmds/watch/watchHandler.js.map +0 -1
  129. package/dist/esm/index.js +0 -3
  130. package/dist/esm/index.js.map +0 -1
  131. package/dist/esm/main.js +0 -11
  132. package/dist/esm/main.js.map +0 -1
  133. package/dist/esm/utils/index.js +0 -4
  134. package/dist/esm/utils/index.js.map +0 -1
  135. package/dist/esm/utils/misc.js.map +0 -1
  136. package/dist/esm/utils/snap-config.js.map +0 -1
  137. package/dist/types/cmds/watch/index.d.ts +0 -9
  138. package/dist/types/index.d.ts +0 -1
  139. /package/dist/{types/builders.d.ts → builders.d.ts} +0 -0
  140. /package/dist/{types/cli.d.ts → cli.d.ts} +0 -0
  141. /package/dist/{types/cmds → cmds}/build/buildHandler.d.ts +0 -0
  142. /package/dist/{types/cmds → cmds}/build/bundle.d.ts +0 -0
  143. /package/dist/{types/cmds → cmds}/build/utils.d.ts +0 -0
  144. /package/dist/{types/cmds → cmds}/eval/evalHandler.d.ts +0 -0
  145. /package/dist/{types/cmds → cmds}/index.d.ts +0 -0
  146. /package/dist/{types/cmds → cmds}/manifest/manifestHandler.d.ts +0 -0
  147. /package/dist/{types/cmds → cmds}/serve/serveHandler.d.ts +0 -0
  148. /package/dist/{types/cmds → cmds}/serve/serveUtils.d.ts +0 -0
  149. /package/dist/{types/cmds → cmds}/watch/watchHandler.d.ts +0 -0
  150. /package/dist/{types/main.d.ts → main.d.ts} +0 -0
  151. /package/dist/{types/utils → utils}/index.d.ts +0 -0
  152. /package/dist/{types/utils → utils}/misc.d.ts +0 -0
  153. /package/dist/{types/utils → utils}/snap-config.d.ts +0 -0
@@ -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,CAAC;IAE/C,IAAA,qBAAO,EAAC,sBAAsB,CAAC,CAAC;IAEhC,MAAM,MAAM,GAAG,cAAI,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC5C,IAAA,uBAAY,EAAC,GAAG,EAAE,GAAG,EAAE;YACrB,MAAM,EAAE,OAAiB;YACzB,OAAO,EAAE;gBACP;oBACE,MAAM,EAAE,MAAM;oBACd,OAAO,EAAE;wBACP;4BACE,GAAG,EAAE,eAAe;4BACpB,KAAK,EAAE,UAAU;yBAClB;wBACD;4BACE,GAAG,EAAE,6BAA6B;4BAClC,KAAK,EAAE,GAAG;yBACX;qBACF;iBACF;aACF;SACF,CAAC,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YAClB,IAAA,2BAAc,EAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YACjC,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC;YACrB,GAAG,CAAC,GAAG,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,IAAA,+BAAkB,EAAC,IAAI,CAAC,CAAC,CAAC;IAExD,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,IAAA,uBAAU,EAAC,OAAO,CAAC,CAAC,CAAC;IAEvD,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;QAC3B,IAAA,2BAAc,EAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QACjC,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;QACtB,IAAA,qBAAO,EAAC,eAAe,CAAC,CAAC;QACzB,OAAO,CAAC,QAAQ,GAAG,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,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,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":"","sources":["../../../src/cmds/watch/watchHandler.ts"],"names":[],"mappings":"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","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,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"]}
@@ -1,17 +1,23 @@
1
- import { logError as logErrorUtil } from '@metamask/snaps-utils';
2
- import { hasProperty } from '@metamask/utils';
3
- import { promises as filesystem } from 'fs';
4
- import path from 'path';
5
- export const permRequestKeys = [
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 = [
6
12
  '@context',
7
13
  'id',
8
14
  'parentCapability',
9
15
  'invoker',
10
16
  'date',
11
17
  'caveats',
12
- 'proof'
18
+ 'proof',
13
19
  ];
14
- export const CONFIG_FILE = 'snap.config.js';
20
+ exports.CONFIG_FILE = 'snap.config.js';
15
21
  // CLI arguments whose values are file paths
16
22
  const pathArguments = new Set([
17
23
  'src',
@@ -21,7 +27,7 @@ const pathArguments = new Set([
21
27
  'bundle',
22
28
  'b',
23
29
  'root',
24
- 'r'
30
+ 'r',
25
31
  ]);
26
32
  /**
27
33
  * Sets global variable snaps which tracks user settings:
@@ -29,22 +35,22 @@ const pathArguments = new Set([
29
35
  * warnings.
30
36
  *
31
37
  * @param argv - Arguments as an object generated by `yargs`.
32
- */ export function setSnapGlobals(argv) {
33
- if ([
34
- 'w',
35
- 'watch'
36
- ].includes(argv._[0])) {
38
+ */
39
+ function setSnapGlobals(argv) {
40
+ if (['w', 'watch'].includes(argv._[0])) {
37
41
  global.snaps.isWatching = true;
38
- } else {
42
+ }
43
+ else {
39
44
  global.snaps.isWatching = false;
40
45
  }
41
- if (hasProperty(argv, 'verboseErrors')) {
46
+ if ((0, utils_1.hasProperty)(argv, 'verboseErrors')) {
42
47
  global.snaps.verboseErrors = booleanStringToBoolean(argv.verboseErrors);
43
48
  }
44
- if (hasProperty(argv, 'suppressWarnings')) {
49
+ if ((0, utils_1.hasProperty)(argv, 'suppressWarnings')) {
45
50
  global.snaps.suppressWarnings = booleanStringToBoolean(argv.suppressWarnings);
46
51
  }
47
52
  }
53
+ exports.setSnapGlobals = setSnapGlobals;
48
54
  /**
49
55
  * Attempts to convert a string to a boolean and throws if the value is invalid.
50
56
  *
@@ -52,51 +58,59 @@ const pathArguments = new Set([
52
58
  * @returns `true` if the value is the string `"true"`, `false` if it is the
53
59
  * string `"false"`, the value if it is already a boolean, or an error
54
60
  * otherwise.
55
- */ export function booleanStringToBoolean(value) {
61
+ */
62
+ function booleanStringToBoolean(value) {
56
63
  if (typeof value === 'boolean') {
57
64
  return value;
58
- } else if (value === 'true') {
65
+ }
66
+ else if (value === 'true') {
59
67
  return true;
60
- } else if (value === 'false') {
68
+ }
69
+ else if (value === 'false') {
61
70
  return false;
62
71
  }
63
72
  throw new Error(`Expected a boolean or the strings "true" or "false". Received: "${String(value)}"`);
64
73
  }
74
+ exports.booleanStringToBoolean = booleanStringToBoolean;
65
75
  /**
66
76
  * Sanitizes inputs. Currently normalizes "./" paths to ".".
67
77
  * Yargs handles other path normalization as specified in builders.
68
78
  *
69
79
  * @param argv - Arguments as an object generated by yargs.
70
- */ export function sanitizeInputs(argv) {
71
- Object.keys(argv).forEach((key)=>{
80
+ */
81
+ function sanitizeInputs(argv) {
82
+ Object.keys(argv).forEach((key) => {
72
83
  if (typeof argv[key] === 'string') {
73
84
  // Node's path.normalize() does not do this
74
85
  if (argv[key] === './') {
75
86
  argv[key] = '.';
76
87
  }
77
88
  if (pathArguments.has(key)) {
78
- argv[key] = path.normalize(argv[key]);
89
+ argv[key] = path_1.default.normalize(argv[key]);
79
90
  }
80
91
  }
81
92
  });
82
93
  }
94
+ exports.sanitizeInputs = sanitizeInputs;
83
95
  /**
84
96
  * Logs an error message to console. Logs original error if it exists and
85
97
  * the verboseErrors global is true.
86
98
  *
87
99
  * @param message - The error message.
88
100
  * @param error - The original error.
89
- */ export function logError(message, error) {
101
+ */
102
+ function logError(message, error) {
90
103
  if (message !== null) {
91
- logErrorUtil(message);
104
+ (0, snaps_utils_1.logError)(message);
92
105
  }
93
106
  if (error && global.snaps.verboseErrors) {
94
- logErrorUtil(error);
107
+ (0, snaps_utils_1.logError)(error);
95
108
  }
96
- if (message === null && (!error || error && !global.snaps.verboseErrors)) {
97
- logErrorUtil('Unknown error.');
109
+ if (message === null && (!error || (error && !global.snaps.verboseErrors))) {
110
+ (0, snaps_utils_1.logError)('Unknown error.');
98
111
  }
99
112
  }
113
+ exports.logError = logError;
100
114
  /**
101
115
  * Logs an error, attempts to unlink the destination file, and kills the
102
116
  * process.
@@ -105,7 +119,8 @@ const pathArguments = new Set([
105
119
  * @param message - The error message.
106
120
  * @param error - The original error.
107
121
  * @param destFilePath - The output file path.
108
- */ export async function writeError(prefix, message, error, destFilePath) {
122
+ */
123
+ async function writeError(prefix, message, error, destFilePath) {
109
124
  let processedPrefix = prefix;
110
125
  if (!prefix.endsWith(' ')) {
111
126
  processedPrefix += ' ';
@@ -113,9 +128,10 @@ const pathArguments = new Set([
113
128
  logError(processedPrefix + message, error);
114
129
  try {
115
130
  if (destFilePath) {
116
- await filesystem.unlink(destFilePath);
131
+ await fs_1.promises.unlink(destFilePath);
117
132
  }
118
- } catch (unlinkError) {
133
+ }
134
+ catch (unlinkError) {
119
135
  logError(`${processedPrefix}Failed to unlink mangled file.`, unlinkError);
120
136
  }
121
137
  // unless the watcher is active, exit
@@ -125,14 +141,16 @@ const pathArguments = new Set([
125
141
  process.exit(1);
126
142
  }
127
143
  }
144
+ exports.writeError = writeError;
128
145
  /**
129
146
  * Trims leading and trailing periods "." and forward slashes "/" from the
130
147
  * given path string.
131
148
  *
132
149
  * @param pathString - The path string to trim.
133
150
  * @returns The trimmed path string.
134
- */ export function trimPathString(pathString) {
151
+ */
152
+ function trimPathString(pathString) {
135
153
  return pathString.replace(/^[./]+|[./]+$/gu, '');
136
154
  }
137
-
155
+ exports.trimPathString = trimPathString;
138
156
  //# sourceMappingURL=misc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"misc.js","sourceRoot":"","sources":["../../src/utils/misc.ts"],"names":[],"mappings":"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","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"]}
@@ -1,12 +1,18 @@
1
- import { hasProperty } from '@metamask/utils';
2
- import path from 'path';
3
- import { object, optional, func, is } from 'superstruct';
4
- import yargsParse from 'yargs-parser';
5
- import builders from '../builders';
6
- import { CONFIG_FILE, logError } from './misc';
7
- export const SnapConfigStruct = object({
8
- cliOptions: optional(object()),
9
- bundlerCustomizer: optional(func())
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.applyConfig = exports.loadConfig = exports.isSnapConfig = exports.SnapConfigStruct = void 0;
7
+ const utils_1 = require("@metamask/utils");
8
+ const path_1 = __importDefault(require("path"));
9
+ const superstruct_1 = require("superstruct");
10
+ const yargs_parser_1 = __importDefault(require("yargs-parser"));
11
+ const builders_1 = __importDefault(require("../builders"));
12
+ const misc_1 = require("./misc");
13
+ exports.SnapConfigStruct = (0, superstruct_1.object)({
14
+ cliOptions: (0, superstruct_1.optional)((0, superstruct_1.object)()),
15
+ bundlerCustomizer: (0, superstruct_1.optional)((0, superstruct_1.func)()),
10
16
  });
11
17
  /**
12
18
  * Check if the given value is a {@link SnapConfig} object. Note that this
@@ -16,9 +22,11 @@ export const SnapConfigStruct = object({
16
22
  * @param value - The value to check.
17
23
  * @returns `true` if the value is a valid {@link SnapConfig} object, `false`
18
24
  * otherwise.
19
- */ export function isSnapConfig(value) {
20
- return is(value, SnapConfigStruct);
25
+ */
26
+ function isSnapConfig(value) {
27
+ return (0, superstruct_1.is)(value, exports.SnapConfigStruct);
21
28
  }
29
+ exports.isSnapConfig = isSnapConfig;
22
30
  let snapConfigCache;
23
31
  /**
24
32
  * Attempt to load the snap config file (`snap.config.js`). By default will use
@@ -28,29 +36,32 @@ let snapConfigCache;
28
36
  *
29
37
  * @param cached - Whether to use the cached config. Defaults to `true`.
30
38
  * @returns The snap config.
31
- */ export function loadConfig(cached = true) {
39
+ */
40
+ function loadConfig(cached = true) {
32
41
  if (snapConfigCache !== undefined && cached) {
33
42
  return snapConfigCache;
34
43
  }
35
44
  let config;
36
45
  try {
37
46
  // eslint-disable-next-line node/global-require, import/no-dynamic-require, @typescript-eslint/no-require-imports
38
- config = require(path.resolve(process.cwd(), CONFIG_FILE));
39
- } catch (error) {
47
+ config = require(path_1.default.resolve(process.cwd(), misc_1.CONFIG_FILE));
48
+ }
49
+ catch (error) {
40
50
  if (error.code === 'MODULE_NOT_FOUND') {
41
51
  snapConfigCache = {};
42
52
  return snapConfigCache;
43
53
  }
44
- logError(`Error during parsing of ${CONFIG_FILE}`, error);
54
+ (0, misc_1.logError)(`Error during parsing of ${misc_1.CONFIG_FILE}`, error);
45
55
  return process.exit(1);
46
56
  }
47
57
  if (!isSnapConfig(config)) {
48
- logError(`Can't validate ${CONFIG_FILE}. Ensure it's a proper javascript file and abides with the structure of a snap configuration file`);
58
+ (0, misc_1.logError)(`Can't validate ${misc_1.CONFIG_FILE}. Ensure it's a proper javascript file and abides with the structure of a snap configuration file`);
49
59
  return process.exit(1);
50
60
  }
51
61
  snapConfigCache = config;
52
62
  return config;
53
63
  }
64
+ exports.loadConfig = loadConfig;
54
65
  // Note that the below function is necessary because yargs' .config() function
55
66
  // leaves much to be desired.
56
67
  //
@@ -67,7 +78,8 @@ let snapConfigCache;
67
78
  * @param processArgv - The command line arguments, i.e., `process.argv`.
68
79
  * @param yargsArgv - The processed `yargs` arguments.
69
80
  * @param yargsInstance - An instance of `yargs`.
70
- */ export function applyConfig(snapConfig, processArgv, yargsArgv, yargsInstance) {
81
+ */
82
+ function applyConfig(snapConfig, processArgv, yargsArgv, yargsInstance) {
71
83
  // Instances of yargs has a number of undocumented functions, including
72
84
  // getOptions. This function returns an object with properties "key" and
73
85
  // "alias", which specify the options associated with the current command and
@@ -80,25 +92,26 @@ let snapConfigCache;
80
92
  // If we set args that aren't valid for the current command, yargs will error
81
93
  // during validation.
82
94
  const { alias: aliases, key: options } = yargsInstance.getOptions();
83
- const parsedProcessArgv = yargsParse(processArgv, {
84
- alias: aliases
95
+ const parsedProcessArgv = (0, yargs_parser_1.default)(processArgv, {
96
+ alias: aliases,
85
97
  });
86
98
  delete parsedProcessArgv._; // irrelevant yargs parser artifact
87
99
  const commandOptions = new Set(Object.keys(options));
88
- const shouldSetArg = (key)=>{
89
- return commandOptions.has(key) && !hasProperty(parsedProcessArgv, key);
100
+ const shouldSetArg = (key) => {
101
+ return commandOptions.has(key) && !(0, utils_1.hasProperty)(parsedProcessArgv, key);
90
102
  };
91
103
  const config = snapConfig.cliOptions ?? {};
92
- for (const key of Object.keys(config)){
93
- if (hasProperty(builders, key)) {
104
+ for (const key of Object.keys(config)) {
105
+ if ((0, utils_1.hasProperty)(builders_1.default, key)) {
94
106
  if (shouldSetArg(key)) {
95
107
  yargsArgv[key] = config[key];
96
108
  }
97
- } else {
98
- logError(`Error: Encountered unrecognized config property "options.${key}" in config file "${CONFIG_FILE}". Remove the property and try again.`);
109
+ }
110
+ else {
111
+ (0, misc_1.logError)(`Error: Encountered unrecognized config property "options.${key}" in config file "${misc_1.CONFIG_FILE}". Remove the property and try again.`);
99
112
  process.exit(1);
100
113
  }
101
114
  }
102
115
  }
103
-
116
+ exports.applyConfig = applyConfig;
104
117
  //# sourceMappingURL=snap-config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snap-config.js","sourceRoot":"","sources":["../../src/utils/snap-config.ts"],"names":[],"mappings":";;;;;;AAAA,2CAA8C;AAE9C,gDAAwB;AACxB,6CAAgE;AAEhE,gEAAsC;AAGtC,2DAAmC;AACnC,iCAA+C;AAIlC,QAAA,gBAAgB,GAAG,IAAA,oBAAM,EAAC;IACrC,UAAU,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;IAC9B,iBAAiB,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,GAAE,CAAC;CACpC,CAAC,CAAC;AASH;;;;;;;;GAQG;AACH,SAAgB,YAAY,CAAC,KAAc;IACzC,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,wBAAgB,CAAC,CAAC;AACrC,CAAC;AAFD,oCAEC;AAED,IAAI,eAAuC,CAAC;AAE5C;;;;;;;;GAQG;AACH,SAAgB,UAAU,CAAC,MAAM,GAAG,IAAI;IACtC,IAAI,eAAe,KAAK,SAAS,IAAI,MAAM,EAAE;QAC3C,OAAO,eAAe,CAAC;KACxB;IAED,IAAI,MAAW,CAAC;IAChB,IAAI;QACF,iHAAiH;QACjH,MAAM,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,kBAAW,CAAC,CAAC,CAAC;KAC5D;IAAC,OAAO,KAAU,EAAE;QACnB,IAAI,KAAK,CAAC,IAAI,KAAK,kBAAkB,EAAE;YACrC,eAAe,GAAG,EAAE,CAAC;YACrB,OAAO,eAAe,CAAC;SACxB;QACD,IAAA,eAAQ,EAAC,2BAA2B,kBAAW,EAAE,EAAE,KAAK,CAAC,CAAC;QAC1D,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACxB;IAED,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE;QACzB,IAAA,eAAQ,EACN,kBAAkB,kBAAW,mGAAmG,CACjI,CAAC;QACF,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACxB;IACD,eAAe,GAAG,MAAM,CAAC;IACzB,OAAO,MAAM,CAAC;AAChB,CAAC;AA1BD,gCA0BC;AAED,8EAA8E;AAC9E,6BAA6B;AAC7B,EAAE;AACF,wEAAwE;AACxE,4DAA4D;AAE5D;;;;;;;;;;;GAWG;AACH,SAAgB,WAAW,CACzB,UAAsB,EACtB,WAAqB,EACrB,SAAoB,EACpB,aAA2B;IAE3B,uEAAuE;IACvE,wEAAwE;IACxE,6EAA6E;IAC7E,+BAA+B;IAC/B,EAAE;IACF,8EAA8E;IAC9E,2EAA2E;IAC3E,oBAAoB;IACpB,EAAE;IACF,6EAA6E;IAC7E,qBAAqB;IACrB,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GACpC,aACD,CAAC,UAAU,EAGX,CAAC;IAEF,MAAM,iBAAiB,GAAG,IAAA,sBAAU,EAAC,WAAW,EAAE;QAChD,KAAK,EAAE,OAAO;KACf,CAA4B,CAAC;IAC9B,OAAO,iBAAiB,CAAC,CAAC,CAAC,CAAC,mCAAmC;IAE/D,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAErD,MAAM,YAAY,GAAG,CAAC,GAAW,EAAW,EAAE;QAC5C,OAAO,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAA,mBAAW,EAAC,iBAAiB,EAAE,GAAG,CAAC,CAAC;IACzE,CAAC,CAAC;IAEF,MAAM,MAAM,GAA4B,UAAU,CAAC,UAAU,IAAI,EAAE,CAAC;IACpE,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;QACrC,IAAI,IAAA,mBAAW,EAAC,kBAAQ,EAAE,GAAG,CAAC,EAAE;YAC9B,IAAI,YAAY,CAAC,GAAG,CAAC,EAAE;gBACrB,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;aAC9B;SACF;aAAM;YACL,IAAA,eAAQ,EACN,4DAA4D,GAAG,qBAAqB,kBAAW,uCAAuC,CACvI,CAAC;YACF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;SACjB;KACF;AACH,CAAC;AAhDD,kCAgDC","sourcesContent":["import { hasProperty } from '@metamask/utils';\nimport type browserify from 'browserify';\nimport path from 'path';\nimport { object, optional, func, Infer, is } from 'superstruct';\nimport { Arguments } from 'yargs';\nimport yargsParse from 'yargs-parser';\nimport yargs from 'yargs/yargs';\n\nimport builders from '../builders';\nimport { CONFIG_FILE, logError } from './misc';\n\nexport type BundleCustomizer = (bundler: browserify.BrowserifyObject) => void;\n\nexport const SnapConfigStruct = object({\n cliOptions: optional(object()),\n bundlerCustomizer: optional(func()),\n});\n\nexport type SnapConfig = Omit<\n Infer<typeof SnapConfigStruct>,\n 'bundlerCustomizer'\n> & {\n bundlerCustomizer?: BundleCustomizer;\n};\n\n/**\n * Check if the given value is a {@link SnapConfig} object. Note that this\n * function does not check the validity of the `bundleCustomizer` property, as\n * it is not possible to check the validity of a function in JavaScript.\n *\n * @param value - The value to check.\n * @returns `true` if the value is a valid {@link SnapConfig} object, `false`\n * otherwise.\n */\nexport function isSnapConfig(value: unknown): value is SnapConfig {\n return is(value, SnapConfigStruct);\n}\n\nlet snapConfigCache: SnapConfig | undefined;\n\n/**\n * Attempt to load the snap config file (`snap.config.js`). By default will use\n * the cached config, if it was loaded before, and `cached` is `true`. If the\n * config file is not found, or the config is invalid, this function will kill\n * the process.\n *\n * @param cached - Whether to use the cached config. Defaults to `true`.\n * @returns The snap config.\n */\nexport function loadConfig(cached = true): SnapConfig {\n if (snapConfigCache !== undefined && cached) {\n return snapConfigCache;\n }\n\n let config: any;\n try {\n // eslint-disable-next-line node/global-require, import/no-dynamic-require, @typescript-eslint/no-require-imports\n config = require(path.resolve(process.cwd(), CONFIG_FILE));\n } catch (error: any) {\n if (error.code === 'MODULE_NOT_FOUND') {\n snapConfigCache = {};\n return snapConfigCache;\n }\n logError(`Error during parsing of ${CONFIG_FILE}`, error);\n return process.exit(1);\n }\n\n if (!isSnapConfig(config)) {\n logError(\n `Can't validate ${CONFIG_FILE}. Ensure it's a proper javascript file and abides with the structure of a snap configuration file`,\n );\n return process.exit(1);\n }\n snapConfigCache = config;\n return config;\n}\n\n// Note that the below function is necessary because yargs' .config() function\n// leaves much to be desired.\n//\n// In particular, it will set all properties included in the config file\n// regardless of the command, which fails during validation.\n\n/**\n * Attempts to read configuration options for package.json and the config file,\n * and apply them to argv if they weren't already set.\n *\n * Arguments are only set per the snap-cli config file if they were not specified\n * on the command line.\n *\n * @param snapConfig - The snap config.\n * @param processArgv - The command line arguments, i.e., `process.argv`.\n * @param yargsArgv - The processed `yargs` arguments.\n * @param yargsInstance - An instance of `yargs`.\n */\nexport function applyConfig(\n snapConfig: SnapConfig,\n processArgv: string[],\n yargsArgv: Arguments,\n yargsInstance: typeof yargs,\n): void {\n // Instances of yargs has a number of undocumented functions, including\n // getOptions. This function returns an object with properties \"key\" and\n // \"alias\", which specify the options associated with the current command and\n // their aliases, respectively.\n //\n // We leverage this to ensure that the config is only applied to args that are\n // valid for the current command, and that weren't specified by the user on\n // the command line.\n //\n // If we set args that aren't valid for the current command, yargs will error\n // during validation.\n const { alias: aliases, key: options } = (\n yargsInstance as any\n ).getOptions() as {\n alias: Record<string, string[]>;\n key: Record<string, unknown>;\n };\n\n const parsedProcessArgv = yargsParse(processArgv, {\n alias: aliases,\n }) as Record<string, unknown>;\n delete parsedProcessArgv._; // irrelevant yargs parser artifact\n\n const commandOptions = new Set(Object.keys(options));\n\n const shouldSetArg = (key: string): boolean => {\n return commandOptions.has(key) && !hasProperty(parsedProcessArgv, key);\n };\n\n const config: Record<string, unknown> = snapConfig.cliOptions ?? {};\n for (const key of Object.keys(config)) {\n if (hasProperty(builders, key)) {\n if (shouldSetArg(key)) {\n yargsArgv[key] = config[key];\n }\n } else {\n logError(\n `Error: Encountered unrecognized config property \"options.${key}\" in config file \"${CONFIG_FILE}\". Remove the property and try again.`,\n );\n process.exit(1);\n }\n }\n}\n"]}