From 2c7b139473d839e1258fe19d40560b806ffe9f1e Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 30 Sep 2024 23:33:40 +0200 Subject: [PATCH] fix: adjust packages.filterByTag --- .../plugin-create-nodes.e2e.test.ts.snap | 51 ++++++++-- .../test/plugin-create-nodes.e2e.test.ts | 98 ++++++++++++++----- examples/projects/cli/package.json | 2 +- examples/projects/core/package.json | 4 +- examples/projects/utils/package.json | 2 +- nx.json | 2 +- .../src/executors/pkg-install/executor.ts | 5 +- .../plugin/normalize-create-nodes-options.ts | 10 +- .../src/plugin/nx-verdaccio.plugin.ts | 1 - projects/nx-verdaccio/src/plugin/schema.ts | 8 +- .../src/plugin/targets/create-targets.ts | 1 + .../src/plugin/targets/environment.targets.ts | 4 +- .../src/plugin/targets/package.targets.ts | 11 ++- .../targets/package.targets.unit-test.ts | 60 ++++++++++++ testing/test-nx-utils/src/lib/utils/nx.ts | 14 ++- 15 files changed, 217 insertions(+), 56 deletions(-) create mode 100644 projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts diff --git a/e2e/nx-verdaccio-e2e/test/__snapshots__/plugin-create-nodes.e2e.test.ts.snap b/e2e/nx-verdaccio-e2e/test/__snapshots__/plugin-create-nodes.e2e.test.ts.snap index 691835ad..9d1c8fba 100644 --- a/e2e/nx-verdaccio-e2e/test/__snapshots__/plugin-create-nodes.e2e.test.ts.snap +++ b/e2e/nx-verdaccio-e2e/test/__snapshots__/plugin-create-nodes.e2e.test.ts.snap @@ -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, }, @@ -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, }, @@ -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", @@ -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, - "projectName": "my-lib-e2e", - "storage": "tmp/environments/my-lib-e2e/storage", + "projectName": "lib-a-e2e", + "storage": "tmp/environments/lib-a-e2e/storage", }, "parallelism": true, }, @@ -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": { diff --git a/e2e/nx-verdaccio-e2e/test/plugin-create-nodes.e2e.test.ts b/e2e/nx-verdaccio-e2e/test/plugin-create-nodes.e2e.test.ts index d973f8fb..266bcfc5 100644 --- a/e2e/nx-verdaccio-e2e/test/plugin-create-nodes.e2e.test.ts +++ b/e2e/nx-verdaccio-e2e/test/plugin-create-nodes.e2e.test.ts @@ -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', @@ -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({ @@ -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, { @@ -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({ @@ -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, { @@ -111,8 +162,8 @@ describe('nx-verdaccio plugin create-nodes-v2', () => { }, }, }); - updateProjectConfiguration(tree, projectE2e, { - root: e2eProjectRoot, + updateProjectConfiguration(tree, projectAE2e, { + root: e2eProjectARoot, projectType: 'application', targets: { e2e: {}, @@ -120,7 +171,7 @@ describe('nx-verdaccio plugin create-nodes-v2', () => { }); 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( @@ -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({ @@ -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', @@ -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({ @@ -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, { @@ -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({ diff --git a/examples/projects/cli/package.json b/examples/projects/cli/package.json index 50789de5..6b87c5bc 100644 --- a/examples/projects/cli/package.json +++ b/examples/projects/cli/package.json @@ -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" } diff --git a/examples/projects/core/package.json b/examples/projects/core/package.json index d6afe50e..72822950 100644 --- a/examples/projects/core/package.json +++ b/examples/projects/core/package.json @@ -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" } diff --git a/examples/projects/utils/package.json b/examples/projects/utils/package.json index 2672020e..78a32b75 100644 --- a/examples/projects/utils/package.json +++ b/examples/projects/utils/package.json @@ -2,6 +2,6 @@ "name": "@push-based/utils", "version": "0.0.1", "dependencies": { - "@push-based/models": "0.0.1" + "@push-based/models": "*" } } diff --git a/nx.json b/nx.json index d6eba320..39683190 100644 --- a/nx.json +++ b/nx.json @@ -29,7 +29,7 @@ "inputs": ["default", "^production"] }, "nxv-env-setup": { - "cache": false + "cache": true }, "nx-release-publish": {}, "lint": { diff --git a/projects/nx-verdaccio/src/executors/pkg-install/executor.ts b/projects/nx-verdaccio/src/executors/pkg-install/executor.ts index 04a86e79..2d68d000 100644 --- a/projects/nx-verdaccio/src/executors/pkg-install/executor.ts +++ b/projects/nx-verdaccio/src/executors/pkg-install/executor.ts @@ -24,8 +24,8 @@ export default async function runNpmInstallExecutor( const { name: packageName, version } = readJsonFile( 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}`); @@ -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, diff --git a/projects/nx-verdaccio/src/plugin/normalize-create-nodes-options.ts b/projects/nx-verdaccio/src/plugin/normalize-create-nodes-options.ts index 7ad7d17d..9b13cb99 100644 --- a/projects/nx-verdaccio/src/plugin/normalize-create-nodes-options.ts +++ b/projects/nx-verdaccio/src/plugin/normalize-create-nodes-options.ts @@ -1,7 +1,7 @@ import type { - BuildEnvEnvironmentsOptions, + NxVerdaccioEnvironmentsOptions, BuildEnvPluginCreateNodeOptions, - BuildEnvPackagesOptions, + NxVerdaccioPackagesOptions, } from './schema'; import { DEFAULT_ENVIRONMENTS_OUTPUT_DIR, @@ -10,13 +10,13 @@ import { export type NormalizedCreateNodeOptions = { environments: Omit< - BuildEnvEnvironmentsOptions, + NxVerdaccioEnvironmentsOptions, 'targetNames' | 'environmentsDir' > & Required< - Pick + Pick >; - packages: BuildEnvPackagesOptions; + packages: NxVerdaccioPackagesOptions; }; export function normalizeCreateNodesOptions( diff --git a/projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts b/projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts index 01cbfc17..a7c703b3 100644 --- a/projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts +++ b/projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts @@ -31,7 +31,6 @@ export const createNodesV2: CreateNodesV2 = [ PROJECT_JSON_FILE_GLOB, async (configFiles, options, context) => { const normalizedOptions = normalizeCreateNodesOptions(options); - const optionsHash = hashObject({ options: options ?? {} }); const nxVerdaccioEnvPluginCachePath = join( workspaceDataDirectory, diff --git a/projects/nx-verdaccio/src/plugin/schema.ts b/projects/nx-verdaccio/src/plugin/schema.ts index c940bb22..8a3b7a3f 100644 --- a/projects/nx-verdaccio/src/plugin/schema.ts +++ b/projects/nx-verdaccio/src/plugin/schema.ts @@ -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; }; diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.ts index 52e7a6d2..c43590fa 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.ts @@ -1,5 +1,6 @@ import type { BuildEnvPluginCreateNodeOptions } from '../schema'; import type { CreateNodesResult, ProjectConfiguration } from '@nx/devkit'; +import { logger } from '@nx/devkit'; import { normalizeCreateNodesOptions } from '../normalize-create-nodes-options'; import { getEnvTargets, diff --git a/projects/nx-verdaccio/src/plugin/targets/environment.targets.ts b/projects/nx-verdaccio/src/plugin/targets/environment.targets.ts index bcb964a5..b62318c9 100644 --- a/projects/nx-verdaccio/src/plugin/targets/environment.targets.ts +++ b/projects/nx-verdaccio/src/plugin/targets/environment.targets.ts @@ -2,7 +2,7 @@ import type { ProjectConfiguration, TargetConfiguration } from '@nx/devkit'; import type { NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; import { join } from 'node:path'; import { TARGET_PACKAGE_INSTALL } from './package.targets'; -import type { BuildEnvEnvironmentsOptions } from '../schema'; +import type { NxVerdaccioEnvironmentsOptions } from '../schema'; import type { StartVerdaccioOptions } from '../../executors/env-bootstrap/verdaccio-registry'; import { uniquePort } from '../../executors/env-bootstrap/unique-port'; import { @@ -130,7 +130,7 @@ export function getEnvTargets( export function updateEnvTargetNames( projectConfig: ProjectConfiguration, - options: Required> + options: Required> ): Record { const { targetNames: envTargetNames } = options; const { targets: existingTargets = {} as TargetConfiguration } = diff --git a/projects/nx-verdaccio/src/plugin/targets/package.targets.ts b/projects/nx-verdaccio/src/plugin/targets/package.targets.ts index 96128ef3..6afe8b64 100644 --- a/projects/nx-verdaccio/src/plugin/targets/package.targets.ts +++ b/projects/nx-verdaccio/src/plugin/targets/package.targets.ts @@ -12,14 +12,17 @@ export function isPkgProject( options: NormalizedCreateNodeOptions['packages'] ): boolean { const { projectType, tags: existingTags = [] } = projectConfig; - const { filterByTags: publishableTagFilters } = options; + const { filterByTags: publishableTagFilters = [] } = options; if (projectType !== 'library') { return false; } // if tags are configured check for at least one given tags - if (existingTags && publishableTagFilters) { - return existingTags.some((existingTag) => - publishableTagFilters.includes(existingTag) + if (publishableTagFilters.length > 0) { + return ( + existingTags.length > 0 && + existingTags.some((existingTag) => + publishableTagFilters.includes(existingTag) + ) ); } diff --git a/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts new file mode 100644 index 00000000..d9903da1 --- /dev/null +++ b/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts @@ -0,0 +1,60 @@ +import { describe, it, expect } from 'vitest'; +import { isPkgProject } from './package.targets'; + +describe('isPkgProject', () => { + it('should return true for projects with projectType is library', () => { + expect( + isPkgProject( + { + root: 'libs/my-lib', + name: 'my-lib', + projectType: 'library', + }, + {} + ) + ).toBe(true); + }); + it('should return true for projects with tags present in plugin options.filterByTags', () => { + expect( + isPkgProject( + { + root: 'libs/my-lib', + name: 'my-lib', + projectType: 'library', + tags: ['publish'], + }, + { + filterByTags: ['publish'], + } + ) + ).toBe(true); + }); + it('should return false for projects with no tags present in plugin options.filterByTags', () => { + expect( + isPkgProject( + { + root: 'libs/my-lib', + name: 'my-lib', + projectType: 'library', + tags: ['type:publish'], + }, + { + filterByTags: ['publish'], + } + ) + ).toBe(false); + }); + + it('should return false for projects with projectType is not library', () => { + expect( + isPkgProject( + { + root: 'apps/my-app', + name: 'my-app', + projectType: 'application', + }, + {} + ) + ).toBe(false); + }); +}); diff --git a/testing/test-nx-utils/src/lib/utils/nx.ts b/testing/test-nx-utils/src/lib/utils/nx.ts index 2bd9ff18..9013dbd4 100644 --- a/testing/test-nx-utils/src/lib/utils/nx.ts +++ b/testing/test-nx-utils/src/lib/utils/nx.ts @@ -104,8 +104,8 @@ export async function nxShowProjectJson( const { code, stderr, stdout } = await executeProcess({ command: 'NX_DAEMON=false NX_PROJECT_GRAPH=false npx', args: objectToCliArgs({ - _: ['nx', 'show', 'project', project], - verbose: true, + _: ['nx', 'show', 'project', project, '--skipNxCache'], + verbose: false, // debug errors json: true, prefix, userconfig, @@ -113,5 +113,13 @@ export async function nxShowProjectJson( cwd, }); - return { code, stderr, projectJson: JSON.parse(stdout) as T }; + try { + return { code, stderr, projectJson: JSON.parse(stdout) as T }; + } catch (error) { + throw new Error( + `Failed parsing show command result for string:\n${stdout}\n${ + (error as Error).message + }` + ); + } }