Skip to content

Commit

Permalink
Adjust clean up script
Browse files Browse the repository at this point in the history
  • Loading branch information
Domi04151309 committed Jul 11, 2024
1 parent 58f8ed3 commit a70d69f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ export async function build(options) {
const entryPoints = await bundler.getEntryPoints(files);
await bundler.bundle(entryPoints);

await new Cleaner(buildOptions).cleanUp(files);
await new Cleaner(buildOptions).cleanUp();
}
29 changes: 19 additions & 10 deletions src/modules/cleaner/cleaner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile, unlink } from 'node:fs/promises';
import { readFile, readdir, unlink } from 'node:fs/promises';
import path from 'node:path';

export class Cleaner {
Expand All @@ -13,35 +13,44 @@ export class Cleaner {
}

/**
* @param {string[]} files
* @returns {Promise<FileInfo[]>}
*/
async #getFiles(files) {
async #getFiles() {
const directoryEntries = await readdir(
this.#options.outDirectory,
{ recursive: true, withFileTypes: true }
);

return await Promise.all(
files
directoryEntries
.filter(
entry => entry.isFile() &&
!this.#options.thirdPartyModules.some(
ignore => path.join(entry.parentPath, entry.name).includes(ignore)
)
)
.map(entry => path.join(entry.parentPath, entry.name))
.filter(
file => !this.#options.thirdPartyModules.some(
ignore => file.includes(ignore)
)
)
.map(async file => {
const realPath = path.join(this.#options.outDirectory, file);
const buffer = await readFile(realPath);
const buffer = await readFile(file);
return {
content: buffer.toString(),
path: realPath
path: file
};
})
);
}

/**
* @param {string[]} files
* @returns {Promise<void>}
*/
async cleanUp(files) {
async cleanUp() {
const promises = [];
const allFiles = await this.#getFiles(files);
const allFiles = await this.#getFiles();

let searchableFiles = allFiles.filter(
file => this.#options.cleaner.searchableExtensions.some(
Expand Down
12 changes: 10 additions & 2 deletions src/modules/cleaner/cleaner.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { describe, expect, it, vi } from 'vitest';
import { readFile, unlink } from 'node:fs/promises';
import { readFile, readdir, unlink } from 'node:fs/promises';
import { BUILD_OPTIONS } from '../shared/test-utils.js';
import { Cleaner } from './cleaner.js';

vi.mock('node:fs/promises', () => ({
readFile: vi.fn(() => Promise.resolve(Buffer.from(''))),
readdir: vi.fn(() => Promise.resolve([
{
isFile: () => true,
name: '.js',
parentPath: ''
}
])),
unlink: vi.fn(() => Promise.resolve())
}));

describe('cleaning', () => {
it('should clean up', async () => {
const cleaner = new Cleaner(BUILD_OPTIONS);
await cleaner.cleanUp(['./README.md', './src/index.js']);
await cleaner.cleanUp();

expect(readdir).toHaveBeenCalled();
expect(readFile).toHaveBeenCalled();
expect(unlink).toHaveBeenCalled();
});
Expand Down

0 comments on commit a70d69f

Please sign in to comment.