Skip to content

Commit 09e8647

Browse files
committed
feat: prefix build logs with target name
1 parent 2ecb39c commit 09e8647

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

Diff for: packages/react-native-builder-bob/src/index.ts

+18-32
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import buildModule from './targets/module';
1212
import buildTypescript from './targets/typescript';
1313
import buildCodegen from './targets/codegen';
1414
import customTarget from './targets/custom';
15-
import type { Options, Report, Target } from './types';
15+
import type { Options, Target } from './types';
1616

1717
type ArgName = 'target';
1818

@@ -58,9 +58,10 @@ yargs
5858
}
5959

6060
if (!(await fs.pathExists(projectPackagePath))) {
61-
logger.exit(
61+
logger.error(
6262
`Couldn't find a 'package.json' file in '${root}'.\n Are you in a project folder?`
6363
);
64+
process.exit(1);
6465
}
6566

6667
const pkg = JSON.parse(await fs.readFile(projectPackagePath, 'utf-8'));
@@ -98,10 +99,10 @@ yargs
9899
}
99100

100101
if (!entryFile) {
101-
logger.exit(
102+
logger.error(
102103
`Couldn't find a 'index.js'. 'index.ts' or 'index.tsx' file under '${source}'.\n Please re-run the CLI after creating it.`
103104
);
104-
return;
105+
process.exit(1);
105106
}
106107

107108
pkg.devDependencies = Object.fromEntries(
@@ -463,71 +464,56 @@ yargs
463464
const result = await explorer.search();
464465

465466
if (!result?.config) {
466-
logger.exit(
467+
logger.error(
467468
`No configuration found. Run '${argv.$0} init' to create one automatically.`
468469
);
470+
process.exit(1);
469471
}
470472

471473
const options: Options = result!.config;
472474

473475
if (!options.targets?.length) {
474-
logger.exit(
476+
logger.error(
475477
`No targets found in the configuration in '${path.relative(
476478
root,
477479
result!.filepath
478480
)}'.`
479481
);
482+
process.exit(1);
480483
}
481484

482485
const source = options.source;
483486

484487
if (!source) {
485-
logger.exit(
488+
logger.error(
486489
`No source option found in the configuration in '${path.relative(
487490
root,
488491
result!.filepath
489492
)}'.`
490493
);
494+
process.exit(1);
491495
}
492496

493497
const output = options.output;
494498

495499
if (!output) {
496-
logger.exit(
500+
logger.error(
497501
`No source option found in the configuration in '${path.relative(
498502
root,
499503
result!.filepath
500504
)}'.`
501505
);
506+
process.exit(1);
502507
}
503508

504509
const exclude =
505510
options.exclude ?? '**/{__tests__,__fixtures__,__mocks__}/**';
506511

507-
const report = {
508-
info: logger.info,
509-
warn: logger.warn,
510-
error: logger.error,
511-
success: logger.success,
512-
};
513-
514512
if (argv.target != null) {
515-
buildTarget(
516-
argv.target,
517-
report,
518-
source as string,
519-
output as string,
520-
exclude
521-
);
513+
buildTarget(argv.target, source as string, output as string, exclude);
522514
} else {
523515
for (const target of options.targets!) {
524-
buildTarget(
525-
target,
526-
report,
527-
source as string,
528-
output as string,
529-
exclude
530-
);
516+
buildTarget(target, source as string, output as string, exclude);
531517
}
532518
}
533519
})
@@ -537,15 +523,14 @@ yargs
537523

538524
async function buildTarget(
539525
target: Exclude<Options['targets'], undefined>[number],
540-
report: Report,
541526
source: string,
542527
output: string,
543528
exclude: string
544529
) {
545530
const targetName = Array.isArray(target) ? target[0] : target;
546531
const targetOptions = Array.isArray(target) ? target[1] : undefined;
547532

548-
report.info(`Building target ${kleur.blue(targetName)}`);
533+
const report = logger.grouped(targetName);
549534

550535
switch (targetName) {
551536
case 'commonjs':
@@ -594,6 +579,7 @@ async function buildTarget(
594579
});
595580
break;
596581
default:
597-
logger.exit(`Invalid target ${kleur.blue(targetName)}.`);
582+
logger.error(`Invalid target ${kleur.blue(targetName)}.`);
583+
process.exit(1);
598584
}
599585
}

Diff for: packages/react-native-builder-bob/src/targets/codegen/patches/patchCodegenAndroidPackage.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const mockPackageJson = {
1919
const mockReport: Report = {
2020
info: console.log,
2121
warn: console.log,
22-
error: console.log,
22+
error: console.error,
2323
success: console.log,
2424
};
2525

Diff for: packages/react-native-builder-bob/src/utils/logger.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ const logger =
44
(
55
type: string,
66
color: kleur.Color,
7+
group?: string,
78
stream: NodeJS.WriteStream = process.stdout
89
) =>
910
(...messages: unknown[]) => {
11+
if (group) {
12+
messages.unshift(
13+
`${kleur.gray('[')}${kleur.blue(group)}${kleur.gray(']')}`
14+
);
15+
}
16+
1017
const message = `${color(kleur.bold(type))} ${messages
1118
.map((message) => {
1219
if (typeof message === 'string') {
@@ -22,10 +29,14 @@ const logger =
2229

2330
export const info = logger('ℹ', kleur.blue);
2431
export const warn = logger('⚠', kleur.yellow);
25-
export const error = logger('✖', kleur.red, process.stderr);
32+
export const error = logger('✖', kleur.red, undefined, process.stderr);
2633
export const success = logger('✔', kleur.green);
2734

28-
export const exit = (...messages: unknown[]) => {
29-
error(...messages);
30-
process.exit(1);
35+
export const grouped = (label: string) => {
36+
return {
37+
info: logger('ℹ', kleur.blue, label),
38+
warn: logger('⚠', kleur.yellow, label),
39+
error: logger('✖', kleur.red, label, process.stderr),
40+
success: logger('✔', kleur.green, label),
41+
};
3142
};

0 commit comments

Comments
 (0)