2 * @fileoverview Helper to locate and load configuration files.
3 * @author Nicholas C. Zakas
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const fs = require("fs"),
13 path = require("path"),
14 stringify = require("json-stable-stringify-without-jsonify");
16 const debug = require("debug")("eslint:config-file");
18 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
23 * Determines sort order for object keys for json-stable-stringify
25 * see: https://github.com/samn/json-stable-stringify#cmp
26 * @param {Object} a The first comparison object ({key: akey, value: avalue})
27 * @param {Object} b The second comparison object ({key: bkey, value: bvalue})
28 * @returns {number} 1 or -1, used in stringify cmp method
30 function sortByKey(a, b) {
31 return a.key > b.key ? 1 : -1;
34 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
39 * Writes a configuration file in JSON format.
40 * @param {Object} config The configuration object to write.
41 * @param {string} filePath The filename to write to.
45 function writeJSONConfigFile(config, filePath) {
46 debug(`Writing JSON config file: ${filePath}`);
48 const content = `${stringify(config, { cmp: sortByKey, space: 4 })}\n`;
50 fs.writeFileSync(filePath, content, "utf8");
54 * Writes a configuration file in YAML format.
55 * @param {Object} config The configuration object to write.
56 * @param {string} filePath The filename to write to.
60 function writeYAMLConfigFile(config, filePath) {
61 debug(`Writing YAML config file: ${filePath}`);
63 // lazy load YAML to improve performance when not used
64 const yaml = require("js-yaml");
66 const content = yaml.safeDump(config, { sortKeys: true });
68 fs.writeFileSync(filePath, content, "utf8");
72 * Writes a configuration file in JavaScript format.
73 * @param {Object} config The configuration object to write.
74 * @param {string} filePath The filename to write to.
75 * @throws {Error} If an error occurs linting the config file contents.
79 function writeJSConfigFile(config, filePath) {
80 debug(`Writing JS config file: ${filePath}`);
83 const stringifiedContent = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};\n`;
86 const { CLIEngine } = require("../cli-engine");
87 const linter = new CLIEngine({
92 const report = linter.executeOnText(stringifiedContent);
94 contentToWrite = report.results[0].output || stringifiedContent;
96 debug("Error linting JavaScript config file, writing unlinted version");
97 const errorMessage = e.message;
99 contentToWrite = stringifiedContent;
100 e.message = "An error occurred while generating your JavaScript config file. ";
101 e.message += "A config file was still generated, but the config file itself may not follow your linting rules.";
102 e.message += `\nError: ${errorMessage}`;
105 fs.writeFileSync(filePath, contentToWrite, "utf8");
110 * Writes a configuration file.
111 * @param {Object} config The configuration object to write.
112 * @param {string} filePath The filename to write to.
114 * @throws {Error} When an unknown file type is specified.
117 function write(config, filePath) {
118 switch (path.extname(filePath)) {
121 writeJSConfigFile(config, filePath);
125 writeJSONConfigFile(config, filePath);
130 writeYAMLConfigFile(config, filePath);
134 throw new Error("Can't write to unknown file type.");
138 //------------------------------------------------------------------------------
140 //------------------------------------------------------------------------------