Skip to content

Commit

Permalink
refactor(#release): remove unnecessary dependencies (#229)
Browse files Browse the repository at this point in the history
### Refactoring
- [#release] Remove the dependencies `fs-extra` and `@types/fs-extra`
because they are not necessary. The native Node.js `fs` module already
fulfills our current needs.
  • Loading branch information
diego-aquino committed Jun 13, 2024
1 parent 3231f38 commit 4b6b110
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 75 deletions.
2 changes: 0 additions & 2 deletions packages/release/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
"types:check": "tsc --noEmit"
},
"dependencies": {
"fs-extra": "^11.2.0",
"yargs": "^17.7.2",
"zod": "^3.23.8",
"zx": "^4.3.0"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.14.0",
"@types/yargs": "^17.0.32",
"@vitest/coverage-istanbul": "1.6.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import filesystem from 'fs-extra';
import filesystem from 'fs/promises';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { ProcessOutput, ProcessPromise } from 'zx';

Expand Down Expand Up @@ -44,8 +44,8 @@ describe('Prepare release command', () => {
],
});

const readJSONSpy = vi.spyOn(filesystem, 'readJSON');
const writeJSONSpy = vi.spyOn(filesystem, 'writeJSON');
const readFileSpy = vi.spyOn(filesystem, 'readFile');
const writeFileSpy = vi.spyOn(filesystem, 'writeFile');

beforeEach(() => {
metadataFileContent.version = '0.0.0';
Expand All @@ -55,18 +55,18 @@ describe('Prepare release command', () => {
config.metadata[0].partialVersions.appendTo = [];
config.tagSuffix = undefined;

readJSONSpy.mockClear();
readJSONSpy.mockImplementation((filePath) => {
readFileSpy.mockClear();
readFileSpy.mockImplementation((filePath) => {
if (filePath === metadataFilePath) {
return Promise.resolve({ ...metadataFileContent });
return Promise.resolve(JSON.stringify(metadataFileContent));
}
return Promise.reject(new Error(`File ${filePath.toLocaleString()} not found.`));
});

writeJSONSpy.mockClear();
writeJSONSpy.mockImplementation((filePath, newMetadataFileContent) => {
if (filePath === metadataFilePath) {
Object.assign(metadataFileContent, newMetadataFileContent);
writeFileSpy.mockClear();
writeFileSpy.mockImplementation((filePath, newMetadataFileContent) => {
if (filePath === metadataFilePath && typeof newMetadataFileContent === 'string') {
Object.assign(metadataFileContent, JSON.parse(newMetadataFileContent));
return Promise.resolve();
}
return Promise.reject(new Error(`File ${filePath.toLocaleString()} not found.`));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import filesystem from 'fs-extra';
import filesystem from 'fs/promises';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { createMetadataFileEntry, createReleaseConfig } from '@tests/factories/release-config';
Expand Down Expand Up @@ -34,25 +34,25 @@ describe('Upgrade version command', () => {
],
});

const readJSONSpy = vi.spyOn(filesystem, 'readJSON');
const writeJSONSpy = vi.spyOn(filesystem, 'writeJSON');
const readFileSpy = vi.spyOn(filesystem, 'readFile');
const writeFileSpy = vi.spyOn(filesystem, 'writeFile');

beforeEach(() => {
metadataFileContent.version = '0.0.0';
metadataFileContent.description = 'description';
config.metadata[0].partialVersions.includeInVersionKey = true;
config.metadata[0].partialVersions.appendTo = [];

readJSONSpy.mockClear();
readJSONSpy.mockImplementation((filePath) => {
readFileSpy.mockClear();
readFileSpy.mockImplementation((filePath) => {
if (filePath === metadataFilePath) {
return Promise.resolve({ ...metadataFileContent });
return Promise.resolve(JSON.stringify(metadataFileContent));
}
return Promise.reject(new Error(`File ${filePath.toLocaleString()} not found.`));
});

writeJSONSpy.mockClear();
writeJSONSpy.mockImplementation((filePath) => {
writeFileSpy.mockClear();
writeFileSpy.mockImplementation((filePath) => {
if (filePath === metadataFilePath) {
return Promise.resolve();
}
Expand All @@ -63,17 +63,17 @@ describe('Upgrade version command', () => {
});

function expectUpgradedMetadataFiles(upgradedVersion: string, extraUpgradedFileContent?: MetadataFileContent) {
expect(readJSONSpy).toHaveBeenCalledTimes(1);
expect(readJSONSpy).toHaveBeenCalledWith(metadataFilePath);
expect(readFileSpy).toHaveBeenCalledTimes(1);
expect(readFileSpy).toHaveBeenCalledWith(metadataFilePath, 'utf8');

expect(writeJSONSpy).toHaveBeenCalledTimes(1);
expect(writeFileSpy).toHaveBeenCalledTimes(1);

const upgradedMetadataFileContent: MetadataFileContent = {
...metadataFileContent,
...extraUpgradedFileContent,
version: upgradedVersion,
};
expect(writeJSONSpy).toHaveBeenCalledWith(metadataFilePath, upgradedMetadataFileContent, { spaces: 2 });
expect(writeFileSpy).toHaveBeenCalledWith(metadataFilePath, JSON.stringify(upgradedMetadataFileContent, null, 2));

expect(runCommandSpy).toHaveBeenCalledTimes(1);
expect(runCommandSpy).toHaveBeenCalledWith(['pnpm style:format ', ''], [metadataFilePath]);
Expand Down Expand Up @@ -225,9 +225,9 @@ describe('Upgrade version command', () => {
const expectedError = new MissingRequiredPartialLabelError();
await expect(upgradePromise).rejects.toThrowError(expectedError);

expect(readJSONSpy).toHaveBeenCalledTimes(1);
expect(readJSONSpy).toHaveBeenCalledWith(metadataFilePath);
expect(writeJSONSpy).not.toHaveBeenCalled();
expect(readFileSpy).toHaveBeenCalledTimes(1);
expect(readFileSpy).toHaveBeenCalledWith(metadataFilePath, 'utf8');
expect(writeFileSpy).not.toHaveBeenCalled();
expect(runCommandSpy).not.toHaveBeenCalled();
});
});
Expand All @@ -245,8 +245,8 @@ describe('Upgrade version command', () => {
expect(upgradeResult.isPartialUpgrade).toBe(true);
expectUpgradedMetadataFiles(upgradedVersionWithPartialLabel);

readJSONSpy.mockClear();
writeJSONSpy.mockClear();
readFileSpy.mockClear();
writeFileSpy.mockClear();
runCommandSpy.mockClear();

metadataFileContent.version = '1.2.1';
Expand Down Expand Up @@ -277,8 +277,8 @@ describe('Upgrade version command', () => {
description: descriptionWithAppendedPartialVersion,
});

readJSONSpy.mockClear();
writeJSONSpy.mockClear();
readFileSpy.mockClear();
writeFileSpy.mockClear();
runCommandSpy.mockClear();

metadataFileContent.version = upgradedVersion;
Expand All @@ -305,9 +305,9 @@ describe('Upgrade version command', () => {
const expectedError = new AppendPartialVersionToNonStringError();
await expect(upgradePromise).rejects.toThrowError(expectedError);

expect(readJSONSpy).toHaveBeenCalledTimes(1);
expect(readJSONSpy).toHaveBeenCalledWith(metadataFilePath);
expect(writeJSONSpy).not.toHaveBeenCalled();
expect(readFileSpy).toHaveBeenCalledTimes(1);
expect(readFileSpy).toHaveBeenCalledWith(metadataFilePath, 'utf8');
expect(writeFileSpy).not.toHaveBeenCalled();
expect(runCommandSpy).not.toHaveBeenCalled();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import filesystem from 'fs-extra';
import filesystem from 'fs/promises';
import { z } from 'zod';

import { MetadataFileEntry } from '@/config/release-config';
Expand All @@ -9,9 +9,12 @@ import { MetadataFileContent, Version } from '../types';
import { formatVersion, parseVersion } from './version';

async function readMetadataFileContent(file: MetadataFileEntry) {
const rawFileContent = (await filesystem.readJSON(file.path)) as unknown;
const stringifiedFileContent = await filesystem.readFile(file.path, 'utf8');
const rawFileContent = JSON.parse(stringifiedFileContent) as unknown;

const metadataFileSchema = z.object({ [file.versionKey]: z.string() }).passthrough();
metadataFileSchema.parse(rawFileContent);

return rawFileContent as MetadataFileContent satisfies z.infer<typeof metadataFileSchema>;
}

Expand All @@ -27,8 +30,9 @@ export function getPrimaryVersion(metadataFiles: MetadataFileEntry[], metadataFi
return primaryVersion;
}

async function writeMetadataFileContent(file: MetadataFileEntry, upgradedFileContent: MetadataFileContent) {
await filesystem.writeJSON(file.path, upgradedFileContent, { spaces: 2 });
async function writeMetadataFileContent(file: MetadataFileEntry, fileContent: MetadataFileContent) {
const stringifiedFileContent = JSON.stringify(fileContent, null, 2);
await filesystem.writeFile(file.path, stringifiedFileContent);
}

export async function writeMetadataFileContents(
Expand Down
7 changes: 4 additions & 3 deletions packages/release/src/config/release-config/release-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import filesystem from 'fs-extra';
import path from 'path';

import { pathExists } from '@/utils/files';

import { CONFIG_FILENAME, PACKAGE_JSON_FILENAME, importedReleaseConfigSchema } from './constants';
import { MissingReleaseConfigError, InvalidReleaseConfigError } from './errors';

Expand All @@ -10,14 +11,14 @@ async function findConfigFilePath() {
while (true) {
const configFilePath = path.join(directory, CONFIG_FILENAME);

const configFileExists = await filesystem.pathExists(configFilePath);
const configFileExists = await pathExists(configFilePath);
if (configFileExists) {
return configFilePath;
}

const packageJSONFilePath = path.join(directory, PACKAGE_JSON_FILENAME);

const packageJSONExists = await filesystem.pathExists(packageJSONFilePath);
const packageJSONExists = await pathExists(packageJSONFilePath);
if (packageJSONExists) {
throw new MissingReleaseConfigError();
}
Expand Down
10 changes: 10 additions & 0 deletions packages/release/src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import filesystem from 'fs/promises';

export async function pathExists(path: string) {
try {
await filesystem.access(path);
return true;
} catch {
return false;
}
}
Loading

0 comments on commit 4b6b110

Please sign in to comment.