Skip to content

Commit

Permalink
fix: ensure clear error when using Node.js < 14.18.0 (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts authored Aug 22, 2022
1 parent 0c09937 commit c705bf9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 48 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/test-app-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,25 @@ jobs:
os: [ubuntu-20.04, macos-11, windows-2022]
group: [0, 1, 2]
include:
- node-version: 14
# Important: Node 14 version must match minimum checked
# for CLI package.json, as well as IntelliJ and VS Code
# node --version checks.
- node-version: 14.18.0
os: ubuntu-latest
group: 0
- node-version: 14
- node-version: 14.18.0
os: ubuntu-latest
group: 1
- node-version: 14
- node-version: 14.18.0
os: ubuntu-latest
group: 2
- node-version: 16
- node-version: 16.0.0
os: ubuntu-latest
group: 0
- node-version: 16
- node-version: 16.0.0
os: ubuntu-latest
group: 1
- node-version: 16
- node-version: 16.0.0
os: ubuntu-latest
group: 2
steps:
Expand Down
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"typescript": "4.7.4"
},
"engines": {
"node": ">=14"
"node": "^14.18.0 || >=16.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ class PreviewJsSharedService : Disposable {
api = runServer(msg.project)
}
openDocsForFirstUsage()
} catch (e: NodeVersionError) {
notificationGroup.createNotification(
"Incompatible Node.js version",
e.message,
NotificationType.ERROR
)
.notify(msg.project)
return@actor
} catch (e: Throwable) {
notificationGroup.createNotification(
"Preview.js crashed",
Expand Down Expand Up @@ -158,15 +166,16 @@ Include the content of the Preview.js logs panel for easier debugging.
checkNodeVersion(nodeVersionProcess)
} catch (e: Error) {
// Unable to start Node. Check WSL if we're on Windows.
if (isWindows()) {
val nodeVersionProcessWsl = processBuilder("node --version", useWsl = true).directory(nodeDirPath.toFile()).start()
if (nodeVersionProcessWsl.waitFor() === 0) {
checkNodeVersion(nodeVersionProcessWsl)
useWsl = true
} else {
// If WSL failed, just ignore it.
throw e
}
if (!isWindows()) {
throw e
}
val nodeVersionProcessWsl = processBuilder("node --version", useWsl = true).directory(nodeDirPath.toFile()).start()
if (nodeVersionProcessWsl.waitFor() === 0) {
checkNodeVersion(nodeVersionProcessWsl)
useWsl = true
} else {
// If WSL failed, just ignore it.
throw e
}
}
val builder = processBuilder("node dist/main.js $port", useWsl)
Expand Down Expand Up @@ -210,11 +219,13 @@ Include the content of the Preview.js logs panel for easier debugging.

private fun checkNodeVersion(process: Process) {
val nodeVersion = readInputStream(process.inputStream)
val matchResult = Regex.fromLiteral("^v(\\d+).*\$").find(nodeVersion)
val matchResult = "v(\\d+)\\.(\\d+)".toRegex().find(nodeVersion)
matchResult?.let {
val majorVersion = matchResult.groups[1]?.value?.toInt()
if (majorVersion != null && majorVersion < 14) {
throw Error("Preview.js needs NodeJS 14+ to run, but current version is: ${nodeVersion}\n\nPlease upgrade then restart your IDE.")
val majorVersion = matchResult.groups[1]!!.value.toInt()
val minorVersion = matchResult.groups[2]!!.value.toInt()
// Minimum version: 14.18.0.
if (majorVersion < 14 || majorVersion === 14 && minorVersion < 18) {
throw NodeVersionError("Preview.js needs NodeJS 14.18.0+ to run, but current version is: ${nodeVersion}\n\nPlease upgrade then restart your IDE.")
}
}
}
Expand Down Expand Up @@ -280,3 +291,5 @@ Include the content of the Preview.js logs panel for easier debugging.

private fun isWindows() = System.getProperty("os.name").lowercase().contains("win")
}

class NodeVersionError(override val message: String) : Exception(message)
10 changes: 6 additions & 4 deletions integrations/vscode/src/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,18 @@ export async function startPreviewJsServer(outputChannel: OutputChannel) {
function checkNodeVersionResult(result: execa.ExecaReturnValue<string>) {
if (result.failed || result.exitCode !== 0) {
throw new Error(
`Preview.js needs NodeJS 14+ but running \`node\` failed.\n\nIs it installed? You may need to restart your IDE.`
`Preview.js needs NodeJS 14.18.0+ but running \`node\` failed.\n\nIs it installed? You may need to restart your IDE.`
);
}
const nodeVersion = result.stdout;
const match = nodeVersion.match(/^v(\d+).*$/);
const match = nodeVersion.match(/^v(\d+)\.(\d+).*$/);
if (match) {
const majorVersion = parseInt(match[1]!, 10);
if (majorVersion < 14) {
const minorVersion = parseInt(match[2]!, 10);
// Minimum version: 14.18.0.
if (majorVersion < 14 || (majorVersion === 14 && minorVersion < 18)) {
throw new Error(
`Preview.js needs NodeJS 14+ to run, but current version is: ${nodeVersion}\n\nPlease upgrade then restart your IDE.`
`Preview.js needs NodeJS 14.18.0+ to run, but current version is: ${nodeVersion}\n\nPlease upgrade then restart your IDE.`
);
}
}
Expand Down
24 changes: 0 additions & 24 deletions loader/src/checkNodeVersion.ts

This file was deleted.

0 comments on commit c705bf9

Please sign in to comment.