Skip to content
Merged
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
29 changes: 19 additions & 10 deletions packages/novu/src/commands/init/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export const installTemplate = async ({
copySource.push(mode === 'ts' ? 'tailwind.config.ts' : '!tailwind.config.js', '!postcss.config.cjs');
}

const renameAgent = template === TemplateTypeEnum.APP_AGENT && agentIdentifier;
if (renameAgent && !/^[a-z0-9]+(?:[-_][a-z0-9]+)*$/.test(agentIdentifier)) {
throw new Error(
`Invalid agent identifier: "${agentIdentifier}". Must be a lowercase slug (a-z, 0-9, hyphens, underscores).`
);
}

await copy(copySource, root, {
parents: true,
cwd: templatePath,
Expand All @@ -82,23 +89,25 @@ export const installTemplate = async ({
case 'README-template.md': {
return 'README.md';
}
case 'support-agent.tsx': {
return renameAgent ? `${agentIdentifier}.tsx` : name;
}
default: {
return name;
}
}
},
});

if (template === TemplateTypeEnum.APP_AGENT && agentIdentifier) {
if (!/^[a-z0-9]+(?:[-_][a-z0-9]+)*$/.test(agentIdentifier)) {
throw new Error(
`Invalid agent identifier: "${agentIdentifier}". Must be a lowercase slug (a-z, 0-9, hyphens, underscores).`
);
}
const agentFile = path.join(root, 'app', 'novu', 'agents', 'support-agent.tsx');
await fs.writeFile(
agentFile,
(await fs.readFile(agentFile, 'utf8')).replace("agent('support-agent',", `agent('${agentIdentifier}',`)
if (renameAgent) {
const camelName = agentIdentifier.replace(/[-_]([a-z0-9])/g, (_, c) => c.toUpperCase());
const files = await glob('**/*.{tsx,ts,md}', { cwd: root, absolute: true, followSymbolicLinks: false });
await Promise.all(
files.map(async (file) => {
const before = await fs.readFile(file, 'utf8');
const after = before.replace(/supportAgent/g, camelName).replace(/support-agent/g, agentIdentifier);
if (after !== before) await fs.writeFile(file, after);
})
);
}

Expand Down
Loading