Skip to content
Open
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
61 changes: 60 additions & 1 deletion packages/cli/src/commands/config-payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export type PaymentsSummary = {
};

export function parsePaymentsSummary(source: string, path = 'sh1pt.config.ts'): PaymentsSummary | undefined {
const payments = readObjectBody(source, 'payments');
const uncommented = maskComments(source);
const payments = readObjectBody(uncommented, 'payments');
if (!payments) return undefined;
const providers = readObjectBody(payments, 'providers');
const defaultProvider = readStringProperty(payments, 'defaultProvider');
Expand All @@ -31,6 +32,64 @@ export function parsePaymentsSummary(source: string, path = 'sh1pt.config.ts'):
};
}

function maskComments(source: string): string {
let result = '';
let quote: '"' | "'" | '`' | undefined;

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

if (quote) {
result += ch;
if (ch === quote && !isEscaped(source, i)) quote = undefined;
continue;
}

if (ch === '"' || ch === "'" || ch === '`') {
quote = ch;
result += ch;
continue;
}

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

if (ch === '/' && next === '*') {
result += ' ';
i += 2;
while (i < source.length) {
if (source[i] === '*' && source[i + 1] === '/') {
result += ' ';
i += 1;
break;
}
result += source[i] === '\n' || source[i] === '\r' ? source[i] : ' ';
i += 1;
}
continue;
}

result += ch;
}

return result;
}

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

function readObjectBody(source: string, property: string): string | undefined {
const match = new RegExp(`(?:^|[,{\\s])${propertyKeyPattern(property)}\\s*:`).exec(source);
if (!match) return undefined;
Expand Down
35 changes: 35 additions & 0 deletions packages/cli/src/commands/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ describe('parsePaymentsSummary', () => {
expect(parsePaymentsSummary('export default defineConfig({ name: "demo" })')).toBeUndefined();
});

it('ignores payments-shaped examples inside comments', () => {
const source = `
export default defineConfig({
// payments: { providers: { fake: { use: 'payment-fake' } } }
/*
payments: {
defaultProvider: 'payment-commented',
providers: { commented: { use: 'payment-commented' } },
},
*/
name: 'demo',
});
`;

expect(parsePaymentsSummary(source)).toBeUndefined();
});

it('does not let a commented example shadow the real payments block', () => {
const summary = parsePaymentsSummary(`
export default defineConfig({
// payments: { providers: { fake: { use: 'payment-fake' } } }
payments: {
defaultProvider: 'coinpay',
providers: {
coinpay: { use: 'payment-coinpay' },
},
},
});
`);

expect(summary?.providers).toEqual([
{ key: 'coinpay', use: 'payment-coinpay', enabled: true, isDefault: true },
]);
});

it('extracts payments when config object keys are quoted', () => {
const summary = parsePaymentsSummary(`
export default defineConfig({
Expand Down
Loading