hopp.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env node
  2. // * The entry point of the CLI
  3. // @ts-check
  4. import chalk from "chalk";
  5. import { spawnSync } from "child_process";
  6. import fs from "fs";
  7. import { cloneDeep } from "lodash-es";
  8. import semver from "semver";
  9. import { fileURLToPath } from "url";
  10. const highlightVersion = (version) => chalk.black.bgYellow(`v${version}`);
  11. const packageJsonPath = fileURLToPath(
  12. new URL("../package.json", import.meta.url)
  13. );
  14. const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
  15. const requiredNodeVersionRange = packageJson.engines?.node || ">=20";
  16. // Extract the major version from the start of the range
  17. const requiredNodeVersion = semver.major(
  18. semver.minVersion(requiredNodeVersionRange) ?? "20"
  19. );
  20. const currentNodeVersion = process.versions.node;
  21. // Last supported version of the CLI for Node.js v18
  22. const lastSupportedVersion = "0.11.1";
  23. if (!semver.satisfies(currentNodeVersion, requiredNodeVersionRange)) {
  24. console.error(
  25. `${chalk.greenBright("Hoppscotch CLI")} requires Node.js ${highlightVersion(requiredNodeVersion)} or higher and you're on Node.js ${highlightVersion(currentNodeVersion)}.`
  26. );
  27. console.error(
  28. `\nIf you prefer staying on Node.js ${highlightVersion("18")}, you can install the last supported version of the CLI:\n` +
  29. `${chalk.green(`npm install -g @hoppscotch/cli@${lastSupportedVersion}`)}`
  30. );
  31. process.exit(1);
  32. }
  33. // Dynamically importing the module after the Node.js version check prevents errors due to unrecognized APIs in older Node.js versions
  34. const { cli } = await import("../dist/index.js");
  35. // As per isolated-vm documentation, we need to supply `--no-node-snapshot` for node >= 20
  36. // src: https://github.com/laverdet/isolated-vm?tab=readme-ov-file#requirements
  37. if (!process.execArgv.includes("--no-node-snapshot")) {
  38. const argCopy = cloneDeep(process.argv);
  39. // Replace first argument with --no-node-snapshot
  40. // We can get argv[0] from process.argv0
  41. argCopy[0] = "--no-node-snapshot";
  42. const result = spawnSync(process.argv0, argCopy, { stdio: "inherit" });
  43. // Exit with the same status code as the spawned process
  44. process.exit(result.status ?? 0);
  45. } else {
  46. cli(process.argv);
  47. }