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
23 changes: 23 additions & 0 deletions packages/cli/src/commands/ship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ export default defineConfig({
expect(config).toContain('"deploy-vercel": { use: "deploy-vercel", config: {} },');
});

it('adds a target after entries whose config strings contain braces', async () => {
const dir = await mkdtemp(join(tmpdir(), 'sh1pt-target-add-braces-'));
const file = join(dir, 'sh1pt.config.ts');
await writeFile(file, `export default {
name: 'demo',
version: '0.0.0',
targets: {
"deploy-netlify": {
use: "deploy-netlify",
config: { successMessage: "deployed }" },
},
},
};
`);

addTargetToConfig(file, 'deploy-vercel');

const config = await readFile(file, 'utf8');
const manifest = await loadManifest(file);
expect(config).toContain('"deploy-vercel": { use: "deploy-vercel", config: {} },');
expect(manifest.targets).toHaveProperty('deploy-vercel');
});

it('removes target entries that were added by the CLI', async () => {
const dir = await mkdtemp(join(tmpdir(), 'sh1pt-target-remove-'));
const file = join(dir, 'sh1pt.config.ts');
Expand Down
65 changes: 62 additions & 3 deletions packages/cli/src/commands/ship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,13 @@ function targetEntriesFromSource(source: string, configPath: string): Record<str
}

function findTargetsObject(source: string): { open: number; close: number } | undefined {
const targets = /targets\s*:\s*{/.exec(source);
const structural = maskStringsAndComments(source);
const targets = /targets\s*:\s*{/.exec(structural);
if (!targets) return undefined;
const open = targets.index + targets[0].lastIndexOf('{');
let depth = 0;
for (let i = open; i < source.length; i++) {
const char = source[i];
for (let i = open; i < structural.length; i++) {
const char = structural[i];
if (char === '{') depth++;
if (char === '}') {
depth--;
Expand All @@ -231,6 +232,64 @@ function findTargetsObject(source: string): { open: number; close: number } | un
return undefined;
}

function maskStringsAndComments(source: string): string {
const masked = source.split('');
let quote: '"' | "'" | '`' | undefined;

for (let i = 0; i < source.length; i++) {
const char = source[i]!;
const next = source[i + 1];

if (quote) {
if (char !== '\n' && char !== '\r') masked[i] = ' ';
if (char === quote && !isEscapedAt(source, i)) quote = undefined;
continue;
}

if (char === '"' || char === "'" || char === '`') {
quote = char;
masked[i] = ' ';
continue;
}

if (char === '/' && next === '/') {
masked[i] = ' ';
masked[i + 1] = ' ';
i += 2;
while (i < source.length && source[i] !== '\n' && source[i] !== '\r') {
masked[i] = ' ';
i++;
}
i--;
continue;
}

if (char === '/' && next === '*') {
masked[i] = ' ';
masked[i + 1] = ' ';
i += 2;
while (i < source.length) {
if (source[i] === '*' && source[i + 1] === '/') {
masked[i] = ' ';
masked[i + 1] = ' ';
i++;
break;
}
if (source[i] !== '\n' && source[i] !== '\r') masked[i] = ' ';
i++;
}
}
}

return masked.join('');
}

function isEscapedAt(source: string, index: number): boolean {
let slashCount = 0;
for (let i = index - 1; i >= 0 && source[i] === '\\'; i--) slashCount++;
return slashCount % 2 === 1;
}

function targetPropertyPattern(id: string): RegExp {
const escaped = id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return new RegExp(`^\\s*['"]?${escaped}['"]?\\s*:\\s*\\{\\s*use:\\s*['"][^'"]+['"]\\s*,\\s*config:\\s*\\{\\s*\\}\\s*\\},?\\r?\\n`, 'm');
Expand Down
Loading