Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update auto-complete template to output completions #19

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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