webmat
Version:
Formats your entire project with clang-format
86 lines • 3.11 kB
JavaScript
;
/**
* @license
* Copyright (c) 2018 Google Inc. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* Code distributed by Google as part of this project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const util_1 = require("util");
const readFile = util_1.promisify(fs.readFile);
const CONFIG_FILENAME = 'formatconfig.json';
exports.DEFAULT_CONFIG_FILENAME = path.join(__dirname, '..', 'default_config.json');
/**
* Resolves a user config against the default config. If the user config has the
* `ignoreDefaults` flag, then it returns the user config. If not, then it
* merges the user config with the default config.
*
* @param defaultConfig default_config.json parsed file contents.
* @param userConfig user config (formatconfig.json) parsed file contents.
*/
function resolveConfigs(defaultConfig, userConfig) {
let activeConfig = null;
if (userConfig && userConfig.ignoreDefaultGlobs) {
const style = {};
if (defaultConfig) {
Object.assign(style, defaultConfig.style);
}
Object.assign(style, userConfig.style);
userConfig.style = style;
return userConfig;
}
if (defaultConfig) {
activeConfig = defaultConfig;
if (userConfig) {
activeConfig.include = activeConfig.include.concat(userConfig.include);
activeConfig.exclude = activeConfig.exclude.concat(userConfig.exclude);
Object.assign(activeConfig.style, userConfig.style);
}
}
if (!activeConfig) {
throw new Error(`Default config file could not be found at ${exports.DEFAULT_CONFIG_FILENAME}.`);
}
return activeConfig;
}
exports.resolveConfigs = resolveConfigs;
/**
* Reads and parses a format config file.
*
* @param path path of the file to be parsed (defaults to formatconfig.json).
*/
async function readConfigFile(path = CONFIG_FILENAME) {
let configContents;
try {
configContents = await readFile(path, 'utf8');
}
catch (e) {
configContents = null;
}
const formatConfig = { include: [], exclude: [], ignoreDefaultGlobs: false, style: {} };
if (configContents) {
let parsedContents;
try {
parsedContents = JSON.parse(configContents);
}
catch (e) {
throw new Error(`${CONFIG_FILENAME} is not valid JSON.` +
'\n' + e);
}
if (parsedContents) {
formatConfig.include = parsedContents.include || [];
formatConfig.exclude = parsedContents.exclude || [];
formatConfig.ignoreDefaultGlobs =
parsedContents.ignoreDefaultGlobs || false;
formatConfig.style = parsedContents.style || {};
}
return formatConfig;
}
return null;
}
exports.readConfigFile = readConfigFile;
//# sourceMappingURL=cli.js.map