Skip to content

Commit

Permalink
refactor: replace ramda clone by structuredClone
Browse files Browse the repository at this point in the history
  • Loading branch information
irmkr committed Sep 28, 2023
1 parent bffa918 commit a3d0cc6
Show file tree
Hide file tree
Showing 24 changed files with 35 additions and 63 deletions.
8 changes: 4 additions & 4 deletions src/analyse/adapt-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as semver from 'semver';

import { clone } from 'ramda';

import { white, green } from 'chalk';

import type { Module, PackageAfterDetermineVersion } from '../types';
Expand All @@ -20,8 +18,10 @@ export const adaptDependencies: Module = (env) => {
}

log('analyse affected packages');
const clonePackages = clone(env.packages);
const releasablePackages = getReleasablePackages(clone(clonePackages));
const clonePackages = structuredClone(env.packages);
const releasablePackages = getReleasablePackages(
structuredClone(clonePackages)
);

log(`${Object.keys(releasablePackages).length} packages will be released`);

Expand Down
4 changes: 1 addition & 3 deletions src/analyse/add-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { clone } from 'ramda';

import {
Module,
Package,
Expand All @@ -20,7 +18,7 @@ export const addDependencies: Module = (env) => {
throw new Error(errorMessage);
}

const envPackages = clone(env.packages);
const envPackages = structuredClone(env.packages);

const packagesNames = Object.keys(envPackages);

Expand Down
4 changes: 1 addition & 3 deletions src/analyse/add-messages-to-packages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { clone } from 'ramda';

import type { Module } from '../types';

import { logger } from '../helpers/logger';
Expand All @@ -19,7 +17,7 @@ export const addMessagesToPackages: Module = (env) => {
}

log('Register messages to packages');
const clonePackages = clone(env.packages);
const clonePackages = structuredClone(env.packages);

env.commitMessages.forEach((commitMessage) => {
commitMessage.affectedPackages?.forEach((affectedPackage) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const addPackageNamesToMessages: Module = (env) => {
log('Analyse affected packages per commit');
const userAffectedPackages = new Set<string>();

const cloneCommitMessages = clone(env.commitMessages);
const cloneCommitMessages = structuredClone(env.commitMessages);

cloneCommitMessages.forEach((commitMessage) => {
const affectedPackages = new Set<string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-env node, jest */
import { clone } from 'ramda';

import type { Env, Message, Module } from '../../../types';
import { envWithConfig } from '../../../fixtures/env';

Expand Down Expand Up @@ -57,7 +55,7 @@ describe('addPackageNamesToMessages Module', () => {
};
const result = addPackageNamesToMessages(mockEnv);

const expectedEnv = clone(mockEnv);
const expectedEnv = structuredClone(mockEnv);
expectedEnv.commitMessages![0].affectedPackages = ['package_1'];
expectedEnv.commitMessages![1].affectedPackages = ['package_2'];
expectedEnv.commitMessages![2].affectedPackages = ['package_3'];
Expand Down
2 changes: 1 addition & 1 deletion src/analyse/create-dependency-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const createDependencyTree: Module = (env) => {
throw new Error(errorMessage);
}

const packagesClone = clone(env.packages);
const packagesClone = structuredClone(env.packages);
const counter = {
default: 0,
devDependencies: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const createPackageJsonContent: Module = (env) => {

log('analyse packageJson Content');

const clonePackages = clone(env.packages);
const clonePackages = structuredClone(env.packages);

Object.keys(clonePackages).forEach((packageName) => {
log(`generating git & npm package.json files for "${white(packageName)}"`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getPackageJson(

const currentPackage = packages![packageName];

const clonePackageJson = clone(currentPackage.packageJson);
const clonePackageJson = structuredClone(currentPackage.packageJson);
const dependsOnMap = isNpm
? Object.fromEntries(
currentPackage.dependsOn.map((dependency) => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-env node, jest */
import { clone } from 'ramda';

import type {
Env,
Module,
Expand Down Expand Up @@ -84,7 +82,7 @@ describe('createPackageJsonContent Module', () => {
});

it('returns processed Env object', () => {
const cloneEnv = clone(mockEnvWithPackages);
const cloneEnv = structuredClone(mockEnvWithPackages);
cloneEnv.packages!.mockPackage1 = {
...cloneEnv.packages!.mockPackage1,
packageJsonGit: { name: 'mockPackage1', version: '*' },
Expand Down
2 changes: 1 addition & 1 deletion src/analyse/determine-direct-increment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const determineDirectIncrement: Module = (env) => {
throw new Error(errorMessage);
}

const clonePackages = clone(env.packages);
const clonePackages = structuredClone(env.packages);
const packagesToRelease: Record<string, Message['level']> = {};

log('analyse registered commitMessages');
Expand Down
2 changes: 1 addition & 1 deletion src/analyse/determine-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const determineVersion: Module = (env) => {

log('analyse packages');

const clonePackages = clone(env.packages);
const clonePackages = structuredClone(env.packages);

const incrementLevels: semver.ReleaseType[] = ['patch', 'minor', 'major'];

Expand Down
4 changes: 1 addition & 3 deletions src/analyse/extend-dependency-ranges.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { clone } from 'ramda';

import type { Module, RelatedPackageTypes } from '../types';

import { logger } from '../helpers/logger';
Expand All @@ -10,7 +8,7 @@ const { log } = logger('[analyse] extend dependency ranges');
const PINNED_VERSION_PATTERN = /^\d+\.\d+\.\d+$/;

export const extendDependencyRanges: Module = (env) => {
const packagesClone = clone(env.packages);
const packagesClone = structuredClone(env.packages);

log('Analysing packages');
let extendCounter = 0;
Expand Down
4 changes: 1 addition & 3 deletions src/analyse/prepare-changelogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { join } from 'path';
import semver from 'semver';

import { white } from 'chalk';
import { clone } from 'ramda';

import { getYearWeek } from '../helpers/get-week-number';

import type {
Expand Down Expand Up @@ -38,7 +36,7 @@ export const prepareChangelogs: Module = (env) => {
throw new Error(errorMessage);
}

const clonePackages = clone(env.packages);
const clonePackages = structuredClone(env.packages);
const releasablePackages = getReleasablePackages(clonePackages);

if (!releasablePackages.length) {
Expand Down
4 changes: 1 addition & 3 deletions src/analyse/spec/adapt-dependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-env node, jest */
import * as semver from 'semver';

import { clone } from 'ramda';

import type {
Env,
Module,
Expand Down Expand Up @@ -410,7 +408,7 @@ describe('adaptDependencies Module', () => {
);

it('returns an Env config object reflecting new ranges', () => {
const expectedEnv = clone(mockEnv);
const expectedEnv = structuredClone(mockEnv);

expectedEnv.packages!.mockPackage4.relatedMessages.push({
date: new Date().toISOString(),
Expand Down
6 changes: 2 additions & 4 deletions src/analyse/spec/add-messages-to-packages.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-env node, jest */
import { clone } from 'ramda';

import type { Env, Module, Package, Message } from '../../types';

import { envWithConfig } from '../../fixtures/env';
Expand Down Expand Up @@ -88,7 +86,7 @@ describe('addMessagesToPackages module', () => {
});

it('throws an error when an invalid "affectedPackage" is registered', () => {
const mockInvalidAffectedPackageEnv = clone(mockEnv);
const mockInvalidAffectedPackageEnv = structuredClone(mockEnv);
mockInvalidAffectedPackageEnv.commitMessages![1].affectedPackages!.push(
'lodash'
);
Expand All @@ -106,7 +104,7 @@ describe('addMessagesToPackages module', () => {

describe('on run', () => {
let result: Env;
const expectedEnv = clone(mockEnv);
const expectedEnv = structuredClone(mockEnv);

beforeAll(() => {
expectedEnv.packages!.mock1Package.messages.push(mockCommitMessages[0]);
Expand Down
4 changes: 1 addition & 3 deletions src/analyse/spec/create-dependency-tree.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-env node, jest */
import { clone } from 'ramda';

import type { Env, Module, Package } from '../../types';

import { envWithConfig } from '../../fixtures/env';
Expand Down Expand Up @@ -84,7 +82,7 @@ describe('[analyse] createDependencyTree module', () => {
type: 'peer',
});

expectedPackages = clone(mockPackages);
expectedPackages = structuredClone(mockPackages);

const mockEnv: Env = {
...envWithConfig,
Expand Down
4 changes: 1 addition & 3 deletions src/analyse/spec/determine-direct-increment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-env node, jest */

import { clone } from 'ramda';

import { envWithConfig } from '../../fixtures/env';
import type { Env, Message, Module, Package } from '../../types';

Expand Down Expand Up @@ -105,7 +103,7 @@ describe('determineDirectIncrement Module', () => {
packages: mockEnvPackages,
commitMessages: mockCommitMessages,
};
const expectedEnv: Env = clone(mockEnv);
const expectedEnv: Env = structuredClone(mockEnv);
const { mock1Package, mock2Package, mock3Package, mock4Package } =
expectedEnv.packages!;
beforeAll(() => {
Expand Down
8 changes: 3 additions & 5 deletions src/analyse/spec/determine-version.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-env node, jest */
import { inc, parse } from 'semver';

import { clone } from 'ramda';

import type {
Env,
Module,
Expand Down Expand Up @@ -101,7 +99,7 @@ describe('determineVersion Module', () => {
const expectedErrorMessage =
'Invalid version "latest" for package "mock0Package"';
mockParse.mockImplementationOnce(() => null);
const mockWrongVersionEnv: Env = clone(mockEnv);
const mockWrongVersionEnv: Env = structuredClone(mockEnv);
mockWrongVersionEnv.packages!.mock0Package.currentVersion = 'latest';

expect(() => determineVersion(mockWrongVersionEnv)).toThrow(
Expand All @@ -115,7 +113,7 @@ describe('determineVersion Module', () => {
mockInc.mockImplementationOnce(() => null);
const expectedErrorMessage =
'version "1.0.0" cannot be increased to "patch" for package "mock0Package"';
const mockWrongVersionEnv: Env = clone(mockEnv);
const mockWrongVersionEnv: Env = structuredClone(mockEnv);

expect(() => determineVersion(mockWrongVersionEnv)).toThrow(
expectedErrorMessage
Expand All @@ -126,7 +124,7 @@ describe('determineVersion Module', () => {

describe('on run', () => {
let result: Env;
const expectedEnv: Env = clone(mockEnv);
const expectedEnv: Env = structuredClone(mockEnv);
(
expectedEnv.packages!.mock0Package as PackageAfterDetermineVersion
).incrementedVersion = '1.0.1';
Expand Down
4 changes: 1 addition & 3 deletions src/analyse/spec/extend-dependency-range.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-env node, jest */
import { clone } from 'ramda';

import type { Env, Module, Package } from '../../types';

import { envWithConfig } from '../../fixtures/env';
Expand Down Expand Up @@ -75,7 +73,7 @@ describe('ExtendDependencyRange Module', () => {
});

const mockEnv: Env = { ...envWithConfig, packages: mockPackages };
expectedPackages = clone(mockPackages);
expectedPackages = structuredClone(mockPackages);
expectedPackages['test-package-1'].dependsOn[1].range = '~1.0.0';
expectedPackages['test-package-1'].determinedIncrementLevel = 0;
expectedPackages['test-package-1'].relatedMessages.push({
Expand Down
6 changes: 2 additions & 4 deletions src/analyse/spec/prepare-changelogs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { clone } from 'ramda';

import type {
Env,
Message,
Expand Down Expand Up @@ -84,7 +82,7 @@ const invalidMockEnv: Env = {
describe('prepareChangelogs Module', () => {
let prepareChangelogs: Module;
let result: Env;
let expectedEnv: Env = clone(mockEnv);
let expectedEnv: Env = structuredClone(mockEnv);

beforeAll(() => {
jest.useFakeTimers('modern').setSystemTime(new Date('2022, 1, 3'));
Expand Down Expand Up @@ -156,7 +154,7 @@ describe('prepareChangelogs Module', () => {
});

it('throws an error when messages are missing for the package', () => {
const invalidMockEnv = clone(mockEnv);
const invalidMockEnv = structuredClone(mockEnv);
invalidMockEnv.packages!.mock2Package.messages = [];
invalidMockEnv.packages!.mock2Package.relatedMessages = [];
const expectedErrorMessage = `No messages found for "white(mock2Package)"`;
Expand Down
4 changes: 1 addition & 3 deletions src/change/spec/write-main-changelog.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import fs from 'fs';

import { clone } from 'ramda';

import { envWithConfig } from '../../fixtures/env';
import type { Module, Env, Message, RelatedMessage } from '../../types';

Expand Down Expand Up @@ -74,7 +72,7 @@ describe('writeMainChangelog Module', () => {
const expectedErrorMessage =
'"changelogPath" attribute not found on env config object';

const invalidEnvConfig = clone(envWithConfig);
const invalidEnvConfig = structuredClone(envWithConfig);
invalidEnvConfig.config!.changelogPath = '';

expect(() => writeMainChangelog(invalidEnvConfig)).toThrow(
Expand Down
4 changes: 3 additions & 1 deletion src/change/write-package-changelogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const writePackageChangelogs: Module = (env) => {
error(errorMessage);
throw new Error(errorMessage);
}
const releasablePackages = getReleasablePackages(clone(env.packages));
const releasablePackages = getReleasablePackages(
structuredClone(env.packages)
);

releasablePackages.forEach((packageName) => {
const currentPackage = env.packages![
Expand Down
4 changes: 1 addition & 3 deletions src/helpers/spec/plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { clone } from 'ramda';

import type { Env, Module, Phase } from '../../types';

import { composeAsync } from '../compose-async';
Expand Down Expand Up @@ -36,7 +34,7 @@ jest.doMock('test-plugin-package-2', () => mockPluginPackage2, {
import { envWithConfig } from '../../fixtures/env';
const mockEnv = { ...envWithConfig };

const mockEnvWithPlugins = clone(mockEnv);
const mockEnvWithPlugins = structuredClone(mockEnv);
mockEnvWithPlugins.config!.plugins = [
'test-plugin-package-1',
'test-plugin-package-2',
Expand Down
6 changes: 3 additions & 3 deletions src/persist/commit-and-tag-packages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { clone } from 'ramda';

import simpleGit from 'simple-git';

import type { Env, Module, PackageAfterPrepareChangelogs } from '../types';
Expand Down Expand Up @@ -28,7 +26,9 @@ const commitAndTagPackages: Module = async (env: Env) => {
});
};

const releasablePackages = getReleasablePackages(clone(env.packages!));
const releasablePackages = getReleasablePackages(
structuredClone(env.packages!)
);
const commitMessageStr = `chore(release): publish new version
${prepareCommit(releasablePackages).join('\n')}`;

Expand Down

0 comments on commit a3d0cc6

Please sign in to comment.