Skip to content

Instantly share code, notes, and snippets.

@naholyr
Last active August 17, 2017 18:12
Show Gist options
  • Save naholyr/7598632 to your computer and use it in GitHub Desktop.
Save naholyr/7598632 to your computer and use it in GitHub Desktop.
Add "use strict" everywhere
find -name '*.js'|grep -v '^./node_modules'|while read f;do if (head -n3 "$f"|grep -Fv 'use strict'>/dev/null);then echo "$f";sed -i -e '1i "use strict"\n' "$f";fi;done
alias rigid="find -name '*.js'|grep -v '^./node_modules'|while read f;do if (head -n3 \"\$f\"|grep -Fv 'use strict'>/dev/null);then echo \"\$f\";sed -i -e '1i \"use strict\";\\n' \"\$f\";fi;done"
find -name '*.js' `# Find all .js files in current directory, recursively` | \
grep -v '^./node_modules' `# Exclude node_modules folder, you may want to add additional folders here` | \
while read f; do
if (head -n3 "$f" | grep -Fv 'use strict' > /dev/null); then `# Check first three lines, thought about shebang`
echo "$f"
sed -i '1i "use strict"\n' "$f" `# Prepend "use strict", that may break your executable scripts`
fi
done

BSD (including Mac OS X) version of sed is not GNU's one, including a subtle difference with in-place remplacements.

On BSD systems, you HAVE TO replace occurrence of sed -i -e with sed -i '' -e to make it work.

@advance512
Copy link

@rex, you are my hero :) Your final command has server me quite a bit.

Set it to use -n8, for our use case. (Comments come before use strict, though I guess it makes sense to see 'use strict' as the first line) Worked perfectly!

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