@codemod-utils/files
Version:
Utilities for handling files
37 lines (36 loc) • 1.02 kB
JavaScript
import { normalize } from 'node:path';
import { renamePathByDirectory } from './rename-path-by-directory.js';
/**
* Creates a mapping of file paths, which can then be passed to
* `copyFiles()` or `moveFiles()`.
*
* @param filePaths
*
* An array of file paths. The array may come from `findFiles()`.
*
* @param options
*
* An object with `from` and `to`.
*
* @example
*
* Map `LICENSE.md` to `ember-container-query/LICENSE.md` (and
* similarly for `README.md`).
*
* ```ts
* const filePaths = ['LICENSE.md', 'README.md'];
*
* const filePathMap = mapFilePaths(filePaths, {
* from: '',
* to: 'ember-container-query',
* });
* ```
*/
export function mapFilePaths(filePaths, options) {
const from = options.from === '' ? '' : normalize(options.from);
const to = options.to === '' ? '' : normalize(options.to);
return new Map(filePaths.map((oldFilePath) => {
const newFilePath = renamePathByDirectory(oldFilePath, { from, to });
return [oldFilePath, newFilePath];
}));
}