diff --git a/src/runtime/generator.ts b/src/runtime/generator.ts index a4996bf..14119de 100644 --- a/src/runtime/generator.ts +++ b/src/runtime/generator.ts @@ -33,7 +33,7 @@ export const runGenerator = async (generator: Fig.Generator, tokens: string[]): } if (template != null) { - suggestions.push(...runTemplates(template)); + suggestions.push(...(await runTemplates(template))); } return suggestions; } catch (e) {} diff --git a/src/runtime/suggestion.ts b/src/runtime/suggestion.ts index 7b4321e..e0baf50 100644 --- a/src/runtime/suggestion.ts +++ b/src/runtime/suggestion.ts @@ -137,8 +137,12 @@ const generatorSuggestions = async ( return filter(suggestions, filterStrategy, partialCmd, undefined); }; -const templateSuggestions = (templates: Fig.Template | undefined, filterStrategy: FilterStrategy | undefined, partialCmd: string | undefined): Suggestion[] => { - return filter(runTemplates(templates ?? []), filterStrategy, partialCmd, undefined); +const templateSuggestions = async ( + templates: Fig.Template | undefined, + filterStrategy: FilterStrategy | undefined, + partialCmd: string | undefined +): Promise => { + return filter(await runTemplates(templates ?? []), filterStrategy, partialCmd, undefined); }; const suggestionSuggestions = ( @@ -201,7 +205,7 @@ export const getSubcommandDrivenRecommendation = async ( const activeArg = subcommand.args instanceof Array ? subcommand.args[0] : subcommand.args; suggestions.push(...(await generatorSuggestions(activeArg?.generators, acceptedTokens, activeArg?.filterStrategy, partialCmd))); suggestions.push(...suggestionSuggestions(activeArg?.suggestions, activeArg?.filterStrategy, partialCmd)); - suggestions.push(...templateSuggestions(activeArg?.template, activeArg?.filterStrategy, partialCmd)); + suggestions.push(...(await templateSuggestions(activeArg?.template, activeArg?.filterStrategy, partialCmd))); } return { @@ -227,7 +231,7 @@ export const getArgDrivenRecommendation = async ( const suggestions = [ ...(await generatorSuggestions(args[0].generators, acceptedTokens, activeArg?.filterStrategy, partialCmd)), ...suggestionSuggestions(args[0].suggestions, activeArg?.filterStrategy, partialCmd), - ...templateSuggestions(args[0].template, activeArg?.filterStrategy, partialCmd), + ...(await templateSuggestions(args[0].template, activeArg?.filterStrategy, partialCmd)), ]; if ((activeArg.isOptional && !activeArg.isVariadic) || (activeArg.isVariadic && activeArg.isOptional && !variadicArgBound)) { diff --git a/src/runtime/template.ts b/src/runtime/template.ts index f7c5897..35f316e 100644 --- a/src/runtime/template.ts +++ b/src/runtime/template.ts @@ -1,33 +1,42 @@ -const filepathsTemplate = (): Fig.Suggestion[] => { - return []; +import fsAsync from "fs/promises"; +import process from "node:process"; + +const filepathsTemplate = async (): Promise => { + const files = await fsAsync.readdir(process.cwd(), { withFileTypes: true }); + return files.filter((f) => f.isFile() || f.isDirectory()).map((f) => ({ name: f.name, priority: 90 })); }; -const foldersTemplate = (): Fig.Suggestion[] => { - return []; +const foldersTemplate = async (): Promise => { + const files = await fsAsync.readdir(process.cwd(), { withFileTypes: true }); + return files.filter((f) => f.isDirectory()).map((f) => ({ name: f.name, priority: 90 })); }; +// TODO: implement history template const historyTemplate = (): Fig.Suggestion[] => { return []; }; +// TODO: implement help template const helpTemplate = (): Fig.Suggestion[] => { return []; }; -export const runTemplates = (template: Fig.TemplateStrings[] | Fig.Template): Fig.Suggestion[] => { +export const runTemplates = async (template: Fig.TemplateStrings[] | Fig.Template): Promise => { const templates = template instanceof Array ? template : [template]; - return templates - .map((t) => { - switch (t) { - case "filepaths": - return filepathsTemplate(); - case "folders": - return foldersTemplate(); - case "history": - return historyTemplate(); - case "help": - return helpTemplate(); - } - }) - .flat(); + return ( + await Promise.all( + templates.map(async (t) => { + switch (t) { + case "filepaths": + return await filepathsTemplate(); + case "folders": + return await foldersTemplate(); + case "history": + return historyTemplate(); + case "help": + return helpTemplate(); + } + }) + ) + ).flat(); };