Created
June 28, 2022 20:00
-
-
Save dkarter/0bf9d615db8fad1fcbdf33c003ba23ee to your computer and use it in GitHub Desktop.
Migrate TypeScript/Javascript projects from relative to absolute paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
relative_to_absolute = fn relative_path, importing_file -> | |
Path.expand(relative_path, Path.dirname(importing_file)) | |
|> String.split("src/") | |
|> List.last() | |
end | |
Path.wildcard('src/**/*.{ts,tsx}') | |
|> Enum.each(fn file -> | |
IO.puts("Migrating #{file}...") | |
contents = File.read!(file) | |
migrated = | |
contents | |
|> String.split("\n") | |
|> Enum.map(fn line -> | |
Regex.replace(~r/from '(\.\.\/.+?)';/, line, fn _match, relative_path -> | |
"from '#{relative_to_absolute.(relative_path, file)}';" | |
end) | |
end) | |
|> Enum.join("\n") | |
File.write!(file, migrated) | |
IO.puts("DONE") | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example tsconfig:
Notably the
baseUrl
is set tosrc
which is where all the imported files live.