UNPKG

@codemod-utils/files

Version:
43 lines (42 loc) 1.06 kB
import { readdirSync, rmSync } from 'node:fs'; import { dirname, join } from 'node:path'; /** * Removes the directories specified in the file path, if they are * empty. * * ⚠️ Likely, you won't need this method but `removeFiles()` instead. * * @param filePath * * A file path. * * @param options * * An object with `projectRoot`. * * @example * * Remove the folder `ember-container-query` if it is empty. * * ```ts * const filePath = 'ember-container-query/LICENSE.md'; * * removeDirectoryIfEmpty(filePath, { * projectRoot, * }); * ``` */ export function removeDirectoryIfEmpty(filePath, options) { const { projectRoot } = options; const directories = dirname(filePath).split('/'); const depth = directories.length; for (let i = 0; i < depth; i++) { const directory = join(projectRoot, ...directories); const numFilesLeft = readdirSync(directory).length; if (numFilesLeft > 0) { continue; } rmSync(directory, { recursive: true }); directories.pop(); } }