Skip to content

Commit

Permalink
fix: adjust packages.filterByTag
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 30, 2024
1 parent c8ef956 commit 2c7b139
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
"configurations": {},
"executor": "@push-based/nx-verdaccio:env-bootstrap",
"options": {
"environmentRoot": "tmp/environments/my-lib-e2e",
"environmentRoot": "tmp/environments/lib-a-e2e",
},
"parallelism": true,
},
Expand All @@ -33,7 +33,7 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
],
"executor": "nx:noop",
"options": {
"environmentRoot": "tmp/environments/my-lib-e2e",
"environmentRoot": "tmp/environments/lib-a-e2e",
},
"parallelism": true,
},
Expand All @@ -42,7 +42,7 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
"configurations": {},
"executor": "@push-based/nx-verdaccio:env-setup",
"options": {
"environmentRoot": "tmp/environments/my-lib-e2e",
"environmentRoot": "tmp/environments/lib-a-e2e",
},
"outputs": [
"{options.environmentRoot}/node_modules",
Expand All @@ -58,10 +58,10 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
"options": {
"clear": true,
"config": ".verdaccio/config.yml",
"environmentDir": "tmp/environments/my-lib-e2e",
"environmentDir": "tmp/environments/lib-a-e2e",
"port": Any<Number>,
"projectName": "my-lib-e2e",
"storage": "tmp/environments/my-lib-e2e/storage",
"projectName": "lib-a-e2e",
"storage": "tmp/environments/lib-a-e2e/storage",
},
"parallelism": true,
},
Expand All @@ -76,6 +76,45 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add environment targets to
}
`;
exports[`nx-verdaccio plugin create-nodes-v2 > should add package targets to library project 1`] = `
{
"nxv-pkg-install": {
"configurations": {},
"dependsOn": [
{
"params": "forward",
"target": "nxv-pkg-publish",
},
{
"params": "forward",
"projects": "dependencies",
"target": "nxv-pkg-install",
},
],
"executor": "@push-based/nx-verdaccio:pkg-install",
"options": {},
"parallelism": true,
},
"nxv-pkg-publish": {
"configurations": {},
"dependsOn": [
{
"params": "forward",
"target": "build",
},
{
"params": "forward",
"projects": "dependencies",
"target": "nxv-pkg-publish",
},
],
"executor": "@push-based/nx-verdaccio:pkg-publish",
"options": {},
"parallelism": true,
},
}
`;
exports[`nx-verdaccio plugin create-nodes-v2 > should add package targets to library project dynamically 1`] = `
{
"nxv-pkg-install": {
Expand Down
98 changes: 75 additions & 23 deletions e2e/nx-verdaccio-e2e/test/plugin-create-nodes.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@ import {
registerPluginInWorkspace,
} from '@push-based/test-nx-utils';
import { updateProjectConfiguration } from 'nx/src/generators/utils/project-configuration';
import { teardownTestFolder } from '@push-based/test-utils';

describe('nx-verdaccio plugin create-nodes-v2', () => {
const e2eProjectName = process.env['NX_TASK_TARGET_PROJECT'];
let tree: Tree;
const project = 'my-lib';
const projectE2e = `${project}-e2e`;
const e2eProjectRoot = join('projects', projectE2e);
const baseDir = `tmp/environments/${e2eProjectName}/__test__/create-nodes-v2`;
const projectA = 'lib-a';
const projectB = 'lib-b';
const projectAE2e = `${projectA}-e2e`;
const e2eProjectARoot = join('projects', projectAE2e);
const baseDir = `tmp/environments/${process.env['NX_TASK_TARGET_PROJECT']}/__test__/create-nodes-v2`;

beforeEach(async () => {
tree = await addJsLibToWorkspace(project);
tree = await addJsLibToWorkspace(projectE2e, tree);
updateProjectConfiguration(tree, projectE2e, {
root: e2eProjectRoot,
tree = await addJsLibToWorkspace(projectA);
await addJsLibToWorkspace(projectB, tree);
await addJsLibToWorkspace(projectAE2e, tree);
updateProjectConfiguration(tree, projectAE2e, {
root: e2eProjectARoot,
projectType: 'application',
});
});

afterEach(async () => {
// await teardownTestFolder(baseDir);
// await teardownTestFolder(baseDir);
});

it('should add package targets to library project dynamically', async () => {
it('should add package targets to library project', async () => {
const cwd = join(baseDir, 'add-pkg-targets');
registerPluginInWorkspace(tree, {
plugin: '@push-based/nx-verdaccio',
Expand All @@ -43,7 +45,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
});
await materializeTree(tree, cwd);

const { code, projectJson } = await nxShowProjectJson(cwd, project);
const { code, projectJson } = await nxShowProjectJson(cwd, projectA);
expect(code).toBe(0);

expect(projectJson.targets).toStrictEqual({
Expand Down Expand Up @@ -79,6 +81,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {

expect(projectJson.targets).toMatchSnapshot();
});

it('should NOT add package targets to application project', async () => {
const cwd = join(baseDir, 'no-pkg-targets');
registerPluginInWorkspace(tree, {
Expand All @@ -91,7 +94,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
});
await materializeTree(tree, cwd);

const { projectJson } = await nxShowProjectJson(cwd, projectE2e);
const { projectJson } = await nxShowProjectJson(cwd, projectAE2e);

expect(projectJson.targets).toStrictEqual(
expect.not.objectContaining({
Expand All @@ -101,6 +104,54 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
);
});

it('should add package targets to library project if some tag of options.packages.filterByTag match', async () => {
const cwd = join(baseDir, 'add-pkg-targets-filterByTag');
registerPluginInWorkspace(tree, {
plugin: '@push-based/nx-verdaccio',
options: {
environments: {
targetNames: ['e2e'],
},
packages: {
filterByTags: ['publish'],
},
},
});
updateProjectConfiguration(tree, projectB, {
root: `projects/${projectB}`,
sourceRoot: 'projects/lib-b/src',
projectType: 'library',
tags: ['publish'],
});
await materializeTree(tree, cwd);

const { projectJson: projectJsonB } = await nxShowProjectJson(
cwd,
projectB
);
expect(projectJsonB.name).toBe(projectB);
expect(projectJsonB.tags).toStrictEqual(['publish']);
expect(projectJsonB.targets).toStrictEqual(
expect.objectContaining({
'nxv-pkg-install': expect.any(Object),
'nxv-pkg-publish': expect.any(Object),
})
);

const { projectJson: projectJsonA } = await nxShowProjectJson(
cwd,
projectA
);
expect(projectJsonA.name).toBe(projectA);
expect(projectJsonA.tags).toBe([]);
expect(projectJsonA.targets).not.toStrictEqual(
expect.not.objectContaining({
'nxv-pkg-install': expect.any(Object),
'nxv-pkg-publish': expect.any(Object),
})
);
});

it('should add environment targets to project with targetName e2e dynamically', async () => {
const cwd = join(baseDir, 'add-env-targets');
registerPluginInWorkspace(tree, {
Expand All @@ -111,16 +162,16 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
},
},
});
updateProjectConfiguration(tree, projectE2e, {
root: e2eProjectRoot,
updateProjectConfiguration(tree, projectAE2e, {
root: e2eProjectARoot,
projectType: 'application',
targets: {
e2e: {},
},
});
await materializeTree(tree, cwd);

const { code, projectJson } = await nxShowProjectJson(cwd, projectE2e);
const { code, projectJson } = await nxShowProjectJson(cwd, projectAE2e);
expect(code).toBe(0);

expect(projectJson.targets).toStrictEqual(
Expand All @@ -136,7 +187,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
'nxv-env-bootstrap': expect.objectContaining({
executor: '@push-based/nx-verdaccio:env-bootstrap',
options: {
environmentRoot: 'tmp/environments/my-lib-e2e',
environmentRoot: 'tmp/environments/lib-a-e2e',
},
}),
'nxv-env-install': expect.objectContaining({
Expand All @@ -148,13 +199,13 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
},
],
executor: 'nx:noop',
options: { environmentRoot: 'tmp/environments/my-lib-e2e' },
options: { environmentRoot: 'tmp/environments/lib-a-e2e' },
}),
'nxv-env-setup': expect.objectContaining({
cache: false,
executor: '@push-based/nx-verdaccio:env-setup',
options: {
environmentRoot: 'tmp/environments/my-lib-e2e',
environmentRoot: 'tmp/environments/lib-a-e2e',
},
outputs: [
'{options.environmentRoot}/node_modules',
Expand All @@ -168,10 +219,10 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
options: expect.objectContaining({
clear: true,
config: '.verdaccio/config.yml',
environmentDir: 'tmp/environments/my-lib-e2e',
environmentDir: 'tmp/environments/lib-a-e2e',
port: expect.any(Number), // random port number
projectName: 'my-lib-e2e',
storage: 'tmp/environments/my-lib-e2e/storage',
projectName: 'lib-a-e2e',
storage: 'tmp/environments/lib-a-e2e/storage',
}),
}),
'nxv-env-verdaccio-stop': expect.objectContaining({
Expand All @@ -194,6 +245,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
},
}).toMatchSnapshot();
});

it('should NOT add environment targets to project without targetName e2e', async () => {
const cwd = join(baseDir, 'no-env-targets');
registerPluginInWorkspace(tree, {
Expand All @@ -206,7 +258,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => {
});
await materializeTree(tree, cwd);

const { projectJson } = await nxShowProjectJson(cwd, projectE2e);
const { projectJson } = await nxShowProjectJson(cwd, projectAE2e);

expect(projectJson.targets).toStrictEqual(
expect.not.objectContaining({
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"dependencies": {
"yargs": "^17.7.2",
"@push-based/core": "0.0.1"
"@push-based/core": "*"
},
"bin": "bin.js"
}
4 changes: 2 additions & 2 deletions examples/projects/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "@push-based/core",
"version": "0.0.1",
"dependencies": {
"@push-based/utils": "0.0.1",
"@push-based/models": "0.0.1",
"@push-based/utils": "*",
"@push-based/models": "*",
"vite": "~5.0.0",
"@nx/vite": "19.8.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "@push-based/utils",
"version": "0.0.1",
"dependencies": {
"@push-based/models": "0.0.1"
"@push-based/models": "*"
}
}
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"inputs": ["default", "^production"]
},
"nxv-env-setup": {
"cache": false
"cache": true
},
"nx-release-publish": {},
"lint": {
Expand Down
5 changes: 2 additions & 3 deletions projects/nx-verdaccio/src/executors/pkg-install/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export default async function runNpmInstallExecutor(
const { name: packageName, version } = readJsonFile<PackageJson>(
join(packageDistPath, 'package.json')
);
const { pkgVersion = version, environmentRoot } = options;
const packageNameAndVersion = `${packageName}@${pkgVersion}`;
const { environmentRoot } = options;
const packageNameAndVersion = `${packageName}@${version}`;

logger.info(`Installing ${packageNameAndVersion} in ${environmentRoot}`);

Expand All @@ -35,7 +35,6 @@ export default async function runNpmInstallExecutor(
_: ['install', `${packageNameAndVersion}`],
fund: false, // avoid polluted terminal
shrinkwrap: false, // avoid package-lock creation or update
// packageLockOnly: true, // avoid package-lock creation or update
save: true, // save to package.json dependencies
}),
cwd: environmentRoot,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
BuildEnvEnvironmentsOptions,
NxVerdaccioEnvironmentsOptions,
BuildEnvPluginCreateNodeOptions,
BuildEnvPackagesOptions,
NxVerdaccioPackagesOptions,
} from './schema';
import {
DEFAULT_ENVIRONMENTS_OUTPUT_DIR,
Expand All @@ -10,13 +10,13 @@ import {

export type NormalizedCreateNodeOptions = {
environments: Omit<
BuildEnvEnvironmentsOptions,
NxVerdaccioEnvironmentsOptions,
'targetNames' | 'environmentsDir'
> &
Required<
Pick<BuildEnvEnvironmentsOptions, 'targetNames' | 'environmentsDir'>
Pick<NxVerdaccioEnvironmentsOptions, 'targetNames' | 'environmentsDir'>
>;
packages: BuildEnvPackagesOptions;
packages: NxVerdaccioPackagesOptions;
};

export function normalizeCreateNodesOptions(
Expand Down
1 change: 0 additions & 1 deletion projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const createNodesV2: CreateNodesV2<BuildEnvPluginCreateNodeOptions> = [
PROJECT_JSON_FILE_GLOB,
async (configFiles, options, context) => {
const normalizedOptions = normalizeCreateNodesOptions(options);

const optionsHash = hashObject({ options: options ?? {} });
const nxVerdaccioEnvPluginCachePath = join(
workspaceDataDirectory,
Expand Down
8 changes: 4 additions & 4 deletions projects/nx-verdaccio/src/plugin/schema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export type BuildEnvEnvironmentsOptions = {
export type NxVerdaccioEnvironmentsOptions = {
environmentsDir?: string;
targetNames?: string[];
filterByTags?: string[];
};
export type BuildEnvPackagesOptions = {
export type NxVerdaccioPackagesOptions = {
environmentsDir?: string;
targetNames?: string[];
filterByTags?: string[];
};
export type BuildEnvPluginCreateNodeOptions = {
environments?: BuildEnvEnvironmentsOptions;
packages?: BuildEnvPackagesOptions;
environments?: NxVerdaccioEnvironmentsOptions;
packages?: NxVerdaccioPackagesOptions;
};
Loading

0 comments on commit 2c7b139

Please sign in to comment.