Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Joycon is a configuration file loader for Node.js that supports multiple formats such as JSON, YAML, and JavaScript. It allows you to load and merge configuration files from different sources, making it easier to manage application settings.
Load Configuration Files
This feature allows you to load configuration files in different formats. The code sample demonstrates how to load JSON and YAML configuration files and access the merged configuration data.
const JoyCon = require('joycon');
const joycon = new JoyCon();
joycon.load(['config.json', 'config.yaml']).then(result => {
console.log(result.data);
});
Specify Custom Loaders
Joycon allows you to specify custom loaders for different file formats. The code sample shows how to add a custom loader for files with a '.custom' extension and load them using Joycon.
const JoyCon = require('joycon');
const joycon = new JoyCon();
joycon.addLoader({
test: /\.custom$/, // Regex to match custom file extension
load: async (filePath) => {
const content = await fs.promises.readFile(filePath, 'utf8');
return parseCustomFormat(content); // Custom parsing logic
}
});
joycon.load(['config.custom']).then(result => {
console.log(result.data);
});
Merge Configuration Files
Joycon can merge multiple configuration files into a single configuration object. The code sample demonstrates how to load and merge JSON and YAML configuration files.
const JoyCon = require('joycon');
const joycon = new JoyCon();
joycon.load(['config.json', 'config.yaml']).then(result => {
const mergedConfig = result.data;
console.log(mergedConfig);
});
Cosmiconfig is a popular configuration file loader that supports multiple formats like JSON, YAML, and JavaScript. It provides a flexible API for loading and merging configuration files. Compared to Joycon, Cosmiconfig offers more built-in features and a larger community, but Joycon provides more flexibility with custom loaders.
RC is a simple configuration loader that supports JSON, INI, and environment variables. It is less flexible than Joycon and Cosmiconfig but is very easy to use for basic configuration loading needs. RC is suitable for simpler use cases where advanced features like custom loaders are not required.
Config is a configuration management tool for Node.js that supports hierarchical configurations and multiple file formats. It is more feature-rich than Joycon, offering environment-specific configurations and runtime configuration changes. However, it is also more complex to set up and use.
JoyCon is zero-dependency but feature-complete.
yarn add joycon
const JoyCon = require('joycon')
const joycon = new JoyCon()
joycon.load(['package-lock.json', 'yarn.lock'])
.then(result => {
// result is {} when files do not exist
// otherwise { path, data }
})
By default only .js
and .json
file are parsed, otherwise raw data will be returned, so you can add custom loader to parse them:
const joycon = new JoyCon()
joycon.addLoader({
test: /\.toml$/,
load(filepath) {
return require('toml').parse(filepath)
}
})
joycon.load(['cargo.toml'])
string[]
The files to search.
The directory to search files.
The directory to stop searching.
You can load config from certain property in a package.json
file. For example, when you set packageKey: 'babel'
, it will load the babel
property in package.json
instead of the entire data.
(str: string) => any
JSON.parse
The function used to parse JSON string.
files
defaults to options.files
.
cwd
defaults to options.cwd
.
stopDir
defaults to options.stopDir
then path.parse(cwd).root
.
If using a single object options
, it will be the same as constructor options.
Search files and resolve the path of the file we found.
There's also .resolveSync
method.
The signature is the same as resolve.
Search files and resolve { path, data }
of the file we found.
There's also .loadSync
method.
interface Loader {
name?: string
test: RegExp
load(filepath: string)?: Promise<any>
loadSync(filepath: string)?: any
}
At least one of load
and loadSync
is required, depending on whether you're calling the synchonous methods or not.
Remove loaders by loader name.
Each JoyCon instance uses its own cache.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
joycon © egoist, Released under the MIT License.
Authored and maintained by egoist with help from contributors (list).
github.com/egoist · GitHub @egoist · Twitter @_egoistlily
FAQs
Load config with ease.
The npm package joycon receives a total of 3,677,621 weekly downloads. As such, joycon popularity was classified as popular.
We found that joycon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.