Skip to content

Commit d0bdf31

Browse files
fix: Throw error when invalid port is used (#188)
* fix: Throw error when invalid port is used * fix: show actual wrong input value
1 parent ca0e02a commit d0bdf31

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/bin/commands/build.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,49 @@ export const buildCommand: yargs.CommandModule<
9898
describe: "Path to the project that we'll create a snapshot from",
9999
type: "string",
100100
demandOption: "Path to the project is required",
101+
})
102+
.check((argv) => {
103+
// Validate ports parameter - ensure all values are valid numbers
104+
if (argv.ports && argv.ports.length > 0) {
105+
const invalidPortsWithOriginal: string[] = [];
106+
107+
// Get the original arguments to show what the user actually typed
108+
const originalArgs = process.argv;
109+
const portArgIndices: number[] = [];
110+
111+
// Find all --ports arguments in the original command
112+
originalArgs.forEach((arg, i) => {
113+
if (arg === '--ports' && i + 1 < originalArgs.length) {
114+
portArgIndices.push(i + 1);
115+
}
116+
});
117+
118+
argv.ports.forEach((port, i) => {
119+
const isInvalid = !Number.isInteger(port) ||
120+
port <= 0 ||
121+
port > 65535 ||
122+
!Number.isFinite(port);
123+
124+
if (isInvalid) {
125+
// Try to get the original input, fallback to the parsed value
126+
const originalInput = portArgIndices[i] ? originalArgs[portArgIndices[i]] : String(port);
127+
invalidPortsWithOriginal.push(originalInput);
128+
}
129+
});
130+
131+
if (invalidPortsWithOriginal.length > 0) {
132+
throw new Error(
133+
`Invalid port value(s): ${invalidPortsWithOriginal.join(
134+
", "
135+
)}. Ports must be integers between 1 and 65535.`
136+
);
137+
}
138+
}
139+
return true;
101140
}),
102141

103142
handler: async (argv) => {
143+
104144
const apiKey = getInferredApiKey();
105145
const api = new API({ apiKey, instrumentation: instrumentedFetch });
106146
const sdk = new CodeSandbox(apiKey);

0 commit comments

Comments
 (0)