|
| 1 | +# Beware of file paths |
| 2 | + |
| 3 | +Windows uses `\` (not `/`) for a path separator. While `node:path` provides `sep` and several methods to hide this pesky detail, you still need to be wary of these cases. |
| 4 | + |
| 5 | + |
| 6 | +## When to always use `/` |
| 7 | + |
| 8 | +### `codemodOptions` |
| 9 | + |
| 10 | +To improve user experience and simplify documentation, user-defined file and folder paths in `codemodOptions` (e.g. `projectRoot`) should always use `/`. |
| 11 | + |
| 12 | +This means, you should not call `normalize()` on such paths in your source code and tests. |
| 13 | + |
| 14 | +```diff |
| 15 | +/* tests/helpers/shared-test-setups/sample-project.ts */ |
| 16 | +- import { normalize } from 'node:path'; |
| 17 | +- |
| 18 | +import type { CodemodOptions, Options } from '../../../src/types/index.js'; |
| 19 | + |
| 20 | +const codemodOptions: CodemodOptions = { |
| 21 | +- projectRoot: normalize('tmp/sample-project'), |
| 22 | ++ projectRoot: 'tmp/sample-project', |
| 23 | +}; |
| 24 | + |
| 25 | +const options: Options = { |
| 26 | +- projectRoot: normalize('tmp/sample-project'), |
| 27 | ++ projectRoot: 'tmp/sample-project', |
| 28 | +}; |
| 29 | + |
| 30 | +export { codemodOptions, options }; |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +### `findFiles()` |
| 35 | + |
| 36 | +Glob pattern(s) use `/` as a path separator. |
| 37 | + |
| 38 | +```ts |
| 39 | +import { findFiles } from '@codemod-utils/files'; |
| 40 | + |
| 41 | +function updateTests(options: Options): void { |
| 42 | + const { projectRoot } = options; |
| 43 | + |
| 44 | + const filePaths = findFiles('tests/**/*-test.{gjs,gts,js,ts}', { |
| 45 | + ignoreList: ['**/*.d.ts'], |
| 46 | + projectRoot, |
| 47 | + }); |
| 48 | + |
| 49 | + // ... |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Since `join()` results in `\`'s on Windows, you should watch out for the code pattern `findFiles(join(...))`. |
| 54 | + |
| 55 | +```diff |
| 56 | +- import { join } from 'node:path'; |
| 57 | ++ import { join, sep } from 'node:path'; |
| 58 | + |
| 59 | +import { findFiles } from '@codemod-utils/files'; |
| 60 | + |
| 61 | ++ function normalizedJoin(...folders: string[]): string { |
| 62 | ++ return join(...folders).replaceAll(sep, '/'); |
| 63 | ++ } |
| 64 | ++ |
| 65 | +function updateHbs(options: Options): void { |
| 66 | + const { folder, projectRoot } = options; |
| 67 | + |
| 68 | +- const components = findFiles(join('app/components', folder, '**/*.hbs'), { |
| 69 | ++ const components = findFiles(normalizedJoin('app/components', folder, '**/*.hbs'), { |
| 70 | + projectRoot, |
| 71 | + }); |
| 72 | + |
| 73 | +- const routes = findFiles(join('app/templates', folder, '**/*.hbs'), { |
| 74 | ++ const routes = findFiles(normalizedJoin('app/templates', folder, '**/*.hbs'), { |
| 75 | + projectRoot, |
| 76 | + }); |
| 77 | + |
| 78 | + // ... |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +### Entity names in Ember |
| 84 | + |
| 85 | +Entity names are dasherized and use `/` for folders. If you need to derive the name from the file path, make sure to use `/`. |
| 86 | + |
| 87 | +```diff |
| 88 | +- import { join } from 'node:path'; |
| 89 | ++ import { join, relative, sep } from 'node:path'; |
| 90 | + |
| 91 | +import { parseFilePath } from '@codemod-utils/files'; |
| 92 | + |
| 93 | +function getModuleName(filePath: string): string { |
| 94 | + let { dir, name } = parseFilePath(filePath); |
| 95 | + |
| 96 | +- dir = dir.replace(/^tests\/acceptance(\/)?/, ''); |
| 97 | ++ dir = relative('tests/acceptance', dir); |
| 98 | + name = name.replace(/-test$/, ''); |
| 99 | + |
| 100 | +- const entityName = join(dir, name); |
| 101 | ++ const entityName = join(dir, name).replaceAll(sep, '/'); |
| 102 | + |
| 103 | + // a.k.a. friendlyTestDescription |
| 104 | + return ['Acceptance', entityName].join(' | '); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | +### Import paths |
| 110 | + |
| 111 | +Import statements in `*.{gjs,gts,js,ts}` files use `/` for the import path. If you need to derive the import path from some file paths, make sure to call `String.replaceAll(sep, '/')`. |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +## When not to use `/` |
| 116 | + |
| 117 | +### Calculations involving file paths |
| 118 | + |
| 119 | +When the input or output of a calculation is a file path, use `sep` or `normalize()` to get the correct path separator. |
| 120 | + |
| 121 | +```diff |
| 122 | ++ import { sep } from 'node:path'; |
| 123 | ++ |
| 124 | +function parseEntity( |
| 125 | + dir: string, |
| 126 | + folderToEntityType: Map<string, string>, |
| 127 | +): { |
| 128 | + entityType: string | undefined; |
| 129 | + remainingPath: string; |
| 130 | +} { |
| 131 | +- const [folder, ...remainingPaths] = dir.split('/'); |
| 132 | ++ const [folder, ...remainingPaths] = dir.split(sep); |
| 133 | + const entityType = folderToEntityType.get(folder!); |
| 134 | + |
| 135 | + if (entityType === undefined) { |
| 136 | + return { |
| 137 | + entityType, |
| 138 | + remainingPath: dir, |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + return { |
| 143 | + entityType, |
| 144 | +- remainingPath: remainingPaths.join('/'), |
| 145 | ++ remainingPath: remainingPaths.join(sep), |
| 146 | + }; |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +```diff |
| 151 | ++ import { normalize } from 'node:path'; |
| 152 | ++ |
| 153 | +import { parseFilePath } from '@codemod-utils/files'; |
| 154 | + |
| 155 | +function getClass(templateFilePath: string) { |
| 156 | + const { dir, ext, name } = parseFilePath(templateFilePath); |
| 157 | + |
| 158 | + const data = { |
| 159 | +- isRouteTemplate: dir.startsWith('app/templates'), |
| 160 | ++ isRouteTemplate: dir.startsWith(normalize('app/templates')), |
| 161 | + isTemplateTag: ext === '.gjs' || ext === '.gts', |
| 162 | + }; |
| 163 | + |
| 164 | + // ... |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +The same rule applies to tests. |
| 169 | + |
| 170 | +```diff |
| 171 | ++ import { normalize } from 'node:path'; |
| 172 | ++ |
| 173 | +import { assert, test } from '@codemod-utils/tests'; |
| 174 | + |
| 175 | +import { parseEntity } from '../../../../src/utils/rename-tests/index.js'; |
| 176 | + |
| 177 | +test('utils | rename-tests | parse-entity > base case', function () { |
| 178 | + const folderToEntityType = new Map([ |
| 179 | + ['components', 'Component'], |
| 180 | + ['helpers', 'Helper'], |
| 181 | + ['modifiers', 'Modifier'], |
| 182 | + ]); |
| 183 | + |
| 184 | + const output = parseEntity( |
| 185 | +- 'components/ui/form', |
| 186 | ++ normalize('components/ui/form'), |
| 187 | + folderToEntityType, |
| 188 | + ); |
| 189 | + |
| 190 | + assert.deepStrictEqual(output, { |
| 191 | + entityType: 'Component', |
| 192 | +- remainingPath: 'ui/form', |
| 193 | ++ remainingPath: normalize('ui/form'), |
| 194 | + }); |
| 195 | +}); |
| 196 | +``` |
| 197 | + |
| 198 | + |
| 199 | +<div align="center"> |
| 200 | + <div> |
| 201 | + Next: <a href="./02-beware-of-line-breaks.md">Beware of line breaks</a> |
| 202 | + </div> |
| 203 | + <div> |
| 204 | + Previous: <a href="./00-introduction.md">Introduction</a> |
| 205 | + </div> |
| 206 | +</div> |
0 commit comments