diff --git a/src/index.js b/src/index.js index e36857e..5be2dd8 100644 --- a/src/index.js +++ b/src/index.js @@ -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(); } diff --git a/src/modules/cleaner/cleaner.js b/src/modules/cleaner/cleaner.js index a978147..4783550 100644 --- a/src/modules/cleaner/cleaner.js +++ b/src/modules/cleaner/cleaner.js @@ -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 { @@ -13,35 +13,44 @@ export class Cleaner { } /** - * @param {string[]} files * @returns {Promise} */ - 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} */ - 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( diff --git a/src/modules/cleaner/cleaner.test.js b/src/modules/cleaner/cleaner.test.js index 4e11056..2bf7746 100644 --- a/src/modules/cleaner/cleaner.test.js +++ b/src/modules/cleaner/cleaner.test.js @@ -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(); });