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

chore: Add a script to generate a list of all the commands and subcommands #1287

Merged
merged 4 commits into from
Dec 11, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"scripts": {
"build": "ts-node ./scripts/build.ts",
"lint": "eslint . && prettier --list-different ./**/*.{js,json}",
"list-all-commands": "yarn ts-node ./scripts/get-all-commands.ts",
"prettier:write": "prettier --write ./**/*.{ts,js,json}",
"test": "jest",
"test-cli": "yarn --cwd 'acceptance-tests' test-ci",
Expand Down
58 changes: 58 additions & 0 deletions scripts/get-all-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { exec } from 'child_process';
import SpinniesManager from '../lib/ui/SpinniesManager';
import util from 'util';

const output = {};

const execAsync = util.promisify(exec);

function generateCommand(parent: string, command: string) {
const parentCommand = parent !== '' ? `${parent} ` : '';
return `hs ${parentCommand}${command} --get-yargs-completions`;
}

async function extractCommands(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This recurses over the commands running --get-yargs-completions and generates a JSON object to keep the hierarchy. We can use this in the future to scrape help documents etc to auto generate docs from the help text in the CLI if we so choose.

toParse: string,
parent: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
commands: any
) {
for (const line of toParse.split('\n')) {
const [arg] = line.split(':');

if (
!commands[arg] &&
// filter out flags, empty strings from splitting, and the completion command
// because they lead to infinite loops because they display the root level commands
// all over again
!arg.startsWith('-') &&
arg !== '' &&
arg !== 'completion'
) {
commands[arg] = {};

const { stdout } = await execAsync(generateCommand(parent, arg));
await extractCommands(
stdout,
// Concatenate the parent command with the current command to recurse the subcommands
`${parent !== '' ? `${parent} ` : ''}${arg}`,
commands[arg]
);
}
}
return commands;
}

(async function() {
SpinniesManager.init();
SpinniesManager.add('extractingCommands', { text: 'Extracting commands' });

const { stdout } = await execAsync(generateCommand('', ''));
const result = await extractCommands(stdout, '', output);

SpinniesManager.succeed('extractingCommands', {
text: 'Done extracting commands',
});

console.log(result);
})();
Loading