I have a directory full of less css files. What is the best way to compile them to normal css? (for deployment)
Id like to run a command as follows:
I have written a hackish script that solves the problem:
#!/usr/bin/env python
#This is responsible for "compiling" less.css files to regular css files for production. It also minifies the css at the same time.
#Usage: give it a start directory as the first parameter and an end directory as the second parameter. It will recursivly run the appropriate command for all css files in the first subdirectory.
import os
import sys
import re
if len(sys.argv) < 3:
sys.exit('ERROR: Too many paths!! No less css compiled')
if len(sys.argv) > 3:
sys.exit('ERROR: Not enough paths!! No less css compiled')
start_dir=os.path.join(os.getcwd(),sys.argv[1])
end_dir=os.path.join(os.getcwd(),sys.argv[2])
pattern=r'\.css$'
pattern=re.compile(pattern)
files_compiled=0
def copy_files(start, end, add=''):
global files_compiled
try:
os.mkdir(end)
except:
pass
for folder in get_immediate_subdirectories(start):
copy_files(os.path.join(start,folder), os.path.join(end+folder), add+folder+'/')
for less in os.listdir(start):
if pattern.search(less):
os.system('lessc -x %s > %s'%(start+less,end+less))
print add+less
files_compiled=files_compiled+1
def get_immediate_subdirectories(dir):
return [name for name in os.listdir(dir)
if os.path.isdir(os.path.join(dir, name))]
Ideally there is a better solution.
Recently I've been experiencing issues with running something like
lessc directory/*.less foo.css
Instead of taking the different LESS's and outputting them into foo.css it would modify the second file in the list. This is not OK with me.
To solve this problem, I made a new less frontend called lessm
.
You can check it out at https://github.com/jive/lessm.
The way you'd use it is
lessm foo.less foo2.less ../bar/bazz.less -o output.css