-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcsscomb.js
More file actions
136 lines (109 loc) · 4.27 KB
/
csscomb.js
File metadata and controls
136 lines (109 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* grunt-csscomb
* https://github.com/csscomb/grunt-csscomb
*
* Copyright (c) 2013 Koji Ishimoto, contributors
* Licensed under the MIT license.
*/
'use strict';
var path = require('path');
module.exports = function (grunt) {
grunt.registerMultiTask('csscomb', 'Sorting CSS properties in specific order.', function () {
var Comb = require('csscomb'),
comb = new Comb(),
HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
function getConfigPath(configPath) {
var dirname, parentDirname;
configPath = configPath || path.join(process.cwd(), '.csscomb.json');
// If we've finally found a config, return its path:
if (grunt.file.exists(configPath)) {
return configPath;
}
dirname = path.dirname(configPath);
parentDirname = path.dirname(dirname);
// If we are in HOME dir already and yet no config file, quit.
// If project is located not under HOME, compare to root instead.
// Since there appears to be no good way to get root path in
// Windows, assume that if current dir has no parent dir, we're in
// root.
if (dirname === HOME || dirname === parentDirname) {
return;
}
// If there is no config in this directory, go one level up and look for
// a config there:
configPath = path.join(parentDirname, '.csscomb.json');
return getConfigPath(configPath);
}
// Get config file from task's options:
var config = grunt.task.current.options().config || getConfigPath();
// Check if config file is set and exists. If not, use default one:
if (config && grunt.file.exists(config)) {
grunt.log.ok('Using custom config file "' + config + '"...');
config = grunt.file.readJSON(config);
} else {
grunt.log.ok('Using default config file...');
config = Comb.getConfig('csscomb');
}
// Configure csscomb:
comb.configure(config);
this.files.forEach(function (f) {
f.src.filter(function (filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
}).forEach(function (src) {
// Placeholder for our css content
var css;
var combed;
if (shouldProcess(src, config)) {
// Get CSS from a source file:
css = grunt.file.read(src);
// Comb it:
grunt.log.ok('Sorting file "' + src + '"...');
var syntax = src.split('.').pop();
try {
combed = comb.processString(css, { syntax: syntax });
grunt.file.write(f.dest, combed);
} catch(e) {
grunt.log.error(e);
}
} else {
grunt.log.ok('Skipping file "' + src + '" because of exclude.');
grunt.file.copy(src, f.dest);
}
});
});
});
function shouldProcess(src, config) {
var excludes = zip(
(config.exclude || []).map(function(pattern) {
return grunt.file.expand(pattern);
})
);
var ok = true;
if (excludes) {
var found = false;
src = src.replace(/^\.\//, '');
for (var i = 0, excludeLength = excludes.length; i < excludeLength && !found; i++) {
if (excludes[i].match(src)) {
found = true;
}
}
ok = ok && !found;
}
return ok;
}
function zip(arrays) {
var returnArray = [];
arrays.forEach(function(value) {
value.forEach(function(item) {
returnArray.push(item);
});
});
return returnArray;
}
};