Skip to content

Commit

Permalink
rename returns a list of files that were renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Heiner committed May 2, 2022
1 parent 7e8c2ab commit b650943
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
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

0 comments on commit b650943

Please sign in to comment.