1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | (function() {
|
8 | var expandModuleNames, extendConfig, findFile, findFileResults, fs, getConfig, loadJSON, loadNpmConfig, path, resolve, stripComments;
|
9 |
|
10 | fs = require('fs');
|
11 |
|
12 | path = require('path');
|
13 |
|
14 | stripComments = require('strip-json-comments');
|
15 |
|
16 | resolve = require('resolve').sync;
|
17 |
|
18 | findFileResults = {};
|
19 |
|
20 | findFile = function(name, dir) {
|
21 | var filename, parent;
|
22 | dir = dir || process.cwd();
|
23 | filename = path.normalize(path.join(dir, name));
|
24 | if (findFileResults[filename]) {
|
25 | return findFileResults[filename];
|
26 | }
|
27 | parent = path.resolve(dir, '../');
|
28 | if (fs.existsSync(filename)) {
|
29 | return findFileResults[filename] = filename;
|
30 | } else if (dir === parent) {
|
31 | return findFileResults[filename] = null;
|
32 | } else {
|
33 | return findFile(name, parent);
|
34 | }
|
35 | };
|
36 |
|
37 | loadNpmConfig = function(dir) {
|
38 | var fp, ref;
|
39 | fp = findFile('package.json', dir);
|
40 | if (fp) {
|
41 | return (ref = loadJSON(fp)) != null ? ref.coffeelintConfig : void 0;
|
42 | }
|
43 | };
|
44 |
|
45 | loadJSON = function(filename) {
|
46 | var e, error;
|
47 | try {
|
48 | return JSON.parse(stripComments(fs.readFileSync(filename).toString()));
|
49 | } catch (error) {
|
50 | e = error;
|
51 | process.stderr.write("Could not load JSON file '" + filename + "': " + e);
|
52 | return null;
|
53 | }
|
54 | };
|
55 |
|
56 | getConfig = function(dir) {
|
57 | var envs, home, npmConfig, projConfig;
|
58 | if (process.env.COFFEELINT_CONFIG && fs.existsSync(process.env.COFFEELINT_CONFIG)) {
|
59 | return loadJSON(process.env.COFFEELINT_CONFIG);
|
60 | }
|
61 | npmConfig = loadNpmConfig(dir);
|
62 | if (npmConfig) {
|
63 | return npmConfig;
|
64 | }
|
65 | projConfig = findFile('coffeelint.json', dir);
|
66 | if (projConfig) {
|
67 | return loadJSON(projConfig);
|
68 | }
|
69 | envs = process.env.USERPROFILE || process.env.HOME || process.env.HOMEPATH;
|
70 | home = path.normalize(path.join(envs, 'coffeelint.json'));
|
71 | if (fs.existsSync(home)) {
|
72 | return loadJSON(home);
|
73 | }
|
74 | };
|
75 |
|
76 | expandModuleNames = function(dir, config) {
|
77 | var coffeelint, data, ruleName;
|
78 | for (ruleName in config) {
|
79 | data = config[ruleName];
|
80 | if (!((data != null ? data.module : void 0) != null)) {
|
81 | continue;
|
82 | }
|
83 | config[ruleName]._module = config[ruleName].module;
|
84 | config[ruleName].module = resolve(data.module, {
|
85 | basedir: dir
|
86 | });
|
87 | }
|
88 | coffeelint = config.coffeelint;
|
89 | if ((coffeelint != null ? coffeelint.transforms : void 0) != null) {
|
90 | coffeelint._transforms = coffeelint.transforms;
|
91 | coffeelint.transforms = coffeelint.transforms.map(function(moduleName) {
|
92 | return resolve(moduleName, {
|
93 | basedir: dir
|
94 | });
|
95 | });
|
96 | }
|
97 | if ((coffeelint != null ? coffeelint.coffeescript : void 0) != null) {
|
98 | coffeelint._coffeescript = coffeelint.coffeescript;
|
99 | coffeelint.coffeescript = resolve(coffeelint.coffeescript, {
|
100 | basedir: dir
|
101 | });
|
102 | }
|
103 | return config;
|
104 | };
|
105 |
|
106 | extendConfig = function(config) {
|
107 | var extendedConfig, parentConfig, rule, ruleName;
|
108 | if (!config["extends"]) {
|
109 | return config;
|
110 | }
|
111 | parentConfig = require(config["extends"]);
|
112 | extendedConfig = {};
|
113 | for (ruleName in config) {
|
114 | rule = config[ruleName];
|
115 | extendedConfig[ruleName] = rule;
|
116 | }
|
117 | for (ruleName in parentConfig) {
|
118 | rule = parentConfig[ruleName];
|
119 | extendedConfig[ruleName] = config[ruleName] || rule;
|
120 | }
|
121 | return extendedConfig;
|
122 | };
|
123 |
|
124 | exports.getConfig = function(filename) {
|
125 | var config, dir;
|
126 | if (filename == null) {
|
127 | filename = null;
|
128 | }
|
129 | if (filename) {
|
130 | dir = path.dirname(path.resolve(filename));
|
131 | } else {
|
132 | dir = process.cwd();
|
133 | }
|
134 | config = getConfig(dir);
|
135 | if (config) {
|
136 | config = extendConfig(config);
|
137 | config = expandModuleNames(dir, config);
|
138 | }
|
139 | return config;
|
140 | };
|
141 |
|
142 | }).call(this);
|