Skip to content

Commit

Permalink
refactor(build-env): intro constants for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 13, 2024
1 parent 6c3936f commit 989d38a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
5 changes: 2 additions & 3 deletions tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: ');
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 989d38a

Please sign in to comment.