Skip to content

Commit

Permalink
reduce report data
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 23, 2024
1 parent b3194a0 commit 42ce76d
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 83 deletions.
131 changes: 89 additions & 42 deletions projects/build-env/src/executors/bootstrap/bootstrap-env.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { join } from 'node:path';
import {
startVerdaccioServer,
type StarVerdaccioOptions,
type StartVerdaccioOptions,
type VercaddioServerResult,
} from './verdaccio-registry';
import { writeFile } from 'node:fs/promises';
import { setupNpmWorkspace } from './npm';
import { formatInfo } from '../../internal/logging';
import { formatError, formatInfo } from '../../internal/logging';
import { VERDACCIO_REGISTRY_JSON } from './constants';
import { logger } from '@nx/devkit';
import {
Expand All @@ -15,57 +15,104 @@ import {
VERDACCIO_ENV_TOKEN,
} from './npm';

export type BootstrapEnvironmentOptions = Partial<
StarVerdaccioOptions & Environment
> & {
keepServerRunning?: boolean;
projectName: string;
environmentRoot: string;
};
export type BootstrapEnvironmentOptions = StartVerdaccioOptions &
Environment & {
projectName: string;
environmentRoot: string;
};

export type BootstrapEnvironmentResult = Environment & {
registry: VercaddioServerResult;
stop: () => void;
};

export async function bootstrapEnvironment(
options
options: BootstrapEnvironmentOptions
): Promise<BootstrapEnvironmentResult> {
const { verbose, environmentRoot, storage } = options;
const registryResult = await startVerdaccioServer({
storage: storage ?? join(environmentRoot, 'storage'),
verbose,
readyWhen: 'Environment ready under',
keepServerRunning: true,
...options,
});
const { verbose, environmentRoot, storage, ...rest } = options;
const parsedStorage = storage ?? join(environmentRoot, 'storage');

await setupNpmWorkspace(environmentRoot, verbose);
const userconfig = join(environmentRoot, '.npmrc');
configureRegistry({ ...registryResult.registry, userconfig }, verbose);
let registryResult;
try {
registryResult = await startVerdaccioServer({
storage: parsedStorage,
verbose,
readyWhen: 'Environment ready under',
...(rest as StartVerdaccioOptions),
});
} catch (error) {
logger.error(
formatError(
`Error starting verdaccio registry: ${(error as Error).message}`,
VERDACCIO_ENV_TOKEN
)
);
throw error;
}

const activeRegistry: BootstrapEnvironmentResult = {
...registryResult,
root: environmentRoot,
};
try {
logger.info(
formatInfo(
`Setup NPM workspace in ${environmentRoot}`,
VERDACCIO_ENV_TOKEN
)
);
await setupNpmWorkspace(environmentRoot, verbose);

logger.info(
formatInfo(
`Save active verdaccio registry data to file: ${activeRegistry.root}`,
VERDACCIO_ENV_TOKEN
)
);
await writeFile(
join(activeRegistry.root, VERDACCIO_REGISTRY_JSON),
JSON.stringify(activeRegistry.registry, null, 2)
);
const { registry } = registryResult;
const { url, port, host } = registry;
const userconfig = join(environmentRoot, '.npmrc');
configureRegistry({ url, port, host, userconfig }, verbose);
} catch (error) {
logger.error(
formatError(
`Error configuring verdaccio NPM registry: ${(error as Error).message}`,
VERDACCIO_ENV_TOKEN
)
);
throw error;
}

logger.info(
formatInfo(
`Environment ready under: ${activeRegistry.root}`,
VERDACCIO_ENV_TOKEN
)
);
try {
logger.info(
formatInfo(
`Save active verdaccio registry data to file: ${join(
environmentRoot,
VERDACCIO_REGISTRY_JSON
)}`,
VERDACCIO_ENV_TOKEN
)
);
await writeFile(
join(environmentRoot, VERDACCIO_REGISTRY_JSON),
JSON.stringify(environmentRoot, null, 2)
); // NOTICE: This is a "readyWhen" condition
logger.info(
formatInfo(
`Environment ready under: ${environmentRoot}`,
VERDACCIO_ENV_TOKEN
)
);
logger.info(
formatInfo(
`File saved: ${join(environmentRoot, VERDACCIO_REGISTRY_JSON)}`,
VERDACCIO_ENV_TOKEN
)
);

return activeRegistry;
return {
...registryResult,
environmentRoot: environmentRoot,
};
} catch (error) {
logger.error(
formatError(
`Error saving verdaccio registry data to ${environmentRoot}: ${
(error as Error).message
}`,
VERDACCIO_ENV_TOKEN
)
);
throw new Error(`Error saving verdaccio registry data. ${error.message}`);
}
}
18 changes: 11 additions & 7 deletions projects/build-env/src/executors/bootstrap/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { formatInfo } from '../../internal/logging';
import { VERDACCIO_ENV_TOKEN } from './npm';
import { join } from 'node:path';
import { VERDACCIO_REGISTRY_JSON } from './constants';
import { DEFAULT_STOP_VERDACCIO_TARGET } from '../../internal/constants';
import {
DEFAULT_BOOTSTRAP_TARGET,
DEFAULT_STOP_VERDACCIO_TARGET,
} from '../../internal/constants';

export type BootstrapExecutorOutput = {
success: boolean;
Expand All @@ -19,12 +22,12 @@ export type BootstrapExecutorOutput = {
export async function bootstrapExecutor(
options: BootstrapExecutorOptions,
context: ExecutorContext
) {
): Promise<BootstrapExecutorOutput> {
const { configurationName, projectName } = context;
const { keepServerRunning, environmentRoot } = options;

logger.info(
`Execute @push-based/build-env:bootstrap with options: ${JSON.stringify(
`Execute @push-based/build-env:${DEFAULT_BOOTSTRAP_TARGET} with options: ${JSON.stringify(
options,
null,
2
Expand Down Expand Up @@ -53,7 +56,7 @@ export async function bootstrapExecutor(
formatInfo(`Verdaccio server running under ${url}`, VERDACCIO_ENV_TOKEN)
);
} else {
await runExecutor(
for await (const s of await runExecutor(
{
project: projectName,
target: DEFAULT_STOP_VERDACCIO_TARGET,
Expand All @@ -63,13 +66,14 @@ export async function bootstrapExecutor(
filePath: join(environmentRoot, VERDACCIO_REGISTRY_JSON),
},
context
);
)) {
}
}

return Promise.resolve({
return {
success: true,
command: 'Bootstrapped environment successfully.',
} satisfies BootstrapExecutorOutput);
};
}

export default bootstrapExecutor;
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('runBootstrapExecutor', () => {
storage: 'tmp/storage',
url: 'http://localhost:4873',
},
root: `tmp/environments/${e2eProjectName}`,
environmentRoot: `tmp/environments/${e2eProjectName}`,
stop: expect.any(Function),
});
runExecutorSpy.mockResolvedValueOnce({
Expand Down
8 changes: 4 additions & 4 deletions projects/build-env/src/executors/bootstrap/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from 'node:path';
import { ensureDirectoryExists } from '../../internal/file-system';
import { formatError, formatInfo } from '../../internal/logging';
import { logger } from '@nx/devkit';
import type { VerdaccioProcessResult } from './verdaccio-registry';
import type { VercaddioServerResult } from './verdaccio-registry';
import { objectToCliArgs } from '../../internal/terminal';

export const NPM_ENV_TOKEN = 'Npm Env: ';
Expand Down Expand Up @@ -49,11 +49,11 @@ export async function setupNpmWorkspace(
export const VERDACCIO_ENV_TOKEN = 'Verdaccio Env: ';

export type Environment = {
root: string;
environmentRoot: string;
};

export type ConfigureRegistryOptions = Pick<
VerdaccioProcessResult,
VercaddioServerResult,
'port' | 'host' | 'url'
> & {
userconfig?: string;
Expand Down Expand Up @@ -93,7 +93,7 @@ export function configureRegistry(
}

export type UnconfigureRegistryOptions = Pick<
VerdaccioProcessResult,
VercaddioServerResult,
'port' | 'host'
> & {
userconfig?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type VerdaccioExecuterOptions = {
clear?: boolean;
};

export type StarVerdaccioOptions = VerdaccioExecuterOptions &
export type StartVerdaccioOptions = VerdaccioExecuterOptions &
StarVerdaccioOnlyOptions;

export async function startVerdaccioServer({
Expand All @@ -82,7 +82,7 @@ export async function startVerdaccioServer({
clear = true,
verbose = true,
...opt
}: StarVerdaccioOptions): Promise<RegistryResult> {
}: StartVerdaccioOptions): Promise<RegistryResult> {
let verdaccioIsRunning = false;

const startServerPromise = () =>
Expand Down
1 change: 1 addition & 0 deletions projects/build-env/src/executors/npm-install/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default async function runNpmInstallExecutor(
fund: false, // avoid polluted terminal
shrinkwrap: false, // avoid package-lock creation or update
save: true, // save to package.json dependencies
userconfig: join('.npmrc'), // enforce local npmrc
}),
cwd: environmentRoot,
verbose: true,
Expand Down
41 changes: 31 additions & 10 deletions projects/build-env/src/executors/setup/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ export default async function runSetupEnvironmentExecutor(
context: ExecutorContext
) {
const { configurationName: configuration, projectName } = context;

const { verbose, environmentRoot, keepServerRunning } =
terminalAndExecutorOptions;
try {
const { verbose, environmentRoot, keepServerRunning } =
terminalAndExecutorOptions;

await runExecutor(
logger.info(
`Bootstrapping environment for ${projectName} in ${environmentRoot}`
);
for await (const s of await runExecutor(
{
project: projectName,
target: DEFAULT_BOOTSTRAP_TARGET,
Expand All @@ -42,12 +43,24 @@ export default async function runSetupEnvironmentExecutor(
{
...terminalAndExecutorOptions,
// we always want to keep the server running as in the following step we install packages
// the keepServerRunning option is only used to stop the server after the installation (or keep it running for debug reasons)
// the `keepServerRunning` passed in `options` is only used to stop the server after the installation (or keep it running for debug reasons)
keepServerRunning: true,
},
context
);
)) {
}
} catch (error) {
logger.error(error);
return {
success: false,
command: `Fails executing target ${DEFAULT_BOOTSTRAP_TARGET}\n ${error.message}`,
};
}

try {
logger.info(`Installing packages for ${projectName} in ${environmentRoot}`);
if (verbose) {
}
await executeProcess({
command: 'nx',
args: objectToCliArgs({
Expand All @@ -57,9 +70,17 @@ export default async function runSetupEnvironmentExecutor(
cwd: process.cwd(),
...(verbose ? { verbose } : {}),
});
} catch (error) {
logger.error(error);
return {
success: false,
command: `Fails executing target ${DEFAULT_INSTALL_TARGET}\n ${error.message}`,
};
}

try {
if (!keepServerRunning) {
await await runExecutor(
for await (const s of await runExecutor(
{
project: projectName,
target: DEFAULT_STOP_VERDACCIO_TARGET,
Expand All @@ -70,15 +91,15 @@ export default async function runSetupEnvironmentExecutor(
filePath: join(environmentRoot, VERDACCIO_REGISTRY_JSON),
},
context
);
)) {
}
} else {
const { url } = readJsonFile<VerdaccioProcessResult>(
join(environmentRoot, VERDACCIO_REGISTRY_JSON)
);
logger.info(`Verdaccio server kept running under : ${url}`);
}
} catch (error) {
// nx build-env cli-e2e
logger.error(error);
return {
success: false,
Expand Down
2 changes: 1 addition & 1 deletion projects/build-env/src/internal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from 'node:path';
export const DEFAULT_ENVIRONMENTS_OUTPUT_DIR = join('tmp', 'environments');
export const NPMRC_FILENAME = '.npmrc';
export const DEFAULT_START_VERDACCIO_TARGET = 'build-env-verdaccio-start';
export const DEFAULT_BOOTSTRAP_TARGET = 'build-env-bootstrap';
export const DEFAULT_BOOTSTRAP_TARGET = 'build-env-env-bootstrap';
export const DEFAULT_INSTALL_TARGET = 'build-env-env-install';
export const DEFAULT_NPM_PUBLISH_TARGET = 'build-env-release-publish';
export const DEFAULT_NPM_INSTALL_TARGET = 'build-env-release-install';
Expand Down
5 changes: 2 additions & 3 deletions projects/build-env/src/plugin/build-env.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
DEFAULT_INSTALL_TARGET,
DEFAULT_SETUP_TARGET,
} from '../internal/constants';
import type { StarVerdaccioOptions } from '../executors/bootstrap/verdaccio-registry';
import type { StartVerdaccioOptions } from '../executors/bootstrap/verdaccio-registry';
import { VERDACCIO_REGISTRY_JSON } from '../executors/bootstrap/constants';
import { uniquePort } from '../executors/bootstrap/unique-port';

Expand Down Expand Up @@ -178,7 +178,7 @@ function verdaccioTargets(
NormalizedCreateNodeOptions['environments'],
'environmentsDir'
> &
StarVerdaccioOptions
StartVerdaccioOptions
): Record<string, TargetConfiguration> {
const { name: environmentProject } = projectConfig;
const { environmentsDir, ...verdaccioOptions } = options;
Expand Down Expand Up @@ -227,7 +227,6 @@ function getEnvTargets(
},
],
options: { environmentRoot },
command: 'echo Dependencies installed!',
},
// runs bootstrap-env, install-env and stop-verdaccio
[DEFAULT_SETUP_TARGET]: {
Expand Down
Loading

0 comments on commit 42ce76d

Please sign in to comment.