Skip to content

Instantly share code, notes, and snippets.

@vidhav
Created December 23, 2015 09:32
Show Gist options
  • Save vidhav/6e3df0a0839ec96df9b6 to your computer and use it in GitHub Desktop.
Save vidhav/6e3df0a0839ec96df9b6 to your computer and use it in GitHub Desktop.
Use jsmin() to minify a js-file or a folder containing js-files.
import os;
import sys;
from jsmin import jsmin;
if len(sys.argv) < 2:
print '- Requires .js-file or directory containing js.files';
exit();
input = os.path.abspath(sys.argv[1]);
name = os.path.splitext(input)[0] + '.min.js';
files = [];
if os.path.isfile(input):
files.append(input);
if os.path.isdir(input):
for file in os.listdir(input):
if file.endswith('js'):
files.append(os.path.join(input, file));
if len(files) == 0:
print '- No files';
else:
content = '';
for file in files:
with open(file) as data:
minified = data.read();
content += jsmin(minified);
if not content:
print '- Nothing was done...';
else:
f = open(name, 'w');
f.write(content.replace('\n', '').replace('\r', ''));
f.close();
print '- Success';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment