Skip to content

Commit

Permalink
wip real tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 3, 2024
1 parent 7b6caa0 commit bc52155
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 70 deletions.
32 changes: 7 additions & 25 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
"libsDir": "projects"
},
"namedInputs": {
"default": [
"{projectRoot}/**/*",
"sharedGlobals"
],
"default": ["{projectRoot}/**/*", "sharedGlobals"],
"production": [
"default",
"!{projectRoot}/.eslintrc.json",
Expand All @@ -22,42 +19,27 @@
"targetDefaults": {
"@nx/esbuild:esbuild": {
"cache": true,
"dependsOn": [
"^build"
],
"inputs": [
"production",
"^production"
]
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
},
"@nx/vite:test": {
"cache": true,
"inputs": [
"default",
"^production"
]
"inputs": ["default", "^production"]
},
"nx-release-publish": {
"dependsOn": [
{
"projects": [
"self"
],
"projects": ["self"],
"target": "build"
},
{
"projects": [
"dependencies"
],
"projects": ["dependencies"],
"target": "nx-release-publish"
}
]
},
"build": {
"inputs": [
"production",
"^production"
]
"inputs": ["production", "^production"]
}
},
"plugins": [
Expand Down
4 changes: 2 additions & 2 deletions tools/bin/npm-env.ts → tools/bin/setup-npm-env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { startNpmEnv, StartVerdaccioAndSetupEnvOptions } from '../utils/env';
import { setupNpmEnv, StartVerdaccioAndSetupEnvOptions } from '../utils/env';
import yargs, { Options } from 'yargs';

const isVerbose: boolean = process.env.NX_VERBOSE_LOGGING === 'true' ?? false;
Expand Down Expand Up @@ -29,5 +29,5 @@ const args = yargs(process.argv.slice(2))
.parse() as StartVerdaccioAndSetupEnvOptions;

(async () => {
await startNpmEnv(args);
await setupNpmEnv(args);
})();
31 changes: 19 additions & 12 deletions tools/bin/teardown-env.ts → tools/bin/teardown-npm-env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {logError, logInfo, startNpmEnv, StartVerdaccioAndSetupEnvOptions} from '../utils/env';
import yargs, {Options} from 'yargs';
import {join} from "node:path";
import {readJsonFile} from "@nx/devkit";
import {VercaddioServerResult} from "../utils/registry";
import {rm} from "node:fs/promises";
import {
logError,
logInfo,
setupNpmEnv,
StartVerdaccioAndSetupEnvOptions,
} from '../utils/env';
import yargs, { Options } from 'yargs';
import { join } from 'node:path';
import { readJsonFile } from '@nx/devkit';
import { VercaddioServerResult } from '../utils/registry';
import { rm } from 'node:fs/promises';

const isVerbose: boolean = process.env.NX_VERBOSE_LOGGING === 'true' ?? false;

Expand All @@ -18,30 +23,32 @@ const args = yargs(process.argv.slice(2))
type: 'boolean',
description: 'Verbose logging',
default: isVerbose,
}
},
} satisfies Partial<Record<keyof StartVerdaccioAndSetupEnvOptions, Options>>)
.parse() as StartVerdaccioAndSetupEnvOptions;

(async () => {
const {workspaceRoot} = args;
const { workspaceRoot } = args;
const registryConfigPath = join(workspaceRoot, 'verdaccio-registry.json');
let registryServerResult: VercaddioServerResult;
try {
registryServerResult = await readJsonFile<VercaddioServerResult>(registryConfigPath);
registryServerResult = await readJsonFile<VercaddioServerResult>(
registryConfigPath
);
} catch (e) {
logError(`No registry config file found at: ${registryConfigPath}`);
return;
}

logInfo('Tearing down environment');
console.table(registryServerResult);
const {pid, storage} = registryServerResult;
await rm(storage, {recursive: true, force: true})
const { pid, storage } = registryServerResult;
await rm(storage, { recursive: true, force: true });
try {
process.kill(Number(pid));
} catch (e) {
logError(`Failes kipplng process with id: ${pid}\n${e}`);
} finally {
await rm(registryConfigPath)
await rm(registryConfigPath);
}
})();
76 changes: 46 additions & 30 deletions tools/plugins/verdaccio-test-env.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {type CreateNodes, readJsonFile, TargetConfiguration,} from '@nx/devkit';
import {dirname, join, relative} from 'node:path';
import type {ProjectConfiguration} from 'nx/src/config/workspace-json-project-json';
import {
type CreateNodes,
readJsonFile,
TargetConfiguration,
} from '@nx/devkit';
import { dirname, join, relative } from 'node:path';
import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json';

const tmpNpmEnv = join('tmp', 'npm-env');

Expand All @@ -20,20 +24,25 @@ export const createNodes: CreateNodes = [
projects: {
[root]: {
targets: {
// e2e project
// === e2e project
// start-verdaccio, stop-verdaccio
...(isNpmEnv && verdaccioTargets(projectConfiguration)),
// setup-npm-env, setup-env, setup-deps
...(isNpmEnv && envTargets(projectConfiguration)),
// dependency project
...(isPublishable && npmTargets({...projectConfiguration, root})),
// === dependency project
// npm-publish, npm-install
...(isPublishable && npmTargets({ ...projectConfiguration, root })),
},
},
},
};
},
];

