Skip to content

Commit

Permalink
refactor(build-env): rename utils functions add test
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 13, 2024
1 parent 35c9bea commit 6c3936f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tooling/build-env/src/executors/kill-process/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type ExecutorContext, logger } from '@nx/devkit';

import type { KillProcessExecutorOptions } from './schema';
import { join } from 'node:path';
import { killProcessFromPid } from '../../internal/utils/process';
import { killProcessFromPid } from '../../internal/utils/kill-process';
import { normalizeOptions } from '../internal/normalize-options';

export type ExecutorOutput = {
Expand Down
6 changes: 3 additions & 3 deletions tooling/build-env/src/executors/npm-install/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { type ExecutorContext, logger, readJsonFile } from '@nx/devkit';
import type { NpmInstallExecutorOptions } from './schema';
import { join } from 'node:path';
import { executeProcess } from '../../internal/utils/execute-process';
import { objectToCliArgs } from '../../internal/utils/terminal-command';
import { objectToCliArgs } from '../../internal/utils/terminal';
import type { PackageJson } from 'nx/src/utils/package-json';
import { getBuildOutput } from '../../internal/utils/utils';
import { getTargetOutputPath } from '../../internal/utils/target';
import { normalizeOptions } from '../internal/normalize-options';

export type NpmInstallExecutorOutput = {
Expand All @@ -24,7 +24,7 @@ export default async function runNpmInstallExecutor(
options: opt,
} = normalizeOptions(context, options);

const packageDistPath = getBuildOutput(
const packageDistPath = getTargetOutputPath(
projectsConfigurations.projects[projectName]?.targets['build']
);
const { name: packageName, version } = readJsonFile<PackageJson>(
Expand Down
6 changes: 3 additions & 3 deletions tooling/build-env/src/executors/npm-publish/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { type ExecutorContext, logger } from '@nx/devkit';
import type { NpmPublishExecutorOptions } from './schema';
import { join, relative } from 'node:path';
import { executeProcess } from '../../internal/utils/execute-process';
import { objectToCliArgs } from '../../internal/utils/terminal-command';
import { getBuildOutput } from '../../internal/utils/utils';
import { objectToCliArgs } from '../../internal/utils/terminal';
import { getTargetOutputPath } from '../../internal/utils/target';
import { normalizeOptions } from '../internal/normalize-options';

export type NpmPublishExecutorOutput = {
Expand All @@ -28,7 +28,7 @@ export default async function runNpmPublishExecutor(
const { environmentRoot } = opt;

const { targets } = projectsConfigurations.projects[projectName];
const packageDistPath = getBuildOutput(targets['build']);
const packageDistPath = getTargetOutputPath(targets['build']);
const userconfig = join(
relativeFromDist(packageDistPath),
join(environmentRoot, '.npmrc')
Expand Down
18 changes: 10 additions & 8 deletions tooling/build-env/src/executors/setup/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { join } from 'node:path';
import runBuildExecutor from '../bootstrap/executor';
import runKillProcessExecutor from '../kill-process/executor';
import { executeProcess } from '../../internal/utils/execute-process';
import { objectToCliArgs } from '../../internal/utils/terminal-command';
import { objectToCliArgs } from '../../internal/utils/terminal';
import type { VerdaccioProcessResult } from '../../internal/verdaccio/verdaccio-registry';
import type { SetupEnvironmentExecutorOptions } from './schema';
import { normalizeOptions } from '../internal/normalize-options';
import { VERDACCIO_REGISTRY_JSON } from '../../internal/verdaccio/verdaccio-npm-env';

import { VERDACCIO_REGISTRY_JSON } from '../../internal/verdaccio/constants';

export type ExecutorOutput = {
success: boolean;
Expand All @@ -33,7 +34,11 @@ export default async function runSetupEnvironmentExecutor(
},
context
);
const {environmentRoot, keepServerRunning, verbose = true} = normalizedOptions;
const {
environmentRoot,
keepServerRunning,
verbose = true,
} = normalizedOptions;

await executeProcess({
command: 'nx',
Expand All @@ -50,15 +55,12 @@ export default async function runSetupEnvironmentExecutor(
await runKillProcessExecutor(
{
...normalizedOptions,
filePath: join(
environmentRoot,
VERDACCIO_REGISTRY_JSON
),
filePath: join(environmentRoot, VERDACCIO_REGISTRY_JSON),
},
context
);
} else {
const {url} = readJsonFile<VerdaccioProcessResult>(
const { url } = readJsonFile<VerdaccioProcessResult>(
join(environmentRoot, VERDACCIO_REGISTRY_JSON)
);
logger.info(`Verdaccio server kept running under : ${url}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { mkdir } from 'node:fs/promises';
import type { TargetConfiguration } from '@nx/devkit';

export function getBuildOutput(target?: TargetConfiguration) {
export function getTargetOutputPath(target?: TargetConfiguration) {
const { options } = target ?? {};
const { outputPath } = options ?? {};
if (!outputPath) {
throw new Error('outputPath is required');
}
return outputPath;
}

export function uniquePort(): number {
return Number((6000 + Number(Math.random() * 1000)).toFixed(0));
}
25 changes: 25 additions & 0 deletions tooling/build-env/src/internal/utils/target.unit-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getTargetOutputPath } from './target';

describe('getTargetOutputPath', () => {
it('should return output path of the build target if given', () => {
expect(
getTargetOutputPath({
options: {
outputPath: 'out-dir',
},
})
).toBe('out-dir');
});

it('should throw if no outputPath is given in options', () => {
expect(
getTargetOutputPath({
options: {},
})
).toThrow('outputPath is required');
});

it('should throw if empty onject is passed', () => {
expect(getTargetOutputPath({})).toThrow('outputPath is required');
});
});
3 changes: 3 additions & 0 deletions tooling/build-env/src/internal/utils/unique-port.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function uniquePort(): number {
return Number((6000 + Number(Math.random() * 1000)).toFixed(0));
}
12 changes: 12 additions & 0 deletions tooling/build-env/src/internal/utils/unique-port.unit-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { uniquePort } from './unique-port';

describe('uniquePort', () => {
it('should return a different number on every call', () => {
const portsArray = new Array(100).fill(0).map(() => uniquePort());
expect(portsArray.length).toBe(new Set(portsArray).size);
});

it('should return random number bigger then 6000', () => {
expect(uniquePort()).toBeGreaterThan(6000);
});
});
1 change: 1 addition & 0 deletions tooling/build-env/src/internal/verdaccio/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VERDACCIO_REGISTRY_JSON = 'verdaccio-registry.json';
3 changes: 2 additions & 1 deletion tooling/build-env/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
import { dirname, join } from 'node:path';
import { DEFAULT_ENVIRONMENTS_OUTPUT_DIR } from '../internal/constants';
import type { StarVerdaccioOptions } from '../internal/verdaccio/verdaccio-registry';
import { VERDACCIO_REGISTRY_JSON } from '../internal/verdaccio/verdaccio-npm-env';

import { VERDACCIO_REGISTRY_JSON } from '../internal/verdaccio/constants';

export function isPublishable(tags: string[]): boolean {
return tags.some((target) => target === 'publishable');
Expand Down

0 comments on commit 6c3936f

Please sign in to comment.