Skip to content

Commit

Permalink
refactor: upgrade to cosmiconfig@5 (#4899)
Browse files Browse the repository at this point in the history
* refactor: upgrade to cosmiconfig@5

* fix: use custom require
  • Loading branch information
ikatyang committed Jul 29, 2018
1 parent 435e853 commit 440ab4f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -20,7 +20,7 @@
"camelcase": "4.1.0",
"chalk": "2.1.0",
"cjk-regex": "1.0.2",
"cosmiconfig": "3.1.0",
"cosmiconfig": "5.0.5",
"dashify": "0.2.2",
"dedent": "0.7.0",
"diff": "3.2.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/build.js
Expand Up @@ -98,7 +98,7 @@ async function run(params) {
await execa("rm", ["-rf", ".cache"]);
}

const bundleCache = new Cache(".cache/", "v2");
const bundleCache = new Cache(".cache/", "v3");
await bundleCache.load();

console.log(chalk.inverse(" Building packages "));
Expand Down
10 changes: 3 additions & 7 deletions scripts/build/config.js
Expand Up @@ -113,13 +113,9 @@ const coreBundles = [
type: "core",
target: "node",
replace: {
// The require-from-string module (a dependency of cosmiconfig) assumes
// that `module.parent` exists, but it only does for `require`:ed modules.
// Usually, require-from-string is _always_ `require`:ed, but when bundled
// with rollup the module is turned into a plain function located directly
// in index.js so `module.parent` does not exist. Defaulting to `module`
// instead seems to work.
"module.parent": "(module.parent || module)"
// cosmiconfig@5 uses `require` to resolve js config, which caused Error:
// Dynamic requires are not currently supported by rollup-plugin-commonjs.
"require(filepath)": "eval('require')(filepath)"
}
}
];
Expand Down
26 changes: 20 additions & 6 deletions src/config/resolve-config.js
Expand Up @@ -7,19 +7,33 @@ const mem = require("mem");

const resolveEditorConfig = require("./resolve-config-editorconfig");

const getExplorerMemoized = mem(opts =>
thirdParty.cosmiconfig("prettier", {
sync: opts.sync,
const getExplorerMemoized = mem(opts => {
const explorer = thirdParty.cosmiconfig("prettier", {
cache: opts.cache,
rcExtensions: true,
transform: result => {
if (result && result.config) {
if (typeof result.config !== "object") {
throw new Error(
`Config is only allowed to be an object, ` +
`but received ${typeof result.config} in "${result.filepath}"`
);
}

delete result.config.$schema;
}
return result;
}
})
);
});

const load = opts.sync ? explorer.loadSync : explorer.load;
const search = opts.sync ? explorer.searchSync : explorer.search;

return {
// cosmiconfig v4 interface
load: (searchPath, configPath) =>
configPath ? load(configPath) : search(searchPath)
};
});

/** @param {{ cache: boolean, sync: boolean }} opts */
function getLoadFunction(opts) {
Expand Down
Expand Up @@ -19,7 +19,7 @@ exports[`show warning with unknown option (stdout) 1`] = `""`;
exports[`show warning with unknown option (write) 1`] = `Array []`;

exports[`throw error with invalid config format (stderr) 1`] = `
"[error] Invalid configuration file: Failed to parse \\"<cwd>/tests_integration/cli/config/invalid/file/.prettierrc\\" as JSON, JS, or YAML.
"[error] Invalid configuration file: Config is only allowed to be an object, but received string in \\"<cwd>/tests_integration/cli/config/invalid/file/.prettierrc\\"
"
`;
Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Expand Up @@ -1504,14 +1504,13 @@ core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"

cosmiconfig@3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-3.1.0.tgz#640a94bf9847f321800403cd273af60665c73397"
cosmiconfig@5.0.5:
version "5.0.5"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.0.5.tgz#a809e3c2306891ce17ab70359dc8bdf661fe2cd0"
dependencies:
is-directory "^0.3.1"
js-yaml "^3.9.0"
parse-json "^3.0.0"
require-from-string "^2.0.1"
parse-json "^4.0.0"

create-ecdh@^4.0.0:
version "4.0.0"
Expand Down Expand Up @@ -3592,6 +3591,10 @@ json-loader@^0.5.4:
version "0.5.4"
resolved "https://registry.yarnpkg.com/json-loader/-/json-loader-0.5.4.tgz#8baa1365a632f58a3c46d20175fc6002c96e37de"

json-parse-better-errors@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"

json-schema-traverse@^0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"
Expand Down Expand Up @@ -4308,11 +4311,12 @@ parse-json@^2.2.0:
dependencies:
error-ex "^1.2.0"

parse-json@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-3.0.0.tgz#fa6f47b18e23826ead32f263e744d0e1e847fb13"
parse-json@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0"
dependencies:
error-ex "^1.3.1"
json-parse-better-errors "^1.0.1"

parse5@3.0.3:
version "3.0.3"
Expand Down Expand Up @@ -4873,10 +4877,6 @@ require-directory@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"

require-from-string@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.1.tgz#c545233e9d7da6616e9d59adfb39fc9f588676ff"

require-main-filename@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1"
Expand Down

0 comments on commit 440ab4f

Please sign in to comment.