function verdaccioTargets(projectConfiguration: ProjectConfiguration): Record<string, TargetConfiguration> {
const {name: projectName} = projectConfiguration;
function verdaccioTargets(
projectConfiguration: ProjectConfiguration
): Record<string, TargetConfiguration> {
const { name: projectName } = projectConfiguration;
return {
'start-verdaccio': {
executor: '@nx/js:verdaccio',
Expand All @@ -44,19 +53,23 @@ function verdaccioTargets(projectConfiguration: ProjectConfiguration): Record<st
},
},
'stop-verdaccio': {
command: 'tsx --tsconfig=tools/tsconfig.tools.json tools/bin/teardown-env.ts --workspaceRoot={args.workspaceRoot}',
command:
'tsx --tsconfig=tools/tsconfig.tools.json tools/bin/teardown-npm-env.ts --workspaceRoot={args.workspaceRoot}',
options: {
workspaceRoot: join(tmpNpmEnv, projectName),
},
},
}
};
}

function envTargets(projectConfiguration: ProjectConfiguration): Record<string, TargetConfiguration> {
const {name: projectName} = projectConfiguration;
function envTargets(
projectConfiguration: ProjectConfiguration
): Record<string, TargetConfiguration> {
const { name: projectName } = projectConfiguration;
return {
'setup-npm-env': {
command: 'tsx --tsconfig=tools/tsconfig.tools.json tools/bin/npm-env.ts',
command:
'tsx --tsconfig=tools/tsconfig.tools.json tools/bin/setup-npm-env.ts',
options: {
projectName,
targetName: 'start-verdaccio',
Expand All @@ -65,17 +78,18 @@ function envTargets(projectConfiguration: ProjectConfiguration): Record<string,
},
},
'setup-env': {
cache: true,
inputs: ["default", "^production", "!{projectRoot}/**/*.md"],
inputs: ['default', '^production', '!{projectRoot}/**/*.md'],
outputs: [
`{workspaceRoot}/${tmpNpmEnv}/${projectName}/node_modules`
`{workspaceRoot}/${tmpNpmEnv}/${projectName}/node_modules`,
`{workspaceRoot}/${tmpNpmEnv}/${projectName}/.npmrc`,
`{workspaceRoot}/${tmpNpmEnv}/${projectName}/package.json`,
],
executor: "nx:run-commands",
executor: 'nx:run-commands',
options: {
commands: [
`nx setup-npm-env ${projectName}`,
`nx setup-deps ${projectName} --envProjectName={args.envProjectName}`,
`nx stop-verdaccio ${projectName} --workspaceRoot={args.workspaceRoot}`
`nx stop-verdaccio ${projectName} --workspaceRoot={args.workspaceRoot}`,
],
workspaceRoot: join(tmpNpmEnv, projectName),
forwardAllArgs: true,
Expand All @@ -89,13 +103,13 @@ function envTargets(projectConfiguration: ProjectConfiguration): Record<string,
projects: 'dependencies',
target: 'npm-install',
params: 'forward',
}
},
],
options: {
envProjectName: projectName,
},
command: 'echo Dependencies installed!',
}
},
};
}

Expand All @@ -105,25 +119,27 @@ const relativeFromPath = (dir) =>
function npmTargets(
projectConfiguration: ProjectConfiguration
): Record<string, TargetConfiguration> {
const {root, name: projectName, targets} = projectConfiguration;
const {build} = targets;
const {options} = build;
const {outputPath} = options;
const { root, name: projectName, targets } = projectConfiguration;
const { build } = targets;
const { options } = build;
const { outputPath } = options;
if (outputPath == null) {
throw new Error('outputPath is required');
}

const {name: packageName, version: pkgVersion} = readJsonFile(
const { name: packageName, version: pkgVersion } = readJsonFile(
join(root, 'package.json')
);
const userconfig = `${relativeFromPath(outputPath)}/${tmpNpmEnv}/{args.envProjectName}/.npmrc`;
const prefix = `${tmpNpmEnv}/{args.envProjectName}`;
const userconfig = `${relativeFromPath(
outputPath
)}/${tmpNpmEnv}/{args.envProjectName}/.npmrc`;
const prefix = `${tmpNpmEnv}/{args.envProjectName}`;
const envProjectName = projectName;

return {
'npm-publish': {
dependsOn: [
{projects: 'self', target: 'build', params: 'forward'},
{ projects: 'self', target: 'build', params: 'forward' },
{
projects: 'dependencies',
target: 'npm-publish',
Expand All @@ -138,7 +154,7 @@ function npmTargets(
},
'npm-install': {
dependsOn: [
{projects: 'self', target: 'npm-publish', params: 'forward'},
{ projects: 'self', target: 'npm-publish', params: 'forward' },
{
projects: 'dependencies',
target: 'npm-install',
Expand All @@ -150,6 +166,6 @@ function npmTargets(
pkgVersion,
envProjectName,
},
}
},
};
}
2 changes: 1 addition & 1 deletion tools/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type NpmTestEnvResult = VerdaccioEnv & {
stop: () => void;
};

export async function startNpmEnv({
export async function setupNpmEnv({
projectName,
verbose = false,
targetName = 'start-verdaccio',
Expand Down
1 change: 1 addition & 0 deletions tools/utils/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function logInfo(msg: string) {
export function logError(msg: string) {
error(msg, 'Verdaccio: ');
}

export type VerdaccioProcessResult = {
protocol: string;
port: string | number;
Expand Down

0 comments on commit bc52155

Please sign in to comment.