Skip to content

Instantly share code, notes, and snippets.

@qgp9
Created July 24, 2017 09:28
Show Gist options
  • Save qgp9/888186d621896423a72d8e2e988adaa8 to your computer and use it in GitHub Desktop.
Save qgp9/888186d621896423a72d8e2e988adaa8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use File::Find;
use File::Copy;
use v5.14;
use strict;
my $path = shift @ARGV || '.';
find({wanted => \&wanted, preprocess => \&preprocess}, $path);
sub preprocess {
return grep { !/node_modules|components/ } @_;
}
sub wanted {
/^mutation-types--(.*?)--(.+)\.txt$/ or return;
print "====== source: $_";
my ($module, $target) = ($1, $2);
$module =~ s|-|/|g;
$module .= '/' if $module;
$target or die "No target in $_";
say "\t--module: $module\t--target: $target";
my $type_file = "mutation-types-$target.js";
my $js_file = "$target.js";
if (-M $type_file < -M $_) {
say " No canges, skip";
return
}
my @types;
@ARGV = $_;
push @types, $_ for map {s/^\s+//;s/\s*(#.*)$//;chomp;$_} grep {!/^\s*(#.*)?$/} <>;
open my $fh_type, '>', $type_file;
print $fh_type map { qq/export const $_ = '${module}$_'\n/ } @types;
close $fh_type;
@ARGV = $js_file;
my $data = join '', <>;
my $bak = $data;
my $new = ' import {' . (join ', ', @types).qq|} from './$type_file'\n|;
$data =~ s|^(//\s*BEGIN-INJECT-MUTATION$).*?(//\s*END)|$1\n$new$2|ms;
say " Backup $target.js to $target.js.bak";
copy "$target.js", "$js_file.bak" or die $!;
say " Inject constants to $target.js";
say " ***WARNING*** : check $target.js FIRST!!";
open my $fh_js, '>', "$target.js";
print $fh_js $data;
close $fh_js;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment