-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.ts
More file actions
149 lines (132 loc) · 4 KB
/
utils.ts
File metadata and controls
149 lines (132 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as fs from "fs";
import { parse as parseSemver } from "semver";
import type { ParsedVersion } from "./types";
/**
* Get the installed Next.js version by reading package.json from node_modules.
*
* @returns The Next.js version string, or undefined if not found
*/
export function getNextjsVersion(): string | undefined {
try {
// Resolve from cwd so we find the app's next, not the SDK's devDependency.
// This also handles monorepos where next is hoisted to the workspace root.
const resolvedPath = require.resolve("next/package.json", { paths: [process.cwd()] });
const packageJson = JSON.parse(fs.readFileSync(resolvedPath, "utf-8"));
return packageJson.version;
} catch {
return undefined;
}
}
/**
* Parse a semantic version string into its components.
*
* @param version - The version string to parse (e.g., "15.0.0-canary.124")
* @returns Parsed version object with major, minor, patch, and prerelease
*/
export function parseVersion(version: string): ParsedVersion {
try {
const parsed = parseSemver(version);
if (!parsed) {
return {
major: undefined,
minor: undefined,
patch: undefined,
prerelease: undefined,
};
}
return {
major: parsed.major,
minor: parsed.minor,
patch: parsed.patch,
prerelease: parsed.prerelease.length > 0 ? parsed.prerelease.join(".") : undefined,
};
} catch {
return {
major: undefined,
minor: undefined,
patch: undefined,
prerelease: undefined,
};
}
}
/**
* Check if the Next.js version requires the instrumentationHook to be set.
* From Next.js 15.0.0-rc.1 onwards, the instrumentationHook is no longer needed
* and Next.js will warn if it's set.
*
* @param version - The Next.js version string
* @returns true if instrumentationHook should be set, false otherwise
*/
export function shouldSetInstrumentationHook(version: string | undefined): boolean {
if (!version) {
// If we can't detect the version, err on the side of setting it
// (better to have a warning than broken instrumentation)
return true;
}
const { major, minor, patch, prerelease } = parseVersion(version);
// Unable to parse version
if (major === undefined || minor === undefined || patch === undefined) {
return true;
}
// Next.js 16+ definitely doesn't need it
if (major >= 16) {
return false;
}
// Next.js 14 and below need it
if (major < 15) {
return true;
}
// Next.js 15.x.x - check specific versions
if (major === 15) {
// 15.0.0 stable and higher don't need it
if (minor > 0 || patch > 0) {
return false;
}
// Check if it's 15.0.0 with no prerelease (stable)
if (minor === 0 && patch === 0 && prerelease === undefined) {
return false;
}
// Check for RC versions (rc.1 and higher don't need it)
if (prerelease?.startsWith("rc.")) {
const rcNumber = parseInt(prerelease.split(".")[1] || "0", 10);
if (rcNumber >= 1) {
return false;
}
}
// Check for canary versions (canary.124 and higher don't need it)
if (prerelease?.startsWith("canary.")) {
const canaryNumber = parseInt(prerelease.split(".")[1] || "0", 10);
if (canaryNumber >= 124) {
return false;
}
}
// All other 15.0.0 prereleases need it
return true;
}
// Default to true for safety
return true;
}
/**
* Log a message if debug mode is enabled.
*
* @param debug - Whether debug mode is enabled
* @param message - The message to log
*/
export function debugLog(debug: boolean, message: string): void {
if (debug) {
// eslint-disable-next-line no-console
console.log(`[Tusk Drift] ${message}`);
}
}
/**
* Log a warning message if warnings are not suppressed.
*
* @param suppress - Whether to suppress the warning
* @param message - The warning message to log
*/
export function warn(suppress: boolean, message: string): void {
if (!suppress) {
// eslint-disable-next-line no-console
console.warn(`[Tusk Drift] ${message}`);
}
}