feat(resolve-config): add `.sync()` method (#2722)

* feat(resolve-config): add `sync` option

* docs(readme): add `sync` option

* fix(clearCache):  always clear both

* chore(deps): switch to 3.0 branch

* feat: replace `sync` option with `.sync()`

* docs(readme): update `.sync()`
master
Ika 2017-09-01 18:17:07 +08:00 committed by Lucas Azzola
parent 2fd77e7718
commit 088aa71b07
5 changed files with 59 additions and 24 deletions

View File

@ -447,6 +447,8 @@ prettier.resolveConfig(filePath).then(options => {
}) })
``` ```
Use `prettier.resolveConfig.sync([filePath [, options]])` if you'd like to use sync version.
#### `prettier.clearConfigCache()` #### `prettier.clearConfigCache()`
As you repeatedly call `resolveConfig`, the file system structure will be cached for performance. As you repeatedly call `resolveConfig`, the file system structure will be cached for performance.

View File

@ -14,7 +14,7 @@
"babel-code-frame": "7.0.0-alpha.12", "babel-code-frame": "7.0.0-alpha.12",
"babylon": "7.0.0-beta.22", "babylon": "7.0.0-beta.22",
"chalk": "2.0.1", "chalk": "2.0.1",
"cosmiconfig": "2.2.2", "cosmiconfig": "davidtheclark/cosmiconfig#3.0",
"dashify": "0.2.2", "dashify": "0.2.2",
"diff": "3.2.0", "diff": "3.2.0",
"esutils": "2.0.2", "esutils": "2.0.2",

View File

@ -3,34 +3,42 @@
const cosmiconfig = require("cosmiconfig"); const cosmiconfig = require("cosmiconfig");
const minimatch = require("minimatch"); const minimatch = require("minimatch");
const withCache = cosmiconfig("prettier"); const asyncWithCache = cosmiconfig("prettier");
const noCache = cosmiconfig("prettier", { cache: false }); const asyncNoCache = cosmiconfig("prettier", { cache: false });
const syncWithCache = cosmiconfig("prettier", { sync: true });
const syncNoCache = cosmiconfig("prettier", { cache: false, sync: true });
function resolveConfig(filePath, opts) { function resolveConfig(filePath, opts) {
const useCache = !(opts && opts.useCache === false); const useCache = !(opts && opts.useCache === false);
return (useCache ? asyncWithCache : asyncNoCache)
return (useCache ? withCache : noCache).load(filePath).then(result => { .load(filePath)
if (!result) { .then(result => {
return null; return !result ? null : mergeOverrides(result.config, filePath);
} });
return mergeOverrides(result.config, filePath);
});
} }
resolveConfig.sync = (filePath, opts) => {
const useCache = !(opts && opts.useCache === false);
const result = (useCache ? syncWithCache : syncNoCache).load(filePath);
return !result ? null : mergeOverrides(result.config, filePath);
};
function clearCache() { function clearCache() {
withCache.clearCaches(); syncWithCache.clearCaches();
asyncWithCache.clearCaches();
} }
function resolveConfigFile(filePath) { function resolveConfigFile(filePath) {
return noCache.load(filePath).then(result => { return asyncNoCache.load(filePath).then(result => {
if (result) { return result ? result.filepath : null;
return result.filepath;
}
return null;
}); });
} }
resolveConfigFile.sync = filePath => {
const result = syncNoCache.load(filePath);
return result ? result.filepath : null;
};
function mergeOverrides(config, filePath) { function mergeOverrides(config, filePath) {
const options = Object.assign({}, config); const options = Object.assign({}, config);
if (filePath && options.overrides) { if (filePath && options.overrides) {

View File

@ -54,6 +54,10 @@ test("API resolveConfig with no args", () => {
}); });
}); });
test("API resolveConfig.sync with no args", () => {
expect(prettier.resolveConfig.sync()).toBeNull();
});
test("API resolveConfig with file arg", () => { test("API resolveConfig with file arg", () => {
const file = path.resolve(path.join(__dirname, "../cli/config/js/file.js")); const file = path.resolve(path.join(__dirname, "../cli/config/js/file.js"));
return prettier.resolveConfig(file).then(result => { return prettier.resolveConfig(file).then(result => {
@ -63,6 +67,13 @@ test("API resolveConfig with file arg", () => {
}); });
}); });
test("API resolveConfig.sync with file arg", () => {
const file = path.resolve(path.join(__dirname, "../cli/config/js/file.js"));
expect(prettier.resolveConfig.sync(file)).toMatchObject({
tabWidth: 8
});
});
test("API resolveConfig with file arg and extension override", () => { test("API resolveConfig with file arg and extension override", () => {
const file = path.resolve( const file = path.resolve(
path.join(__dirname, "../cli/config/no-config/file.ts") path.join(__dirname, "../cli/config/no-config/file.ts")
@ -73,3 +84,12 @@ test("API resolveConfig with file arg and extension override", () => {
}); });
}); });
}); });
test("API resolveConfig.sync with file arg and extension override", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/no-config/file.ts")
);
expect(prettier.resolveConfig.sync(file)).toMatchObject({
semi: true
});
});

