Skip to content

Commit

Permalink
fix(type-safe-api): fix template discovery on node 18.17.x
Browse files Browse the repository at this point in the history
Node 18.17.x has a bug where `readdirSync` returns invalid entries when
both `recursive` and `withFileTypes` is specified:

nodejs/node#48858

We use our own implementation to recursively find template files to
ensure we work for older versions of node.
  • Loading branch information
cogwirrel committed Oct 31, 2024
1 parent a590627 commit bf9b347
Show file tree
Hide file tree
Showing 7 changed files with 457 additions and 451 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,14 @@ const resolveTemplateDir = (rootScriptDir: string, templateDir: string) => {
throw new Error(`Template directory ${templateDir} does not exist!`);
};

export const listFilesInDirRecursive = (dir: string): string[] => {
if ((fs.lstatSync(dir)).isDirectory()) {
return fs.readdirSync(dir).map((f) => listFilesInDirRecursive(path.join(dir, f))).flatMap(x => x);
} else {
return [dir];
}
};

export default async (argv: string[], rootScriptDir: string) => {
const args = parse<Arguments>({
specPath: { type: String },
Expand All @@ -1051,10 +1059,8 @@ export default async (argv: string[], rootScriptDir: string) => {
}

// Read all .ejs files in each template directory
const templates = args.templateDirs.flatMap(t => fs.readdirSync(resolveTemplateDir(rootScriptDir, t), {
recursive: true,
withFileTypes: true
}).filter(f => f.isFile() && f.name.endsWith('.ejs') && !f.name.endsWith('.partial.ejs')).map(f => path.join(f.parentPath ?? f.path, f.name)));
const templates = args.templateDirs.flatMap(t => listFilesInDirRecursive(resolveTemplateDir(rootScriptDir, t))
.filter(f => f.endsWith('.ejs') && !f.endsWith('.partial.ejs')));

// Render the templates with the data from the spec
const renderedFiles = await Promise.all(templates.map(async (template) => {
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bf9b347

Please sign in to comment.