Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cb9085d
feat(cli): add the guided init, the menu and unattended mode
Decipher Nov 18, 2026
c73701a
chore: merge the release tooling from the branch below
Decipher Nov 18, 2026
838cd00
chore: merge the pipeline triggers, the prose check and the slash tri…
Decipher Nov 18, 2026
19f2edd
chore: merge the fixes to init and the checks from the branch below
Decipher Nov 18, 2026
491a5d6
chore: merge the fixes to the release check from the branch below
Decipher Nov 18, 2026
2defc08
chore: merge the fixes to init's next steps and module info from the …
Decipher Nov 18, 2026
792cd63
fix(cli): name what kept init from asking for a directory
Decipher Nov 18, 2026
be36d20
chore: merge the test suite's git environment fix from the branch below
Decipher Nov 18, 2026
0d7afd5
chore: merge the registry name checks and the fix to a forced init fr…
Decipher Nov 18, 2026
cdeae86
fix(cli): take an option given before the command of a group
Decipher Nov 18, 2026
501cfdf
fix(cli): take an option and its value given before any command
Decipher Nov 18, 2026
fe44e8e
chore: merge the attribution check's fixes from the branch below
Decipher Nov 19, 2026
5412a13
chore: merge the cd line and config path fixes from the branch below
Decipher Nov 19, 2026
e15a4ca
chore: merge the branch below after its rebase onto the release line
Decipher Nov 19, 2026
4029746
fix(cli): keep a starter's install output out of init, and check that…
Decipher Nov 19, 2026
f0b16f1
fix(cli): name an engine's project after the site, where the starter …
Decipher Nov 19, 2026
ea4b4dd
fix(cli): say what a starter needs from a backend that exists already
Decipher Nov 19, 2026
1669c99
fix(cli): make a site's project name in one pass over its directory's…
Decipher Nov 19, 2026
ccfe51f
chore: merge the Node version check from the branch below
Decipher Nov 19, 2026
662352f
chore: merge running on an older Node from the branch below
Decipher Nov 19, 2026
75da8e3
chore: merge the test fix for the Node search from the branch below
Decipher Nov 19, 2026
485d3e0
fix(cli): name the menu's detail choices by what they open
Decipher Nov 19, 2026
54cb81f
chore: merge the launcher review fixes from the branch below
Decipher Nov 19, 2026
c030766
chore: merge the release lookup and attribution fixes from the branch…
Decipher Nov 19, 2026
d2e8359
chore: merge the release guide note from the branch below
Decipher Nov 19, 2026
4a062f6
chore: merge the icon and banner from the branch below
Decipher Nov 19, 2026
c23e115
chore: merge 0.x from the branch below
Decipher Nov 19, 2026
aae4f61
chore: merge the registry revision from the branch below
Decipher Nov 19, 2026
01a96fc
chore(cli): raise the revision of the starter registry
Decipher Nov 19, 2026
e523066
chore: merge the registry test helper from the branch below
Decipher Nov 19, 2026
7bd916e
fix(cli): show the note about the PHP backend with the PHP engine alone
Decipher Nov 19, 2026
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 .changeset/first-release-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ First release of the Druxt command-line tool, with the binary `drx`.
- `drx init` creates a site from a starter in a registry, records its layout and parts in `drx.config.json` and prints what the site needs next. `--engine` chooses how the backend runs, from the engines the starter lists or `none` for a backend that exists already. `init` keeps that engine's files only and warns when its commands are not on `PATH`.
- `drx module list`, `drx module search` and `drx module info` read the Druxt module registry, and in a project show what is installed and whether each module's range admits the Druxt there.
- `drx info` prints the versions of the CLI, Node and the packages of each part of a project.
- At a terminal, bare `drx` opens a menu and `drx init` is a guided run that asks before it writes. `--no-interactive`, `DRX_NO_INTERACTIVE=1`, `CI` or no terminal turn every prompt off.
- The registry holds one starter, `quickstart`, and the `monorepo` layout is the one available.
31 changes: 16 additions & 15 deletions .cspell-project-words.txt
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
# Add a term here rather than switching the check off for a file.
# Words this project uses that the standard dictionaries do not carry.
actionlint
alacritty
autobuild
automerge
citty
clack
COLORTERM
commitlint
consola
corepack
cspell
cweagans
dbaeumer
DDEV
docroot
drush
druxt
druxtjs
druxtsite
esbenp
giget
gitleaks
gpgsign
interruptible
knip
Ksection
Lando
linkify
lndo
magicast
markdownlint
mise
Nuxt
nvmrc
opencode
unjs
unstub
vitest
yamllint
drush
PATHEXT
alacritty
COLORTERM
truecolor
wordmark
magicast
cweagans
docroot
relock
repatch
autobuild
linkify
SARIF
gpgsign
truecolor
unjs
unstub
vitest
wordmark
yamllint
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ jobs:
test -f node_modules/@druxt/cli/starters/index.json
./node_modules/.bin/drx info
./node_modules/.bin/create-druxt --help
# The unattended path, as a pipeline runs it: no prompt, defaults applied.
./node_modules/.bin/drx init pack-site --yes --no-interactive --json
test -f pack-site/drx.config.json

