Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"read-yaml-file": "^2.1.0",
"socket.io": "^4.8.0",
"svgo": "catalog:",
"tab": "https://pkg.pr.new/bombshell-dev/tab@4595b27",
"typescript": "catalog:",
"validate-npm-package-name": "^5.0.0",
"vite": "catalog:",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/cli/cli-executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const cliExecId = new Date().toISOString();
import * as _ from 'lodash-es';
import Debug from 'debug';
import { DmnoCommand } from './lib/dmno-command';
// import {Completion, script} from "tab";

import { addDocsCommand } from './lib/cli-schema-generation';
import { customizeHelp } from './lib/help-customizations';
Expand All @@ -26,20 +27,24 @@ import { PluginCommand } from './commands/plugin.command';
import { InitCommand } from './commands/init.command';
import { ClearCacheCommand } from './commands/clear-cache.command';
import { PrintEnvCommand } from './commands/printenv.command';
import { CompleteCommand } from './commands/complete.command';

const debug = Debug('dmno:cli');

const program = new DmnoCommand('dmno')
.description('dmnno cli - https://dmno.dev')
.version('0.0.1');

program.enablePositionalOptions();

program.addCommand(ResolveCommand);
program.addCommand(RunCommand);
program.addCommand(DevCommand);
program.addCommand(InitCommand);
program.addCommand(ClearCacheCommand);
program.addCommand(PluginCommand);
program.addCommand(PrintEnvCommand);
program.addCommand(CompleteCommand);

// have to pass through the root program for this one so we can access all the subcommands
addDocsCommand(program);
Expand Down
72 changes: 72 additions & 0 deletions packages/core/src/cli/commands/complete.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { DmnoCommand } from '../lib/dmno-command';
import { Completion } from 'tab';

//TODO: this must be set to hidden.
const completeCmd = new DmnoCommand('complete')
.summary('Autocompletion')
.description('Internal command for shell autocompletion (do not use directly).')
.argument('<tokens...>', 'Tokens for autocompletion')
.example('dmno complete resolve --f', 'Provide suggestions for the "resolve" command when typing "--f"')
.passThroughOptions();

completeCmd.action(async (tokens: string[]): Promise<void> => {

const completion = new Completion();

// we get the root command to access all the registered commands.
const parent = completeCmd.parent;
if (parent && parent.commands) {
// in this step, we add each command
for (const cmd of parent.commands) {
const cmdName = cmd.name();
const cmdDescription = cmd.description() || '';

completion.addCommand(
cmdName,
'',
[], // here we can define positional args to return.
async () => {
return [];
}
);
}

// here, for each command we add all its options.
for (const cmd of parent.commands) {
const cmdName = cmd.name();
if (Array.isArray(cmd.options)) {
for (const option of cmd.options) {
// console.log('HERE', cmdName, option.long);
completion.addOption(
cmdName,
option.long,
"",
async (previousArgs, toComplete, endsWithSpace) => {
// here we can define suggestions to return.
if (option.long === '--format') {
return [
{ value: 'json', description: 'Output as JSON' },
{ value: 'env', description: 'Output as ENV file' }
];
}
if (option.long === '--watch') {
return [
{ value: 'true', description: 'Enable watch mode' },
{ value: 'false', description: 'Disable watch mode' }
];
}
return []
}
);
}
}
}
}

const tokenStrings = tokens.map(token => token.toString());
await completion.parse(tokenStrings);
process.exit(0);
});

export default completeCmd;
export { completeCmd as CompleteCommand };
15 changes: 13 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.