-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New CLI help reference for studio and db commands (#10390)
* feat: new, formatted astro db help * feat: add "studio commands" section to cli * chore: changeset * Update packages/db/src/core/cli/print-help.ts Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update .changeset/thirty-bags-live.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> --------- Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
- Loading branch information
1 parent
02aeb01
commit 236cdbb
Showing
4 changed files
with
102 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"astro": patch | ||
"@astrojs/db": patch | ||
--- | ||
|
||
Adds `--help` reference for new db and studio CLI commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { bgWhite, black, dim, bgGreen, green, bold } from 'kleur/colors'; | ||
|
||
/** | ||
* Uses implementation from Astro core | ||
* @see https://github.com/withastro/astro/blob/main/packages/astro/src/core/messages.ts#L303 | ||
*/ | ||
export function printHelp({ | ||
commandName, | ||
headline, | ||
usage, | ||
tables, | ||
description, | ||
}: { | ||
commandName: string; | ||
headline?: string; | ||
usage?: string; | ||
tables?: Record<string, [command: string, help: string][]>; | ||
description?: string; | ||
}) { | ||
const linebreak = () => ''; | ||
const title = (label: string) => ` ${bgWhite(black(` ${label} `))}`; | ||
const table = (rows: [string, string][], { padding }: { padding: number }) => { | ||
const split = process.stdout.columns < 60; | ||
let raw = ''; | ||
|
||
for (const row of rows) { | ||
if (split) { | ||
raw += ` ${row[0]}\n `; | ||
} else { | ||
raw += `${`${row[0]}`.padStart(padding)}`; | ||
} | ||
raw += ' ' + dim(row[1]) + '\n'; | ||
} | ||
|
||
return raw.slice(0, -1); // remove latest \n | ||
}; | ||
|
||
let message = []; | ||
|
||
if (headline) { | ||
message.push( | ||
linebreak(), | ||
` ${bgGreen(black(` ${commandName} `))} ${green( | ||
`v${process.env.PACKAGE_VERSION ?? ''}` | ||
)} ${headline}` | ||
); | ||
} | ||
|
||
if (usage) { | ||
message.push(linebreak(), ` ${green(commandName)} ${bold(usage)}`); | ||
} | ||
|
||
if (tables) { | ||
function calculateTablePadding(rows: [string, string][]) { | ||
return rows.reduce((val, [first]) => Math.max(val, first.length), 0) + 2; | ||
} | ||
const tableEntries = Object.entries(tables); | ||
const padding = Math.max(...tableEntries.map(([, rows]) => calculateTablePadding(rows))); | ||
for (const [tableTitle, tableRows] of tableEntries) { | ||
message.push(linebreak(), title(tableTitle), table(tableRows, { padding })); | ||
} | ||
} | ||
|
||
if (description) { | ||
message.push(linebreak(), `${description}`); | ||
} | ||
|
||
console.log(message.join('\n') + '\n'); | ||
} |