Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
88 changes: 58 additions & 30 deletions packages/paykit/src/cli/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import { templates } from "../templates/index";
import {
defaultConfigPath,
detectBetterAuth,
detectFramework,
detectNextJsRouterPath,
detectPackageManager,
Expand Down Expand Up @@ -93,9 +94,40 @@
})`;
}

function buildIdentifyBlock(includeIdentify: boolean, useBetterAuthIdentify: string | null) {
const identifyBlock = !includeIdentify
? ""
: useBetterAuthIdentify
? `
identify: async (request) => {
const session = await auth.api.getSession({ headers: request.headers });
if (!session) return null;
return {
customerId: session.user.id,
email: session.user.email,
name: session.user.name,
};
},`
: `
identify: async (request) => {
// Replace with your auth logic, for example:
// const session = await auth.api.getSession({ headers: request.headers });
// if (!session) return null;
// return {
// customerId: session.user.id,
// email: session.user.email,
// name: session.user.name,
// };
return null;
},`;

return identifyBlock;
}

function generateConfigFile(
templateId: string,
includeIdentify: boolean,
useBetterAuthIdentify: string | null,
provider: InitProvider,
): string {
const productImports =
Expand All @@ -112,23 +144,11 @@
? `\nimport { ${productImports.join(", ")} } from "./paykit-products";`
: "";

const identifyBlock = includeIdentify
? `
identify: async (request) => {
// Replace with your auth logic, for example:
// const session = await auth.api.getSession({ headers: request.headers });
// if (!session) return null;
// return {
// customerId: session.user.id,
// email: session.user.email,
// name: session.user.name,
// };
return null;
},`
: "";
const identifyBlock = buildIdentifyBlock(includeIdentify, useBetterAuthIdentify);

return `${providerImport(provider)}
import { createPayKit } from "paykitjs";${importLine}
${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify};"` : ""}
Comment thread
anirudhprmar marked this conversation as resolved.
Outdated

export const paykit = createPayKit({
database: process.env.DATABASE_URL!,
Expand Down Expand Up @@ -172,6 +192,7 @@
function generateConfigFileFromProductsModule(
productNames: string[],
includeIdentify: boolean,
useBetterAuthIdentify: string | null,
provider: InitProvider,
productsImportPath = "./paykit-products",
): string {
Expand All @@ -183,23 +204,11 @@
? `\nimport { ${uniqueProductNames.join(", ")} } from "${productsImportPath}";`
: "";

const identifyBlock = includeIdentify
? `
identify: async (request) => {
// Replace with your auth logic, for example:
// const session = await auth.api.getSession({ headers: request.headers });
// if (!session) return null;
// return {
// customerId: session.user.id,
// email: session.user.email,
// name: session.user.name,
// };
return null;
},`
: "";
const identifyBlock = buildIdentifyBlock(includeIdentify, useBetterAuthIdentify);

return `${providerImport(provider)}
import { createPayKit } from "paykitjs";${importLine}
${includeIdentify && useBetterAuthIdentify ? `import { auth } from "${useBetterAuthIdentify};"` : ""}
Comment thread
anirudhprmar marked this conversation as resolved.
Outdated

export const paykit = createPayKit({
database: process.env.DATABASE_URL!,
Expand Down Expand Up @@ -335,6 +344,11 @@
const existingConfig = findExistingFile(cwd, POSSIBLE_CONFIG_PATHS);
const existingClient = findExistingFile(cwd, POSSIBLE_CLIENT_PATHS);
const existingProvider = detectExistingProvider(cwd, existingConfig);
const usesBetterAuth = detectBetterAuth(cwd);
const existingBetterAuthConfig = findExistingFile(cwd, [
"src/server/better-auth/config.ts",
"src/lib/auth.ts",
]);

let provider: string | symbol = "stripe";
if (existingProvider) {
Expand Down Expand Up @@ -549,18 +563,32 @@

const files: FileToWrite[] = [];

usesBetterAuth && existingBetterAuthConfig
? p.log.step(`Detected Better Auth: ${picocolors.bold(existingBetterAuthConfig)}`)
: null;

Check warning on line 568 in packages/paykit/src/cli/commands/init.ts

View workflow job for this annotation

GitHub Actions / lint

eslint(no-unused-expressions)

Expected expression to be used
Comment thread
anirudhprmar marked this conversation as resolved.
Outdated

// Config
if (!existingConfig || hasLegacyProductsModule) {
files.push({
path: configPath,
content: existingProductsModule
? generateConfigFileFromProductsModule(
existingProductsModule,
clientPath !== null,
clientPath !== null || usesBetterAuth,
usesBetterAuth && existingBetterAuthConfig
? resolveImportPath(configPath, existingBetterAuthConfig, cwd, framework)
: null,
selectedProvider,
existingProductsImportPath,
)
: generateConfigFile(templateId as string, clientPath !== null, selectedProvider),
: generateConfigFile(
templateId as string,
clientPath !== null || usesBetterAuth,
usesBetterAuth && existingBetterAuthConfig
? resolveImportPath(configPath, existingBetterAuthConfig, cwd, framework)
: null,
selectedProvider,
),
});
}

Expand Down
12 changes: 12 additions & 0 deletions packages/paykit/src/cli/utils/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ export function detectFramework(cwd: string): Framework | null {
return null;
}

export function detectBetterAuth(cwd: string) {
const pkg = readPackageJson(cwd);

if (pkg) {
if (hasDependency(pkg, "better-auth")) {
return true;
}
}

return false;
}

export function detectNextJsRouterPath(cwd: string): string {
const rootFiles = safeReaddir(cwd);
const hasAppDir = rootFiles.includes("app");
Expand Down
Loading