mad_static.erl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. -module(mad_static).
  2. -copyright('Yuri Artemev').
  3. -compile(export_all).
  4. -define(NODE(Bin), "node_modules/.bin/"++Bin).
  5. main(Config, ["watch"]) ->
  6. case mad_utils:get_value(static, Config, []) of
  7. [] -> false;
  8. SC ->
  9. Port = mad_utils:get_value(assets_port, SC, 3000),
  10. install_deps(), serve_static(Port)
  11. end;
  12. main(Config, _Params) ->
  13. case mad_utils:get_value(static, Config, []) of
  14. [] -> false;
  15. SC ->
  16. Files = mad_utils:get_value(files, SC, []),
  17. install_deps(), compile_static(Files)
  18. end.
  19. install_deps() ->
  20. case filelib:is_dir("node_modules/mincer-erl") of
  21. true -> false;
  22. _ ->
  23. case sh:oneliner("npm install mincer-erl") of
  24. {_,0,_} -> false;
  25. {_,_,_} -> io:format("error while installing mincer-erl~n"), true
  26. end
  27. end.
  28. % FIXME exit
  29. serve_static(Port) ->
  30. PortStr = integer_to_list(Port),
  31. Res = sh:oneliner([?NODE("mincer-erl-serve"), "-p " ++ PortStr]),
  32. case Res of
  33. {_,0,_} -> false;
  34. {_,_,_} -> io:format("error while serving assets~n"), true end.
  35. compile_static(Files) ->
  36. Res = sh:oneliner([?NODE("mincer-erl-compile")] ++ Files),
  37. case Res of
  38. {_,0,_} -> false;
  39. {_,_,_} -> io:format("error while compiling assets~n"), true end.