Skip to content

Commit

Permalink
fix: update auto-complete template to output completions
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Molisani <[email protected]>
  • Loading branch information
molisani committed Oct 17, 2024
1 parent 36948dd commit 1b48b10
Show file tree
Hide file tree
Showing 26 changed files with 2,103 additions and 1,960 deletions.
241 changes: 127 additions & 114 deletions packages/create-app/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export const localContextText = `\
import type { CommandContext } from "@stricli/core";
export interface LocalContext extends CommandContext {
readonly process: NodeJS.Process;
// ...
readonly process: NodeJS.Process;
// ...
}
export function buildContext(process: NodeJS.Process): LocalContext {
return {
process,
};
return {
process,
};
}
`;

Expand All @@ -53,29 +53,29 @@ import os from "node:os";
import path from "node:path";
export interface LocalContext extends CommandContext, StricliAutoCompleteContext {
readonly process: NodeJS.Process;
// ...
readonly process: NodeJS.Process;
// ...
}
export function buildContext(process: NodeJS.Process): LocalContext {
return {
process,
os,
fs,
path,
};
return {
process,
os,
fs,
path,
};
}
`;

export const singleCommandImplText = `\
import type { LocalContext } from "./context";
interface CommandFlags {
readonly count: number;
readonly count: number;
}
export default async function(this: LocalContext, flags: CommandFlags, name: string): Promise<void> {
this.process.stdout.write(\`Hello \${name}!\\n\`.repeat(flags.count));
this.process.stdout.write(\`Hello \${name}!\\n\`.repeat(flags.count));
}
`;

Expand All @@ -84,130 +84,130 @@ import { buildApplication, buildCommand, numberParser } from "@stricli/core";
import { name, version, description } from "../package.json";
const command = buildCommand({
loader: async () => import("./impl"),
parameters: {
positional: {
kind: "tuple",
parameters: [
{
brief: "Your name",
parse: String,
loader: async () => import("./impl"),
parameters: {
positional: {
kind: "tuple",
parameters: [
{
brief: "Your name",
parse: String,
},
],
},
flags: {
count: {
kind: "parsed",
brief: "Number of times to say hello",
parse: numberParser,
},
},
],
},
flags: {
count: {
kind: "parsed",
brief: "Number of times to say hello",
parse: numberParser,
},
docs: {
brief: description,
},
},
docs: {
brief: description,
},
});
export const app = buildApplication(command, {
name,
versionInfo: {
currentVersion: version,
},
name,
versionInfo: {
currentVersion: version,
},
});
`;

export const multiCommandSubdirImplText = `\
import type { LocalContext } from "../../context";
interface SubdirCommandFlags {
// ...
// ...
}
export default async function(this: LocalContext, flags: SubdirCommandFlags): Promise<void> {
// ...
// ...
}
`;

export const multiCommandSubdirCommandText = `\
import { buildCommand } from "@stricli/core";
export const subdirCommand = buildCommand({
loader: async () => import("./impl"),
parameters: {
positional: {
kind: "tuple",
parameters: [],
loader: async () => import("./impl"),
parameters: {
positional: {
kind: "tuple",
parameters: [],
},
},
docs: {
brief: "Command in subdirectory",
},
},
docs: {
brief: "Command in subdirectory",
},
});
`;

export const multiCommandNestedImplText = `\
import type { LocalContext } from "../../context";
interface FooCommandFlags {
// ...
// ...
}
export async function foo(this: LocalContext, flags: FooCommandFlags): Promise<void> {
// ...
// ...
}
interface BarCommandFlags {
// ...
// ...
}
export async function bar(this: LocalContext, flags: BarCommandFlags): Promise<void> {
// ...
// ...
}
`;

export const multiCommandNestedCommandsText = `\
import { buildCommand, buildRouteMap } from "@stricli/core";
export const fooCommand = buildCommand({
loader: async () => {
const { foo } = await import("./impl");
return foo;
},
parameters: {
positional: {
kind: "tuple",
parameters: [],
loader: async () => {
const { foo } = await import("./impl");
return foo;
},
parameters: {
positional: {
kind: "tuple",
parameters: [],
},
},
docs: {
brief: "Nested foo command",
},
},
docs: {
brief: "Nested foo command",
},
});
export const barCommand = buildCommand({
loader: async () => {
const { bar } = await import("./impl");
return bar;
},
parameters: {
positional: {
kind: "tuple",
parameters: [],
loader: async () => {
const { bar } = await import("./impl");
return bar;
},
parameters: {
positional: {
kind: "tuple",
parameters: [],
},
},
docs: {
brief: "Nested bar command",
},
},
docs: {
brief: "Nested bar command",
},
});
export const nestedRoutes = buildRouteMap({
routes: {
foo: fooCommand,
bar: barCommand,
},
docs: {
brief: "Nested commands",
},
routes: {
foo: fooCommand,
bar: barCommand,
},
docs: {
brief: "Nested commands",
},
});
`;

Expand All @@ -218,20 +218,20 @@ import { subdirCommand } from "./commands/subdir/command";
import { nestedRoutes } from "./commands/nested/commands";
const routes = buildRouteMap({
routes: {
subdir: subdirCommand,
nested: nestedRoutes,
},
docs: {
brief: description,
},
routes: {
subdir: subdirCommand,
nested: nestedRoutes,
},
docs: {
brief: description,
},
});
export const app = buildApplication(routes, {
name,
versionInfo: {
currentVersion: version,
},
name,
versionInfo: {
currentVersion: version,
},
});
`;

Expand All @@ -244,26 +244,26 @@ import { subdirCommand } from "./commands/subdir/command";
import { nestedRoutes } from "./commands/nested/commands";
const routes = buildRouteMap({
routes: {
subdir: subdirCommand,
nested: nestedRoutes,
install: buildInstallCommand("${command}", { bash: "${autcCommand}" }),
uninstall: buildUninstallCommand("${command}", { bash: true }),
},
docs: {
brief: description,
hideRoute: {
install: true,
uninstall: true,
routes: {
subdir: subdirCommand,
nested: nestedRoutes,
install: buildInstallCommand("${command}", { bash: "${autcCommand}" }),
uninstall: buildUninstallCommand("${command}", { bash: true }),
},
docs: {
brief: description,
hideRoute: {
install: true,
uninstall: true,
},
},
},
});
export const app = buildApplication(routes, {
name,
versionInfo: {
currentVersion: version,
},
name,
versionInfo: {
currentVersion: version,
},
});
`;
}
Expand Down Expand Up @@ -291,9 +291,16 @@ import { buildContext } from "../context";
import { app } from "../app";
const inputs = process.argv.slice(3);
if (process.env["COMP_LINE"]?.endsWith(" ")) {
inputs.push("");
inputs.push("");
}
await proposeCompletions(app, inputs, buildContext(process));
try {
for (const { completion } of await proposeCompletions(app, inputs, buildContext(process))) {
process.stdout.write(\`\${completion}\\n\`);
}
} catch {
// ignore
}
`;

export const binBashCompleteScriptText = `\
Expand All @@ -303,7 +310,13 @@ import { buildContext } from "../context";
import { app } from "../app";
const inputs = process.argv.slice(3);
if (process.env["COMP_LINE"]?.endsWith(" ")) {
inputs.push("");
inputs.push("");
}
void proposeCompletions(app, inputs, buildContext(process));
void proposeCompletions(app, inputs, buildContext(process)).then((completions) => {
for (const { completion } of completions) {
process.stdout.write(\`\${completion}\\n\`);
}
}, () => {
// ignore
});
`;
4 changes: 2 additions & 2 deletions packages/create-app/src/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default async function (this: LocalContext, flags: CreateProjectFlags, di
await writeFile("src/context.ts", localContextText);
}

await writeFile("package.json", JSON.stringify(packageJson, void 0, 2));
await writeFile("package.json", JSON.stringify(packageJson, void 0, 4));

await writeFile(".gitignore", gitignoreText);

Expand All @@ -176,7 +176,7 @@ export default async function (this: LocalContext, flags: CreateProjectFlags, di
include: srcTsconfig.include,
exclude: srcTsconfig.exclude,
};
await writeFile("src/tsconfig.json", JSON.stringify(sourceTsconfigJson, void 0, 2));
await writeFile("src/tsconfig.json", JSON.stringify(sourceTsconfigJson, void 0, 4));

if (flags.template === "single") {
await writeFile("src/impl.ts", singleCommandImplText);
Expand Down
Loading

0 comments on commit 1b48b10

Please sign in to comment.