Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjust file matcher to work with configuration less setup #70

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion e2e/nx-verdaccio-e2e/fixtures/basic-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import { join } from 'node:path';

export const REPO_NAME = 'nx-ts-repo';
export const envRoot = `tmp/environments/${process.env['NX_TASK_TARGET_PROJECT']}`;
export const workspaceRoot = join(envRoot, '__test__', REPO_NAME);
export const projectName = 'pkg';
export const e2eProjectName = 'pkg-e2e';
8 changes: 7 additions & 1 deletion e2e/nx-verdaccio-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
"sourceRoot": "projects/nx-verdaccio-e2e/test",
"projectType": "application",
"tags": ["type:e2e", "type:e2e-vi", "npm-env"],
"implicitDependencies": ["nx-verdaccio"],
"implicitDependencies": ["@push-based/nx-verdaccio"],
"targets": {
"lint": {},
"nxv-env-setup": {
"cache": false,
"options": {
"skipInstall": true
}
},
"e2e": {
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
Expand Down
221 changes: 149 additions & 72 deletions e2e/nx-verdaccio-e2e/setup/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,45 @@ import {
objectToCliArgs,
updateJson,
} from '@push-based/test-utils';
import { dirname, join } from 'node:path';
import { copyFile, mkdir } from 'node:fs/promises';
import {copyFile, lstat, mkdir, rm, readdir} from 'fs/promises';
import {join} from 'path';
import {dirname, join} from 'node:path';
import {copyFile, writeFile, mkdir, symlink, readlink} from 'node:fs/promises';
import {
logger,
NxJsonConfiguration,
PluginConfiguration,
PluginConfiguration, ProjectConfiguration,
TargetConfiguration,
} from '@nx/devkit';
import { PackageJson } from 'nx/src/utils/package-json';
import {PackageJson} from 'nx/src/utils/package-json';
import {writeFileDefaults} from "memfs/lib/node/options";
import {readFile} from "@nx/plugin/testing";
import {REPO_NAME} from "../fixtures/basic-nx-workspace";

export async function setup({
envRoot,
projectName,
repoName,
}: {
envRoot,
projectName,
repoName,
}: {
envRoot: string;
repoName: string;
projectName: string;
}) {
await mkdir(envRoot, { recursive: true });
// dedupe packages because symlink copy problems
await mkdir(envRoot, {recursive: true});

await copyFile(
join(getTestEnvironmentRoot(projectName), '.npmrc'),
join(envRoot, '.npmrc')
);

// setup nx environment for e2e tests
logger.info(`Created nx workspace under ${envRoot}`);
await executeProcess({
command: 'npx',
args: objectToCliArgs({
_: ['--yes', '--quiet', 'create-nx-workspace'],
name: repoName,
_: ['--yes', '--quiet', 'create-nx-workspace@18'],
name: `${repoName}-18`,
preset: 'ts',
ci: 'skip',
e2eTestRunner: 'none',
Expand All @@ -41,89 +53,89 @@ export async function setup({
cwd: dirname(envRoot),
});

logger.info(`Add project & target`);
await executeProcess({
command: 'nx',
args: objectToCliArgs({
_: ['generate', '@nx/js:library', 'pkg'],
directory: 'packages/pkg',
bundler: 'tsc',
unitTestRunner: 'none',
linter: 'none',
interactive: false,
}),
verbose: true,
cwd: envRoot,
});
await updateJson<PackageJson>(
join(envRoot, 'packages', 'pkg', 'package.json'),
(json) => ({
...json,
nx: {
...json?.nx,
logger.info(`Add project & target`);
await executeProcess({
command: 'nx',
args: objectToCliArgs({
_: ['generate', '@nx/js:library', 'pkg'],
name: `pkg`,
directory: 'packages/pkg',
bundler: 'tsc',
unitTestRunner: 'none',
linter: 'none',
interactive: false,
}),
verbose: true,
cwd: envRoot,
});

logger.info(`Add e2e project & target`);
await executeProcess({
command: 'nx',
args: objectToCliArgs({
_: ['generate', '@nx/js:library', 'pkg-e2e'],
name: `pkg-e2e`,
directory: 'packages/pkg-e2e',
bundler: 'tsc',
unitTestRunner: 'none',
linter: 'none',
interactive: false,
}),
verbose: true,
cwd: envRoot,
});
/*
await updateJson<ProjectConfiguration>(
join(envRoot, 'packages', 'pkg-e2e', 'project.json'),
(json) => ({
...json,
name: 'pkg-e2e',
root: join(envRoot, 'packages', 'pkg-e2e'),
projectType: 'application',
targets: {
...json?.nx?.targets,
build: {
options: {
outputPath: ['dist/pkg'],
},
command: 'echo "lib"',
...json?.targets,
e2e: {
command: 'echo "e2e"',
},
},
},
})
);
}
})
);
*/
logger.info(`Install @push-based/nx-verdaccio`);

logger.info(`Add e2e project & target`);
await executeProcess({
command: 'nx',
command: 'npm',
args: objectToCliArgs({
_: ['generate', '@nx/js:library', 'pkg-e2e'],
directory: 'packages/pkg-e2e',
bundler: 'tsc',
unitTestRunner: 'none',
linter: 'none',
interactive: false,
_: ['install', '@push-based/nx-verdaccio'],
save: true,
}),
verbose: true,
cwd: envRoot,
verbose: true,
});
await updateJson<PackageJson>(
join(envRoot, 'packages', 'pkg-e2e', 'package.json'),
(json) =>
updatePackageJsonNxTargets(json, {
...json?.nx?.targets,
e2e: {
command: 'echo "e2e"',
},
})
);

logger.info(`Install @push-based/nx-verdaccio`);
await mkdir(
join(
getTestEnvironmentRoot(projectName),
DEFAULT_TEST_FIXTURE_DIST,
repoName
),
{ recursive: true }
);
await copyFile(
join(getTestEnvironmentRoot(projectName), '.npmrc'),
join(envRoot, '.npmrc')
{recursive: true}
);


await executeProcess({
command: 'npm',
args: objectToCliArgs({
_: ['install', '@push-based/nx-verdaccio'],
save: true,
_: ['dedupe'],
}),
cwd: envRoot,
verbose: true,
cwd: dirname(envRoot),
});
}

export async function registerNxVerdaccioPlugin(envRoot: string) {
export async function registerNxVerdaccioPlugin(
envRoot: string,
options?: PluginConfiguration
) {
logger.info(`register nx-verdaccio plugin`);
await updateJson<NxJsonConfiguration>(join(envRoot, 'nx.json'), (json) =>
registerPluginInNxJson(json, {
Expand All @@ -133,11 +145,35 @@ export async function registerNxVerdaccioPlugin(envRoot: string) {
targetNames: ['e2e'],
},
},
...options,
})
);
}

function registerPluginInNxJson(

async function updatePackageJsonTargets(
targetFile: string,
projectConfig: ProjectConfiguration
) {
let json = undefined;
try {
const j = await readFile(targetFile);
json = JSON.parse(j.toString());
} catch (e) {
json = {}
}
await writeFile(targetFile, JSON.stringify({
...json,
...projectConfig,
targets: {
...json?.targets,
...projectConfig.targets,
},
}, null, 2));
}


export function registerPluginInNxJson(
nxJson: NxJsonConfiguration,
pluginConfig: PluginConfiguration
) {
Expand All @@ -162,3 +198,44 @@ function updatePackageJsonNxTargets(
},
};
}

/**
* This function avoids issues with symlinks and other edge cases.
*
*/
export async function copyDirectory(
src: string,
dest: string,
{exclude = [], cleanup}?: { exclude?: string[], cleanup?: boolean } = {exclude: []}
) {
if (cleanup) {
await rm(dest, {recursive: true});
}
// Ensure the destination directory exists
await mkdir(dest, {recursive: true});

// Read the contents of the source directory
const entries = await readdir(src, {withFileTypes: true});

for (const entry of entries) {
const srcPath = join(src, entry.name);
const destPath = join(dest, entry.name);

// Skip excluded directories or files
if (exclude.includes(entry.name)) continue;

const stats = await lstat(srcPath);

if (stats.isSymbolicLink()) {
// Handle symbolic links if necessary (copy the link itself or resolve it)
const linkTarget = await readlink(srcPath);
await symlink(linkTarget, destPath);
} else if (stats.isDirectory()) {
// Recursively copy directories
await copyDirectory(srcPath, destPath, {exclude});
} else if (stats.isFile()) {
// Copy files
await copyFile(srcPath, destPath);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`nx-verdaccio plugin create-nodes-v2 > should add package targets to library project 1`] = `
exports[`in a fresh Nx workspace > with nx-verdaccio plugin installed > should add package targets to library project 1`] = `
{
"build": {
"configurations": {},
"executor": "nx:run-commands",
"options": {
"command": "echo "lib"",
"outputPath": [
"dist/pkg",
],
},
"parallelism": true,
},
"nxv-pkg-install": {
"configurations": {},
"dependsOn": [
Expand Down Expand Up @@ -36,5 +47,50 @@ exports[`nx-verdaccio plugin create-nodes-v2 > should add package targets to lib
"options": {},
"parallelism": true,
},
"typecheck": {
"cache": true,
"configurations": {},
"dependsOn": [
"^typecheck",
],
"executor": "nx:run-commands",
"inputs": [
"{workspaceRoot}/tsconfig.base.json",
"{projectRoot}/tsconfig.json",
"{projectRoot}/tsconfig.lib.json",
"{projectRoot}/src/**/*.ts",
"^production",
{
"externalDependencies": [
"typescript",
],
},
],
"metadata": {
"description": "Runs type-checking for the project.",
"help": {
"command": "npx tsc --build --help",
"example": {
"args": [
"--force",
],
},
},
"technologies": [
"typescript",
],
},
"options": {
"command": "tsc --build --emitDeclarationOnly --pretty --verbose",
"cwd": "packages/pkg",
},
"outputs": [
"{projectRoot}/dist",
],
"parallelism": true,
"syncGenerators": [
"@nx/js:typescript-sync",
],
},
}
`;
Loading
Loading