PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/atom-compile-less.coffee

https://github.com/freshl/atom-compile-less
CoffeeScript | 81 lines | 43 code | 26 blank | 12 comment | 8 complexity | 09aa1d095abb672bcec3bcceadaeb06a MD5 | raw file
  1. # REQUIRES
  2. less = require "less"
  3. fs = require "fs"
  4. path = require "path"
  5. # loophole fix
  6. # thanks to yhsiang http://discuss.atom.io/t/atom-content-security-policy-error/4425/5
  7. {Function} = require "loophole"
  8. # HELPER FUNCTIONS
  9. getFileContents = (filePath, callback) ->
  10. content = ''
  11. fs.readFile filePath, 'utf-8', read = (err, data) ->
  12. throw err if err
  13. callback data
  14. # MAIN FUNCTIONS
  15. compileFile = (filepath) ->
  16. outputCompressed = atom.config.get('atom-compile-less.compressCss')
  17. # COMPILE LESS TO CSS
  18. getFileContents filepath, (content) ->
  19. throw err if !content
  20. parser = new less.Parser({ paths: [path.dirname filepath] })
  21. parser.parse(content, (err, parsedContent) =>
  22. throw err if err
  23. outputCss = parsedContent.toCSS({ compress: outputCompressed })
  24. cssFilePath = filepath.replace(".less", ".css")
  25. # SAVE COMPILED FILE
  26. fs.writeFile( cssFilePath, outputCss, (err) ->
  27. console.log "FAILED TO COMPILE LESS: " + cssFilePath, err if err
  28. console.log "LESS FILE COMPILED TO: " + cssFilePath
  29. )
  30. )
  31. atomCompileLess = ->
  32. currentEditor = atom.workspace.getActiveEditor()
  33. if currentEditor
  34. # SET COMPILE VARS
  35. currentFilePath = currentEditor.getPath()
  36. if currentFilePath.substr(-4) == "less"
  37. # SET CONFIG VARS
  38. projectPath = atom.project.getPath()
  39. projectMainLess = atom.project.getPath() + atom.config.get('atom-compile-less.mainLessFile')
  40. includeMainFile = atom.config.get('atom-compile-less.compileMainFile')
  41. # COMPILE FILE
  42. compileFile currentFilePath
  43. compileFile projectMainLess if includeMainFile
  44. #loophole fix
  45. global.Function = Function
  46. # MODULE EXPORT
  47. module.exports =
  48. configDefaults:
  49. mainLessFile: "/main.less",
  50. compileMainFile: true,
  51. compressCss: true
  52. activate: (state) =>
  53. atom.workspaceView.command "core:save", => atomCompileLess()
  54. deactivate: ->
  55. serialize: ->