Skip to content

Commit

Permalink
[BREAKING] refactor: remove --silent CLI flag (#780)
Browse files Browse the repository at this point in the history
* fix: add silent flag to setupInteractions for sync logs

* fix: pass silent flag in start commands to setupInteractions

* refactor: remove silent flag all together

* fix: arg name in LoggerPlugin

* chore: remove silent arg from tests

* chore: add changeset
  • Loading branch information
jbroma authored Nov 3, 2024
1 parent 679bcd8 commit e937211
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 58 deletions.
9 changes: 9 additions & 0 deletions .changeset/bright-scissors-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@callstack/repack": major
---

Removed `--silent` CLI flag for start command.

For silencing output, you can use shell redirection instead:
- Unix/macOS: `npx react-native start > /dev/null 2>&1`
- Windows: `npx react-native start > nul 2>&1`
1 change: 0 additions & 1 deletion apps/tester-app/__tests__/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ describe('start command', () => {
const args = {
port,
platform,
silent: true,
logFile: path.join(TMP_DIR, 'server.log'),
webpackConfig: path.join(__dirname, 'configs', configFile),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,29 @@ describe('setupInteractions', () => {
it('should log a warning if setRawMode is not available', () => {
mockProcess.stdin.setRawMode = undefined as any;

setupInteractions({}, mockLogger, mockProcess, mockReadline);
setupInteractions(
{},
{
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
}
);

expect(mockLogger.warn).toHaveBeenCalledWith(
'Interactive mode is not supported in this environment'
);
});

it('should set up keypress events and interactions', () => {
setupInteractions({}, mockLogger, mockProcess, mockReadline);
setupInteractions(
{},
{
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
}
);

expect(mockReadline.emitKeypressEvents).toHaveBeenCalledWith(
mockProcess.stdin
Expand All @@ -67,7 +81,14 @@ describe('setupInteractions', () => {
});

it('should handle ctrl+c and ctrl+z keypresses', () => {
setupInteractions({}, mockLogger, mockProcess, mockReadline);
setupInteractions(
{},
{
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
}
);

const keypressHandler = (mockProcess.stdin.on as jest.Mock).mock
.calls[0][1];
Expand All @@ -86,7 +107,11 @@ describe('setupInteractions', () => {
onOpenDevTools: jest.fn(),
};

setupInteractions(handlers, mockLogger, mockProcess, mockReadline);
setupInteractions(handlers, {
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
});

const keypressHandler = (mockProcess.stdin.on as jest.Mock).mock
.calls[0][1];
Expand All @@ -109,7 +134,11 @@ describe('setupInteractions', () => {
onReload: jest.fn(),
};

setupInteractions(handlers, mockLogger, mockProcess, mockReadline);
setupInteractions(handlers, {
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
});

expect(mockProcess.stdout.write).toHaveBeenCalledWith(' r: Reload app\n');
expect(mockProcess.stdout.write).toHaveBeenCalledWith(
Expand All @@ -132,7 +161,11 @@ describe('setupInteractions', () => {
// onOpenDevMenu - unsupported
};

setupInteractions(handlers, mockLogger, mockProcess, mockReadline);
setupInteractions(handlers, {
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
});

const keypressHandler = (mockProcess.stdin.on as jest.Mock).mock
.calls[0][1];
Expand Down Expand Up @@ -164,7 +197,11 @@ describe('setupInteractions', () => {
onOpenDevTools: jest.fn(),
};

setupInteractions(handlers, mockLogger, mockProcess, mockReadline);
setupInteractions(handlers, {
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
});

const keypressHandler = (mockProcess.stdin.on as jest.Mock).mock
.calls[0][1];
Expand All @@ -179,7 +216,11 @@ describe('setupInteractions', () => {
onOpenDevTools: jest.fn(),
};

setupInteractions(handlers, mockLogger, mockProcess, mockReadline);
setupInteractions(handlers, {
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
});

const keypressHandler = (mockProcess.stdin.on as jest.Mock).mock
.calls[0][1];
Expand All @@ -199,9 +240,11 @@ describe('setupInteractions', () => {
onOpenDevMenu() {},
onReload() {},
},
mockLogger,
mockProcess,
mockReadline
{
logger: mockLogger,
process: mockProcess,
readline: mockReadline,
}
);

expect(mockProcess.stdout.write).toHaveBeenNthCalledWith(
Expand Down
15 changes: 10 additions & 5 deletions packages/repack/src/commands/common/setupInteractions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import defaultReadline from 'node:readline';
import nodeReadline from 'node:readline';
import * as colorette from 'colorette';
import type { Logger } from '../../types';

Expand All @@ -22,10 +22,16 @@ export function setupInteractions(
onOpenDevMenu?: () => void;
onOpenDevTools?: () => void;
},
logger: Logger = console,
process: NodeJS.Process = global.process,
readline: typeof defaultReadline = defaultReadline
options?: {
logger?: Logger;
process?: NodeJS.Process;
readline?: typeof nodeReadline;
}
) {
const logger = options?.logger ?? console;
const process = options?.process ?? global.process;
const readline = options?.readline ?? nodeReadline;

if (!process.stdin.setRawMode) {
logger.warn('Interactive mode is not supported in this environment');
return;
Expand Down Expand Up @@ -97,6 +103,5 @@ export function setupInteractions(

process.stdout.write(isSupported ? text : colorette.italic(text));
}

process.stdout.write('\nPress Ctrl+c or Ctrl+z to quit the dev server\n\n');
}
4 changes: 0 additions & 4 deletions packages/repack/src/commands/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ export const startCommandOptions = [
name: '--reverse-port',
description: 'ADB reverse port on starting devServers only for Android',
},
{
name: '--silent',
description: 'Silents all logs to the console/stdout',
},
{
name: '--verbose',
description: 'Enables verbose logging',
Expand Down
21 changes: 7 additions & 14 deletions packages/repack/src/commands/rspack/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,20 @@ export async function start(

const reversePort = reversePortArg ?? process.argv.includes('--reverse-port');

const isSilent = args.silent;
const isVerbose = args.verbose;

const showHttpRequests = isSilent ? false : isVerbose || args.logRequests;
const showHttpRequests = isVerbose || args.logRequests;

const reporter = composeReporters(
[
new ConsoleReporter({
asJson: args.json,
level: isSilent ? 'silent' : isVerbose ? 'verbose' : 'normal',
}),
new ConsoleReporter({ asJson: args.json, isVerbose }),
args.logFile ? new FileReporter({ filename: args.logFile }) : undefined,
].filter(Boolean) as Reporter[]
);

if (!isSilent) {
const version = packageJson.version;
process.stdout.write(
colorette.bold(colorette.cyan('📦 Re.Pack ' + version + '\n\n'))
);
}
const version = packageJson.version;
process.stdout.write(
colorette.bold(colorette.cyan('📦 Re.Pack ' + version + '\n\n'))
);

// @ts-ignore
const compiler = new Compiler(cliOptions, reporter);
Expand Down Expand Up @@ -119,7 +112,7 @@ export async function start(
});
},
},
ctx.log
{ logger: ctx.log }
);
}

Expand Down
1 change: 0 additions & 1 deletion packages/repack/src/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export interface StartArguments {
logRequests?: boolean;
platform?: string;
reversePort?: boolean;
silent?: boolean;
verbose?: boolean;
config?: string;
webpackConfig?: string;
Expand Down
21 changes: 7 additions & 14 deletions packages/repack/src/commands/webpack/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,20 @@ export async function start(_: string[], config: Config, args: StartArguments) {

const reversePort = reversePortArg ?? process.argv.includes('--reverse-port');

const isSilent = args.silent;
const isVerbose = args.verbose;

const showHttpRequests = isSilent ? false : isVerbose || args.logRequests;
const showHttpRequests = isVerbose || args.logRequests;

const reporter = composeReporters(
[
new ConsoleReporter({
asJson: args.json,
level: isSilent ? 'silent' : isVerbose ? 'verbose' : 'normal',
}),
new ConsoleReporter({ asJson: args.json, isVerbose }),
args.logFile ? new FileReporter({ filename: args.logFile }) : undefined,
].filter(Boolean) as Reporter[]
);

if (!isSilent) {
const version = packageJson.version;
process.stdout.write(
colorette.bold(colorette.cyan('📦 Re.Pack ' + version + '\n\n'))
);
}
const version = packageJson.version;
process.stdout.write(
colorette.bold(colorette.cyan('📦 Re.Pack ' + version + '\n\n'))
);

const compiler = new Compiler(cliOptions, reporter, isVerbose);

Expand Down Expand Up @@ -118,7 +111,7 @@ export async function start(_: string[], config: Config, args: StartArguments) {
});
},
},
ctx.log
{ logger: ctx.log }
);
}

Expand Down
9 changes: 2 additions & 7 deletions packages/repack/src/logging/reporters/ConsoleReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { LogEntry, LogType, Reporter } from '../types';

export interface ConsoleReporterConfig {
asJson?: boolean;
level?: 'silent' | 'normal' | 'verbose';
isVerbose?: boolean;
isWorker?: boolean;
}

Expand Down Expand Up @@ -77,13 +77,8 @@ class InteractiveConsoleReporter implements Reporter {
constructor(private config: ConsoleReporterConfig) {}

process(log: LogEntry) {
// Do not log anything in silent mode
if (this.config.level === 'silent') {
return;
}

// Do not log debug messages in non-verbose mode
if (log.type === 'debug' && this.config.level !== 'verbose') {
if (log.type === 'debug' && !this.config.isVerbose) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/repack/src/plugins/LoggerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class LoggerPlugin implements RspackPluginInstance {
reporters.push(
new ConsoleReporter({
isWorker: Boolean(process.env[WORKER_ENV_KEY]),
level: process.env[VERBOSE_ENV_KEY] ? 'verbose' : 'normal',
isVerbose: Boolean(process.env[VERBOSE_ENV_KEY]),
})
);
}
Expand Down

0 comments on commit e937211

Please sign in to comment.