Skip to content

Commit

Permalink
Merge pull request #23 from karmaniverous:feature/GH-17-add-handlebar…
Browse files Browse the repository at this point in the history
…s-partials-to-config

[GH-17] added partial template support
  • Loading branch information
karmaniverous authored Aug 14, 2024
2 parents e2deec8 + 67490bf commit f5c5b63
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const configSchema = z
)
.nullable()
.optional(),
partials: z.record(z.string()).nullable().optional(),
sso: z
.object({
groups: z
Expand Down Expand Up @@ -200,6 +201,7 @@ export const configSchema = z
cli_defaults: cliParamsSchema.nullable().optional(),
cli_defaults_path: z.string().nullable().optional(),
generators: z.record(z.string()).nullable().optional(),
partials: z.record(z.string()).nullable().optional(),
path: z.string(),
shared_config_path: z.string(),
})
Expand Down
45 changes: 43 additions & 2 deletions src/generateWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,47 @@ export const generateWorkspace = async ({
if (
config.workspaces[workspace].generators &&
_.size(config.workspaces[workspace].generators)
)
) {
// Load partials.
const partialsConfig = {
...(config.partials ?? {}),
...(config.workspaces[workspace].partials ?? {}),
};

const [partialKeys, partialPaths] = _.zip(..._.entries(partialsConfig));

if (_.size(partialKeys)) {
if (stdOut)
process.stdout.write(chalk.black.dim(`\nRegistering partials...`));

try {
const partials = await Promise.all(
partialPaths.map((partialPath) =>
fs.readFile(resolve(pkgDir, partialPath!), 'utf8'),
),
);

Handlebars.registerPartial(
_.zipObject(
partialKeys as string[],
partials.map((partial) =>
Handlebars.compile(partial, { noEscape: true }),
),
),
);

if (stdOut) process.stdout.write(chalk.green(' Done!\n'));
} catch (error) {
if (stdOut) {
process.stdout.write(chalk.red(' Processing error!\n\n'));
console.log(chalk.red(getErrorMessage(error)), '\n');
}

throw error;
}
}

// Generate artifacts.
for (const [targetPath, templatePath] of _.entries(
config.workspaces[workspace].generators,
)) {
Expand All @@ -61,7 +101,8 @@ export const generateWorkspace = async ({
throw error;
}
}
else if (stdOut) process.stdout.write(chalk.black.dim(' No templates.\n\n'));
} else if (stdOut)
process.stdout.write(chalk.black.dim(' No templates.\n\n'));

if (stdOut) process.stdout.write(chalk.green.bold('\nDone!\n\n'));
};

0 comments on commit f5c5b63

Please sign in to comment.