Skip to content

Commit 09735a9

Browse files
committed
feat: autocomplete module
1 parent 40c6d4b commit 09735a9

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

Diff for: packages/create-app/src/files.ts

+32
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,35 @@ void proposeCompletions(app, inputs, buildContext(process)).then((completions) =
320320
// ignore
321321
});
322322
`;
323+
324+
export const binFishCompleteModuleText = `#!/usr/bin/env node
325+
import { proposeCompletions } from "@stricli/core";
326+
import { buildContext } from "../context";
327+
import { app } from "../app";
328+
329+
const inputs = process.argv.slice(2);
330+
331+
try {
332+
const completions = await proposeCompletions(app, inputs, buildContext(process));
333+
for (const { completion } of completions) {
334+
process.stdout.write(\`\${completion}\n\`);
335+
}
336+
} catch {
337+
// ignore errors
338+
}
339+
`;
340+
341+
export const binFishCompleteScriptText = `#!/usr/bin/env node
342+
import { proposeCompletions } from "@stricli/core";
343+
import { buildContext } from "../context";
344+
import { app } from "../app";
345+
346+
const inputs = process.argv.slice(2);
347+
void proposeCompletions(app, inputs, buildContext(process)).then((completions) => {
348+
for (const { completion } of completions) {
349+
process.stdout.write(\`\${completion}\n\`);
350+
}
351+
}, () => {
352+
// ignore errors
353+
});
354+
`;

Diff for: packages/create-app/src/impl.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type { LocalContext } from "./context";
66
import {
77
binBashCompleteModuleText,
88
binBashCompleteScriptText,
9+
binFishCompleteModuleText,
10+
binFishCompleteScriptText,
911
binCliModuleText,
1012
binCliScriptText,
1113
buildMultiCommandAppWithAutoCompleteText,
@@ -40,6 +42,10 @@ function calculateBashCompletionCommand(command: string): string {
4042
return `__${command}_bash_complete`;
4143
}
4244

45+
function calculateFishCompletionCommand(command: string): string {
46+
return `__${command}_fish_complete`;
47+
}
48+
4349
type PackageJsonTemplateValues = Pick<PackageJson.PackageJsonStandard, "name"> &
4450
Required<Pick<PackageJson.PackageJsonStandard, "author" | "description" | "license" | "type">>;
4551

@@ -82,7 +88,7 @@ function buildPackageJson(
8288
};
8389
}
8490

85-
function addAutoCompleteBin(packageJson: LocalPackageJson, bashCompleteCommandName: string): LocalPackageJson {
91+
function addAutoCompleteBin(packageJson: LocalPackageJson, bashCompleteCommandName: string, fishCompleteCommandName: string): LocalPackageJson {
8692
return {
8793
...packageJson,
8894
dependencies: {
@@ -92,11 +98,12 @@ function addAutoCompleteBin(packageJson: LocalPackageJson, bashCompleteCommandNa
9298
bin: {
9399
...(packageJson.bin as Record<string, string>),
94100
[bashCompleteCommandName]: "dist/bash-complete.js",
101+
[fishCompleteCommandName]: "dist/fish-complete.js",
95102
},
96103
tsup: {
97104
...packageJson.tsup,
98105
/* c8 ignore next */
99-
entry: [...(packageJson.tsup?.entry ?? []), "src/bin/bash-complete.ts"],
106+
entry: [...(packageJson.tsup?.entry ?? []), "src/bin/bash-complete.ts", "src/bin/fish-complete.ts"],
100107
},
101108
};
102109
}
@@ -158,15 +165,16 @@ export default async function (this: LocalContext, flags: CreateProjectFlags, di
158165
);
159166

160167
const bashCommandName = calculateBashCompletionCommand(commandName);
168+
const fishCommandName = calculateFishCompletionCommand(commandName);
161169

162170
if (flags.autoComplete) {
163-
packageJson = addAutoCompleteBin(packageJson, bashCommandName);
171+
packageJson = addAutoCompleteBin(packageJson, bashCommandName, fishCommandName);
164172
if (flags.template === "multi") {
165173
packageJson = addPostinstallScript(packageJson, `${commandName} install`);
166174
} else {
167175
packageJson = addPostinstallScript(
168176
packageJson,
169-
`npx @stricli/auto-complete@latest install ${commandName} --bash ${bashCommandName}`,
177+
`npx @stricli/auto-complete@latest install ${commandName} --bash ${bashCommandName} --fish ${fishCommandName}`,
170178
);
171179
}
172180
await writeFile("src/context.ts", localContextWithAutoCompleteText);
@@ -209,5 +217,10 @@ export default async function (this: LocalContext, flags: CreateProjectFlags, di
209217
"src/bin/bash-complete.ts",
210218
flags.type === "module" ? binBashCompleteModuleText : binBashCompleteScriptText,
211219
);
220+
221+
await writeFile(
222+
"src/bin/fish-complete.ts",
223+
flags.type === "module" ? binFishCompleteModuleText : binFishCompleteScriptText,
224+
);
212225
}
213226
}

0 commit comments

Comments
 (0)