# The one step that needs the network: a site from the real quickstart
# tarball the registry entry points at.
Expand Down
3 changes: 3 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ test:pack:
- test -f node_modules/@druxt/cli/starters/index.json
- ./node_modules/.bin/drx info
- ./node_modules/.bin/create-druxt --help
# The unattended path, as a pipeline runs it: no prompt, defaults applied.
- ./node_modules/.bin/drx init pack-site --yes --no-interactive --json
- test -f pack-site/drx.config.json
- section_end "pack"

# The one job that needs the network: it creates a site from the real
Expand Down
125 changes: 110 additions & 15 deletions README.md

Large diffs are not rendered by default.

189 changes: 174 additions & 15 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { stripVTControlCharacters } from 'node:util'
import { defineCommand, renderUsage, runCommand, type CommandDef } from 'citty'
import {
defineCommand,
renderUsage,
runCommand,
type ArgsDef,
type CommandDef,
} from 'citty'
import { infoCommand } from './commands/info.js'
import { initCommand } from './commands/init.js'
import { runMenu } from './commands/menu.js'
import { moduleCommand } from './commands/module.js'
import { defaultContext, type CliContext, type TextStream } from './context.js'
import { DrxError } from './errors.js'
import { createOutput } from './output.js'
import { cliVersion } from './package-root.js'

