From 989d38a676014da2cecf758e51553bac4ed1fc1a Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 13 Sep 2024 17:31:35 +0300 Subject: [PATCH] refactor(build-env): intro constants for utils --- .../internal/verdaccio/verdaccio-npm-env.ts | 5 +- .../verdaccio/verdaccio-npm-env.unit-test.ts | 81 +++++++++++++++++++ .../internal/verdaccio/verdaccio-registry.ts | 4 +- 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.unit-test.ts diff --git a/tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.ts b/tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.ts index 78adaba5..909acf27 100644 --- a/tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.ts +++ b/tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.ts @@ -8,8 +8,9 @@ import { import { writeFile } from 'node:fs/promises'; import { setupNpmWorkspace } from '../utils/npm'; import { error, info } from '../utils/logging'; -import { objectToCliArgs } from '../utils/terminal-command'; +import { objectToCliArgs } from '../utils/terminal'; import { execSync } from 'node:child_process'; +import { VERDACCIO_REGISTRY_JSON } from './constants'; function logInfo(msg: string) { info(msg, 'Verdaccio Env: '); @@ -19,8 +20,6 @@ function errorLog(msg: string) { error(msg, 'Verdaccio Env: '); } -export const VERDACCIO_REGISTRY_JSON = 'verdaccio-registry.json'; - export const verdaccioEnvLogger = { info: logInfo, error: errorLog, diff --git a/tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.unit-test.ts b/tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.unit-test.ts new file mode 100644 index 00000000..b23aa19e --- /dev/null +++ b/tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.unit-test.ts @@ -0,0 +1,81 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { configureRegistry, verdaccioEnvLogger } from './verdaccio-npm-env'; +import { bold, gray, red } from 'ansis'; +import { execSync } from 'node:child_process'; +import { objectToCliArgs } from '../utils/terminal'; +import type { VerdaccioProcessResult } from './verdaccio-registry'; + +describe('verdaccioEnvLogger.info', () => { + let consoleInfoSpy; + beforeEach(() => { + consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(vi.fn()); + }); + + it('should log info', () => { + verdaccioEnvLogger.info('message'); + expect(consoleInfoSpy).toHaveBeenCalledTimes(1); + expect(consoleInfoSpy).toHaveBeenCalledWith( + `${gray('>')} ${gray(bold('Verdaccio Env: '))} ${'message'}` + ); + }); +}); + +describe('logError', () => { + let consoleErrorSpy; + beforeEach(() => { + consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(vi.fn()); + }); + + it('should log error', () => { + verdaccioEnvLogger.error('message'); + expect(consoleErrorSpy).toHaveBeenCalledTimes(1); + expect(consoleErrorSpy).toHaveBeenCalledWith( + `${red('>')} ${red(bold('Verdaccio Env: '))} ${'message'}` + ); + }); +}); + +describe('configureRegistry', () => { + it('should set the npm registry and authToken', () => { + const processResult: VerdaccioProcessResult & { userconfig?: string } = { + port: 4873, + host: 'localhost', + protocol: 'http', + url: 'http://localhost:4873', + userconfig: 'test-config', + }; + + configureRegistry(processResult, false); + + expect(objectToCliArgs).toHaveBeenCalledWith({ userconfig: 'test-config' }); + + expect(execSync).toHaveBeenCalledWith( + 'npm config set registry="http://localhost:4873" --userconfig=test-config' + ); + + expect(execSync).toHaveBeenCalledWith( + 'npm config set //localhost:4873/:_authToken "secretVerdaccioToken" --userconfig=test-config' + ); + }); + + it('should log registry and authToken commands if verbose is true', () => { + const processResult = { + port: 4873, + host: 'localhost', + url: 'http://localhost:4873', + userconfig: 'test-config', + }; + + configureRegistry(processResult, true); + + expect(verdaccioEnvLogger.info).toHaveBeenCalledWith( + 'Set registry:\nnpm config set registry="http://localhost:4873" --userconfig=test-config' + ); + + expect(verdaccioEnvLogger.info).toHaveBeenCalledWith( + 'Set authToken:\nnpm config set //localhost:4873/:_authToken "secretVerdaccioToken" --userconfig=test-config' + ); + + expect(execSync).toHaveBeenCalledTimes(2); + }); +}); diff --git a/tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts b/tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts index ff1ab4ed..b37fdabf 100644 --- a/tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts +++ b/tooling/build-env/src/internal/verdaccio/verdaccio-registry.ts @@ -2,9 +2,9 @@ import { gray, bold, red } from 'ansis'; import { join } from 'node:path'; import { error, info } from '../utils/logging'; import { logger } from '@nx/devkit'; -import { objectToCliArgs } from '../utils/terminal-command'; +import { objectToCliArgs } from '../utils/terminal'; import { executeProcess } from '../utils/execute-process'; -import { uniquePort } from '../utils/utils'; +import { uniquePort } from '../utils/unique-port'; import { getEnvironmentsRoot } from '../../shared/setup'; export function logInfo(msg: string) {