mad_static.erl 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. -module(mad_static).
  2. -compile(export_all).
  3. main(_Config, ["min"]) ->
  4. SysConfig = try {ok,[S]} = file:consult("sys.config"), S catch _:_ -> [] end,
  5. N2O = proplists:get_value(n2o,SysConfig,[]),
  6. AppName = proplists:get_value(app,N2O,sample),
  7. Minify = proplists:get_value(minify,N2O,{[],[]}),
  8. Command = lists:concat(["uglify -s ",string:join(element(2,Minify),","),
  9. " -o ",element(1,Minify),"/",AppName,".min.js"]),
  10. case sh:run(Command) of
  11. {_,0,_} -> {ok,static};
  12. {_,_,_} -> mad:info("minifyjs not installed. try `npm install -g uglify`~n"),
  13. {error,"Minifier."}
  14. end;
  15. main(_, _) -> {error,"Arguments needed."}.
  16. replace(S,A,B) -> re:replace(S,A,B,[global,{return,list}]).
  17. app([]) -> app(["web","sample"]);
  18. app([Name]) -> app(["web",Name]);
  19. app([Skeleton,Name|_]) ->
  20. mad_repl:load(),
  21. Apps = ets:tab2list(filesystem),
  22. try
  23. [ begin
  24. case string:str(File,"priv/"++Skeleton) of
  25. 1 -> Relative = unicode:characters_to_list(
  26. Name ++ replace(replace(File,"sample",Name),"priv/"++Skeleton, []), utf8),
  27. mad:info("Created: ~p~n",[Relative]),
  28. filelib:ensure_dir(Relative),
  29. BinNew = replace(Bin, "sample", Name),
  30. file:write_file(Relative, BinNew);
  31. _ -> skip
  32. end end || {File,Bin} <- Apps, is_list(File)],
  33. {ok,Name}
  34. catch _:_ -> {error,"Skeleton failed."} end.