Skip to content

Commit

Permalink
feat: ✨ prefix output for --no-pretty with the package name when ru…
Browse files Browse the repository at this point in the history
…nning concurrently #90
  • Loading branch information
folke committed May 25, 2020
1 parent 9e30e5c commit 4aa68a2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/concurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function createCommand(
const command = new CommandParser(pkg, pkg.root)
.parse(cmd)
.setCwd(pkg.root)
.setPackageName(pkg.name)
command.name = `${chalk.cyanBright(pkg.name)} at ${chalk.grey(
relative(workspace.root, pkg.root)
)}`
Expand Down
7 changes: 5 additions & 2 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class CommandFormatter {
public cmd: string,
public level: number,
public spinner: Spinner | undefined,
public options: RunnerOptions
public options: RunnerOptions,
public packageName?: string
) {}

private format(prefix: string, text: string) {
Expand All @@ -32,7 +33,9 @@ export class CommandFormatter {

write(data: string) {
if (!this.options.pretty) {
const prefix = `${chalk.grey.dim(`[${basename(this.cmd)}]`)} `
let cmdName = basename(this.cmd)
if (this.packageName) cmdName = `${this.packageName}::${cmdName}`
const prefix = `${chalk.grey.dim(`[${cmdName}]`)} `
data = prefix + this.format(prefix, data)
this.output += `${data}\n`
if (!this.options.silent) console.log(data)
Expand Down
10 changes: 10 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class Command {
children: Command[] = []
concurrent = false
cwd?: string
packageName?: string

// eslint-disable-next-line @typescript-eslint/require-await
beforeRun = async () => {
return
Expand Down Expand Up @@ -50,6 +52,14 @@ export class Command {
return this
}

setPackageName(name: string) {
this.packageName = name
for (const c of this.children) {
c.setPackageName(name)
}
return this
}

debug(showConcurrent = false): DebugCommand {
const args = this.args.slice(1).join(" ")
let cmd = `${this.type}:${this.name}${args.length ? ` ${args}` : ""}`
Expand Down
24 changes: 16 additions & 8 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ export class Runner {
if (this.options.raw) return
const title = this.formatCommand(cmd)
if (!this.options.pretty) {
if (cmd.type == CommandType.script) console.log(`❯ ${title}`)
else console.log(title)
const prefix = cmd.packageName
? `${chalk.grey.dim(` (${cmd.packageName})`)}`
: ""
if (cmd.type == CommandType.script) console.log(`❯ ${title}${prefix}`)
else console.log(title + prefix)
} else return this.spinner.start(title, level, parentSpinner)
}

Expand Down Expand Up @@ -62,12 +65,18 @@ export class Runner {
const cmdSpinner = this.formatStart(cmd, level, parentSpinner)
try {
if (!this.options.dryRun) {
const formatter = new CommandFormatter(
args[0],
level,
cmdSpinner,
this.options,
cmd.packageName
)
await this.spawn(
args[0],
args.slice(1),
level,
formatter,
cmd.cwd,
cmdSpinner,
cmd.env
)
}
Expand All @@ -87,7 +96,8 @@ export class Runner {
cmd.name,
level,
spinner,
this.options
this.options,
cmd.packageName
)

if (isBuildScript) {
Expand Down Expand Up @@ -149,13 +159,11 @@ export class Runner {
spawn(
cmd: string,
args: string[],
level: number,
formatter: CommandFormatter,
cwd?: string,
spinner?: Spinner,
env?: Record<string, string>
) {
const spawner = new Spawner(cmd, args, cwd, env)
const formatter = new CommandFormatter(cmd, level, spinner, this.options)

if (this.options.pretty)
spawner.onData = (line: string) => formatter.write(line)
Expand Down

0 comments on commit 4aa68a2

Please sign in to comment.