Skip to content

Commit

Permalink
fix: adjust verbose logging
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Sep 28, 2024
2 parents b19ba08 + 848cef3 commit 6d3bf2f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 56 deletions.
20 changes: 12 additions & 8 deletions projects/nx-verdaccio-env/src/executors/env-bootstrap/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ export async function bootstrapExecutor(
context: ExecutorContext
): Promise<BootstrapExecutorOutput> {
const { configurationName, projectName } = context;
const { keepServerRunning, environmentRoot } = options;
const { keepServerRunning, environmentRoot, verbose } = options;

logger.info(
`Execute ${PACKAGE_NAME}:${TARGET_ENVIRONMENT_BOOTSTRAP} with options: ${JSON.stringify(
options,
null,
2
)}`
);
if (verbose) {
logger.info(
`Execute ${PACKAGE_NAME}:${TARGET_ENVIRONMENT_BOOTSTRAP} with options: ${JSON.stringify(
options,
null,
2
)}`
);
}

let bootstrapResult: BootstrapEnvironmentResult;
try {
bootstrapResult = await bootstrapEnvironment({
projectName,
environmentRoot,
verbose,
});
} catch (error) {
logger.error(error);
Expand All @@ -64,6 +67,7 @@ export async function bootstrapExecutor(
configuration: configurationName,
},
{
verbose,
filePath: join(environmentRoot, VERDACCIO_REGISTRY_JSON),
},
context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('runBootstrapExecutor', () => {
runBootstrapExecutor(
{
environmentRoot: `tmp/environments/${e2eProjectName}`,
verbose: true,
},
context
)
Expand All @@ -79,6 +80,7 @@ describe('runBootstrapExecutor', () => {
`Execute ${PACKAGE_NAME}:pb-ve-env-bootstrap with options: ${JSON.stringify(
{
environmentRoot: `tmp/environments/${e2eProjectName}`,
verbose: true,
},
null,
2
Expand All @@ -90,6 +92,7 @@ describe('runBootstrapExecutor', () => {
stopVerdaccioTask,
{
filePath: `tmp/environments/${e2eProjectName}/verdaccio-registry.json`,
verbose: true,
},
context
);
Expand Down Expand Up @@ -128,11 +131,6 @@ describe('runBootstrapExecutor', () => {
command: 'Failed to env-bootstrap environment',
});

expect(infoLoggerSpy).toHaveBeenCalledTimes(1);
expect(infoLoggerSpy).toHaveBeenCalledWith(
`Execute ${PACKAGE_NAME}:pb-ve-env-bootstrap with options: {}`
);

expect(errorLoggerSpy).toHaveBeenCalledTimes(1);
expect(errorLoggerSpy).toHaveBeenCalledWith(
Error('Failed to env-bootstrap environment')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function startVerdaccioServer({
port = String(uniquePort()),
location = 'none',
clear = true,
verbose = true,
verbose,
storage = DEFAULT_VERDACCIO_STORAGE_DIR,
...opt
}: StartVerdaccioOptions): Promise<RegistryResult> {
Expand All @@ -91,7 +91,7 @@ export async function startVerdaccioServer({
args: objectToCliArgs({
_: [TARGET_ENVIRONMENT_VERDACCIO_START, projectName ?? '', '--'],
port,
verbose,
...(verbose !== undefined ? { verbose } : {}),
location,
clear,
storage,
Expand Down Expand Up @@ -130,15 +130,15 @@ export async function startVerdaccioServer({
},
};

logger.info(
formatInfo(
`Registry started on URL: ${bold(
result.registry.url
)}, ProcessID: ${bold(String(childProcess?.pid))}`,
VERDACCIO_TOKEN
)
);
if (verbose) {
logger.info(
formatInfo(
`Registry started on URL: ${bold(
result.registry.url
)}, ProcessID: ${bold(String(childProcess?.pid))}`,
VERDACCIO_TOKEN
)
);
logger.info(formatInfo('', VERDACCIO_TOKEN));
console.table(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('startVerdaccioServer', () => {

const result = await startVerdaccioServer({
projectName: 'test-project',
verbose: true,
});

expect(result).toStrictEqual({
Expand Down
23 changes: 13 additions & 10 deletions projects/nx-verdaccio-env/src/executors/kill-process/executor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ExecutorContext, logger } from '@nx/devkit';
import { logger } from '@nx/devkit';
import type { KillProcessExecutorOptions } from './schema';
import { join } from 'node:path';
import { killProcessFromPid } from './kill-process';
import { killProcessFromFilePath } from './kill-process';
import {
DEFAULT_PROCESS_FILENAME,
EXECUTOR_ENVIRONMENT_KILL_PROCESS,
Expand All @@ -26,19 +26,22 @@ export default async function runKillProcessExecutor(
filePath = join(environmentRoot ?? '', DEFAULT_PROCESS_FILENAME),
} = options;

logger.info(
`Execute ${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_KILL_PROCESS} with options: ${JSON.stringify(
options,
null,
2
)}`
);
if (verbose) {
logger.info(
`Execute ${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_KILL_PROCESS} with options: ${JSON.stringify(
options,
null,
2
)}`
);
}

try {
if (pid) {
process.kill(Number(pid));
logger.info(`Killed process with id: ${pid}.`);
} else {
killProcessFromPid(filePath, { cleanFs, dryRun, verbose });
killProcessFromFilePath(filePath, { cleanFs, dryRun, verbose });
}
} catch (error) {
logger.error(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ vi.mock('@nx/devkit', async () => {
describe('runKillProcessExecutor', () => {
const killSpy = vi.spyOn(process, 'kill').mockImplementation(vi.fn());
const killProcessFromPidSpy = vi
.spyOn(killProcessModule, 'killProcessFromPid')
.spyOn(killProcessModule, 'killProcessFromFilePath')
.mockImplementation(vi.fn());

beforeEach(() => {
Expand Down Expand Up @@ -70,13 +70,7 @@ describe('runKillProcessExecutor', () => {
expect(killSpy).toHaveBeenCalledWith(777);

expect(logger.info).toHaveBeenCalledTimes(1);
expect(logger.info).toHaveBeenCalledWith(
`Execute ${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_KILL_PROCESS} with options: ${JSON.stringify(
{ pid: 777 },
null,
2
)}`
);
expect(logger.info).toHaveBeenCalledWith('Killed process with id: 777.');
});

it('should load file kill process with pid from file', async () => {
Expand Down Expand Up @@ -122,14 +116,8 @@ describe('runKillProcessExecutor', () => {
}
);

expect(logger.info).toHaveBeenCalledTimes(1);
expect(logger.info).toHaveBeenCalledWith(
`Execute ${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_KILL_PROCESS} with options: ${JSON.stringify(
{ filePath: 'tmp/environments/my-lib' },
null,
2
)}`
);
expect(logger.info).toHaveBeenCalledTimes(0);
// expect(logger.info).toHaveBeenCalledWith('Killed process with id: 777.')
});

it('should handle error caused by process kill', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { logger, readJsonFile } from '@nx/devkit';
import { rm } from 'node:fs/promises';

export async function killProcessFromPid(
export async function killProcessFromFilePath(
filePath: string,
options?: {
cleanFs?: boolean;
Expand Down Expand Up @@ -31,6 +31,7 @@ export async function killProcessFromPid(
}
} else {
process.kill(Number(pid));
logger.info(`Killed process with id: ${pid}.`);
}
} catch (e) {
logger.error(`Failed killing process with id: ${pid}\n${e}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from 'vitest';
import { killProcessFromPid } from './kill-process';
import { killProcessFromFilePath } from './kill-process';
import { logger, readJsonFile } from '@nx/devkit';
import { rm } from 'node:fs/promises';

Expand All @@ -21,7 +21,7 @@ describe('killProcessFromPid', () => {
it('should kill the process if pid is found and dryRun is false', async () => {
vi.mocked(readJsonFile).mockReturnValue({ pid: 1234 });

await killProcessFromPid('path/to/file', { dryRun: false });
await killProcessFromFilePath('path/to/file', { dryRun: false });

expect(processKillSpy).toHaveBeenCalledWith(1234);
expect(rm).toHaveBeenCalledWith('path/to/file');
Expand All @@ -30,7 +30,10 @@ describe('killProcessFromPid', () => {
it('should not kill the process if dryRun is true but log a warning', async () => {
vi.mocked(readJsonFile).mockReturnValue({ pid: 1234 });

await killProcessFromPid('path/to/file', { dryRun: true, verbose: true });
await killProcessFromFilePath('path/to/file', {
dryRun: true,
verbose: true,
});

expect(processKillSpy).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalledWith(
Expand All @@ -44,7 +47,7 @@ describe('killProcessFromPid', () => {
throw new Error('File not found');
});

await expect(killProcessFromPid('path/to/file')).rejects.toThrowError(
await expect(killProcessFromFilePath('path/to/file')).rejects.toThrowError(
'Could not load path/to/file to get pid'
);
expect(processKillSpy).not.toHaveBeenCalled();
Expand All @@ -54,7 +57,7 @@ describe('killProcessFromPid', () => {
it('should throw an error if pid is not found in the file', async () => {
vi.mocked(readJsonFile).mockReturnValue({});

await expect(killProcessFromPid('path/to/file')).rejects.toThrowError(
await expect(killProcessFromFilePath('path/to/file')).rejects.toThrowError(
'no pid found in file path/to/file'
);
expect(processKillSpy).not.toHaveBeenCalled();
Expand All @@ -68,7 +71,7 @@ describe('killProcessFromPid', () => {
throw new Error('Failed to kill process');
});

await killProcessFromPid('path/to/file', { dryRun: false });
await killProcessFromFilePath('path/to/file', { dryRun: false });

expect(logger.error).toHaveBeenCalledWith(
'Failed killing process with id: 1234\nError: Failed to kill process'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export function getEnvTargets(
// runs env-bootstrap-env, install-env and stop-verdaccio
[TARGET_ENVIRONMENT_SETUP]: {
outputs: ['{options.environmentRoot}'],
cache: false,
executor: `${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_SETUP}`,
options: {
environmentRoot,
Expand Down

0 comments on commit 6d3bf2f

Please sign in to comment.