@metamask/snaps-cli 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.0.2]
10
+ ### Fixed
11
+ - Fix Webpack being unable to find `swc-loader` in some cases ([#1798](https://github.com/MetaMask/snaps/pull/1798))
12
+ - Check minimum Node version in CLI ([#1797](https://github.com/MetaMask/snaps/pull/1797))
13
+
9
14
  ## [2.0.1]
10
15
  ### Fixed
11
16
  - Disable the `fullySpecified` rule for `.js` imports in the default Webpack config ([#1780](https://github.com/MetaMask/snaps/pull/1780))
@@ -51,7 +56,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
51
56
  - The version of the package no longer needs to match the version of all other
52
57
  MetaMask Snaps packages.
53
58
 
54
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@2.0.1...HEAD
59
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@2.0.2...HEAD
60
+ [2.0.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@2.0.1...@metamask/snaps-cli@2.0.2
55
61
  [2.0.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@2.0.0...@metamask/snaps-cli@2.0.1
56
62
  [2.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@0.38.4-flask.1...@metamask/snaps-cli@2.0.0
57
63
  [0.38.4-flask.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@0.38.3-flask.1...@metamask/snaps-cli@0.38.4-flask.1
package/dist/cjs/cli.js CHANGED
@@ -2,12 +2,21 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- Object.defineProperty(exports, "cli", {
6
- enumerable: true,
7
- get: function() {
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ checkNodeVersion: function() {
13
+ return checkNodeVersion;
14
+ },
15
+ cli: function() {
8
16
  return cli;
9
17
  }
10
18
  });
19
+ const _semver = /*#__PURE__*/ _interop_require_default(require("semver"));
11
20
  const _yargs = /*#__PURE__*/ _interop_require_default(require("yargs"));
12
21
  const _helpers = require("yargs/helpers");
13
22
  const _builders = /*#__PURE__*/ _interop_require_default(require("./builders"));
@@ -18,7 +27,31 @@ function _interop_require_default(obj) {
18
27
  default: obj
19
28
  };
20
29
  }
30
+ const MINIMUM_NODE_16_VERSION = '16.17.0';
31
+ const MINIMUM_NODE_18_VERSION = '18.6.0';
32
+ function checkNodeVersion(nodeVersion = process.version.slice(1)) {
33
+ const majorVersion = _semver.default.major(nodeVersion);
34
+ const message = `Node version ${nodeVersion} is not supported. Please use Node ${MINIMUM_NODE_16_VERSION} or later.`;
35
+ if (majorVersion < 16) {
36
+ (0, _utils.error)(message);
37
+ // eslint-disable-next-line n/no-process-exit
38
+ process.exit(1);
39
+ }
40
+ // Node 16 and 18 have a different minimum version requirement, so we need to
41
+ // check for both.
42
+ if (majorVersion === 16 && _semver.default.lt(nodeVersion, MINIMUM_NODE_16_VERSION)) {
43
+ (0, _utils.error)(message);
44
+ // eslint-disable-next-line n/no-process-exit
45
+ process.exit(1);
46
+ }
47
+ if (majorVersion === 18 && _semver.default.lt(nodeVersion, MINIMUM_NODE_18_VERSION)) {
48
+ (0, _utils.error)(`Node version ${nodeVersion} is not supported. Please use Node ${MINIMUM_NODE_18_VERSION} or later.`);
49
+ // eslint-disable-next-line n/no-process-exit
50
+ process.exit(1);
51
+ }
52
+ }
21
53
  async function cli(argv, commands) {
54
+ checkNodeVersion();
22
55
  await (0, _yargs.default)((0, _helpers.hideBin)(argv)).scriptName('mm-snap').usage('Usage: $0 <command> [options]').example('$0 build', `Build './src/index.js' as './dist/bundle.js'`).example('$0 build --config ./snap.config.build.ts', `Build './src/index.js' as './dist/bundle.js' using the config in './snap.config.build.ts'`).example('$0 manifest --fix', `Check the snap manifest, and fix any errors`).example('$0 watch --port 8000', `The snap input file for changes, and serve it on port 8000`).example('$0 serve --port 8000', `Serve the snap bundle on port 8000`).command(commands).option('config', _builders.default.config).option('verboseErrors', _builders.default.verboseErrors).option('suppressWarnings', _builders.default.suppressWarnings).strict().middleware(async (args)=>{
23
56
  // eslint-disable-next-line require-atomic-updates
24
57
  args.context = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/cli.ts"],"sourcesContent":["import yargs from 'yargs';\nimport { hideBin } from 'yargs/helpers';\n\nimport builders from './builders';\nimport { getConfigByArgv } from './config';\nimport { error, getYargsErrorMessage, sanitizeInputs } from './utils';\n\n/**\n * The main CLI entry point function. This processes the command line args, and\n * runs the appropriate function.\n *\n * @param argv - The raw command line arguments, i.e., `process.argv`.\n * @param commands - The list of commands to use.\n */\nexport async function cli(argv: string[], commands: any) {\n await yargs(hideBin(argv))\n .scriptName('mm-snap')\n .usage('Usage: $0 <command> [options]')\n\n .example('$0 build', `Build './src/index.js' as './dist/bundle.js'`)\n .example(\n '$0 build --config ./snap.config.build.ts',\n `Build './src/index.js' as './dist/bundle.js' using the config in './snap.config.build.ts'`,\n )\n .example('$0 manifest --fix', `Check the snap manifest, and fix any errors`)\n .example(\n '$0 watch --port 8000',\n `The snap input file for changes, and serve it on port 8000`,\n )\n .example('$0 serve --port 8000', `Serve the snap bundle on port 8000`)\n\n .command(commands)\n\n .option('config', builders.config)\n .option('verboseErrors', builders.verboseErrors)\n .option('suppressWarnings', builders.suppressWarnings)\n\n .strict()\n\n .middleware(async (args: any) => {\n // eslint-disable-next-line require-atomic-updates\n args.context = {\n config: await getConfigByArgv(args),\n };\n\n sanitizeInputs(args);\n }, false)\n\n .demandCommand(1, 'You must specify at least one command.')\n\n .fail((message, failure) => {\n error(getYargsErrorMessage(message, failure));\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n })\n\n .help()\n .alias('help', 'h')\n .parseAsync();\n}\n"],"names":["cli","argv","commands","yargs","hideBin","scriptName","usage","example","command","option","builders","config","verboseErrors","suppressWarnings","strict","middleware","args","context","getConfigByArgv","sanitizeInputs","demandCommand","fail","message","failure","error","getYargsErrorMessage","process","exit","help","alias","parseAsync"],"mappings":";;;;+BAcsBA;;;eAAAA;;;8DAdJ;yBACM;iEAEH;wBACW;uBAC4B;;;;;;AASrD,eAAeA,IAAIC,IAAc,EAAEC,QAAa;IACrD,MAAMC,IAAAA,cAAK,EAACC,IAAAA,gBAAO,EAACH,OACjBI,UAAU,CAAC,WACXC,KAAK,CAAC,iCAENC,OAAO,CAAC,YAAY,CAAC,4CAA4C,CAAC,EAClEA,OAAO,CACN,4CACA,CAAC,yFAAyF,CAAC,EAE5FA,OAAO,CAAC,qBAAqB,CAAC,2CAA2C,CAAC,EAC1EA,OAAO,CACN,wBACA,CAAC,0DAA0D,CAAC,EAE7DA,OAAO,CAAC,wBAAwB,CAAC,kCAAkC,CAAC,EAEpEC,OAAO,CAACN,UAERO,MAAM,CAAC,UAAUC,iBAAQ,CAACC,MAAM,EAChCF,MAAM,CAAC,iBAAiBC,iBAAQ,CAACE,aAAa,EAC9CH,MAAM,CAAC,oBAAoBC,iBAAQ,CAACG,gBAAgB,EAEpDC,MAAM,GAENC,UAAU,CAAC,OAAOC;QACjB,kDAAkD;QAClDA,KAAKC,OAAO,GAAG;YACbN,QAAQ,MAAMO,IAAAA,uBAAe,EAACF;QAChC;QAEAG,IAAAA,qBAAc,EAACH;IACjB,GAAG,OAEFI,aAAa,CAAC,GAAG,0CAEjBC,IAAI,CAAC,CAACC,SAASC;QACdC,IAAAA,YAAK,EAACC,IAAAA,2BAAoB,EAACH,SAASC;QACpC,6CAA6C;QAC7CG,QAAQC,IAAI,CAAC;IACf,GAECC,IAAI,GACJC,KAAK,CAAC,QAAQ,KACdC,UAAU;AACf"}
1
+ {"version":3,"sources":["../../src/cli.ts"],"sourcesContent":["import semver from 'semver';\nimport yargs from 'yargs';\nimport { hideBin } from 'yargs/helpers';\n\nimport builders from './builders';\nimport { getConfigByArgv } from './config';\nimport { error, getYargsErrorMessage, sanitizeInputs } from './utils';\n\nconst MINIMUM_NODE_16_VERSION = '16.17.0';\nconst MINIMUM_NODE_18_VERSION = '18.6.0';\n\n/**\n * Check the Node version. If the Node version is less than the minimum required\n * version, this logs an error and exits the process.\n *\n * @param nodeVersion - The Node version to check.\n */\nexport function checkNodeVersion(\n nodeVersion: string = process.version.slice(1),\n) {\n const majorVersion = semver.major(nodeVersion);\n const message = `Node version ${nodeVersion} is not supported. Please use Node ${MINIMUM_NODE_16_VERSION} or later.`;\n\n if (majorVersion < 16) {\n error(message);\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n }\n\n // Node 16 and 18 have a different minimum version requirement, so we need to\n // check for both.\n if (majorVersion === 16 && semver.lt(nodeVersion, MINIMUM_NODE_16_VERSION)) {\n error(message);\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n }\n\n if (majorVersion === 18 && semver.lt(nodeVersion, MINIMUM_NODE_18_VERSION)) {\n error(\n `Node version ${nodeVersion} is not supported. Please use Node ${MINIMUM_NODE_18_VERSION} or later.`,\n );\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n }\n}\n\n/**\n * The main CLI entry point function. This processes the command line args, and\n * runs the appropriate function.\n *\n * @param argv - The raw command line arguments, i.e., `process.argv`.\n * @param commands - The list of commands to use.\n */\nexport async function cli(argv: string[], commands: any) {\n checkNodeVersion();\n\n await yargs(hideBin(argv))\n .scriptName('mm-snap')\n .usage('Usage: $0 <command> [options]')\n\n .example('$0 build', `Build './src/index.js' as './dist/bundle.js'`)\n .example(\n '$0 build --config ./snap.config.build.ts',\n `Build './src/index.js' as './dist/bundle.js' using the config in './snap.config.build.ts'`,\n )\n .example('$0 manifest --fix', `Check the snap manifest, and fix any errors`)\n .example(\n '$0 watch --port 8000',\n `The snap input file for changes, and serve it on port 8000`,\n )\n .example('$0 serve --port 8000', `Serve the snap bundle on port 8000`)\n\n .command(commands)\n\n .option('config', builders.config)\n .option('verboseErrors', builders.verboseErrors)\n .option('suppressWarnings', builders.suppressWarnings)\n\n .strict()\n\n .middleware(async (args: any) => {\n // eslint-disable-next-line require-atomic-updates\n args.context = {\n config: await getConfigByArgv(args),\n };\n\n sanitizeInputs(args);\n }, false)\n\n .demandCommand(1, 'You must specify at least one command.')\n\n .fail((message, failure) => {\n error(getYargsErrorMessage(message, failure));\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n })\n\n .help()\n .alias('help', 'h')\n .parseAsync();\n}\n"],"names":["checkNodeVersion","cli","MINIMUM_NODE_16_VERSION","MINIMUM_NODE_18_VERSION","nodeVersion","process","version","slice","majorVersion","semver","major","message","error","exit","lt","argv","commands","yargs","hideBin","scriptName","usage","example","command","option","builders","config","verboseErrors","suppressWarnings","strict","middleware","args","context","getConfigByArgv","sanitizeInputs","demandCommand","fail","failure","getYargsErrorMessage","help","alias","parseAsync"],"mappings":";;;;;;;;;;;IAiBgBA,gBAAgB;eAAhBA;;IAoCMC,GAAG;eAAHA;;;+DArDH;8DACD;yBACM;iEAEH;wBACW;uBAC4B;;;;;;AAE5D,MAAMC,0BAA0B;AAChC,MAAMC,0BAA0B;AAQzB,SAASH,iBACdI,cAAsBC,QAAQC,OAAO,CAACC,KAAK,CAAC,EAAE;IAE9C,MAAMC,eAAeC,eAAM,CAACC,KAAK,CAACN;IAClC,MAAMO,UAAU,CAAC,aAAa,EAAEP,YAAY,mCAAmC,EAAEF,wBAAwB,UAAU,CAAC;IAEpH,IAAIM,eAAe,IAAI;QACrBI,IAAAA,YAAK,EAACD;QACN,6CAA6C;QAC7CN,QAAQQ,IAAI,CAAC;IACf;IAEA,6EAA6E;IAC7E,kBAAkB;IAClB,IAAIL,iBAAiB,MAAMC,eAAM,CAACK,EAAE,CAACV,aAAaF,0BAA0B;QAC1EU,IAAAA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ultLoader({ legacy, sourceMap }) {
91
91
  * We use the `swc-loader` to transpile TypeScript and JavaScript files.
92
92
  * This is a Webpack loader that uses the `SWC` compiler, which is a much
93
93
  * faster alternative to Babel and TypeScript's own compiler.
94
- */ loader: 'swc-loader',
94
+ */ loader: require.resolve('swc-loader'),
95
95
  /**
96
96
  * The options for the `swc-loader`. These can be overridden in the
97
97
  * `.swcrc` file.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/webpack/utils.ts"],"sourcesContent":["import { dim } from 'chalk';\nimport { promises as fs } from 'fs';\nimport { builtinModules } from 'module';\nimport type { Ora } from 'ora';\nimport { dirname, resolve } from 'path';\nimport type { Configuration } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\n\nexport const BROWSERSLIST_FILE = resolve(\n dirname(\n // eslint-disable-next-line n/no-extraneous-require\n require.resolve('@metamask/snaps-cli/package.json'),\n ),\n '.browserslistrc',\n);\n\nexport const WEBPACK_FALLBACKS = {\n assert: require.resolve('assert/'),\n buffer: require.resolve('buffer/'),\n console: require.resolve('console-browserify'),\n constants: require.resolve('constants-browserify'),\n crypto: require.resolve('crypto-browserify'),\n domain: require.resolve('domain-browser'),\n events: require.resolve('events/'),\n http: require.resolve('stream-http'),\n https: require.resolve('https-browserify'),\n os: require.resolve('os-browserify/browser'),\n path: require.resolve('path-browserify'),\n punycode: require.resolve('punycode/'),\n process: require.resolve('process/browser'),\n querystring: require.resolve('querystring-es3'),\n stream: require.resolve('stream-browserify'),\n /* eslint-disable @typescript-eslint/naming-convention */\n _stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),\n _stream_passthrough: require.resolve(\n 'readable-stream/lib/_stream_passthrough',\n ),\n _stream_readable: require.resolve('readable-stream/lib/_stream_readable'),\n _stream_transform: require.resolve('readable-stream/lib/_stream_transform'),\n _stream_writable: require.resolve('readable-stream/lib/_stream_writable'),\n string_decoder: require.resolve('string_decoder/'),\n /* eslint-enable @typescript-eslint/naming-convention */\n sys: require.resolve('util/'),\n timers: require.resolve('timers-browserify'),\n tty: require.resolve('tty-browserify'),\n url: require.resolve('url/'),\n util: require.resolve('util/'),\n vm: require.resolve('vm-browserify'),\n zlib: require.resolve('browserify-zlib'),\n};\n\n/**\n * Get the default loader for JavaScript and TypeScript files, based on the\n * config object.\n *\n * - If the `legacy` option is set, we use the custom `browserify` loader. This\n * uses the legacy Browserify config to transpile the code.\n * - Otherwise, we use the `swc-loader`. This is a Webpack loader that uses the\n * `SWC` compiler, which is a much faster alternative to Babel and TypeScript's\n * own compiler.\n *\n * @param config - The processed snap Webpack config.\n * @param config.legacy - The legacy config object, if any.\n * @param config.sourceMap - Whether to generate source maps.\n * @see https://swc.rs/docs/usage/swc-loader\n * @returns The default loader.\n */\nexport async function getDefaultLoader({\n legacy,\n sourceMap,\n}: ProcessedWebpackConfig) {\n if (legacy) {\n return {\n /**\n * If the snap uses the legacy config, we use the custom `browserify`\n * loader. This uses the legacy Browserify config to transpile the code.\n * This is necessary for backwards compatibility with the\n * `bundlerCustomizer` function.\n */\n loader: resolve(__dirname, 'loaders', 'browserify'),\n\n /**\n * The options for the `browserify` loader. These can be overridden in the\n * snap config.\n */\n options: legacy,\n };\n }\n\n const targets = await getBrowserslistTargets();\n return {\n /**\n * We use the `swc-loader` to transpile TypeScript and JavaScript files.\n * This is a Webpack loader that uses the `SWC` compiler, which is a much\n * faster alternative to Babel and TypeScript's own compiler.\n */\n loader: 'swc-loader',\n\n /**\n * The options for the `swc-loader`. These can be overridden in the\n * `.swcrc` file.\n *\n * @see https://swc.rs/docs/configuration/swcrc\n */\n options: {\n sync: false,\n\n /**\n * This tells SWC to generate source maps. We set it to the\n * `sourceMap` value from the config object.\n *\n * This must be enabled if source maps are enabled in the config.\n */\n sourceMaps: Boolean(getDevTool(sourceMap)),\n\n jsc: {\n parser: {\n /**\n * This tells the parser to parse TypeScript files. If you\n * don't need to support TypeScript, you can set this to\n * `ecmascript` instead, but there's no harm in leaving it\n * as `typescript`.\n *\n * @see https://swc.rs/docs/configuration/compilation#jscparser\n */\n syntax: 'typescript',\n },\n },\n\n /**\n * The module configuration. This tells SWC how to output the\n * transpiled code.\n *\n * @see https://swc.rs/docs/configuration/modules\n */\n module: {\n /**\n * This tells SWC to output CommonJS modules. MetaMask Snaps\n * doesn't support ES modules yet, so this is necessary.\n *\n * @see https://swc.rs/docs/configuration/modules#commonjs\n */\n type: 'commonjs',\n },\n\n env: {\n targets: targets.join(', '),\n },\n },\n };\n}\n\n/**\n * Get the Webpack devtool configuration based on the given snap config.\n *\n * - If `sourceMap` is `inline`, return `inline-source-map`.\n * - If `sourceMap` is `true`, return `source-map`.\n * - Otherwise, return `false`.\n *\n * @param sourceMap - The `sourceMap` value from the snap config.\n * @returns The Webpack devtool configuration.\n */\nexport function getDevTool(\n sourceMap: ProcessedWebpackConfig['sourceMap'],\n): Configuration['devtool'] {\n if (sourceMap === 'inline') {\n return 'inline-source-map';\n }\n\n if (sourceMap === true) {\n return 'source-map';\n }\n\n return false;\n}\n\n/**\n * Get a function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n *\n * @param spinner - The spinner to update.\n * @param spinnerText - The initial spinner text. This will be prepended to the\n * percentage.\n * @returns A function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n */\n// Note: This is extracted for testing purposes.\nexport function getProgressHandler(spinner?: Ora, spinnerText?: string) {\n return (percentage: number) => {\n if (spinner && spinnerText) {\n spinner.text = `${spinnerText} ${dim(\n `(${Math.round(percentage * 100)}%)`,\n )}`;\n }\n };\n}\n\n/**\n * Get the targets from the `.browserslistrc` file.\n *\n * @returns The browser targets as an array of strings.\n */\nexport async function getBrowserslistTargets() {\n const contents = await fs.readFile(BROWSERSLIST_FILE, 'utf8');\n return contents\n .split('\\n')\n .map((line) => line.trim())\n .filter((line) => line && !line.startsWith('#'));\n}\n\n/**\n * Get a singular or plural string based on the given count. This is useful for\n * generating messages like \"1 error\" or \"2 errors\". By default, the plural\n * string is the singular string with an \"s\" appended to it.\n *\n * This assumes that the text is in English, and likely won't work for some\n * other languages.\n *\n * @param count - The count.\n * @param singular - The singular string.\n * @param plural - The plural string.\n * @returns The singular or plural string.\n * @example\n * ```typescript\n * pluralize(1, 'error'); // => 'error'\n * pluralize(2, 'error'); // => 'errors'\n * pluralize(1, 'error', 'problem'); // => 'error'\n * pluralize(2, 'error', 'problems'); // => 'problems'\n * ```\n */\nexport function pluralize(\n count: number,\n singular: string,\n plural = `${singular}s`,\n) {\n return count === 1 ? singular : plural;\n}\n\n/**\n * Get an object that can be used as fallback config for Webpack's\n * `fallback` config.\n *\n * @param polyfills - The polyfill object from the snap config.\n * @returns The webpack fallback config.\n */\nexport function getFallbacks(polyfills: ProcessedWebpackConfig['polyfills']): {\n [index: string]: string | false;\n} {\n if (polyfills === true) {\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS] ?? false,\n ]),\n );\n }\n\n if (polyfills === false) {\n return Object.fromEntries(builtinModules.map((name) => [name, false]));\n }\n\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n polyfills[name as keyof ProcessedWebpackConfig['polyfills']]\n ? WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS]\n : false,\n ]),\n );\n}\n"],"names":["BROWSERSLIST_FILE","WEBPACK_FALLBACKS","getDefaultLoader","getDevTool","getProgressHandler","getBrowserslistTargets","pluralize","getFallbacks","resolve","dirname","require","assert","buffer","console","constants","crypto","domain","events","http","https","os","path","punycode","process","querystring","stream","_stream_duplex","_stream_passthrough","_stream_readable","_stream_transform","_stream_writable","string_decoder","sys","timers","tty","url","util","vm","zlib","legacy","sourceMap","loader","__dirname","options","targets","sync","sourceMaps","Boolean","jsc","parser","syntax","module","type","env","join","spinner","spinnerText","percentage","text","dim","Math","round","contents","fs","readFile","split","map","line","trim","filter","startsWith","count","singular","plural","polyfills","Object","fromEntries","builtinModules","name"],"mappings":";;;;;;;;;;;IASaA,iBAAiB;eAAjBA;;IAQ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AASA,QAAQ,CAACA,KAAKG,UAAU,CAAC;AAC/C;AAsBO,SAAShE,UACdiE,KAAa,EACbC,QAAgB,EAChBC,SAAS,CAAC,EAAED,SAAS,CAAC,CAAC;IAEvB,OAAOD,UAAU,IAAIC,WAAWC;AAClC;AASO,SAASlE,aAAamE,SAA8C;IAGzE,IAAIA,cAAc,MAAM;QACtB,OAAOC,OAAOC,WAAW,CACvBC,sBAAc,CAACX,GAAG,CAAC,CAACY,OAAS;gBAC3BA;gBACA7E,iBAAiB,CAAC6E,KAAuC,IAAI;aAC9D;IAEL;IAEA,IAAIJ,cAAc,OAAO;QACvB,OAAOC,OAAOC,WAAW,CAACC,sBAAc,CAACX,GAAG,CAAC,CAACY,OAAS;gBAACA;gBAAM;aAAM;IACtE;IAEA,OAAOH,OAAOC,WAAW,CACvBC,sBAAc,CAACX,GAAG,CAAC,CAACY,OAAS;YAC3BA;YACAJ,SAAS,CAACI,KAAkD,GACxD7E,iBAAiB,CAAC6E,KAAuC,GACzD;SACL;AAEL"}
1
+ {"version":3,"sources":["../../../src/webpack/utils.ts"],"sourcesContent":["import { dim } from 'chalk';\nimport { promises as fs } from 'fs';\nimport { builtinModules } from 'module';\nimport type { Ora } from 'ora';\nimport { dirname, resolve } from 'path';\nimport type { Configuration } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\n\nexport const BROWSERSLIST_FILE = resolve(\n dirname(\n // eslint-disable-next-line n/no-extraneous-require\n require.resolve('@metamask/snaps-cli/package.json'),\n ),\n '.browserslistrc',\n);\n\nexport const WEBPACK_FALLBACKS = {\n assert: require.resolve('assert/'),\n buffer: require.resolve('buffer/'),\n console: require.resolve('console-browserify'),\n constants: require.resolve('constants-browserify'),\n crypto: require.resolve('crypto-browserify'),\n domain: require.resolve('domain-browser'),\n events: require.resolve('events/'),\n http: require.resolve('stream-http'),\n https: require.resolve('https-browserify'),\n os: require.resolve('os-browserify/browser'),\n path: require.resolve('path-browserify'),\n punycode: require.resolve('punycode/'),\n process: require.resolve('process/browser'),\n querystring: require.resolve('querystring-es3'),\n stream: require.resolve('stream-browserify'),\n /* eslint-disable @typescript-eslint/naming-convention */\n _stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),\n _stream_passthrough: require.resolve(\n 'readable-stream/lib/_stream_passthrough',\n ),\n _stream_readable: require.resolve('readable-stream/lib/_stream_readable'),\n _stream_transform: require.resolve('readable-stream/lib/_stream_transform'),\n _stream_writable: require.resolve('readable-stream/lib/_stream_writable'),\n string_decoder: require.resolve('string_decoder/'),\n /* eslint-enable @typescript-eslint/naming-convention */\n sys: require.resolve('util/'),\n timers: require.resolve('timers-browserify'),\n tty: require.resolve('tty-browserify'),\n url: require.resolve('url/'),\n util: require.resolve('util/'),\n vm: require.resolve('vm-browserify'),\n zlib: require.resolve('browserify-zlib'),\n};\n\n/**\n * Get the default loader for JavaScript and TypeScript files, based on the\n * config object.\n *\n * - If the `legacy` option is set, we use the custom `browserify` loader. This\n * uses the legacy Browserify config to transpile the code.\n * - Otherwise, we use the `swc-loader`. This is a Webpack loader that uses the\n * `SWC` compiler, which is a much faster alternative to Babel and TypeScript's\n * own compiler.\n *\n * @param config - The processed snap Webpack config.\n * @param config.legacy - The legacy config object, if any.\n * @param config.sourceMap - Whether to generate source maps.\n * @see https://swc.rs/docs/usage/swc-loader\n * @returns The default loader.\n */\nexport async function getDefaultLoader({\n legacy,\n sourceMap,\n}: ProcessedWebpackConfig) {\n if (legacy) {\n return {\n /**\n * If the snap uses the legacy config, we use the custom `browserify`\n * loader. This uses the legacy Browserify config to transpile the code.\n * This is necessary for backwards compatibility with the\n * `bundlerCustomizer` function.\n */\n loader: resolve(__dirname, 'loaders', 'browserify'),\n\n /**\n * The options for the `browserify` loader. These can be overridden in the\n * snap config.\n */\n options: legacy,\n };\n }\n\n const targets = await getBrowserslistTargets();\n return {\n /**\n * We use the `swc-loader` to transpile TypeScript and JavaScript files.\n * This is a Webpack loader that uses the `SWC` compiler, which is a much\n * faster alternative to Babel and TypeScript's own compiler.\n */\n loader: require.resolve('swc-loader'),\n\n /**\n * The options for the `swc-loader`. These can be overridden in the\n * `.swcrc` file.\n *\n * @see https://swc.rs/docs/configuration/swcrc\n */\n options: {\n sync: false,\n\n /**\n * This tells SWC to generate source maps. We set it to the\n * `sourceMap` value from the config object.\n *\n * This must be enabled if source maps are enabled in the config.\n */\n sourceMaps: Boolean(getDevTool(sourceMap)),\n\n jsc: {\n parser: {\n /**\n * This tells the parser to parse TypeScript files. If you\n * don't need to support TypeScript, you can set this to\n * `ecmascript` instead, but there's no harm in leaving it\n * as `typescript`.\n *\n * @see https://swc.rs/docs/configuration/compilation#jscparser\n */\n syntax: 'typescript',\n },\n },\n\n /**\n * The module configuration. This tells SWC how to output the\n * transpiled code.\n *\n * @see https://swc.rs/docs/configuration/modules\n */\n module: {\n /**\n * This tells SWC to output CommonJS modules. MetaMask Snaps\n * doesn't support ES modules yet, so this is necessary.\n *\n * @see https://swc.rs/docs/configuration/modules#commonjs\n */\n type: 'commonjs',\n },\n\n env: {\n targets: targets.join(', '),\n },\n },\n };\n}\n\n/**\n * Get the Webpack devtool configuration based on the given snap config.\n *\n * - If `sourceMap` is `inline`, return `inline-source-map`.\n * - If `sourceMap` is `true`, return `source-map`.\n * - Otherwise, return `false`.\n *\n * @param sourceMap - The `sourceMap` value from the snap config.\n * @returns The Webpack devtool configuration.\n */\nexport function getDevTool(\n sourceMap: ProcessedWebpackConfig['sourceMap'],\n): Configuration['devtool'] {\n if (sourceMap === 'inline') {\n return 'inline-source-map';\n }\n\n if (sourceMap === true) {\n return 'source-map';\n }\n\n return false;\n}\n\n/**\n * Get a function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n *\n * @param spinner - The spinner to update.\n * @param spinnerText - The initial spinner text. This will be prepended to the\n * percentage.\n * @returns A function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n */\n// Note: This is extracted for testing purposes.\nexport function getProgressHandler(spinner?: Ora, spinnerText?: string) {\n return (percentage: number) => {\n if (spinner && spinnerText) {\n spinner.text = `${spinnerText} ${dim(\n `(${Math.round(percentage * 100)}%)`,\n )}`;\n }\n };\n}\n\n/**\n * Get the targets from the `.browserslistrc` file.\n *\n * @returns The browser targets as an array of strings.\n */\nexport async function getBrowserslistTargets() {\n const contents = await fs.readFile(BROWSERSLIST_FILE, 'utf8');\n return contents\n .split('\\n')\n .map((line) => line.trim())\n .filter((line) => line && !line.startsWith('#'));\n}\n\n/**\n * Get a singular or plural string based on the given count. This is useful for\n * generating messages like \"1 error\" or \"2 errors\". By default, the plural\n * string is the singular string with an \"s\" appended to it.\n *\n * This assumes that the text is in English, and likely won't work for some\n * other languages.\n *\n * @param count - The count.\n * @param singular - The singular string.\n * @param plural - The plural string.\n * @returns The singular or plural string.\n * @example\n * ```typescript\n * pluralize(1, 'error'); // => 'error'\n * pluralize(2, 'error'); // => 'errors'\n * pluralize(1, 'error', 'problem'); // => 'error'\n * pluralize(2, 'error', 'problems'); // => 'problems'\n * ```\n */\nexport function pluralize(\n count: number,\n singular: string,\n plural = `${singular}s`,\n) {\n return count === 1 ? singular : plural;\n}\n\n/**\n * Get an object that can be used as fallback config for Webpack's\n * `fallback` config.\n *\n * @param polyfills - The polyfill object from the snap config.\n * @returns The webpack fallback config.\n */\nexport function getFallbacks(polyfills: ProcessedWebpackConfig['polyfills']): {\n [index: string]: string | false;\n} {\n if (polyfills === true) {\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS] ?? false,\n ]),\n );\n }\n\n if (polyfills === false) {\n return Object.fromEntries(builtinModules.map((name) => [name, false]));\n }\n\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n polyfills[name as keyof ProcessedWebpackConfig['polyfills']]\n ? WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS]\n : false,\n ]),\n );\n}\n"],"names":["BROWSERSLIST_FILE","WEBPACK_FALLBACKS","getDefaultLoader","getDevTool","getProgressHandler","getBrowserslistTargets","pluralize","getFallbacks","resolve","dirname","require","assert","buffer","console","constants","crypto","domain","events","http","https","os","path","punycode","process","querystring","stream","_stream_duplex","_stream_passthrough","_stream_readable","_stream_transform","_stream_writable","string_decoder","sys","timers","tty","url","util","vm","zlib","legacy","sourceMap","loader","__dirname","options","targets","sync","sourceMaps","Boolean","jsc","parser","syntax","module","type","env","join","spinner","spinnerText","percentage","text","dim","Math","round","contents","fs","readFile","split","map","line","trim","filter","startsWith","count","singular","plural","polyfills","Object","fromEntries","builtinModules","name"],"mappings":";;;;;;;;;;;IASaA,iBAAiB;eAAjBA;;IAQAC,iBAAiB;eAAjBA;;IAmDSC,gBAAgB;eAAhBA;;IA+FNC,UAAU;eAAVA;;IAyBAC,kBAAkB;eAAlBA;;IAeMC,sBAAsB;eAAtBA;;IA4BNC,SAAS;eAATA;;IAeAC,YAAY;eAAZA;;;uBAtPI;oBACW;wBACA;sBAEE;AAK1B,MAAMP,oBAAoBQ,IAAAA,aAAO,EACtCC,IAAAA,aAAO,EACL,mDAAmD;AACnDC,QAAQF,OAAO,CAAC,sCAElB;AAGK,MAAMP,oBAAoB;IAC/BU,QAAQD,QAAQF,OAAO,CAAC;IACxBI,QAAQF,QAAQF,OAAO,CAAC;IACxBK,SAASH,QAAQF,OAAO,CAAC;IACzBM,WAAWJ,QAAQF,OAAO,CAAC;IAC3BO,QAAQL,QAAQF,OAAO,CAAC;IACxBQ,QAAQN,QAAQF,OAAO,CAAC;IACxBS,QAAQP,QAAQF,OAAO,CAAC;IACxBU,MAAMR,QAAQF,OAAO,CAAC;IACtBW,OAAOT,QAAQF,OAAO,CAAC;IACvBY,IAAIV,QAAQF,OAAO,CAAC;IACpBa,MAAMX,QAAQF,OAAO,CAAC;IACtBc,UAAUZ,QAAQF,OAAO,CAAC;IAC1Be,SAASb,QAAQF,OAAO,CAAC;IACzBgB,aAAad,QAAQF,OAAO,CAAC;IAC7BiB,QAAQf,QAAQF,OAAO,CAAC;IACxB,wDAAwD,G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EAAEC,WAAoB;IACpE,OAAO,CAACC;QACN,IAAIF,WAAWC,aAAa;YAC1BD,QAAQG,IAAI,GAAG,CAAC,EAAEF,YAAY,CAAC,EAAEG,IAAAA,UAAG,EAClC,CAAC,CAAC,EAAEC,KAAKC,KAAK,CAACJ,aAAa,KAAK,EAAE,CAAC,EACpC,CAAC;QACL;IACF;AACF;AAOO,eAAepD;IACpB,MAAMyD,WAAW,MAAMC,YAAE,CAACC,QAAQ,CAAChE,mBAAmB;IACtD,OAAO8D,SACJG,KAAK,CAAC,MACNC,GAAG,CAAC,CAACC,OAASA,KAAKC,IAAI,IACvBC,MAAM,CAAC,CAACF,OAASA,QAAQ,CAACA,KAAKG,UAAU,CAAC;AAC/C;AAsBO,SAAShE,UACdiE,KAAa,EACbC,QAAgB,EAChBC,SAAS,CAAC,EAAED,SAAS,CAAC,CAAC;IAEvB,OAAOD,UAAU,IAAIC,WAAWC;AAClC;AASO,SAASlE,aAAamE,SAA8C;IAGzE,IAAIA,cAAc,MAAM;QACtB,OAAOC,OAAOC,WAAW,CACvBC,sBAAc,CAACX,GAAG,CAAC,CAACY,OAAS;gBAC3BA;gBACA7E,iBAAiB,CAAC6E,KAAuC,IAAI;aAC9D;IAEL;IAEA,IAAIJ,cAAc,OAAO;QACvB,OAAOC,OAAOC,WAAW,CAACC,sBAAc,CAACX,GAAG,CAAC,CAACY,OAAS;gBAACA;gBAAM;aAAM;IACtE;IAEA,OAAOH,OAAOC,WAAW,CACvBC,sBAAc,CAACX,GAAG,CAAC,CAACY,OAAS;YAC3BA;YACAJ,SAAS,CAACI,KAAkD,GACxD7E,iBAAiB,CAAC6E,KAAuC,GACzD;SACL;AAEL"}
package/dist/esm/cli.js CHANGED
@@ -1,8 +1,37 @@
1
+ import semver from 'semver';
1
2
  import yargs from 'yargs';
2
3
  import { hideBin } from 'yargs/helpers';
3
4
  import builders from './builders';
4
5
  import { getConfigByArgv } from './config';
5
6
  import { error, getYargsErrorMessage, sanitizeInputs } from './utils';
7
+ const MINIMUM_NODE_16_VERSION = '16.17.0';
8
+ const MINIMUM_NODE_18_VERSION = '18.6.0';
9
+ /**
10
+ * Check the Node version. If the Node version is less than the minimum required
11
+ * version, this logs an error and exits the process.
12
+ *
13
+ * @param nodeVersion - The Node version to check.
14
+ */ export function checkNodeVersion(nodeVersion = process.version.slice(1)) {
15
+ const majorVersion = semver.major(nodeVersion);
16
+ const message = `Node version ${nodeVersion} is not supported. Please use Node ${MINIMUM_NODE_16_VERSION} or later.`;
17
+ if (majorVersion < 16) {
18
+ error(message);
19
+ // eslint-disable-next-line n/no-process-exit
20
+ process.exit(1);
21
+ }
22
+ // Node 16 and 18 have a different minimum version requirement, so we need to
23
+ // check for both.
24
+ if (majorVersion === 16 && semver.lt(nodeVersion, MINIMUM_NODE_16_VERSION)) {
25
+ error(message);
26
+ // eslint-disable-next-line n/no-process-exit
27
+ process.exit(1);
28
+ }
29
+ if (majorVersion === 18 && semver.lt(nodeVersion, MINIMUM_NODE_18_VERSION)) {
30
+ error(`Node version ${nodeVersion} is not supported. Please use Node ${MINIMUM_NODE_18_VERSION} or later.`);
31
+ // eslint-disable-next-line n/no-process-exit
32
+ process.exit(1);
33
+ }
34
+ }
6
35
  /**
7
36
  * The main CLI entry point function. This processes the command line args, and
8
37
  * runs the appropriate function.
@@ -10,6 +39,7 @@ import { error, getYargsErrorMessage, sanitizeInputs } from './utils';
10
39
  * @param argv - The raw command line arguments, i.e., `process.argv`.
11
40
  * @param commands - The list of commands to use.
12
41
  */ export async function cli(argv, commands) {
42
+ checkNodeVersion();
13
43
  await yargs(hideBin(argv)).scriptName('mm-snap').usage('Usage: $0 <command> [options]').example('$0 build', `Build './src/index.js' as './dist/bundle.js'`).example('$0 build --config ./snap.config.build.ts', `Build './src/index.js' as './dist/bundle.js' using the config in './snap.config.build.ts'`).example('$0 manifest --fix', `Check the snap manifest, and fix any errors`).example('$0 watch --port 8000', `The snap input file for changes, and serve it on port 8000`).example('$0 serve --port 8000', `Serve the snap bundle on port 8000`).command(commands).option('config', builders.config).option('verboseErrors', builders.verboseErrors).option('suppressWarnings', builders.suppressWarnings).strict().middleware(async (args)=>{
14
44
  // eslint-disable-next-line require-atomic-updates
15
45
  args.context = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/cli.ts"],"sourcesContent":["import yargs from 'yargs';\nimport { hideBin } from 'yargs/helpers';\n\nimport builders from './builders';\nimport { getConfigByArgv } from './config';\nimport { error, getYargsErrorMessage, sanitizeInputs } from './utils';\n\n/**\n * The main CLI entry point function. This processes the command line args, and\n * runs the appropriate function.\n *\n * @param argv - The raw command line arguments, i.e., `process.argv`.\n * @param commands - The list of commands to use.\n */\nexport async function cli(argv: string[], commands: any) {\n await yargs(hideBin(argv))\n .scriptName('mm-snap')\n .usage('Usage: $0 <command> [options]')\n\n .example('$0 build', `Build './src/index.js' as './dist/bundle.js'`)\n .example(\n '$0 build --config ./snap.config.build.ts',\n `Build './src/index.js' as './dist/bundle.js' using the config in './snap.config.build.ts'`,\n )\n .example('$0 manifest --fix', `Check the snap manifest, and fix any errors`)\n .example(\n '$0 watch --port 8000',\n `The snap input file for changes, and serve it on port 8000`,\n )\n .example('$0 serve --port 8000', `Serve the snap bundle on port 8000`)\n\n .command(commands)\n\n .option('config', builders.config)\n .option('verboseErrors', builders.verboseErrors)\n .option('suppressWarnings', builders.suppressWarnings)\n\n .strict()\n\n .middleware(async (args: any) => {\n // eslint-disable-next-line require-atomic-updates\n args.context = {\n config: await getConfigByArgv(args),\n };\n\n sanitizeInputs(args);\n }, false)\n\n .demandCommand(1, 'You must specify at least one command.')\n\n .fail((message, failure) => {\n error(getYargsErrorMessage(message, failure));\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n })\n\n .help()\n .alias('help', 'h')\n .parseAsync();\n}\n"],"names":["yargs","hideBin","builders","getConfigByArgv","error","getYargsErrorMessage","sanitizeInputs","cli","argv","commands","scriptName","usage","example","command","option","config","verboseErrors","suppressWarnings","strict","middleware","args","context","demandCommand","fail","message","failure","process","exit","help","alias","parseAsync"],"mappings":"AAAA,OAAOA,WAAW,QAAQ;AAC1B,SAASC,OAAO,QAAQ,gBAAgB;AAExC,OAAOC,cAAc,aAAa;AAClC,SAASC,eAAe,QAAQ,WAAW;AAC3C,SAASC,KAAK,EAAEC,oBAAoB,EAAEC,cAAc,QAAQ,UAAU;AAEtE;;;;;;CAMC,GACD,OAAO,eAAeC,IAAIC,IAAc,EAAEC,QAAa;IACrD,MAAMT,MAAMC,QAAQO,OACjBE,UAAU,CAAC,WACXC,KAAK,CAAC,iCAENC,OAAO,CAAC,YAAY,CAAC,4CAA4C,CAAC,EAClEA,OAAO,CACN,4CACA,CAAC,yFAAyF,CAAC,EAE5FA,OAAO,CAAC,qBAAqB,CAAC,2CAA2C,CAAC,EAC1EA,OAAO,CACN,wBACA,CAAC,0DAA0D,CAAC,EAE7DA,OAAO,CAAC,wBAAwB,CAAC,kCAAkC,CAAC,EAEpEC,OAAO,CAACJ,UAERK,MAAM,CAAC,UAAUZ,SAASa,MAAM,EAChCD,MAAM,CAAC,iBAAiBZ,SAASc,aAAa,EAC9CF,MAAM,CAAC,oBAAoBZ,SAASe,gBAAgB,EAEpDC,MAAM,GAENC,UAAU,CAAC,OAAOC;QACjB,kDAAkD;QAClDA,KAAKC,OAAO,GAAG;YACbN,QAAQ,MAAMZ,gBAAgBiB;QAChC;QAEAd,eAAec;IACjB,GAAG,OAEFE,aAAa,CAAC,GAAG,0CAEjBC,IAAI,CAAC,CAACC,SAASC;QACdrB,MAAMC,qBAAqBmB,SAASC;QACpC,6CAA6C;QAC7CC,QAAQC,IAAI,CAAC;IACf,GAECC,IAAI,GACJC,KAAK,CAAC,QAAQ,KACdC,UAAU;AACf"}
1
+ {"version":3,"sources":["../../src/cli.ts"],"sourcesContent":["import semver from 'semver';\nimport yargs from 'yargs';\nimport { hideBin } from 'yargs/helpers';\n\nimport builders from './builders';\nimport { getConfigByArgv } from './config';\nimport { error, getYargsErrorMessage, sanitizeInputs } from './utils';\n\nconst MINIMUM_NODE_16_VERSION = '16.17.0';\nconst MINIMUM_NODE_18_VERSION = '18.6.0';\n\n/**\n * Check the Node version. If the Node version is less than the minimum required\n * version, this logs an error and exits the process.\n *\n * @param nodeVersion - The Node version to check.\n */\nexport function checkNodeVersion(\n nodeVersion: string = process.version.slice(1),\n) {\n const majorVersion = semver.major(nodeVersion);\n const message = `Node version ${nodeVersion} is not supported. Please use Node ${MINIMUM_NODE_16_VERSION} or later.`;\n\n if (majorVersion < 16) {\n error(message);\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n }\n\n // Node 16 and 18 have a different minimum version requirement, so we need to\n // check for both.\n if (majorVersion === 16 && semver.lt(nodeVersion, MINIMUM_NODE_16_VERSION)) {\n error(message);\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n }\n\n if (majorVersion === 18 && semver.lt(nodeVersion, MINIMUM_NODE_18_VERSION)) {\n error(\n `Node version ${nodeVersion} is not supported. Please use Node ${MINIMUM_NODE_18_VERSION} or later.`,\n );\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n }\n}\n\n/**\n * The main CLI entry point function. This processes the command line args, and\n * runs the appropriate function.\n *\n * @param argv - The raw command line arguments, i.e., `process.argv`.\n * @param commands - The list of commands to use.\n */\nexport async function cli(argv: string[], commands: any) {\n checkNodeVersion();\n\n await yargs(hideBin(argv))\n .scriptName('mm-snap')\n .usage('Usage: $0 <command> [options]')\n\n .example('$0 build', `Build './src/index.js' as './dist/bundle.js'`)\n .example(\n '$0 build --config ./snap.config.build.ts',\n `Build './src/index.js' as './dist/bundle.js' using the config in './snap.config.build.ts'`,\n )\n .example('$0 manifest --fix', `Check the snap manifest, and fix any errors`)\n .example(\n '$0 watch --port 8000',\n `The snap input file for changes, and serve it on port 8000`,\n )\n .example('$0 serve --port 8000', `Serve the snap bundle on port 8000`)\n\n .command(commands)\n\n .option('config', builders.config)\n .option('verboseErrors', builders.verboseErrors)\n .option('suppressWarnings', builders.suppressWarnings)\n\n .strict()\n\n .middleware(async (args: any) => {\n // eslint-disable-next-line require-atomic-updates\n args.context = {\n config: await getConfigByArgv(args),\n };\n\n sanitizeInputs(args);\n }, false)\n\n .demandCommand(1, 'You must specify at least one command.')\n\n .fail((message, failure) => {\n error(getYargsErrorMessage(message, failure));\n // eslint-disable-next-line n/no-process-exit\n process.exit(1);\n })\n\n .help()\n .alias('help', 'h')\n .parseAsync();\n}\n"],"names":["semver","yargs","hideBin","builders","getConfigByArgv","error","getYargsErrorMessage","sanitizeInputs","MINIMUM_NODE_16_VERSION","MINIMUM_NODE_18_VERSION","checkNodeVersion","nodeVersion","process","version","slice","majorVersion","major","message","exit","lt","cli","argv","commands","scriptName","usage","example","command","option","config","verboseErrors","suppressWarnings","strict","middleware","args","context","demandCommand","fail","failure","help","alias","parseAsync"],"mappings":"AAAA,OAAOA,YAAY,SAAS;AAC5B,OAAOC,WAAW,QAAQ;AAC1B,SAASC,OAAO,QAAQ,gBAAgB;AAExC,OAAOC,cAAc,aAAa;AAClC,SAASC,eAAe,QAAQ,WAAW;AAC3C,SAASC,KAAK,EAAEC,oBAAoB,EAAEC,cAAc,QAAQ,UAAU;AAEtE,MAAMC,0BAA0B;AAChC,MAAMC,0BAA0B;AAEhC;;;;;CAKC,GACD,OAAO,SAASC,iBACdC,cAAsBC,QAAQC,OAAO,CAACC,KAAK,CAAC,EAAE;IAE9C,MAAMC,eAAef,OAAOgB,KAAK,CAACL;IAClC,MAAMM,UAAU,CAAC,aAAa,EAAEN,YAAY,mCAAmC,EAAEH,wBAAwB,UAAU,CAAC;IAEpH,IAAIO,eAAe,IAAI;QACrBV,MAAMY;QACN,6CAA6C;QAC7CL,QAAQM,IAAI,CAAC;IACf;IAEA,6EAA6E;IAC7E,kBAAkB;IAClB,IAAIH,iBAAiB,MAAMf,OAAOmB,EAAE,CAACR,aAAaH,0BAA0B;QAC1EH,MAAMY;QACN,6CAA6C;QAC7CL,QAAQM,IAAI,CAAC;IACf;IAEA,IAAIH,iBAAiB,MAAMf,OAAOmB,EAAE,CAACR,aAAaF,0BAA0B;QAC1EJ,MACE,CAAC,aAAa,EAAEM,YAAY,mCAAmC,EAAEF,wBAAwB,UAAU,CAAC;QAEtG,6CAA6C;QAC7CG,QAAQM,IAAI,CAAC;IACf;AACF;AAEA;;;;;;CAMC,GACD,OAAO,eAAeE,IAAIC,IAAc,EAAEC,QAAa;IACrDZ;IAEA,MAAMT,MAAMC,QAAQmB,OACjBE,UAAU,CAAC,WACXC,KAAK,CAAC,iCAENC,OAAO,CAAC,YAAY,CAAC,4CAA4C,CAAC,EAClEA,OAAO,CACN,4CACA,CAAC,yFAAyF,CAAC,EAE5FA,OAAO,CAAC,qBAAqB,CAAC,2CAA2C,CAAC,EAC1EA,OAAO,CACN,wBACA,CAAC,0DAA0D,CAAC,EAE7DA,OAAO,CAAC,wBAAwB,CAAC,kCAAkC,CAAC,EAEpEC,OAAO,CAACJ,UAERK,MAAM,CAAC,UAAUxB,SAASyB,MAAM,EAChCD,MAAM,CAAC,iBAAiBxB,SAAS0B,aAAa,EAC9CF,MAAM,CAAC,oBAAoBxB,SAAS2B,gBAAgB,EAEpDC,MAAM,GAENC,UAAU,CAAC,OAAOC;QACjB,kDAAkD;QAClDA,KAAKC,OAAO,GAAG;YACbN,QAAQ,MAAMxB,gBAAgB6B;QAChC;QAEA1B,eAAe0B;IACjB,GAAG,OAEFE,aAAa,CAAC,GAAG,0CAEjBC,IAAI,CAAC,CAACnB,SAASoB;QACdhC,MAAMC,qBAAqBW,SAASoB;QACpC,6CAA6C;QAC7CzB,QAAQM,IAAI,CAAC;IACf,GAECoB,IAAI,GACJC,KAAK,CAAC,QAAQ,KACdC,UAAU;AACf"}
@@ -70,7 +70,7 @@ export const WEBPACK_FALLBACKS = {
70
70
  * We use the `swc-loader` to transpile TypeScript and JavaScript files.
71
71
  * This is a Webpack loader that uses the `SWC` compiler, which is a much
72
72
  * faster alternative to Babel and TypeScript's own compiler.
73
- */ loader: 'swc-loader',
73
+ */ loader: require.resolve('swc-loader'),
74
74
  /**
75
75
  * The options for the `swc-loader`. These can be overridden in the
76
76
  * `.swcrc` file.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/webpack/utils.ts"],"sourcesContent":["import { dim } from 'chalk';\nimport { promises as fs } from 'fs';\nimport { builtinModules } from 'module';\nimport type { Ora } from 'ora';\nimport { dirname, resolve } from 'path';\nimport type { Configuration } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\n\nexport const BROWSERSLIST_FILE = resolve(\n dirname(\n // eslint-disable-next-line n/no-extraneous-require\n require.resolve('@metamask/snaps-cli/package.json'),\n ),\n '.browserslistrc',\n);\n\nexport const WEBPACK_FALLBACKS = {\n assert: require.resolve('assert/'),\n buffer: require.resolve('buffer/'),\n console: require.resolve('console-browserify'),\n constants: require.resolve('constants-browserify'),\n crypto: require.resolve('crypto-browserify'),\n domain: require.resolve('domain-browser'),\n events: require.resolve('events/'),\n http: require.resolve('stream-http'),\n https: require.resolve('https-browserify'),\n os: require.resolve('os-browserify/browser'),\n path: require.resolve('path-browserify'),\n punycode: require.resolve('punycode/'),\n process: require.resolve('process/browser'),\n querystring: require.resolve('querystring-es3'),\n stream: require.resolve('stream-browserify'),\n /* eslint-disable @typescript-eslint/naming-convention */\n _stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),\n _stream_passthrough: require.resolve(\n 'readable-stream/lib/_stream_passthrough',\n ),\n _stream_readable: require.resolve('readable-stream/lib/_stream_readable'),\n _stream_transform: require.resolve('readable-stream/lib/_stream_transform'),\n _stream_writable: require.resolve('readable-stream/lib/_stream_writable'),\n string_decoder: require.resolve('string_decoder/'),\n /* eslint-enable @typescript-eslint/naming-convention */\n sys: require.resolve('util/'),\n timers: require.resolve('timers-browserify'),\n tty: require.resolve('tty-browserify'),\n url: require.resolve('url/'),\n util: require.resolve('util/'),\n vm: require.resolve('vm-browserify'),\n zlib: require.resolve('browserify-zlib'),\n};\n\n/**\n * Get the default loader for JavaScript and TypeScript files, based on the\n * config object.\n *\n * - If the `legacy` option is set, we use the custom `browserify` loader. This\n * uses the legacy Browserify config to transpile the code.\n * - Otherwise, we use the `swc-loader`. This is a Webpack loader that uses the\n * `SWC` compiler, which is a much faster alternative to Babel and TypeScript's\n * own compiler.\n *\n * @param config - The processed snap Webpack config.\n * @param config.legacy - The legacy config object, if any.\n * @param config.sourceMap - Whether to generate source maps.\n * @see https://swc.rs/docs/usage/swc-loader\n * @returns The default loader.\n */\nexport async function getDefaultLoader({\n legacy,\n sourceMap,\n}: ProcessedWebpackConfig) {\n if (legacy) {\n return {\n /**\n * If the snap uses the legacy config, we use the custom `browserify`\n * loader. This uses the legacy Browserify config to transpile the code.\n * This is necessary for backwards compatibility with the\n * `bundlerCustomizer` function.\n */\n loader: resolve(__dirname, 'loaders', 'browserify'),\n\n /**\n * The options for the `browserify` loader. These can be overridden in the\n * snap config.\n */\n options: legacy,\n };\n }\n\n const targets = await getBrowserslistTargets();\n return {\n /**\n * We use the `swc-loader` to transpile TypeScript and JavaScript files.\n * This is a Webpack loader that uses the `SWC` compiler, which is a much\n * faster alternative to Babel and TypeScript's own compiler.\n */\n loader: 'swc-loader',\n\n /**\n * The options for the `swc-loader`. These can be overridden in the\n * `.swcrc` file.\n *\n * @see https://swc.rs/docs/configuration/swcrc\n */\n options: {\n sync: false,\n\n /**\n * This tells SWC to generate source maps. We set it to the\n * `sourceMap` value from the config object.\n *\n * This must be enabled if source maps are enabled in the config.\n */\n sourceMaps: Boolean(getDevTool(sourceMap)),\n\n jsc: {\n parser: {\n /**\n * This tells the parser to parse TypeScript files. If you\n * don't need to support TypeScript, you can set this to\n * `ecmascript` instead, but there's no harm in leaving it\n * as `typescript`.\n *\n * @see https://swc.rs/docs/configuration/compilation#jscparser\n */\n syntax: 'typescript',\n },\n },\n\n /**\n * The module configuration. This tells SWC how to output the\n * transpiled code.\n *\n * @see https://swc.rs/docs/configuration/modules\n */\n module: {\n /**\n * This tells SWC to output CommonJS modules. MetaMask Snaps\n * doesn't support ES modules yet, so this is necessary.\n *\n * @see https://swc.rs/docs/configuration/modules#commonjs\n */\n type: 'commonjs',\n },\n\n env: {\n targets: targets.join(', '),\n },\n },\n };\n}\n\n/**\n * Get the Webpack devtool configuration based on the given snap config.\n *\n * - If `sourceMap` is `inline`, return `inline-source-map`.\n * - If `sourceMap` is `true`, return `source-map`.\n * - Otherwise, return `false`.\n *\n * @param sourceMap - The `sourceMap` value from the snap config.\n * @returns The Webpack devtool configuration.\n */\nexport function getDevTool(\n sourceMap: ProcessedWebpackConfig['sourceMap'],\n): Configuration['devtool'] {\n if (sourceMap === 'inline') {\n return 'inline-source-map';\n }\n\n if (sourceMap === true) {\n return 'source-map';\n }\n\n return false;\n}\n\n/**\n * Get a function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n *\n * @param spinner - The spinner to update.\n * @param spinnerText - The initial spinner text. This will be prepended to the\n * percentage.\n * @returns A function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n */\n// Note: This is extracted for testing purposes.\nexport function getProgressHandler(spinner?: Ora, spinnerText?: string) {\n return (percentage: number) => {\n if (spinner && spinnerText) {\n spinner.text = `${spinnerText} ${dim(\n `(${Math.round(percentage * 100)}%)`,\n )}`;\n }\n };\n}\n\n/**\n * Get the targets from the `.browserslistrc` file.\n *\n * @returns The browser targets as an array of strings.\n */\nexport async function getBrowserslistTargets() {\n const contents = await fs.readFile(BROWSERSLIST_FILE, 'utf8');\n return contents\n .split('\\n')\n .map((line) => line.trim())\n .filter((line) => line && !line.startsWith('#'));\n}\n\n/**\n * Get a singular or plural string based on the given count. This is useful for\n * generating messages like \"1 error\" or \"2 errors\". By default, the plural\n * string is the singular string with an \"s\" appended to it.\n *\n * This assumes that the text is in English, and likely won't work for some\n * other languages.\n *\n * @param count - The count.\n * @param singular - The singular string.\n * @param plural - The plural string.\n * @returns The singular or plural string.\n * @example\n * ```typescript\n * pluralize(1, 'error'); // => 'error'\n * pluralize(2, 'error'); // => 'errors'\n * pluralize(1, 'error', 'problem'); // => 'error'\n * pluralize(2, 'error', 'problems'); // => 'problems'\n * ```\n */\nexport function pluralize(\n count: number,\n singular: string,\n plural = `${singular}s`,\n) {\n return count === 1 ? singular : plural;\n}\n\n/**\n * Get an object that can be used as fallback config for Webpack's\n * `fallback` config.\n *\n * @param polyfills - The polyfill object from the snap config.\n * @returns The webpack fallback config.\n */\nexport function getFallbacks(polyfills: ProcessedWebpackConfig['polyfills']): {\n [index: string]: string | false;\n} {\n if (polyfills === true) {\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS] ?? false,\n ]),\n );\n }\n\n if (polyfills === false) {\n return Object.fromEntries(builtinModules.map((name) => [name, false]));\n }\n\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n polyfills[name as keyof ProcessedWebpackConfig['polyfills']]\n ? WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS]\n : false,\n ]),\n );\n}\n"],"names":["dim","promises","fs","builtinModules","dirname","resolve","BROWSERSLIST_FILE","require","WEBPACK_FALLBACKS","assert","buffer","console","constants","crypto","domain","events","http","https","os","path","punycode","process","querystring","stream","_stream_duplex","_stream_passthrough","_stream_readable","_stream_transform","_stream_writable","string_decoder","sys","timers","tty","url","util","vm","zlib","getDefaultLoader","legacy","sourceMap","loader","__dirname","options","targets","getBrowserslistTargets","sync","sourceMaps","Boolean","getDevTool","jsc","parser","syntax","module","type","env","join","getProgressHandler","spinner","spinnerText","percentage","text","Math","round","contents","readFile","split","map","line","trim","filter","startsWith","pluralize","count","singular","plural","getFallbacks","polyfills","Object","fromEntries","name"],"mappings":"AAAA,SAASA,GAAG,QAAQ,QAAQ;AAC5B,SAASC,YAAYC,EAAE,QAAQ,KAAK;AACpC,SAASC,cAAc,QAAQ,SAAS;AAExC,SAASC,OAAO,EAAEC,OAAO,QAAQ,OAAO;AAKxC,OAAO,MAAMC,oBAAoBD,QAC/BD,QACE,mDAAmD;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C;IAEvB,OAAOD,UAAU,IAAIC,WAAWC;AAClC;AAEA;;;;;;CAMC,GACD,OAAO,SAASC,aAAaC,SAA8C;IAGzE,IAAIA,cAAc,MAAM;QACtB,OAAOC,OAAOC,WAAW,CACvB3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;gBAC3BA;gBACAvE,iBAAiB,CAACuE,KAAuC,IAAI;aAC9D;IAEL;IAEA,IAAIH,cAAc,OAAO;QACvB,OAAOC,OAAOC,WAAW,CAAC3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;gBAACA;gBAAM;aAAM;IACtE;IAEA,OAAOF,OAAOC,WAAW,CACvB3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;YAC3BA;YACAH,SAAS,CAACG,KAAkD,GACxDvE,iBAAiB,CAACuE,KAAuC,GACzD;SACL;AAEL"}
1
+ {"version":3,"sources":["../../../src/webpack/utils.ts"],"sourcesContent":["import { dim } from 'chalk';\nimport { promises as fs } from 'fs';\nimport { builtinModules } from 'module';\nimport type { Ora } from 'ora';\nimport { dirname, resolve } from 'path';\nimport type { Configuration } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\n\nexport const BROWSERSLIST_FILE = resolve(\n dirname(\n // eslint-disable-next-line n/no-extraneous-require\n require.resolve('@metamask/snaps-cli/package.json'),\n ),\n '.browserslistrc',\n);\n\nexport const WEBPACK_FALLBACKS = {\n assert: require.resolve('assert/'),\n buffer: require.resolve('buffer/'),\n console: require.resolve('console-browserify'),\n constants: require.resolve('constants-browserify'),\n crypto: require.resolve('crypto-browserify'),\n domain: require.resolve('domain-browser'),\n events: require.resolve('events/'),\n http: require.resolve('stream-http'),\n https: require.resolve('https-browserify'),\n os: require.resolve('os-browserify/browser'),\n path: require.resolve('path-browserify'),\n punycode: require.resolve('punycode/'),\n process: require.resolve('process/browser'),\n querystring: require.resolve('querystring-es3'),\n stream: require.resolve('stream-browserify'),\n /* eslint-disable @typescript-eslint/naming-convention */\n _stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),\n _stream_passthrough: require.resolve(\n 'readable-stream/lib/_stream_passthrough',\n ),\n _stream_readable: require.resolve('readable-stream/lib/_stream_readable'),\n _stream_transform: require.resolve('readable-stream/lib/_stream_transform'),\n _stream_writable: require.resolve('readable-stream/lib/_stream_writable'),\n string_decoder: require.resolve('string_decoder/'),\n /* eslint-enable @typescript-eslint/naming-convention */\n sys: require.resolve('util/'),\n timers: require.resolve('timers-browserify'),\n tty: require.resolve('tty-browserify'),\n url: require.resolve('url/'),\n util: require.resolve('util/'),\n vm: require.resolve('vm-browserify'),\n zlib: require.resolve('browserify-zlib'),\n};\n\n/**\n * Get the default loader for JavaScript and TypeScript files, based on the\n * config object.\n *\n * - If the `legacy` option is set, we use the custom `browserify` loader. This\n * uses the legacy Browserify config to transpile the code.\n * - Otherwise, we use the `swc-loader`. This is a Webpack loader that uses the\n * `SWC` compiler, which is a much faster alternative to Babel and TypeScript's\n * own compiler.\n *\n * @param config - The processed snap Webpack config.\n * @param config.legacy - The legacy config object, if any.\n * @param config.sourceMap - Whether to generate source maps.\n * @see https://swc.rs/docs/usage/swc-loader\n * @returns The default loader.\n */\nexport async function getDefaultLoader({\n legacy,\n sourceMap,\n}: ProcessedWebpackConfig) {\n if (legacy) {\n return {\n /**\n * If the snap uses the legacy config, we use the custom `browserify`\n * loader. This uses the legacy Browserify config to transpile the code.\n * This is necessary for backwards compatibility with the\n * `bundlerCustomizer` function.\n */\n loader: resolve(__dirname, 'loaders', 'browserify'),\n\n /**\n * The options for the `browserify` loader. These can be overridden in the\n * snap config.\n */\n options: legacy,\n };\n }\n\n const targets = await getBrowserslistTargets();\n return {\n /**\n * We use the `swc-loader` to transpile TypeScript and JavaScript files.\n * This is a Webpack loader that uses the `SWC` compiler, which is a much\n * faster alternative to Babel and TypeScript's own compiler.\n */\n loader: require.resolve('swc-loader'),\n\n /**\n * The options for the `swc-loader`. These can be overridden in the\n * `.swcrc` file.\n *\n * @see https://swc.rs/docs/configuration/swcrc\n */\n options: {\n sync: false,\n\n /**\n * This tells SWC to generate source maps. We set it to the\n * `sourceMap` value from the config object.\n *\n * This must be enabled if source maps are enabled in the config.\n */\n sourceMaps: Boolean(getDevTool(sourceMap)),\n\n jsc: {\n parser: {\n /**\n * This tells the parser to parse TypeScript files. If you\n * don't need to support TypeScript, you can set this to\n * `ecmascript` instead, but there's no harm in leaving it\n * as `typescript`.\n *\n * @see https://swc.rs/docs/configuration/compilation#jscparser\n */\n syntax: 'typescript',\n },\n },\n\n /**\n * The module configuration. This tells SWC how to output the\n * transpiled code.\n *\n * @see https://swc.rs/docs/configuration/modules\n */\n module: {\n /**\n * This tells SWC to output CommonJS modules. MetaMask Snaps\n * doesn't support ES modules yet, so this is necessary.\n *\n * @see https://swc.rs/docs/configuration/modules#commonjs\n */\n type: 'commonjs',\n },\n\n env: {\n targets: targets.join(', '),\n },\n },\n };\n}\n\n/**\n * Get the Webpack devtool configuration based on the given snap config.\n *\n * - If `sourceMap` is `inline`, return `inline-source-map`.\n * - If `sourceMap` is `true`, return `source-map`.\n * - Otherwise, return `false`.\n *\n * @param sourceMap - The `sourceMap` value from the snap config.\n * @returns The Webpack devtool configuration.\n */\nexport function getDevTool(\n sourceMap: ProcessedWebpackConfig['sourceMap'],\n): Configuration['devtool'] {\n if (sourceMap === 'inline') {\n return 'inline-source-map';\n }\n\n if (sourceMap === true) {\n return 'source-map';\n }\n\n return false;\n}\n\n/**\n * Get a function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n *\n * @param spinner - The spinner to update.\n * @param spinnerText - The initial spinner text. This will be prepended to the\n * percentage.\n * @returns A function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n */\n// Note: This is extracted for testing purposes.\nexport function getProgressHandler(spinner?: Ora, spinnerText?: string) {\n return (percentage: number) => {\n if (spinner && spinnerText) {\n spinner.text = `${spinnerText} ${dim(\n `(${Math.round(percentage * 100)}%)`,\n )}`;\n }\n };\n}\n\n/**\n * Get the targets from the `.browserslistrc` file.\n *\n * @returns The browser targets as an array of strings.\n */\nexport async function getBrowserslistTargets() {\n const contents = await fs.readFile(BROWSERSLIST_FILE, 'utf8');\n return contents\n .split('\\n')\n .map((line) => line.trim())\n .filter((line) => line && !line.startsWith('#'));\n}\n\n/**\n * Get a singular or plural string based on the given count. This is useful for\n * generating messages like \"1 error\" or \"2 errors\". By default, the plural\n * string is the singular string with an \"s\" appended to it.\n *\n * This assumes that the text is in English, and likely won't work for some\n * other languages.\n *\n * @param count - The count.\n * @param singular - The singular string.\n * @param plural - The plural string.\n * @returns The singular or plural string.\n * @example\n * ```typescript\n * pluralize(1, 'error'); // => 'error'\n * pluralize(2, 'error'); // => 'errors'\n * pluralize(1, 'error', 'problem'); // => 'error'\n * pluralize(2, 'error', 'problems'); // => 'problems'\n * ```\n */\nexport function pluralize(\n count: number,\n singular: string,\n plural = `${singular}s`,\n) {\n return count === 1 ? singular : plural;\n}\n\n/**\n * Get an object that can be used as fallback config for Webpack's\n * `fallback` config.\n *\n * @param polyfills - The polyfill object from the snap config.\n * @returns The webpack fallback config.\n */\nexport function getFallbacks(polyfills: ProcessedWebpackConfig['polyfills']): {\n [index: string]: string | false;\n} {\n if (polyfills === true) {\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS] ?? false,\n ]),\n );\n }\n\n if (polyfills === false) {\n return Object.fromEntries(builtinModules.map((name) => [name, false]));\n }\n\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n polyfills[name as keyof ProcessedWebpackConfig['polyfills']]\n ? WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS]\n : false,\n ]),\n );\n}\n"],"names":["dim","promises","fs","builtinModules","dirname","resolve","BROWSERSLIST_FILE","require","WEBPACK_FALLBACKS","assert","buffer","console","constants","crypto","domain","events","http","https","os","path","punycode","process","querystring","stream","_stream_duplex","_stream_passthrough","_stream_readable","_stream_transform","_stream_writable","string_decoder","sys","timers","tty","url","util","vm","zlib","getDefaultLoader","legacy","sourceMap","loader","__dirname","options","targets","getBrowserslistTargets","sync","sourceMaps","Boolean","getDevTool","jsc","parser","syntax","module","type","env","join","getProgressHandler","spinner","spinnerText","percentage","text","Math","round","contents","readFile","split","map","line","trim","filter","startsWith","pluralize","count","singular","plural","getFallbacks","polyfills","Object","fromEntries","name"],"mappings":"AAAA,SAASA,GAAG,QAAQ,QAAQ;AAC5B,SAASC,YAAYC,EAAE,QAAQ,KAAK;AACpC,SAASC,cAAc,QAAQ,SAAS;AAExC,SAASC,OAAO,EAAEC,OAAO,QAAQ,OAAO;AAKxC,OAAO,MAAMC,oBAAoBD,QAC/BD,QACE,mDAAmD;AACnDG,QAAQF,OAAO,CAAC,sCAElB,mBACA;AAEF,OAAO,MAAMG,oBAAoB;IAC/BC,QAAQF,QAAQF,OAAO,CAAC;IACxBK,QAAQH,QAAQF,OAAO,CAAC;IACxBM,SAASJ,QAAQF,OAAO,CAAC;IACzBO,WAAWL,QAAQF,OAAO,CAAC;IAC3BQ,QAAQN,QAAQF,OAAO,CAAC;IACxBS,QAAQP,QAAQF,OAAO,CAAC;IACxBU,QAAQR,QAAQF,OAAO,CAAC;IACxBW,MAAMT,QAAQF,OAAO,CAAC;IACtBY,OAAOV,QAAQF,OAAO,CAAC;IACvBa,IAAIX,QAAQF,OAAO,CAAC;IACpBc,MAAMZ,QAAQF,OAAO,CAAC;IACtBe,UAAUb,QAAQF,OAAO,CAAC;IAC1BgB,SAASd,QAAQF,OAAO,CAAC;IACzBiB,aAAaf,QAAQF,OAAO,CAAC;IAC7BkB,QAAQhB,QAAQF,OAAO,CAAC;IACxB,wDAAwD,GACxDmB,gBAAgBjB,QAAQF,OAAO,CAAC;IAChCoB,qBAAqBlB,QAAQF,OAAO,CAClC;IAEFqB,kBAAkBnB,QAAQF,OAAO,CAAC;IAClCsB,mBAAmBpB,QAAQF,OAAO,CAAC;IACnCuB,kBAAkBrB,QAAQF,OAAO,CAAC;IAClCwB,gBAAg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,CAAC,CAAC,EAAE6D,KAAKC,KAAK,CAACH,aAAa,KAAK,EAAE,CAAC,EACpC,CAAC;QACL;IACF;AACF;AAEA;;;;CAIC,GACD,OAAO,eAAef;IACpB,MAAMmB,WAAW,MAAM7D,GAAG8D,QAAQ,CAAC1D,mBAAmB;IACtD,OAAOyD,SACJE,KAAK,CAAC,MACNC,GAAG,CAAC,CAACC,OAASA,KAAKC,IAAI,IACvBC,MAAM,CAAC,CAACF,OAASA,QAAQ,CAACA,KAAKG,UAAU,CAAC;AAC/C;AAEA;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,SAASC,UACdC,KAAa,EACbC,QAAgB,EAChBC,SAAS,CAAC,EAAED,SAAS,CAAC,CAAC;IAEvB,OAAOD,UAAU,IAAIC,WAAWC;AAClC;AAEA;;;;;;CAMC,GACD,OAAO,SAASC,aAAaC,SAA8C;IAGzE,IAAIA,cAAc,MAAM;QACtB,OAAOC,OAAOC,WAAW,CACvB3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;gBAC3BA;gBACAvE,iBAAiB,CAACuE,KAAuC,IAAI;aAC9D;IAEL;IAEA,IAAIH,cAAc,OAAO;QACvB,OAAOC,OAAOC,WAAW,CAAC3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;gBAACA;gBAAM;aAAM;IACtE;IAEA,OAAOF,OAAOC,WAAW,CACvB3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;YAC3BA;YACAH,SAAS,CAACG,KAAkD,GACxDvE,iBAAiB,CAACuE,KAAuC,GACzD;SACL;AAEL"}
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Check the Node version. If the Node version is less than the minimum required
3
+ * version, this logs an error and exits the process.
4
+ *
5
+ * @param nodeVersion - The Node version to check.
6
+ */
7
+ export declare function checkNodeVersion(nodeVersion?: string): void;
1
8
  /**
2
9
  * The main CLI entry point function. This processes the command line args, and
3
10
  * runs the appropriate function.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-cli",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "A CLI for developing MetaMask Snaps.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -56,7 +56,7 @@
56
56
  "@babel/preset-env": "^7.20.12",
57
57
  "@babel/preset-typescript": "^7.20.12",
58
58
  "@metamask/snaps-utils": "^2.0.1",
59
- "@metamask/snaps-webpack-plugin": "^2.0.0",
59
+ "@metamask/snaps-webpack-plugin": "^2.0.1",
60
60
  "@metamask/utils": "^8.1.0",
61
61
  "@swc/core": "1.3.78",
62
62
  "assert": "^2.0.0",
@@ -79,6 +79,7 @@
79
79
  "punycode": "^2.3.0",
80
80
  "querystring-es3": "^0.2.1",
81
81
  "readable-stream": "^3.6.2",
82
+ "semver": "^7.5.4",
82
83
  "serve-handler": "^6.1.5",
83
84
  "stream-browserify": "^3.0.0",
84
85
  "stream-http": "^3.2.0",