Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save netmikey/cb29ac391953f2c67aa102b28fd4fe16 to your computer and use it in GitHub Desktop.
Save netmikey/cb29ac391953f2c67aa102b28fd4fe16 to your computer and use it in GitHub Desktop.
A shell command to make variables used in angular templates public
#
# A macOS shell command to patch large code bases and make variables used in
# templates public.
# To be executed from the project root. It uses the error output of the
# angular-cli build command. angular-cli must be installed in version 1.0.0.
#
# Yes, this is awfully hacky and changes should be reviewed - but as always:
# this commes at best effort and without any warranties, you should use git
# anyway ;)
#
# For the discussion, see https://github.com/angular/angular-cli/issues/5620
#
npm run build -- -aot 2>&1 | grep "is .* and only accessible within class" > errors.out.tmp
cat errors.out.tmp | grep "ng://" | sed 's@^.*ERROR in @@' | sed -E 's@ng://([^ ]+) \([0-9]+,[0-9]+\): Property '\''([^'\'']+)'\''[^'\'']+'\''([^'\'']+).*@\1 \2@' | sed -E 's@.html @.ts @' | awk '!x[$0]++' | sed -E 's@([^ ]+) ([^ ]+)@sed -i '\'''\'' -E '\''s/(private|protected) \2([:\( ])/public \2\\2/'\'' \1@' | while read -r line; do sh -c "$line"; done
cat errors.out.tmp | grep "\$\$_gendir.*.ngfactory.ts " | sed 's@^.*ERROR in @@' | sed 's@\$\$_gendir/\(.*\).ngfactory@\1@' | sed -E 's@([^ ]+) \([0-9]+,[0-9]+\): Property '\''([^'\'']+)'\''[^'\'']+'\''([^'\'']+).*@\1 \2@' | awk '!x[$0]++' | sed -E 's@([^ ]+) ([^ ]+)@sed -i '\'''\'' -E '\''s/private \2([: ])/public \2\\1/'\'' \1@' | while read -r line; do sh -c "$line"; done
rm errors.out.tmp
@tmmueller
Copy link

Many thanks! This version worked on Windows in git bash, and I added a bit to handle readonly properties.

ng build --prod 2>&1 | grep -E "is .* and only accessible within class" | sed 's/ng:///C://ng:///c//g' | tee errors.out.tmp

cat errors.out.tmp | grep "ng://" | sed 's@^.ERROR in @@' | sed -E 's@ng://([^ ]+) ([0-9]+,[0-9]+): Property '''([^'\'']+)'''[^'\'']+'''([^'\'']+).@\1 \2@' | sed -E 's@.html @.ts @' | awk '!x[$0]++' | sed -E 's@([^ ]+) ([^ ]+)@Sed -i -E '''s/(private|protected)( readonly)? \2([:( ])/public\2 \2\3/''' \1@' | while read -r line; do sh -c "$line"; done
cat errors.out.tmp | grep "$$_gendir..ngfactory.ts " | sed 's@^.ERROR in @@' | sed 's@$$_gendir/(.).ngfactory@\1@' | sed -E 's@([^ ]+) ([0-9]+,[0-9]+): Property '''([^'\'']+)'''[^'\'']+'''([^'\'']+).@\1 \2@' | awk '!x[$0]++' | sed -E 's@([^ ]+) ([^ ]+)@Sed -i -E '''s/private \2([: ])/public \2\1/''' \1@' | while read -r line; do sh -c "$line"; done
rm errors.out.tmp

@arturohernandez10
Copy link

You could use a modern IDE and run the fixes for this inspection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment