Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/targets/deploy-vercel/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ describe('Vercel deployment target', () => {
});
});

it('rejects invalid Vercel config before plan or CLI work', async () => {
await expect(adapter.build(fakeBuildContext() as any, {
dir: ' ',
})).rejects.toThrow('deploy-vercel requires dir');

await expect(adapter.ship(fakeShipContext({
dryRun: true,
}) as any, {
project: 'bad/project',
})).rejects.toThrow('project must contain only letters');

await expect(adapter.ship(fakeShipContext({
dryRun: true,
}) as any, {
org: 'bad org',
})).rejects.toThrow('org must contain only letters');
});

it('requires a vault token for real deployments', async () => {
await expect(adapter.ship(fakeShipContext({
dryRun: false,
Expand Down
31 changes: 31 additions & 0 deletions packages/targets/deploy-vercel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,41 @@ interface Config {
dir?: string;
}

function requireText(value: string | undefined, field: string): string {
const text = value?.trim();
if (!text) throw new Error(`deploy-vercel requires ${field}`);
return text;
}

function optionalText(value: string | undefined, field: string): string | undefined {
return value === undefined ? undefined : requireText(value, field);
}

function optionalSlug(value: string | undefined, field: string): string | undefined {
const slug = optionalText(value, field);
if (slug && !/^[A-Za-z0-9._-]+$/.test(slug)) {
throw new Error(`deploy-vercel ${field} must contain only letters, numbers, dots, underscores, or hyphens`);
}
return slug;
Comment on lines +22 to +27

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Slug regex is more permissive than Vercel actually allows

The pattern /^[A-Za-z0-9._-]+$/ permits uppercase letters, dots, and underscores. Vercel project and org slugs are lowercase alphanumeric with hyphens only (no dots, no underscores, no uppercase). Passing a slug like My_Project.v2 will not be caught here, but the Vercel CLI will reject it at runtime. This won't cause silent data loss but does mean the local validation gives a false "clean" result for names that will ultimately fail.

}

function normalizedConfig(config: Config): Config {
return {
...config,
project: optionalSlug(config.project, 'project'),
org: optionalSlug(config.org, 'org'),
dir: optionalText(config.dir, 'dir'),
};
}

function deployDir(ctx: { projectDir: string }, config: Config): string {
config = normalizedConfig(config);
if (!config.dir) return ctx.projectDir;
return isAbsolute(config.dir) ? config.dir : join(ctx.projectDir, config.dir);
}

function deployArgs(ctx: { channel: string; projectDir: string }, config: Config, token?: string): string[] {
config = normalizedConfig(config);
const prod = config.prod ?? ctx.channel === 'stable';
const args = ['--yes', 'vercel', 'deploy', deployDir(ctx, config), '--yes'];
if (prod) args.push('--prod');
Expand All @@ -24,6 +53,7 @@ function deployArgs(ctx: { channel: string; projectDir: string }, config: Config
}

function renderPlan(ctx: { channel: string; projectDir: string }, config: Config): string {
config = normalizedConfig(config);
const prod = config.prod ?? ctx.channel === 'stable';
return `${JSON.stringify({
provider: 'vercel',
Expand Down Expand Up @@ -51,6 +81,7 @@ export default defineTarget<Config>({
return { artifact: planPath };
},
async ship(ctx, config) {
config = normalizedConfig(config);
const prod = config.prod ?? ctx.channel === 'stable';
ctx.log(`vercel deploy ${prod ? '--prod' : ''} · project=${config.project ?? 'linked'}`);
if (ctx.dryRun) return { id: 'dry-run', meta: { command: ['npx', ...deployArgs(ctx, config)] } };
Expand Down
Loading