feat: implement filepath & folder templates

Signed-off-by: Chapman Pendery <cpendery@vt.edu>
This commit is contained in:
Chapman Pendery
2023-10-08 00:58:33 -07:00
parent b11c479f22
commit 84acea01da
3 changed files with 37 additions and 24 deletions
+1 -1
View File
@@ -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) {}
+8 -4
View File
@@ -137,8 +137,12 @@ const generatorSuggestions = async (
return filter<Fig.Suggestion>(suggestions, filterStrategy, partialCmd, undefined);
};
const templateSuggestions = (templates: Fig.Template | undefined, filterStrategy: FilterStrategy | undefined, partialCmd: string | undefined): Suggestion[] => {
return filter<Fig.Suggestion>(runTemplates(templates ?? []), filterStrategy, partialCmd, undefined);
const templateSuggestions = async (
templates: Fig.Template | undefined,
filterStrategy: FilterStrategy | undefined,
partialCmd: string | undefined
): Promise<Suggestion[]> => {
return filter<Fig.Suggestion>(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)) {
+28 -19
View File
@@ -1,33 +1,42 @@
const filepathsTemplate = (): Fig.Suggestion[] => {
return [];
import fsAsync from "fs/promises";
import process from "node:process";
const filepathsTemplate = async (): Promise<Fig.Suggestion[]> => {
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<Fig.Suggestion[]> => {
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<Fig.Suggestion[]> => {
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();
};