diff --git a/e2e/nx-verdaccio-e2e/setup/setup.ts b/e2e/nx-verdaccio-e2e/setup/setup.ts index 381a00a..d49cee4 100644 --- a/e2e/nx-verdaccio-e2e/setup/setup.ts +++ b/e2e/nx-verdaccio-e2e/setup/setup.ts @@ -5,24 +5,29 @@ import { objectToCliArgs, updateJson, } from '@push-based/test-utils'; -import {copyFile, lstat, mkdir, readdir} from 'fs/promises'; -import {join} from 'path'; -import {dirname, join} from 'node:path'; -import {copyFile, mkdir, symlink, readlink} from 'node:fs/promises'; -import {logger, NxJsonConfiguration, PluginConfiguration, TargetConfiguration,} from '@nx/devkit'; -import {PackageJson} from 'nx/src/utils/package-json'; +import { copyFile, lstat, mkdir, readdir } from 'fs/promises'; +import { join } from 'path'; +import { dirname, join } from 'node:path'; +import { copyFile, mkdir, symlink, readlink } from 'node:fs/promises'; +import { + logger, + NxJsonConfiguration, + PluginConfiguration, + TargetConfiguration, +} from '@nx/devkit'; +import { PackageJson } from 'nx/src/utils/package-json'; export async function setup({ - envRoot, - projectName, - repoName, - }: { + envRoot, + projectName, + repoName, +}: { envRoot: string; repoName: string; projectName: string; }) { // dedupe packages because symlink copy problems - await mkdir(envRoot, {recursive: true}); + await mkdir(envRoot, { recursive: true }); // setup nx environment for e2e tests logger.info(`Created nx workspace under ${envRoot}`); await executeProcess({ @@ -113,7 +118,7 @@ export async function setup({ DEFAULT_TEST_FIXTURE_DIST, repoName ), - {recursive: true} + { recursive: true } ); await copyFile( join(getTestEnvironmentRoot(projectName), '.npmrc'), @@ -123,16 +128,17 @@ export async function setup({ await executeProcess({ command: 'npm', args: objectToCliArgs({ - _: ['dedupe'] + _: ['dedupe'], }), verbose: true, cwd: dirname(envRoot), }); - - } -export async function registerNxVerdaccioPlugin(envRoot: string, options?: PluginConfiguration) { +export async function registerNxVerdaccioPlugin( + envRoot: string, + options?: PluginConfiguration +) { logger.info(`register nx-verdaccio plugin`); await updateJson(join(envRoot, 'nx.json'), (json) => registerPluginInNxJson(json, { @@ -142,7 +148,7 @@ export async function registerNxVerdaccioPlugin(envRoot: string, options?: Plugi targetNames: ['e2e'], }, }, - ...options + ...options, }) ); } @@ -173,17 +179,20 @@ function updatePackageJsonNxTargets( }; } - /** * This function avoids issues with symlinks and other edge cases. * */ -export async function copyDirectory(src: string, dest: string, exclude: string[] = []) { +export async function copyDirectory( + src: string, + dest: string, + exclude: string[] = [] +) { // Ensure the destination directory exists - await mkdir(dest, {recursive: true}); + await mkdir(dest, { recursive: true }); // Read the contents of the source directory - const entries = await readdir(src, {withFileTypes: true}); + const entries = await readdir(src, { withFileTypes: true }); for (const entry of entries) { const srcPath = join(src, entry.name); 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 98d457a..249b528 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 @@ -1,39 +1,41 @@ -import type {Tree} from '@nx/devkit'; -import {join} from 'node:path'; -import {afterEach, beforeAll, expect} from 'vitest'; -import {nxShowProjectJson,} from '@push-based/test-nx-utils'; -import {copyDirectory, registerNxVerdaccioPlugin} from '../setup/setup'; -import {mkdir} from 'node:fs/promises'; -import {TARGET_PACKAGE_INSTALL, TARGET_PACKAGE_PUBLISH,} from '@push-based/nx-verdaccio'; -import {DEFAULT_TEST_FIXTURE_DIST, getTestEnvironmentRoot} from '@push-based/test-utils'; +import type { Tree } from '@nx/devkit'; +import { join } from 'node:path'; +import { afterEach, beforeAll, expect } from 'vitest'; +import { nxShowProjectJson } from '@push-based/test-nx-utils'; +import { copyDirectory, registerNxVerdaccioPlugin } from '../setup/setup'; +import { mkdir } from 'node:fs/promises'; +import { + TARGET_PACKAGE_INSTALL, + TARGET_PACKAGE_PUBLISH, +} from '@push-based/nx-verdaccio'; +import { + DEFAULT_TEST_FIXTURE_DIST, + getTestEnvironmentRoot, +} from '@push-based/test-utils'; // eslint-disable-next-line @nx/enforce-module-boundaries -import {REPO_NAME} from "../fixtures/basic-nx-workspace"; +import { REPO_NAME } from '../fixtures/basic-nx-workspace'; describe('in a fresh Nx workspace', () => { - - const projectName = process.env['NX_TASK_TARGET_PROJECT']; const envRoot = getTestEnvironmentRoot(projectName); const basicNxReopPath = join(envRoot, DEFAULT_TEST_FIXTURE_DIST, REPO_NAME); const baseDir = join(envRoot, DEFAULT_TEST_FIXTURE_DIST, 'create-nodes-v2'); beforeAll(async () => { - await mkdir(baseDir, {recursive: true}); + await mkdir(baseDir, { recursive: true }); await copyDirectory(basicNxReopPath, baseDir, []); - }) + }); afterEach(async () => { // await teardownTestFolder(baseDir); }); describe('with nx-verdaccio plugin installed', () => { - beforeAll(async () => { await registerNxVerdaccioPlugin(baseDir); - }) + }); it('should add package targets to library project', async () => { - - const {code, projectJson} = await nxShowProjectJson(baseDir, 'pkg'); + const { code, projectJson } = await nxShowProjectJson(baseDir, 'pkg'); expect(code).toBe(0); expect(projectJson.targets).toStrictEqual({ @@ -69,7 +71,7 @@ describe('in a fresh Nx workspace', () => { expect(projectJson.targets).toMatchSnapshot(); }); -/* + /* it('should NOT add package targets to application project', async () => { const cwd = join(baseDir, 'no-pkg-targets'); registerPluginInWorkspace(tree, { @@ -276,7 +278,5 @@ describe('in a fresh Nx workspace', () => { ); }); */ - }); - }); diff --git a/projects/nx-verdaccio/package.json b/projects/nx-verdaccio/package.json index 4172ae8..f63fd94 100644 --- a/projects/nx-verdaccio/package.json +++ b/projects/nx-verdaccio/package.json @@ -21,11 +21,15 @@ "sourceRoot": "packages/pkg/src", "projectType": "library", "name": "nx-verdaccio", - "tags": ["publishable"], + "tags": [ + "publishable" + ], "targets": { "build": { "executor": "@nx/js:tsc", - "outputs": ["{options.outputPath}"], + "outputs": [ + "{options.outputPath}" + ], "options": { "outputPath": "dist/nx-verdaccio", "main": "src/index.ts", diff --git a/projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts b/projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts index 6a80f3e..ab4b592 100644 --- a/projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts +++ b/projects/nx-verdaccio/src/plugin/nx-verdaccio.plugin.ts @@ -7,15 +7,27 @@ import { type ProjectConfiguration, readJsonFile, } from '@nx/devkit'; -import {dirname, join} from 'node:path'; -import type {NxVerdaccioCreateNodeOptions} from './schema'; -import {normalizeCreateNodesOptions, type NormalizedCreateNodeOptions,} from './normalize-create-nodes-options'; -import {hashObject} from 'nx/src/hasher/file-hasher'; -import {workspaceDataDirectory} from 'nx/src/utils/cache-directory'; -import {PLUGIN_NAME} from './constants'; -import {getCacheRecord, readTargetsCache, setCacheRecord, writeTargetsToCache,} from './caching'; -import {createProjectConfiguration} from './targets/create-targets'; -import {getPackageJsonNxConfig, getProjectConfig, getProjectJsonNxConfig} from "./project-config"; +import { dirname, join } from 'node:path'; +import type { NxVerdaccioCreateNodeOptions } from './schema'; +import { + normalizeCreateNodesOptions, + type NormalizedCreateNodeOptions, +} from './normalize-create-nodes-options'; +import { hashObject } from 'nx/src/hasher/file-hasher'; +import { workspaceDataDirectory } from 'nx/src/utils/cache-directory'; +import { PLUGIN_NAME } from './constants'; +import { + getCacheRecord, + readTargetsCache, + setCacheRecord, + writeTargetsToCache, +} from './caching'; +import { createProjectConfiguration } from './targets/create-targets'; +import { + getPackageJsonNxConfig, + getProjectConfig, + getProjectJsonNxConfig, +} from './project-config'; const PROJECT_JSON_FILE_GLOB = '**/project.json'; const PACKAGE_JSON_FILE_GLOB = '**/package.json'; @@ -24,7 +36,7 @@ const FILE_GLOB = `**/{project,package}.json`; export const createNodesV2: CreateNodesV2 = [ FILE_GLOB, async (configFiles, options, context) => { - const optionsHash = hashObject({options: options ?? {}}); + const optionsHash = hashObject({ options: options ?? {} }); const nxVerdaccioEnvPluginCachePath = join( workspaceDataDirectory, `push-based--${PLUGIN_NAME}-${optionsHash}.hash` @@ -34,12 +46,19 @@ export const createNodesV2: CreateNodesV2 = [ return await createNodesFromFiles( async (projectConfigurationFile, internalOptions) => { const isPkgJson = projectConfigurationFile.endsWith('package.json'); - if(isPkgJson) { - throw new Error('!!!!!!!!!!!!!!!!!!') + if (isPkgJson) { + throw new Error('!!!!!!!!!!!!!!!!!!'); } - const [primaryConfig, fallback] = isPkgJson ? [getPackageJsonNxConfig, getProjectJsonNxConfig] : [getProjectJsonNxConfig, getPackageJsonNxConfig]; - const projectConfiguration: ProjectConfiguration = await getProjectConfig(projectConfigurationFile, primaryConfig, fallback); + const [primaryConfig, fallback] = isPkgJson + ? [getPackageJsonNxConfig, getProjectJsonNxConfig] + : [getProjectJsonNxConfig, getPackageJsonNxConfig]; + const projectConfiguration: ProjectConfiguration = + await getProjectConfig( + projectConfigurationFile, + primaryConfig, + fallback + ); if ( !('name' in projectConfiguration) || typeof projectConfiguration.name !== 'string' @@ -71,7 +90,7 @@ export const createNodesV2: CreateNodesV2 = [ ); } } - const {targets, namedInputs = {}} = cachedProjectConfiguration; + const { targets, namedInputs = {} } = cachedProjectConfiguration; return { projects: { [projectRoot]: { diff --git a/projects/nx-verdaccio/src/plugin/project-config.ts b/projects/nx-verdaccio/src/plugin/project-config.ts index 2e58dd3..bb506d5 100644 --- a/projects/nx-verdaccio/src/plugin/project-config.ts +++ b/projects/nx-verdaccio/src/plugin/project-config.ts @@ -1,6 +1,6 @@ -import type {ProjectConfiguration} from '@nx/devkit'; -import {readFile} from 'node:fs/promises'; -import {dirname, join} from 'node:path'; +import type { ProjectConfiguration } from '@nx/devkit'; +import { readFile } from 'node:fs/promises'; +import { dirname, join } from 'node:path'; export async function getProjectJsonNxConfig(packageJsonFile: string) { const projectConfig: ProjectConfiguration = await readFile( @@ -14,20 +14,22 @@ export async function getProjectJsonNxConfig(packageJsonFile: string) { } export async function getPackageJsonNxConfig(packageJsonFile: string) { - const {nx: pkgNxConfig = {}} = await readFile( + const { nx: pkgNxConfig = {} } = await readFile( join(process.cwd(), packageJsonFile), 'utf8' ).then(JSON.parse); return pkgNxConfig; } -export async function getProjectConfig(projectConfigFile, getConfig:(projectConfigFile: string) => Promise, fallback: (projectConfigFile: string) => Promise) { - +export async function getProjectConfig( + projectConfigFile, + getConfig: (projectConfigFile: string) => Promise, + fallback: (projectConfigFile: string) => Promise +) { const pkgNxConfig = await getConfig(projectConfigFile); if (!('name' in pkgNxConfig) || typeof pkgNxConfig?.name !== 'string') { return await fallback(projectConfigFile); } return pkgNxConfig; - }