1 | "use strict";
|
2 |
|
3 | const beautify = require('js-beautify');
|
4 | const jdfUtils = require('jdf-utils');
|
5 | const $ = jdfUtils.base;
|
6 | const f = jdfUtils.file;
|
7 | const logger = require('jdf-log');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | exports.init = function(filename){
|
14 | if(!typeof(filename) !== 'undefined'){
|
15 | if(f.isDir(filename)){
|
16 | var filelist = f.getdirlist(filename, '(html|vm|tpl|css|scss|less|js)$');
|
17 |
|
18 | filelist.forEach(function(file){
|
19 | route(file);
|
20 | });
|
21 |
|
22 | }else{
|
23 | route(filename);
|
24 | }
|
25 | }
|
26 |
|
27 | function route(file){
|
28 | var exists = f.exists(file);
|
29 |
|
30 | if(exists){
|
31 | if($.is.html(file) || $.is.vm(file) || $.is.tpl(file)){
|
32 | htmlFormat(file);
|
33 |
|
34 | }else if($.is.css(file) || $.is.less(file) || $.is.sass(file)){
|
35 | cssFormat(file);
|
36 |
|
37 | }else if($.is.js(file)){
|
38 | jsFormat(file);
|
39 |
|
40 | }else{
|
41 | logger.warn('can not format the [' + file + '].\n');
|
42 | }
|
43 | }else{
|
44 | logger.error(file + ' may be not exist.');
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | function htmlFormat(filename){
|
50 | var content = f.read(filename);
|
51 |
|
52 | f.write(filename, beautify.html_beautify(content));
|
53 | logger.info('jdf format ['+filename+'] success');
|
54 | }
|
55 |
|
56 | function cssFormat(filename){
|
57 | var content = f.read(filename);
|
58 |
|
59 | f.write(filename, beautify.css_beautify(content));
|
60 | logger.info('jdf format ['+filename+'] success');
|
61 | }
|
62 |
|
63 | function jsFormat(filename){
|
64 | var content = f.read(filename);
|
65 |
|
66 | f.write(filename, beautify.js_beautify(content));
|
67 | logger.info('jdf format ['+filename+'] success');
|
68 | }
|