1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
11 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
12 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13 | function verb(n) { return function (v) { return step([n, v]); }; }
|
14 | function step(op) {
|
15 | if (f) throw new TypeError("Generator is already executing.");
|
16 | while (_) try {
|
17 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
18 | if (y = 0, t) op = [op[0] & 2, t.value];
|
19 | switch (op[0]) {
|
20 | case 0: case 1: t = op; break;
|
21 | case 4: _.label++; return { value: op[1], done: false };
|
22 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
23 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24 | default:
|
25 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29 | if (t[2]) _.ops.pop();
|
30 | _.trys.pop(); continue;
|
31 | }
|
32 | op = body.call(thisArg, _);
|
33 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35 | }
|
36 | };
|
37 | var __importStar = (this && this.__importStar) || function (mod) {
|
38 | if (mod && mod.__esModule) return mod;
|
39 | var result = {};
|
40 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
41 | result["default"] = mod;
|
42 | return result;
|
43 | };
|
44 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
45 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
46 | };
|
47 | Object.defineProperty(exports, "__esModule", { value: true });
|
48 | var fs = __importStar(require("fs"));
|
49 | var path = __importStar(require("path"));
|
50 | var semver = __importStar(require("semver"));
|
51 | var fnmatch_1 = __importDefault(require("./lib/fnmatch"));
|
52 | var ini_1 = require("./lib/ini");
|
53 | exports.parseString = ini_1.parseString;
|
54 | var package_json_1 = __importDefault(require("../package.json"));
|
55 | var knownProps = {
|
56 | end_of_line: true,
|
57 | indent_style: true,
|
58 | indent_size: true,
|
59 | insert_final_newline: true,
|
60 | trim_trailing_whitespace: true,
|
61 | charset: true,
|
62 | };
|
63 | function fnmatch(filepath, glob) {
|
64 | var matchOptions = { matchBase: true, dot: true, noext: true };
|
65 | glob = glob.replace(/\*\*/g, '{*,**/**/**}');
|
66 | return fnmatch_1.default(filepath, glob, matchOptions);
|
67 | }
|
68 | function getConfigFileNames(filepath, options) {
|
69 | var paths = [];
|
70 | do {
|
71 | filepath = path.dirname(filepath);
|
72 | paths.push(path.join(filepath, options.config));
|
73 | } while (filepath !== options.root);
|
74 | return paths;
|
75 | }
|
76 | function processMatches(matches, version) {
|
77 |
|
78 |
|
79 | if ('indent_style' in matches
|
80 | && matches.indent_style === 'tab'
|
81 | && !('indent_size' in matches)
|
82 | && semver.gte(version, '0.10.0')) {
|
83 | matches.indent_size = 'tab';
|
84 | }
|
85 |
|
86 |
|
87 | if ('indent_size' in matches
|
88 | && !('tab_width' in matches)
|
89 | && matches.indent_size !== 'tab') {
|
90 | matches.tab_width = matches.indent_size;
|
91 | }
|
92 |
|
93 | if ('indent_size' in matches
|
94 | && 'tab_width' in matches
|
95 | && matches.indent_size === 'tab') {
|
96 | matches.indent_size = matches.tab_width;
|
97 | }
|
98 | return matches;
|
99 | }
|
100 | function processOptions(options, filepath) {
|
101 | if (options === void 0) { options = {}; }
|
102 | return {
|
103 | config: options.config || '.editorconfig',
|
104 | version: options.version || package_json_1.default.version,
|
105 | root: path.resolve(options.root || path.parse(filepath).root),
|
106 | };
|
107 | }
|
108 | function buildFullGlob(pathPrefix, glob) {
|
109 | switch (glob.indexOf('/')) {
|
110 | case -1:
|
111 | glob = '**/' + glob;
|
112 | break;
|
113 | case 0:
|
114 | glob = glob.substring(1);
|
115 | break;
|
116 | default:
|
117 | break;
|
118 | }
|
119 | return path.join(pathPrefix, glob);
|
120 | }
|
121 | function extendProps(props, options) {
|
122 | if (props === void 0) { props = {}; }
|
123 | if (options === void 0) { options = {}; }
|
124 | for (var key in options) {
|
125 | if (options.hasOwnProperty(key)) {
|
126 | var value = options[key];
|
127 | var key2 = key.toLowerCase();
|
128 | var value2 = value;
|
129 | if (knownProps[key2]) {
|
130 | value2 = value.toLowerCase();
|
131 | }
|
132 | try {
|
133 | value2 = JSON.parse(value);
|
134 | }
|
135 | catch (e) { }
|
136 | if (typeof value === 'undefined' || value === null) {
|
137 |
|
138 |
|
139 | value2 = String(value);
|
140 | }
|
141 | props[key2] = value2;
|
142 | }
|
143 | }
|
144 | return props;
|
145 | }
|
146 | function parseFromConfigs(configs, filepath, options) {
|
147 | return processMatches(configs
|
148 | .reverse()
|
149 | .reduce(function (matches, file) {
|
150 | var pathPrefix = path.dirname(file.name);
|
151 | file.contents.forEach(function (section) {
|
152 | var glob = section[0];
|
153 | var options2 = section[1];
|
154 | if (!glob) {
|
155 | return;
|
156 | }
|
157 | var fullGlob = buildFullGlob(pathPrefix, glob);
|
158 | if (!fnmatch(filepath, fullGlob)) {
|
159 | return;
|
160 | }
|
161 | matches = extendProps(matches, options2);
|
162 | });
|
163 | return matches;
|
164 | }, {}), options.version);
|
165 | }
|
166 | function getConfigsForFiles(files) {
|
167 | var configs = [];
|
168 | for (var i in files) {
|
169 | if (files.hasOwnProperty(i)) {
|
170 | var file = files[i];
|
171 | var contents = ini_1.parseString(file.contents);
|
172 | configs.push({
|
173 | name: file.name,
|
174 | contents: contents,
|
175 | });
|
176 | if ((contents[0][1].root || '').toLowerCase() === 'true') {
|
177 | break;
|
178 | }
|
179 | }
|
180 | }
|
181 | return configs;
|
182 | }
|
183 | function readConfigFiles(filepaths) {
|
184 | return __awaiter(this, void 0, void 0, function () {
|
185 | return __generator(this, function (_a) {
|
186 | return [2 , Promise.all(filepaths.map(function (name) { return new Promise(function (resolve) {
|
187 | fs.readFile(name, 'utf8', function (err, data) {
|
188 | resolve({
|
189 | name: name,
|
190 | contents: err ? '' : data,
|
191 | });
|
192 | });
|
193 | }); }))];
|
194 | });
|
195 | });
|
196 | }
|
197 | function readConfigFilesSync(filepaths) {
|
198 | var files = [];
|
199 | var file;
|
200 | filepaths.forEach(function (filepath) {
|
201 | try {
|
202 | file = fs.readFileSync(filepath, 'utf8');
|
203 | }
|
204 | catch (e) {
|
205 | file = '';
|
206 | }
|
207 | files.push({
|
208 | name: filepath,
|
209 | contents: file,
|
210 | });
|
211 | });
|
212 | return files;
|
213 | }
|
214 | function opts(filepath, options) {
|
215 | if (options === void 0) { options = {}; }
|
216 | var resolvedFilePath = path.resolve(filepath);
|
217 | return [
|
218 | resolvedFilePath,
|
219 | processOptions(options, resolvedFilePath),
|
220 | ];
|
221 | }
|
222 | function parseFromFiles(filepath, files, options) {
|
223 | if (options === void 0) { options = {}; }
|
224 | return __awaiter(this, void 0, void 0, function () {
|
225 | var _a, resolvedFilePath, processedOptions;
|
226 | return __generator(this, function (_b) {
|
227 | _a = opts(filepath, options), resolvedFilePath = _a[0], processedOptions = _a[1];
|
228 | return [2 , files.then(getConfigsForFiles)
|
229 | .then(function (configs) { return parseFromConfigs(configs, resolvedFilePath, processedOptions); })];
|
230 | });
|
231 | });
|
232 | }
|
233 | exports.parseFromFiles = parseFromFiles;
|
234 | function parseFromFilesSync(filepath, files, options) {
|
235 | if (options === void 0) { options = {}; }
|
236 | var _a = opts(filepath, options), resolvedFilePath = _a[0], processedOptions = _a[1];
|
237 | return parseFromConfigs(getConfigsForFiles(files), resolvedFilePath, processedOptions);
|
238 | }
|
239 | exports.parseFromFilesSync = parseFromFilesSync;
|
240 | function parse(_filepath, _options) {
|
241 | if (_options === void 0) { _options = {}; }
|
242 | return __awaiter(this, void 0, void 0, function () {
|
243 | var _a, resolvedFilePath, processedOptions, filepaths;
|
244 | return __generator(this, function (_b) {
|
245 | _a = opts(_filepath, _options), resolvedFilePath = _a[0], processedOptions = _a[1];
|
246 | filepaths = getConfigFileNames(resolvedFilePath, processedOptions);
|
247 | return [2 , readConfigFiles(filepaths)
|
248 | .then(getConfigsForFiles)
|
249 | .then(function (configs) { return parseFromConfigs(configs, resolvedFilePath, processedOptions); })];
|
250 | });
|
251 | });
|
252 | }
|
253 | exports.parse = parse;
|
254 | function parseSync(_filepath, _options) {
|
255 | if (_options === void 0) { _options = {}; }
|
256 | var _a = opts(_filepath, _options), resolvedFilePath = _a[0], processedOptions = _a[1];
|
257 | var filepaths = getConfigFileNames(resolvedFilePath, processedOptions);
|
258 | var files = readConfigFilesSync(filepaths);
|
259 | return parseFromConfigs(getConfigsForFiles(files), resolvedFilePath, processedOptions);
|
260 | }
|
261 | exports.parseSync = parseSync;
|