Skip to content

Commit

Permalink
feat(nx-verdaccio): stop verdaccio in case of executors error
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Oct 12, 2024
1 parent b3391de commit 98cba24
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function bootstrapEnvironment(
const { verbose, environmentRoot, storage, ...rest } = options;
const parsedStorage = storage ?? join(environmentRoot, 'storage');

let registryResult: RegistryResult
let registryResult: RegistryResult;
try {
registryResult = await startVerdaccioServer({
storage: parsedStorage,
Expand Down Expand Up @@ -60,6 +60,7 @@ export async function bootstrapEnvironment(
VERDACCIO_ENV_TOKEN
)
);
process.kill(Number(registry.pid));
throw error;
}

Expand Down Expand Up @@ -87,6 +88,7 @@ export async function bootstrapEnvironment(
VERDACCIO_ENV_TOKEN
)
);
process.kill(Number(registry.pid));
throw new Error(`Error saving verdaccio registry data. ${error.message}`);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { bold } from 'ansis';
import { logger } from '@nx/devkit';
import { type ExecutorContext, logger } from '@nx/devkit';
import { join } from 'node:path';
import { objectToCliArgs } from '../../internal/terminal';
import { executeProcess } from '../../internal/execute-process';
import { uniquePort } from './unique-port';
import { formatError, formatInfo } from '../../internal/logging';
import { TARGET_ENVIRONMENT_VERDACCIO_START } from '../../plugin/targets/environment.targets';
import { DEFAULT_VERDACCIO_STORAGE_DIR } from './constants';
import {
TARGET_ENVIRONMENT_VERDACCIO_START,
TARGET_ENVIRONMENT_VERDACCIO_STOP,
} from '../../plugin/targets/environment.targets';
import {
DEFAULT_VERDACCIO_STORAGE_DIR,
VERDACCIO_REGISTRY_JSON,
} from './constants';
import { runSingleExecutor } from '../../internal/run-executor';

const VERDACCIO_TOKEN = 'Verdaccio: ';

Expand Down Expand Up @@ -165,3 +173,26 @@ export async function startVerdaccioServer({
throw error;
}
}

export function stopVerdaccioServer(options: {
projectName: string;
verbose?: boolean;
configuration?: string;
environmentRoot: string;
context: ExecutorContext;
}): Promise<void> {
const { projectName, verbose, context, configuration, environmentRoot } =
options;
return runSingleExecutor(
{
project: projectName,
target: TARGET_ENVIRONMENT_VERDACCIO_STOP,
configuration,
},
{
...(verbose ? { verbose } : {}),
filePath: join(environmentRoot, VERDACCIO_REGISTRY_JSON),
},
context
);
}
52 changes: 33 additions & 19 deletions projects/nx-verdaccio/src/executors/env-setup/executor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { type ExecutorContext, logger, readJsonFile } from '@nx/devkit';
import { type ExecutorContext, logger } from '@nx/devkit';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { executeProcess } from '../../internal/execute-process';
import { objectToCliArgs } from '../../internal/terminal';
import type { VerdaccioProcessResult } from '../env-bootstrap/verdaccio-registry';
import {
stopVerdaccioServer,
type VerdaccioProcessResult,
} from '../env-bootstrap/verdaccio-registry';
import type { SetupEnvironmentExecutorOptions } from './schema';

import { VERDACCIO_REGISTRY_JSON } from '../env-bootstrap/constants';
import {
TARGET_ENVIRONMENT_BOOTSTRAP,
TARGET_ENVIRONMENT_INSTALL,
TARGET_ENVIRONMENT_VERDACCIO_STOP,
} from '../../plugin/targets/environment.targets';
import { runSingleExecutor } from '../../internal/run-executor';

Expand Down Expand Up @@ -62,6 +64,14 @@ export default async function runSetupEnvironmentExecutor(
});
} catch (error) {
logger.error(error.message);
await stopVerdaccioServer({
projectName,
verbose,
context,
configuration,
environmentRoot,
});

return {
success: false,
command: `Fails executing target ${TARGET_ENVIRONMENT_INSTALL}\n ${error.message}`,
Expand All @@ -70,25 +80,29 @@ export default async function runSetupEnvironmentExecutor(

try {
if (!keepServerRunning) {
await runSingleExecutor(
{
project: projectName,
target: TARGET_ENVIRONMENT_VERDACCIO_STOP,
configuration,
},
{
...(verbose ? { verbose } : {}),
filePath: join(environmentRoot, VERDACCIO_REGISTRY_JSON),
},
context
);
await stopVerdaccioServer({
projectName,
verbose,
context,
configuration,
environmentRoot,
});
} else {
const { url } = readJsonFile<VerdaccioProcessResult>(
join(environmentRoot, VERDACCIO_REGISTRY_JSON)
);
const { url } = await readFile(
join(environmentRoot, VERDACCIO_REGISTRY_JSON),
'utf8'
).then((file) => JSON.parse(file) as VerdaccioProcessResult);
logger.info(`Verdaccio server kept running under : ${url}`);
}
} catch (error) {
await stopVerdaccioServer({
projectName,
verbose,
context,
configuration,
environmentRoot,
});

logger.error(error.message);
return {
success: false,
Expand Down

0 comments on commit 98cba24

Please sign in to comment.