UNPKG

@codemod-utils/files

Version:
44 lines (43 loc) 1.07 kB
import { join, normalize, sep } from 'node:path'; /** * Forms a new file path by altering the path's directory. * * @param filePath * * A file path. * * @param options * * An object with `from` and `to`. * * @return * * A file path. * * @example * * Prepare to move components from `addon` to `ember-container-query/src`. * * ```ts * const oldFilePath = 'addon/components/container-query.hbs'; * * const newFilePath = renamePathByDirectory(oldFilePath, { * from: 'addon', * to: 'ember-container-query/src', * }); * * // newFilePath -> 'ember-container-query/src/components/container-query.hbs' * ``` */ export function renamePathByDirectory(filePath, options) { filePath = normalize(filePath); const from = options.from === '' ? '' : normalize(options.from); const to = options.to === '' ? '' : normalize(options.to); if (from === '') { return join(to, filePath); } if (!filePath.startsWith(`${from}${sep}`)) { return filePath; } return join(to, filePath.replace(`${from}${sep}`, '')); }