Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@kblestarge
Copy link

kblestarge commented Apr 25, 2017

Should this be run on the project root level or somewhere else? I tried it on the root but nothing happened... maybe it was intended to be executed from within the src folder? or the app folder?

I'm running it on macOs Sierra v10.12.2

@netmikey
Copy link
Author

@kblestarge Yes, it should be run on the project root level. It builds the project once to gather the full error output, so it might take a couple of minutes (depending on your build speed) before something happens. I used it with angular-cli 1.0.0.

@kblestarge
Copy link

@angular/cli: 1.0.0
node: 6.9.3
os: darwin x64
@angular/animations: 4.0.2
@angular/common: 4.0.2
@angular/compiler: 4.0.2
@angular/compiler-cli: 4.0.2
@angular/core: 4.0.2
@angular/forms: 4.0.2
@angular/http: 4.0.2
@angular/material: 2.0.0-beta.3
@angular/platform-browser: 4.0.2
@angular/platform-browser-dynamic: 4.0.2
@angular/platform-server: 4.0.2
@angular/router: 4.0.2
@angular/cli: 1.0.0

I'm using angular-cli 1.0.0 also, but it doesn't seem to be working for me... Is there some kind of error log I can open after I run the script to see what is wrong? Running the script takes less than a second and there aren't any changes to the code as a result. I don't know what is wrong...

@kblestarge
Copy link

sh_angular

@netmikey
Copy link
Author

@kblestarge sorry for not responding in so long, somehow I didn't get notified about the activity in this gist...

Could you try maybe copy/pasting the 3 lines one by one into the terminal? Maybe you'll see more output or some hint that way...

@devongermano
Copy link

devongermano commented Aug 29, 2017

I'm using Angular-CLI 1.3.1. Replacing the first line with:
ng build --prod 2>&1 | grep -E "is .* and only accessible within class" | tee errors.out.tmp
(tee just so I can see when it finishes) worked for me.

@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