Advertisement
joebodo

apps.minify.lua

Sep 6th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. LUA_PATH = LUA_PATH .. ':/sys/apis/minify'
  2.  
  3. local util = require'Util'
  4. local Parser = require'ParseLua'
  5. local Format_Mini = require'FormatMini'
  6.  
  7. local ParseLua = Parser.ParseLua
  8. --local PrintTable = util.PrintTable
  9.  
  10. local function minify(inFilename, outFilename)
  11.     write(string.format('%s -> %s ', inFilename, outFilename))
  12.  
  13.     local sourceText = Util.readFile(inFilename)
  14.     if not sourceText then
  15.         printError("Failed to open `"..inFilename.."` for reading")
  16.         return
  17.     end
  18.  
  19.     local st, ast = ParseLua(sourceText)
  20.     if not st then
  21.         --we failed to parse the file, show why
  22.         printError(ast)
  23.         return
  24.     end
  25.  
  26.     local isize = fs.getSize(inFilename)
  27.  
  28.     local outf = io.open(outFilename, 'w')
  29.     if not outf then
  30.         printError("Failed to open `"..outFilename.."` for writing")
  31.         return
  32.     end
  33.  
  34.     outf:write(Format_Mini(ast))
  35.     outf:close()
  36.  
  37.     local osize = fs.getSize(outFilename)
  38.  
  39.     write(string.format('(%d%%)', 100 - math.floor(osize / isize * 100)))
  40.     print()
  41. end
  42.  
  43. local arg = { ... }
  44.  
  45. if #arg == 1 then
  46.     local inPath = Util.resolve(DIR, arg[1])
  47.  
  48.     if not fs.exists(inPath) then
  49.         error('File does not exist: ' .. inPath)
  50.     end
  51.     if fs.isDir(inPath) then
  52.         for _,filename in pairs(fs.list(inPath)) do
  53.             local rf = Util.resolve(inPath, filename)
  54.             if not fs.isDir(rf) then
  55.                 minify(rf, rf)
  56.             end
  57.         end
  58.     else
  59.         minify(inPath, inPath)
  60.     end
  61.  
  62. elseif #arg == 2 then
  63.  
  64.     minify(Util.resolve(DIR, arg[1]), Util.resolve(DIR, arg[2]))
  65.  
  66. else
  67.     error("Invalid arguments, Usage:\nminify source [destination]")
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement