Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] project options using package.json #1086

Closed
wants to merge 10 commits into from
36 changes: 26 additions & 10 deletions bin/prettier.js
Expand Up @@ -9,6 +9,7 @@ const chalk = require("chalk");
const minimist = require("minimist");
const readline = require("readline");
const prettier = require("../index");
const { getProjectOptions } = require("../src/util");

const argv = minimist(process.argv.slice(2), {
boolean: [
Expand Down Expand Up @@ -118,15 +119,10 @@ function getTrailingComma() {
}
}

const options = {
printWidth: getIntOption("print-width"),
tabWidth: getIntOption("tab-width"),
bracketSpacing: argv["bracket-spacing"],
singleQuote: argv["single-quote"],
jsxBracketSameLine: argv["jsx-bracket-same-line"],
trailingComma: getTrailingComma(),
parser: getParserOption()
};
const options = Object.assign(
getProjectOptions(process.cwd()),
getCliOptions()
);

function format(input) {
if (argv["debug-print-doc"]) {
Expand Down Expand Up @@ -218,7 +214,7 @@ if (stdin) {
let input;
try {
input = fs.readFileSync(filename, "utf8");
} catch(e) {
} catch (e) {
// Add newline to split errors from filename line.
process.stdout.write("\n");

Expand Down Expand Up @@ -295,3 +291,23 @@ function eachFilename(patterns, callback) {
});
});
}

function getCliOptions() {
const cliOptions = {
printWidth: getIntOption("print-width"),
tabWidth: getIntOption("tab-width"),
bracketSpacing: argv["bracket-spacing"],
singleQuote: argv["single-quote"],
jsxBracketSameLine: argv["jsx-bracket-same-line"],
trailingComma: getTrailingComma(),
parser: getParserOption()
};

Object.keys(cliOptions).forEach(function clearUndefinedValues(optionName) {
if (cliOptions[optionName] === undefined) {
delete cliOptions[optionName];
}
});

return cliOptions;
}
42 changes: 42 additions & 0 deletions src/util.js
@@ -1,6 +1,8 @@
"use strict";

var types = require("ast-types");
var path = require("path");
var fs = require("fs");
var n = types.namedTypes;

function comparePos(pos1, pos2) {
Expand Down Expand Up @@ -305,9 +307,49 @@ function getPrecedence(op) {
return PRECEDENCE[op];
}

function getProjectOptions(searchDirectory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole thing can be replaced by https://www.npmjs.com/package/read-pkg-up except for the part to keep searching if no prettier-entry

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, or just https://www.npmjs.com/package/pkg-conf which is then even easier

while (searchDirectory !== path.sep) {
const packagePath = path.join(searchDirectory, "package.json");
const packageJson = readPackageJson(packagePath);

if (packageJson && packageJson.prettier) {
return packageJson.prettier;
}

// continue searching
searchDirectory = path.resolve(searchDirectory, "..");
}

return {};
}

function readPackageJson(packagePath) {
let content;

try {
content = fs.readFileSync(packagePath);
} catch (e) {
return null;
}

content = content.toString("utf-8");

// Remove BOM
if (content.charCodeAt(0) === 0xfeff) {
content = content.slice(1);
}

try {
return JSON.parse(content);
} catch (e) {
return null;
}
}

module.exports = {
comparePos,
getPrecedence,
getProjectOptions,
fixFaultyLocations,
isExportDeclaration,
getParentExportDeclaration,
Expand Down