2024-02-05 17:25:05 +00:00
#!/usr/bin/env node
// * The entry point of the CLI
2024-04-19 15:38:46 +00:00
// @ts-check
2024-02-05 17:25:05 +00:00
2024-12-02 16:19:46 +00:00
import chalk from "chalk" ;
2024-04-19 15:38:46 +00:00
import { spawnSync } from "child_process" ;
2024-12-02 16:19:46 +00:00
import fs from "fs" ;
2024-04-19 15:38:46 +00:00
import { cloneDeep } from "lodash-es" ;
2024-12-02 16:19:46 +00:00
import semver from "semver" ;
import { fileURLToPath } from "url" ;
const highlightVersion = ( version ) => chalk . black . bgYellow ( ` v ${ version } ` ) ;
const packageJsonPath = fileURLToPath (
new URL ( "../package.json" , import . meta . url )
) ;
const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , "utf-8" ) ) ;
2025-11-24 08:51:29 +00:00
const requiredNodeVersionRange = packageJson . engines ? . node || ">=22" ;
2024-04-19 15:38:46 +00:00
2024-12-02 16:19:46 +00:00
// Extract the major version from the start of the range
const requiredNodeVersion = semver . major (
2025-11-24 08:51:29 +00:00
semver . minVersion ( requiredNodeVersionRange ) ? ? "22"
2024-12-02 16:19:46 +00:00
) ;
const currentNodeVersion = process . versions . node ;
2025-11-24 08:51:29 +00:00
// Last supported version of the CLI for Node.js v20
const lastSupportedVersion = "0.26.0" ;
2024-12-02 16:19:46 +00:00
if ( ! semver . satisfies ( currentNodeVersion , requiredNodeVersionRange ) ) {
console . error (
` ${ chalk . greenBright ( "Hoppscotch CLI" ) } requires Node.js ${ highlightVersion ( requiredNodeVersion ) } or higher and you're on Node.js ${ highlightVersion ( currentNodeVersion ) } . `
) ;
console . error (
2025-11-24 08:51:29 +00:00
` \n If you prefer staying on Node.js ${ highlightVersion ( "20" ) } , you can install the last supported version of the CLI: \n ` +
` ${ chalk . green ( ` npm install -g @hoppscotch/cli@ ${ lastSupportedVersion } ` ) } alongside ${ highlightVersion ( "2025.10.1" ) } of the Hoppscotch app. \n `
2024-12-02 16:19:46 +00:00
) ;
process . exit ( 1 ) ;
}
// Dynamically importing the module after the Node.js version check prevents errors due to unrecognized APIs in older Node.js versions
const { cli } = await import ( "../dist/index.js" ) ;
2024-04-19 15:38:46 +00:00
// As per isolated-vm documentation, we need to supply `--no-node-snapshot` for node >= 20
// src: https://github.com/laverdet/isolated-vm?tab=readme-ov-file#requirements
2024-12-02 16:19:46 +00:00
if ( ! process . execArgv . includes ( "--no-node-snapshot" ) ) {
2024-04-19 15:38:46 +00:00
const argCopy = cloneDeep ( process . argv ) ;
// Replace first argument with --no-node-snapshot
// We can get argv[0] from process.argv0
argCopy [ 0 ] = "--no-node-snapshot" ;
2024-12-02 16:19:46 +00:00
const result = spawnSync ( process . argv0 , argCopy , { stdio : "inherit" } ) ;
2024-04-19 15:38:46 +00:00
// Exit with the same status code as the spawned process
process . exit ( result . status ? ? 0 ) ;
} else {
cli ( process . argv ) ;
}