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 1 commit
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 @@ -56,6 +56,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 ./**/*.{js,json}",
"test": "jest",
"test-cli": "yarn --cwd 'acceptance-tests' test-ci",
Expand Down
53 changes: 53 additions & 0 deletions scripts/get-all-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { exec } from 'child_process';
import SpinniesManager from '../lib/ui/SpinniesManager';
import util from 'util';

const output = {};

const execAsync = util.promisify(exec);

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: Record<string, any>
) {
for (const line of toParse.split('\n')) {
const arg = line.split(':')[0];

if (
!commands[arg] &&
!arg.startsWith('-') &&
arg !== '' &&
arg !== 'completion'
) {
commands[arg] = {};

const commandToRun = `hs ${
parent !== '' ? `${parent} ` : ''
}${arg} --get-yargs-completions`;

const { stdout } = await execAsync(commandToRun);
await extractCommands(
stdout,
`${parent !== '' ? `${parent} ` : ''}${arg}`,
commands[arg]
);
}
}
return commands;
}

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

const { stdout } = await execAsync('hs --get-yargs-completions');
const result = await extractCommands(stdout, '', output);

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

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