function mainCommand(context: CliContext) {
return defineCommand({
/** `drx` and every command under it. */
export function mainCommand(context: CliContext): CommandDef {
return defineCommand<ArgsDef>({
meta: {
name: 'drx',
version: cliVersion(),
description: 'The Druxt command-line tool',
},
args: {
interactive: {
type: 'boolean',
default: true,
description: 'Ask for what is missing when there is a terminal',
negativeDescription:
'Never ask, draw or wait: for CI and scripts. DRX_NO_INTERACTIVE=1 does the same',
},
},
subCommands: {
init: initCommand(context),
info: infoCommand(context),
Expand Down Expand Up @@ -45,7 +63,9 @@ async function usage(
let command = main
let parent: CommandDef | undefined
let path = 'drx'
for (const name of argv.filter((argument) => !argument.startsWith('-'))) {
// With the options after the command, a value is never taken for a name.
const names = optionsAfterCommand(main, argv)
for (const name of names.filter((argument) => !argument.startsWith('-'))) {
const next = (
command.subCommands as Record<string, CommandDef> | undefined
)?.[name]
Expand All @@ -61,6 +81,102 @@ async function usage(
return renderUsage(command, parent)
}

/** True when an option takes the argument after it as its value. */
function takesValue(option: string, args: ArgsDef): boolean {
if (option.includes('=')) return false
const name = option.replace(/^--?/, '')
return Object.entries(args).some(([key, definition]) => {
if (definition.type !== 'string' && definition.type !== 'enum') return false
const alias = 'alias' in definition ? definition.alias : undefined
return key === name || [alias ?? []].flat().includes(name)
})
}

const argsOf = (command: CommandDef) => (command.args ?? {}) as ArgsDef

/** Every command under one, each with the names that lead to it. */
function commandPaths(
command: CommandDef,
path: string[] = []
): { path: string[]; command: CommandDef }[] {
const below = (command.subCommands ?? {}) as Record<string, CommandDef>
return [
...(path.length > 0 ? [{ path, command }] : []),
...Object.entries(below).flatMap(([name, next]) =>
commandPaths(next, [...path, name])
),
]
}

/**
* Where the names that lead to a command are in the arguments, when nothing
* but options comes before each. An option is read as the command itself
* defines it, so the argument after one that takes a value is its value and
* never a name. Undefined when the arguments do not name the command.
*/
function namesAt(
argv: string[],
path: string[],
args: ArgsDef
): number[] | undefined {
const found: number[] = []
let index = 0
for (const name of path) {
while (argv[index]?.startsWith('-') && argv[index] !== '--') {
index += takesValue(argv[index] as string, args) ? 2 : 1
}
if (argv[index] !== name) return undefined
found.push(index)
index += 1
}
return found
}

/**
* The arguments with every option moved to after the command it was given
* before, so `--json module --type contrib list` reads as
* `module list --json --type contrib`. citty hands a command only what
* follows its name, and an option before it would be dropped without a
* word, or its value taken for the name of a command. The command then
* takes the option or refuses it, as it does one given after its name. The
* options of `drx` itself stay where they are.
*/
export function optionsAfterCommand(
main: CommandDef,
argv: string[]
): string[] {
// The longest run of names the arguments start with is the command. Of
// two as long, it is the one that takes more of what comes first as the
// values of its own options.
let names: number[] = []
for (const { path, command } of commandPaths(main)) {
const found = namesAt(argv, path, argsOf(command))
if (!found || found.length < names.length) continue
const later = (found.at(-1) as number) > (names.at(-1) ?? -1)
if (found.length > names.length || later) names = found
}
const last = names.at(-1)
if (last === undefined) return argv

const own = new Set(
Object.entries(argsOf(main)).flatMap(([name, definition]) =>
definition.type === 'boolean' ? [`--${name}`, `--no-${name}`] : []
)
)
const kept: string[] = []
const moved: string[] = []
for (const [index, argument] of argv.slice(0, last).entries()) {
if (names.includes(index)) continue
;(own.has(argument) ? kept : moved).push(argument)
}
return [
...kept,
...names.map((index) => argv[index] as string),
...moved,
...argv.slice(last + 1),
]
}

/** The code and message to report for anything a command throws. */
function describe(error: unknown): { code: string; message: string } {
if (error instanceof DrxError) {
Expand All @@ -81,17 +197,31 @@ function describe(error: unknown): { code: string; message: string } {
}
}

/** The flag that turns every prompt off. It goes before or after a command. */
const NO_INTERACTIVE = '--no-interactive'

/**
* Runs the CLI with the given arguments and returns the exit code. It never
* exits the process or throws, so a caller decides what to do with the code.
* What says nothing may be asked although there is a terminal: `CI`,
* `DRX_NO_INTERACTIVE` or `--no-interactive`, each of which says no whatever
* the terminal is. A command then takes its defaults, and fails naming the
* flag of a value that has none. Undefined when nothing turned the questions
* off, or when there is no terminal to ask on in the first place.
*/
export async function runCli(
argv: string[],
overrides: Partial<CliContext> = {}
): Promise<number> {
const context = { ...defaultContext(), ...overrides }
function questionsOff(context: CliContext, argv: string[]): string | undefined {
const on = (value: string | undefined) =>
value !== undefined && value !== '' && value !== '0' && value !== 'false'
if (!context.interactive) return undefined
if (argv.includes(NO_INTERACTIVE)) return NO_INTERACTIVE
if (on(context.env.DRX_NO_INTERACTIVE)) return 'DRX_NO_INTERACTIVE'
if (on(context.env.CI)) return 'CI'
return undefined
}

/** Runs one command line in a context that is already settled. */
async function execute(argv: string[], context: CliContext): Promise<number> {
const main = mainCommand(context)
const { stdout, stderr } = context
const json = argv.includes('--json')

try {
if (argv.includes('--help') || argv.includes('-h')) {
Expand All @@ -104,20 +234,49 @@ export async function runCli(
stdout.write(`${cliVersion()}\n`)
return 0
}
if (argv.length === 0) {
// Bare `drx`: a menu for a person, the usage for anything else.
if (argv.every((argument) => argument === '--no-banner')) {
if (context.interactive) {
return await runMenu(context, { banner: argv.length === 0 }, (next) =>
execute(next, context)
)
}
stderr.write(`${plain(stderr, await renderUsage(main))}\n`)
return 1
}
await runCommand(main, { rawArgs: argv })
await runCommand(main, { rawArgs: optionsAfterCommand(main, argv) })
return 0
} catch (error) {
const failure = describe(error)
if (argv.includes('--json')) {
if (json) {
stdout.write(
`${JSON.stringify({ ok: false, error: failure }, null, 2)}\n`
)
}
stderr.write(`Error: ${failure.message}\n`)
// In a terminal the line carries the mark for a failure. The word stays.
const output = createOutput(context, json)
const mark = output.decorated ? `${output.err.mark('failed')} ` : ''
stderr.write(`${mark}Error: ${failure.message}\n`)
return 1
}
}

/**
* Runs the CLI with the given arguments and returns the exit code. It never
* exits the process or throws, so a caller decides what to do with the code.
*/
export async function runCli(
argv: string[],
overrides: Partial<CliContext> = {}
): Promise<number> {
const context = { ...defaultContext(), ...overrides }
const off = questionsOff(context, argv)
return execute(
argv.filter((argument) => argument !== NO_INTERACTIVE),
{
...context,
interactive: context.interactive && off === undefined,
...(off === undefined ? {} : { questionsOff: off }),
}
)
}
14 changes: 8 additions & 6 deletions packages/cli/src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineCommand, type ArgsDef } from 'citty'
import { adapterFor, type PackageVersion } from '../adapters/index.js'
import { assertKnownOptions } from '../args.js'
import type { CliContext } from '../context.js'
import { columns, createOutput } from '../output.js'
import { columns, createOutput, type Output } from '../output.js'
import { cliVersion } from '../package-root.js'
import type { Part } from '../parts.js'
import {
Expand Down Expand Up @@ -46,9 +46,10 @@ async function reportPart(root: string, part: Part): Promise<PartReport> {
}
}

function partLines(part: PartReport): string[] {
function partLines(part: PartReport, output: Output): string[] {
const heading = [
`${part.role} ${part.kind} ${part.path}/`,
// The role is in words and in its colour: blue backend, green frontend.
output.out.role(part.role, `${part.role} ${part.kind} ${part.path}/`),
...columns(
[
...(part.engine ? [['Engine', part.engine]] : []),
Expand Down Expand Up @@ -82,7 +83,7 @@ function partLines(part: PartReport): string[] {
return [...heading, ...columns(rows, ' ')]
}

function text(report: InfoReport, cwd: string): string {
function text(report: InfoReport, cwd: string, output: Output): string {
const lines = columns([
['drx', report.cli.version],
['Node', report.node],
Expand All @@ -105,7 +106,7 @@ function text(report: InfoReport, cwd: string): string {
])
)
for (const part of project.parts) {
lines.push('', ...partLines(part))
lines.push('', ...partLines(part, output))
}
return lines.join('\n')
}
Expand All @@ -128,6 +129,7 @@ export function infoCommand(context: CliContext) {
async run({ args: parsed, rawArgs }) {
assertKnownOptions(rawArgs, args, 'info')
const output = createOutput(context, parsed.json === true)
output.header()

const report: InfoReport = {
cli: { name: '@druxt/cli', version: cliVersion() },
Expand All @@ -143,7 +145,7 @@ export function infoCommand(context: CliContext) {
}
report.project = { root, ...config, parts }
}
output.result(report, text(report, context.cwd))
output.result(report, text(report, context.cwd, output))
},
})
}
Loading
Loading