Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dedeibel/65ebfc4ccb53a758952e4d812c832831 to your computer and use it in GitHub Desktop.
Save dedeibel/65ebfc4ccb53a758952e4d812c832831 to your computer and use it in GitHub Desktop.
Converts module relative Agular 2 / Typescript Imports to tsconfig.json relative ones
#!/bin/perl
#
# Run on unix like Systems
#
# Execute this from the directory where your "tsconfig.json" and "app" resides as following:
#
# find . -name "*.ts" | xargs -n 1 perl convert.pl
#
# Add this to tslint.json 'compilerOptions'
#
# "paths": {
# "app/*": [
# "./app/*"
# ]
# }
#
# Works for me.
#
use Cwd;
use File::Basename;
use File::Copy;
$cwd=cwd();
$file=shift;
$dir=dirname($file);
open(my $fh, '<', "$file") or die "open failed: $!";
open(my $ofh, '>', "$file.tmp") or die "open failed: $!";
while (<$fh>) {
my $line = $_;
if ($line =~ /import\s+\{.+\}\s+from\s+'(\..*)';/) {
my $oldpath = $1;
my $relpath = $dir .'/'. $1 . '.ts';
my $path = `realpath "$relpath"`
or die "path not found $relpath";
$path =~ s/^$cwd\///;
$path =~ s/\.ts$//;
chomp($path);
$line =~ s/$oldpath/$path/;
print $ofh $line;
}
else {
print $ofh $line;
}
}
move("$file.tmp", $file)
or die "could not move $file.tmp";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment