Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename plugin: pass back a list of modified files #172

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/ts-migrate/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ yargs
(args) => {
const rootDir = path.resolve(process.cwd(), args.folder);
const { sources } = args;
const exitCode = rename({ rootDir, sources });
process.exit(exitCode);
const renamedFiles = rename({ rootDir, sources });
if (renamedFiles === null) {
process.exit(-1);
}
},
)
.command(
Expand Down
13 changes: 8 additions & 5 deletions packages/ts-migrate/commands/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@ interface RenameParams {
sources?: string | string[];
}

export default function rename({ rootDir, sources }: RenameParams): number {
export default function rename({
rootDir,
sources,
}: RenameParams): Array<{ oldFile: string; newFile: string }> | null {
const configFile = path.resolve(rootDir, 'tsconfig.json');
if (!fs.existsSync(configFile)) {
log.error('Could not find tsconfig.json at', configFile);
return -1;
return null;
}

let jsFiles: string[];
try {
jsFiles = findJSFiles(rootDir, configFile, sources);
} catch (err) {
log.error(err);
return -1;
return null;
}

if (jsFiles.length === 0) {
log.info('No JS/JSX files to rename.');
return 0;
return [];
}

const toRename = jsFiles
Expand All @@ -55,7 +58,7 @@ export default function rename({ rootDir, sources }: RenameParams): number {
updateProjectJson(rootDir);

log.info('Done.');
return 0;
return toRename;
}

function findJSFiles(rootDir: string, configFile: string, sources?: string | string[]) {
Expand Down
30 changes: 28 additions & 2 deletions packages/ts-migrate/tests/commands/rename/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,36 @@ describe('rename command', () => {
const outputDir = path.resolve(__dirname, 'output');
copyDir(inputDir, rootDir);

const exitCode = rename({ rootDir });
const renamedFiles = rename({ rootDir });
const renamedFilesRelativeToRootDir = renamedFiles
?.map(({ oldFile, newFile }) => {
const filePathRelativeToRootDir = (filePath: string) => path.relative(rootDir, filePath);
return {
oldFile: filePathRelativeToRootDir(oldFile),
newFile: filePathRelativeToRootDir(newFile),
};
})
.sort((a, b) => {
const getSortKey = ({ oldFile, newFile }: typeof a) => oldFile + newFile;
const sortKeyA = getSortKey(a);
const sortKeyB = getSortKey(b);

if (sortKeyA === sortKeyB) {
return 0;
}

return sortKeyA < sortKeyB ? -1 : 1;
});

const [rootData, outputData] = getDirData(rootDir, outputDir);
expect(rootData).toEqual(outputData);
expect(exitCode).toBe(0);
expect(renamedFilesRelativeToRootDir).toEqual([
{ oldFile: 'dir-a/file-2.js', newFile: 'dir-a/file-2.ts' },
{ oldFile: 'dir-a/file-3.jsx', newFile: 'dir-a/file-3.tsx' },
{ oldFile: 'dir-a/file-4.js', newFile: 'dir-a/file-4.tsx' },
{ oldFile: 'dir-a/file-5.js', newFile: 'dir-a/file-5.tsx' },
{ oldFile: 'dir-a/file-6.js', newFile: 'dir-a/file-6.tsx' },
{ oldFile: 'file-1.js', newFile: 'file-1.ts' },
]);
});
});