View File

@ -1045,16 +1045,17 @@ core-util-is@~1.0.0:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
cosmiconfig@2.2.2: cosmiconfig@davidtheclark/cosmiconfig#3.0:
version "2.2.2" version "2.2.2"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-2.2.2.tgz#6173cebd56fac042c1f4390edf7af6c07c7cb892" resolved "https://codeload.github.com/davidtheclark/cosmiconfig/tar.gz/12239b263684a7268883eccb30714446e105ff95"
dependencies: dependencies:
is-directory "^0.3.1" is-directory "^0.3.1"
js-yaml "^3.4.3" is-promise "^2.1.0"
js-yaml "^3.9.0"
minimist "^1.2.0" minimist "^1.2.0"
object-assign "^4.1.0"
os-homedir "^1.0.1" os-homedir "^1.0.1"
parse-json "^2.2.0" parse-json "^2.2.0"
please-upgrade-node "^3.0.1"
require-from-string "^1.1.0" require-from-string "^1.1.0"
create-ecdh@^4.0.0: create-ecdh@^4.0.0:
@ -2409,14 +2410,14 @@ js-tokens@^3.0.0:
version "3.0.1" version "3.0.1"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
js-yaml@^3.4.3, js-yaml@^3.7.0, js-yaml@^3.8.4: js-yaml@^3.7.0, js-yaml@^3.8.4:
version "3.8.4" version "3.8.4"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.4.tgz#520b4564f86573ba96662af85a8cafa7b4b5a6f6" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.4.tgz#520b4564f86573ba96662af85a8cafa7b4b5a6f6"
dependencies: dependencies:
argparse "^1.0.7" argparse "^1.0.7"
esprima "^3.1.1" esprima "^3.1.1"
js-yaml@^3.8.1: js-yaml@^3.8.1, js-yaml@^3.9.0:
version "3.9.1" version "3.9.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.9.1.tgz#08775cebdfdd359209f0d2acd383c8f86a6904a0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.9.1.tgz#08775cebdfdd359209f0d2acd383c8f86a6904a0"
dependencies: dependencies:
@ -3079,6 +3080,10 @@ pkg-dir@^1.0.0:
dependencies: dependencies:
find-up "^1.0.0" find-up "^1.0.0"
please-upgrade-node@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.0.1.tgz#0a681f2c18915e5433a5ca2cd94e0b8206a782db"
pluralize@^4.0.0: pluralize@^4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-4.0.0.tgz#59b708c1c0190a2f692f1c7618c446b052fd1762" resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-4.0.0.tgz#59b708c1c0190a2f692f1c7618c446b052fd1762"