Skip to content

Commit

Permalink
fix: envRoot derivation in executor
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Oct 18, 2024
2 parents 1ee841f + f6b28e1 commit ae6b9aa
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 73 deletions.
6 changes: 6 additions & 0 deletions .verdaccio/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ uplinks:
maxage: 60m

packages:
# allow publishing local packages
'@push-based/*':
access: $all
publish: $all
unpublish: $all

'**':
# give all users (including non-authenticated users) full access
# because it is a local registry
Expand Down
2 changes: 1 addition & 1 deletion code-pushup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const taskTimeTasks: TaskTimeAuditOption[] = [
{task: 'models-e2e:e2e'},
{task: 'models-e2e:nxv-e2e'},
{task: 'nx-verdaccio-e2e:nxv-e2e'},
{task: 'cli-e2e-original:original-e2e'},
{task: 'cli-e2e-original:original-e2e', options: { exclude: ["nx-verdaccio"]}},
];
const cacheSizeTasks = [
'models-e2e:nxv-env-setup',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
"nxv-e2e": {
"configurations": {},
"dependsOn": [
"e2e",
{
"params": "forward",
"target": "e2e",
},
],
"executor": "@push-based/nx-verdaccio:env-teardown",
"options": {},
Expand All @@ -26,9 +29,7 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
"nxv-env-bootstrap": {
"configurations": {},
"executor": "@push-based/nx-verdaccio:env-bootstrap",
"options": {
"environmentRoot": "tmp/environments/lib-a-e2e",
},
"options": {},
"parallelism": true,
},
"nxv-env-install": {
Expand All @@ -40,8 +41,9 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
"target": "nxv-pkg-install",
},
],
"executor": "nx:noop",
"executor": "nx:run-commands",
"options": {
"command": "echo "dependencies installed for tmp/environments/lib-a-e2e"",
"environmentRoot": "tmp/environments/lib-a-e2e",
},
"parallelism": true,
Expand All @@ -65,9 +67,7 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
},
"^production",
],
"options": {
"environmentRoot": "tmp/environments/lib-a-e2e",
},
"options": {},
"outputs": [
"{options.environmentRoot}/.npmrc",
"{options.environmentRoot}/package.json",
Expand Down
24 changes: 14 additions & 10 deletions e2e/nx-verdaccio-e2e/test/plugin-create-nodes.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
const { code, projectJson } = await nxShowProjectJson(cwd, projectAE2e);
expect(code).toBe(0);

expect(projectJson.targets).toStrictEqual(
expect(projectJson.targets).toEqual(
expect.objectContaining({
e2e: expect.objectContaining({
dependsOn: [
Expand All @@ -199,9 +199,6 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
}),
[TARGET_ENVIRONMENT_BOOTSTRAP]: expect.objectContaining({
executor: '@push-based/nx-verdaccio:env-bootstrap',
options: {
environmentRoot: 'tmp/environments/lib-a-e2e',
},
}),
[TARGET_ENVIRONMENT_INSTALL]: expect.objectContaining({
dependsOn: [
Expand All @@ -211,15 +208,17 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
target: TARGET_PACKAGE_INSTALL,
},
],
executor: 'nx:noop',
options: { environmentRoot: 'tmp/environments/lib-a-e2e' },
executor: 'nx:run-commands',
options: {
environmentRoot: 'tmp/environments/lib-a-e2e',
command:
'echo "dependencies installed for tmp/environments/lib-a-e2e"',
},
}),
[TARGET_ENVIRONMENT_SETUP]: expect.objectContaining({
cache: true,
executor: '@push-based/nx-verdaccio:env-setup',
options: {
environmentRoot: 'tmp/environments/lib-a-e2e',
},
options: {},
inputs: [
'{projectRoot}/project.json',
{
Expand Down Expand Up @@ -259,7 +258,12 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
}),
[TARGET_ENVIRONMENT_E2E]: expect.objectContaining({
executor: '@push-based/nx-verdaccio:env-teardown',
dependsOn: ['e2e'],
dependsOn: [
{
params: 'forward',
target: 'e2e',
},
],
}),
[TARGET_ENVIRONMENT_TEARDOWN]: expect.objectContaining({
executor: '@push-based/nx-verdaccio:env-teardown',
Expand Down
6 changes: 5 additions & 1 deletion examples/e2e/cli-e2e-original/tooling/original.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ function verdaccioTargets(): Record<string, TargetConfiguration> {
function npmTargets(
projectConfiguration: ProjectConfiguration & { name: string }
): Record<string, TargetConfiguration> {
const { root } = projectConfiguration;
const { root, name, tags } = projectConfiguration;
const outputPath = getBuildOutputPathFromBuildTarget(projectConfiguration);

const { name: packageName, version: pkgVersion } = readJsonFile(
join(root, 'package.json')
);
//
if(!tags.some(i => i === 'type:example')) {
return {};
}
return {
'original-npm-publish': {
command: 'npm publish',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../../plugin/targets/environment.targets';
import { runSingleExecutor } from '../../internal/run-executor';
import { PACKAGE_NAME } from '../../plugin/constants';
import { getEnvironmentRoot } from '../../internal/environment-root';

export type BootstrapExecutorOutput = {
success: boolean;
Expand All @@ -26,7 +27,8 @@ export async function bootstrapExecutor(
context: ExecutorContext
): Promise<BootstrapExecutorOutput> {
const { configurationName, projectName } = context;
const { keepServerRunning, environmentRoot, verbose } = options;
const { keepServerRunning, verbose } = options;
const environmentRoot = getEnvironmentRoot(context, options);

if (verbose) {
logger.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import * as bootstrapExecutorModule from './bootstrap-env';
import { PACKAGE_NAME } from '../../plugin/constants';
import { TARGET_ENVIRONMENT_VERDACCIO_STOP } from '../../plugin/targets/environment.targets';
import { MockAsyncIterableIterator } from '@push-based/test-utils';
import { ExecutorContext } from '@nx/devkit';

describe('runBootstrapExecutor', () => {
const e2eProjectName = 'my-lib-e2e';
const e2eProjectsConfiguration = {
root: `e2e/${e2eProjectName}`,
};
const context = {
const context: ExecutorContext = {
cwd: 'test',
isVerbose: false,
root: 'tmp/environments/test',
Expand All @@ -22,6 +23,9 @@ describe('runBootstrapExecutor', () => {
[e2eProjectName]: e2eProjectsConfiguration,
},
},
nxJsonConfiguration: {
plugins: [],
},
};
const stopVerdaccioTask = {
project: e2eProjectName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
}
},
"additionalProperties": true,
"required": ["environmentRoot"]
"required": []
}
15 changes: 9 additions & 6 deletions projects/nx-verdaccio/src/executors/env-bootstrap/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type BootstrapExecutorOptions = Partial<{
keepServerRunning: boolean;
environmentRoot: string;
printConfig: boolean;
verbose: boolean;
}>;
import { Environment } from './npm';

export type BootstrapExecutorOptions = Partial<
{
keepServerRunning: boolean;
printConfig: boolean;
verbose: boolean;
} & Environment
>;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
VERDACCIO_REGISTRY_JSON,
} from './constants';
import { runSingleExecutor } from '../../internal/run-executor';
import { getEnvironmentRoot } from '../../internal/environment-root';

const VERDACCIO_TOKEN = 'Verdaccio: ';

Expand Down Expand Up @@ -179,8 +180,8 @@ export function stopVerdaccioServer(options: {
environmentRoot: string;
context: ExecutorContext;
}): Promise<void> {
const { projectName, verbose, context, configuration, environmentRoot } =
options;
const { projectName, verbose, context, configuration } = options;
const environmentRoot = getEnvironmentRoot(context, options);
return runSingleExecutor(
{
project: projectName,
Expand Down
9 changes: 7 additions & 2 deletions projects/nx-verdaccio/src/executors/env-setup/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from '../../plugin/targets/environment.targets';
import { runSingleExecutor } from '../../internal/run-executor';
import { rm } from 'node:fs/promises';
import { options } from 'yargs';
import { getEnvironmentRoot } from '../../internal/environment-root';

export type ExecutorOutput = {
success: boolean;
Expand All @@ -27,8 +29,11 @@ export default async function runSetupEnvironmentExecutor(
context: ExecutorContext
) {
const { configurationName: configuration, projectName } = context;
const { verbose, environmentRoot, keepServerRunning } =
terminalAndExecutorOptions;
const { verbose, keepServerRunning } = terminalAndExecutorOptions;
const environmentRoot = getEnvironmentRoot(
context,
terminalAndExecutorOptions
);
try {
await runSingleExecutor(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ describe('runSetupEnvironmentExecutor', () => {
};

await expect(
runSetupEnvironmentExecutor(
{
environmentRoot: 'tmp/environments/my-lib-e2e',
},
context
)
runSetupEnvironmentExecutor({}, context)
).resolves.toStrictEqual({
success: true,
command: 'Environment env-setup complete.',
Expand All @@ -108,7 +103,6 @@ describe('runSetupEnvironmentExecutor', () => {
target: TARGET_ENVIRONMENT_BOOTSTRAP,
},
{
environmentRoot: 'tmp/environments/my-lib-e2e',
keepServerRunning: true,
},
context
Expand Down
2 changes: 1 addition & 1 deletion projects/nx-verdaccio/src/executors/env-setup/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
}
},
"additionalProperties": true,
"required": ["environmentRoot"]
"required": []
}
15 changes: 9 additions & 6 deletions projects/nx-verdaccio/src/executors/env-setup/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type SetupEnvironmentExecutorOptions = Partial<{
environmentRoot: string;
keepServerRunning: boolean;
progress: boolean;
verbose: boolean;
}>;
import { Environment } from '../env-bootstrap/npm';

export type SetupEnvironmentExecutorOptions = Partial<
Environment & {
keepServerRunning: boolean;
progress: boolean;
verbose: boolean;
}
>;
16 changes: 2 additions & 14 deletions projects/nx-verdaccio/src/executors/env-teardown/teardown-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { VERDACCIO_REGISTRY_JSON } from '../env-bootstrap/constants';
import { fileExists } from '../../internal/file-system';
import { rm } from 'node:fs/promises';
import runKillProcessExecutor from '../kill-process/executor';
import { DEFAULT_ENVIRONMENTS_OUTPUT_DIR } from '../../plugin/constants';
import { type ExpandedPluginConfiguration } from 'nx/src/config/nx-json';
import type { NxVerdaccioCreateNodeOptions } from '../../plugin/schema';
import { getEnvironmentRoot } from '../../internal/environment-root';

export const gitClient: SimpleGit = simpleGit(process.cwd());
export type TeardownEnvironmentOptions = Environment & { verbose?: boolean };
Expand All @@ -19,17 +17,7 @@ export async function teardownEnvironment(
options: TeardownEnvironmentOptions,
git: SimpleGit = gitClient
): Promise<void> {
const { environmentRoot: optEnvironmentRoot } = options;
const plugin = context.nxJsonConfiguration.plugins.find((pCfg) => {
return (
typeof pCfg === 'object' && pCfg?.plugin === '@push-based/nx-verdaccio'
);
}) as ExpandedPluginConfiguration<NxVerdaccioCreateNodeOptions>;
const environmentsDir =
plugin.options.environments?.environmentsDir ??
DEFAULT_ENVIRONMENTS_OUTPUT_DIR;
const environmentRoot =
optEnvironmentRoot ?? join(environmentsDir, context.projectName);
const environmentRoot = getEnvironmentRoot(context, options);
// kill verdaccio process if running
const registryPath = join(environmentRoot, VERDACCIO_REGISTRY_JSON);
const registryJsonExists = await fileExists(registryPath);
Expand Down
27 changes: 27 additions & 0 deletions projects/nx-verdaccio/src/internal/environment-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { join } from 'node:path';
import { ExecutorContext } from '@nx/devkit';
import { ExpandedPluginConfiguration } from 'nx/src/config/nx-json';
import { NxVerdaccioCreateNodeOptions } from '../plugin/schema';
import { DEFAULT_ENVIRONMENTS_OUTPUT_DIR } from '../plugin/constants';
import { Environment } from '../executors/env-bootstrap/npm';

export function getEnvironmentDir(context: ExecutorContext) {
const plugin = context.nxJsonConfiguration?.plugins?.find((pCfg) => {
return (
typeof pCfg === 'object' && pCfg?.plugin === '@push-based/nx-verdaccio'
);
}) as ExpandedPluginConfiguration<NxVerdaccioCreateNodeOptions>;
return (
plugin?.options?.environments?.environmentsDir ??
DEFAULT_ENVIRONMENTS_OUTPUT_DIR
);
}

export function getEnvironmentRoot(
context: ExecutorContext,
options: Partial<Environment>
) {
const { environmentRoot: optEnvironmentRoot } = options;
const environmentsDir = getEnvironmentDir(context);
return optEnvironmentRoot ?? join(environmentsDir, context.projectName);
}
12 changes: 3 additions & 9 deletions projects/nx-verdaccio/src/plugin/targets/environment.targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ export function verdaccioTargets(
[TARGET_ENVIRONMENT_VERDACCIO_START]: {
// @TODO: consider using the executor function directly to reduce the number of targets
// https://github.com/nrwl/nx/blob/b73f1e0e0002c55fc0bacaa1557140adb9eec8de/packages/js/src/executors/verdaccio/verdaccio.impl.ts#L22
outputs: [
`{options.environmentRoot}/${VERDACCIO_STORAGE_DIR}`,
],
outputs: [`{options.environmentRoot}/${VERDACCIO_STORAGE_DIR}`],
executor: '@nx/js:verdaccio',
options: {
config: '.verdaccio/config.yml',
Expand Down Expand Up @@ -113,7 +111,6 @@ export function getEnvTargets(
return {
[TARGET_ENVIRONMENT_BOOTSTRAP]: {
executor: `${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_BOOTSTRAP}`,
options: { environmentRoot },
},
// intermediate task just here to execute dependent pkg-install tasks with the correct environmentProject
[TARGET_ENVIRONMENT_INSTALL]: {
Expand All @@ -126,7 +123,7 @@ export function getEnvTargets(
],
options: { environmentRoot },
// This is here to make it appear in the graph in older nx versions (otherwise it is filtered out)
command: `echo "dependencies installed for ${environmentRoot}"`
command: `echo "dependencies installed for ${environmentRoot}"`,
},
// runs env-bootstrap-env, install-env and stop-verdaccio
[TARGET_ENVIRONMENT_SETUP]: {
Expand Down Expand Up @@ -155,17 +152,14 @@ export function getEnvTargets(
],
cache: true,
executor: `${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_SETUP}`,
options: {
environmentRoot,
},
},
[TARGET_ENVIRONMENT_TEARDOWN]: {
executor: `${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_TEARDOWN}`,
},
[TARGET_ENVIRONMENT_E2E]: {
dependsOn: targetNames.map((targetName) => ({
target: targetName,
params: 'forward'
params: 'forward',
})),
executor: `${PACKAGE_NAME}:${EXECUTOR_ENVIRONMENT_TEARDOWN}`,
},
Expand Down
Loading

0 comments on commit ae6b9aa

Please sign in to comment.