cosmiconfig
Advanced tools
Comparing version 9.0.0-alpha.1 to 9.0.0-alpha.2
@@ -76,2 +76,3 @@ "use strict"; | ||
'package.json', | ||
'package.yaml', | ||
'.config/config.json', | ||
@@ -78,0 +79,0 @@ '.config/config.yaml', |
@@ -12,3 +12,2 @@ "use strict"; | ||
const ExplorerBase_js_1 = require("./ExplorerBase.js"); | ||
const loaders_js_1 = require("./loaders.js"); | ||
const merge_1 = require("./merge"); | ||
@@ -62,3 +61,4 @@ const util_js_1 = require("./util.js"); | ||
error.code === 'EISDIR' || | ||
error.code === 'ENOTDIR') { | ||
error.code === 'ENOTDIR' || | ||
error.code === 'EACCES') { | ||
continue; | ||
@@ -112,13 +112,14 @@ } | ||
} | ||
if (path_1.default.basename(filepath) === 'package.json') { | ||
return ((0, util_js_1.getPropertyByPath)((0, loaders_js_1.loadJson)(filepath, contents), this.config.packageProp ?? this.config.moduleName) ?? null); | ||
const extension = path_1.default.extname(filepath); | ||
const loader = this.config.loaders[extension || 'noExt'] ?? | ||
this.config.loaders['default']; | ||
if (!loader) { | ||
throw new Error(`No loader specified for ${(0, ExplorerBase_js_1.getExtensionDescription)(extension)}`); | ||
} | ||
const extension = path_1.default.extname(filepath); | ||
try { | ||
const loader = this.config.loaders[extension || 'noExt'] ?? | ||
this.config.loaders['default']; | ||
if (loader !== undefined) { | ||
// eslint-disable-next-line @typescript-eslint/return-await | ||
return await loader(filepath, contents); | ||
const loadedContents = await loader(filepath, contents); | ||
if (path_1.default.basename(filepath, extension) !== 'package') { | ||
return loadedContents; | ||
} | ||
return ((0, util_js_1.getPropertyByPath)(loadedContents, this.config.packageProp ?? this.config.moduleName) ?? null); | ||
} | ||
@@ -129,3 +130,2 @@ catch (error) { | ||
} | ||
throw new Error(`No loader specified for ${(0, ExplorerBase_js_1.getExtensionDescription)(extension)}`); | ||
} | ||
@@ -152,4 +152,7 @@ async #fileExists(path) { | ||
yield { path: currentDir, isGlobalConfig: false }; | ||
if (await this.#fileExists(path_1.default.join(currentDir, 'package.json'))) { | ||
break; | ||
for (const ext of ['json', 'yaml']) { | ||
const packageFile = path_1.default.join(currentDir, `package.${ext}`); | ||
if (await this.#fileExists(packageFile)) { | ||
break; | ||
} | ||
} | ||
@@ -156,0 +159,0 @@ const parentDir = path_1.default.dirname(currentDir); |
@@ -12,3 +12,2 @@ "use strict"; | ||
const ExplorerBase_js_1 = require("./ExplorerBase.js"); | ||
const loaders_js_1 = require("./loaders.js"); | ||
const merge_1 = require("./merge"); | ||
@@ -62,3 +61,4 @@ const util_js_1 = require("./util.js"); | ||
error.code === 'EISDIR' || | ||
error.code === 'ENOTDIR') { | ||
error.code === 'ENOTDIR' || | ||
error.code === 'EACCES') { | ||
continue; | ||
@@ -112,12 +112,14 @@ } | ||
} | ||
if (path_1.default.basename(filepath) === 'package.json') { | ||
return ((0, util_js_1.getPropertyByPath)((0, loaders_js_1.loadJson)(filepath, contents), this.config.packageProp ?? this.config.moduleName) ?? null); | ||
const extension = path_1.default.extname(filepath); | ||
const loader = this.config.loaders[extension || 'noExt'] ?? | ||
this.config.loaders['default']; | ||
if (!loader) { | ||
throw new Error(`No loader specified for ${(0, ExplorerBase_js_1.getExtensionDescription)(extension)}`); | ||
} | ||
const extension = path_1.default.extname(filepath); | ||
try { | ||
const loader = this.config.loaders[extension || 'noExt'] ?? | ||
this.config.loaders['default']; | ||
if (loader !== undefined) { | ||
return loader(filepath, contents); | ||
const loadedContents = loader(filepath, contents); | ||
if (path_1.default.basename(filepath, extension) !== 'package') { | ||
return loadedContents; | ||
} | ||
return ((0, util_js_1.getPropertyByPath)(loadedContents, this.config.packageProp ?? this.config.moduleName) ?? null); | ||
} | ||
@@ -128,3 +130,2 @@ catch (error) { | ||
} | ||
throw new Error(`No loader specified for ${(0, ExplorerBase_js_1.getExtensionDescription)(extension)}`); | ||
} | ||
@@ -151,4 +152,7 @@ #fileExists(path) { | ||
yield { path: currentDir, isGlobalConfig: false }; | ||
if (this.#fileExists(path_1.default.join(currentDir, 'package.json'))) { | ||
break; | ||
for (const ext of ['json', 'yaml']) { | ||
const packageFile = path_1.default.join(currentDir, `package.${ext}`); | ||
if (this.#fileExists(packageFile)) { | ||
break; | ||
} | ||
} | ||
@@ -155,0 +159,0 @@ const parentDir = path_1.default.dirname(currentDir); |
@@ -26,3 +26,15 @@ "use strict"; | ||
catch (error) { | ||
return (0, exports.loadJsSync)(filepath, ''); | ||
try { | ||
return (0, exports.loadJsSync)(filepath, ''); | ||
} | ||
catch (requireError) { | ||
if (requireError.code === 'ERR_REQUIRE_ESM' || | ||
(requireError instanceof SyntaxError && | ||
requireError | ||
.toString() | ||
.includes('Cannot use import statement outside a module'))) { | ||
throw error; | ||
} | ||
throw requireError; | ||
} | ||
} | ||
@@ -95,20 +107,23 @@ }; | ||
const compiledFilepath = `${filepath.slice(0, -2)}mjs`; | ||
let transpiledContent; | ||
try { | ||
const config = resolveTsConfig(path_1.default.dirname(filepath)) ?? {}; | ||
config.compilerOptions = { | ||
...config.compilerOptions, | ||
module: typescript.ModuleKind.ES2022, | ||
moduleResolution: typescript.ModuleResolutionKind.Bundler, | ||
target: typescript.ScriptTarget.ES2022, | ||
noEmit: false, | ||
}; | ||
content = typescript.transpileModule(content, config).outputText; | ||
await (0, promises_1.writeFile)(compiledFilepath, content); | ||
const { href } = (0, url_1.pathToFileURL)(compiledFilepath); | ||
return (await import(href)).default; | ||
try { | ||
const config = resolveTsConfig(path_1.default.dirname(filepath)) ?? {}; | ||
config.compilerOptions = { | ||
...config.compilerOptions, | ||
module: typescript.ModuleKind.ES2022, | ||
moduleResolution: typescript.ModuleResolutionKind.Bundler, | ||
target: typescript.ScriptTarget.ES2022, | ||
noEmit: false, | ||
}; | ||
transpiledContent = typescript.transpileModule(content, config).outputText; | ||
await (0, promises_1.writeFile)(compiledFilepath, transpiledContent); | ||
} | ||
catch (error) { | ||
error.message = `TypeScript Error in ${filepath}:\n${error.message}`; | ||
throw error; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/return-await | ||
return await (0, exports.loadJs)(compiledFilepath, transpiledContent); | ||
} | ||
catch (error) { | ||
error.message = `TypeScript Error in ${filepath}:\n${error.message}`; | ||
throw error; | ||
} | ||
finally { | ||
@@ -115,0 +130,0 @@ if ((0, fs_1.existsSync)(compiledFilepath)) { |
{ | ||
"name": "cosmiconfig", | ||
"version": "9.0.0-alpha.1", | ||
"version": "9.0.0-alpha.2", | ||
"description": "Find and load configuration from a package.json property, rc file, TypeScript module, and more!", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -201,2 +201,3 @@ # cosmiconfig | ||
If you load a `package.json` file, the result will be derived from whatever property is specified as your [`packageProp`]. | ||
`package.yaml` will work as well if you specify these file names in your [`searchPlaces`]. | ||
@@ -276,3 +277,3 @@ You can do the same thing synchronously with [`explorerSync.load()`]. | ||
- `none`: Only checks in the current working directory. | ||
- `project`: Starts in the current working directory, traversing upwards until a `package.json` file is found. | ||
- `project`: Starts in the current working directory, traversing upwards until a `package.{json,yaml}` file is found. | ||
- `global`: Starts in the current working directory, traversing upwards until the configured [`stopDir`] | ||
@@ -345,2 +346,4 @@ (or the current user's home directory if none is given). Then, if no configuration is found, also look in the | ||
`package.yaml` (used by pnpm) works the same way. | ||
Examples, with a module named `porgy`: | ||
@@ -501,3 +504,3 @@ | ||
Name of the property in `package.json` to look for. | ||
Name of the property in `package.json` (or `package.yaml`) to look for. | ||
@@ -504,0 +507,0 @@ Use a period-delimited string or an array of strings to describe a path to nested properties. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
783
4
106668
39
1108