@@ -120,10 +120,10 @@ type Data = {
120120function getModuleName(filePath : string ): string {
121121 let { dir, name } = parseFilePath (filePath );
122122
123- dir = dir . replace ( / ^ tests\ / acceptance( \/ ) ? / , ' ' );
123+ dir = relative ( ' tests/acceptance' , dir );
124124 name = name .replace (/ -test$ / , ' ' );
125125
126- const entityName = join (dir , name );
126+ const entityName = join (dir , name ). replaceAll ( sep , ' / ' ) ;
127127
128128 // a.k.a. friendlyTestDescription
129129 return [' Acceptance' , entityName ].join (' | ' );
@@ -179,7 +179,7 @@ I highlighted only how `getModuleName()` and `renameModule()` differ between `re
179179
180180``` diff
181181import { readFileSync, writeFileSync } from 'node:fs';
182- import { join } from 'node:path';
182+ import { join, relative, sep } from 'node:path';
183183
184184import { AST } from '@codemod-utils/ast-javascript';
185185import { findFiles, parseFilePath } from '@codemod-utils/files';
@@ -194,11 +194,11 @@ type Data = {
194194function getModuleName(filePath: string): string {
195195 let { dir, name } = parseFilePath(filePath);
196196
197- - dir = dir.replace(/^ tests\ /acceptance(\/)?/, '' );
198- + dir = dir.replace(/^ tests\ /integration(\/)?/, '' );
197+ - dir = relative(' tests/acceptance', dir );
198+ + dir = relative(' tests/integration', dir );
199199 name = name.replace(/-test$/, '');
200200
201- const entityName = join(dir, name);
201+ const entityName = join(dir, name).replaceAll(sep, '/') ;
202202
203203 // a.k.a. friendlyTestDescription
204204- return ['Acceptance', entityName].join(' | ');
@@ -339,10 +339,10 @@ We say that an [iterative method](https://en.wikipedia.org/wiki/Iterative_method
339339function getModuleName(filePath: string): string {
340340 let { dir, name } = parseFilePath(filePath);
341341
342- dir = dir.replace(/^ tests\ /integration(\/)?/, ' ' );
342+ dir = relative( ' tests/integration' , dir );
343343 name = name.replace(/-test$/, ' ' );
344344
345- const entityName = join(dir, name);
345+ const entityName = join(dir, name).replaceAll(sep, ' / ' ) ;
346346
347347 // a.k.a. friendlyTestDescription
348348 return [' Integration' , entityName].join(' | ' );
@@ -355,7 +355,7 @@ Let's correct the overshoot by adding the entity type (here, we represent it as
355355function getModuleName(filePath: string): string {
356356 let { dir, name } = parseFilePath(filePath);
357357
358- dir = dir.replace(/^ tests\ / integration( \/ ) ? /, ' ' );
358+ dir = relative( ' tests/integration' , dir );
359359 name = name.replace(/-test$/, ' ' );
360360
361361 const entityType = /* ... * /;
@@ -387,6 +387,8 @@ const entityName = join(remainingPath, name);
387387Let me first show you the solution for ` parseEntity()` , then explain the particular approach.
388388
389389` ` ` ts
390+ import { sep } from ' node:path' ;
391+
390392const folderToEntityType = new Map([
391393 [' components' , ' Component' ],
392394 [' helpers' , ' Helper' ],
@@ -397,12 +399,12 @@ function parseEntity(dir: string): {
397399 entityType: string | undefined;
398400 remainingPath: string;
399401} {
400- const [folder, ...remainingPaths] = dir.split(' / ' );
402+ const [folder, ...remainingPaths] = dir.split(sep );
401403 const entityType = folderToEntityType.get(folder! );
402404
403405 return {
404406 entityType,
405- remainingPath: remainingPaths.join(' / ' ),
407+ remainingPath: remainingPaths.join(sep ),
406408 };
407409}
408410` ` `
@@ -419,7 +421,7 @@ The implementations for `renameModule()` and `renameIntegrationTests()` remain u
419421
420422```diff
421423import { readFileSync, writeFileSync } from ' node:fs' ;
422- import { join } from ' node:path' ;
424+ import { join, relative, sep } from ' node:path' ;
423425
424426import { AST } from ' @codemod-utils/ast-javascript' ;
425427import { findFiles, parseFilePath } from ' @codemod-utils/files' ;
@@ -441,24 +443,24 @@ type Data = {
441443+ entityType: string | undefined;
442444+ remainingPath: string;
443445+ } {
444- + const [folder, ...remainingPaths] = dir.split(' / ' );
446+ + const [folder, ...remainingPaths] = dir.split(sep );
445447+ const entityType = folderToEntityType.get(folder!);
446448+
447449+ return {
448450+ entityType,
449- + remainingPath: remainingPaths.join(' / ' ),
451+ + remainingPath: remainingPaths.join(sep ),
450452+ };
451453+ }
452454+
453455function getModuleName(filePath: string): string {
454456 let { dir, name } = parseFilePath(filePath);
455457
456- dir = dir.replace(/^ tests\ /integration(\/)?/, ' ' );
458+ dir = relative( ' tests/integration' , dir );
457459 name = name.replace(/-test$/, ' ' );
458460
459- - const entityName = join(dir, name);
461+ - const entityName = join(dir, name).replaceAll(sep, ' / ' ) ;
460462+ const { entityType, remainingPath } = parseEntity(dir);
461- + const entityName = join(remainingPath, name);
463+ + const entityName = join(remainingPath, name).replaceAll(sep, ' / ' ) ;
462464
463465 // a.k.a. friendlyTestDescription
464466- return [' Integration' , entityName].join(' | ' );
0 commit comments