Git Product home page Git Product logo

yajuu's Introduction

yajuu

NPM version

Yajuu Configuration Loader

Features

Usage

Install package:

# npm
npm install yajuu

# yarn
yarn add yajuu

# pnpm
pnpm install yajuu

Import:

// ESM
import { loadConfig, watchConfig } from "yajuu";

Load configuration:

// Get loaded config
const { config } = await loadConfig({});

// Get resolved config and extended layers
const { config, configFile, layers } = await loadConfig({});

Loading priority

yajuu merged config sources with unjs/defu by below order:

  1. Config overrides passed by options
  2. Config file in CWD
  3. RC file in CWD
  4. Global RC file in the user's home directory
  5. Config from package.json
  6. Default config passed by options
  7. Extended config layers

Options

cwd

Resolve configuration from this working directory. The default is process.cwd()

name

Configuration base name. The default is config.

configName

Configuration file name without extension. Default is generated from name (name=foo => foo.config).

Set to false to avoid loading the config file.

rcFile

RC Config file name. Default is generated from name (name=foo => .foorc).

Set to false to disable loading RC config.

globalRC

Load RC config from the workspace directory and the user's home directory. Only enabled when rcFile is provided. Set to false to disable this functionality.

dotenv

Loads .env file if enabled. It is disabled by default.

packageJson

Loads config from nearest package.json file. It is disabled by default.

If true value is passed, yajuu uses name field from package.json.

You can also pass either a string or an array of strings as a value to use those fields.

defaults

Specify default configuration. It has the lowest priority and is applied after extending config.

defaultConfig

Specify default configuration. It is applied before extending config.

overrides

Specify override configuration. It has the highest priority and is applied before extending config.

kazuya

Custom so1ve/kazuya instance used to import configuration files.

kazuyaOptions

Custom so1ve/kazuya options to import configuration files.

envName

Environment name used for environment specific configuration.

The default is process.env.NODE_ENV. You can set envName to false or an empty string to disable the feature.

Extending configuration

If resolved config contains a extends key, it will be used to extend the configuration.

Extending can be nested and each layer can extend from one base or more.

The final config is merged result of extended options and user options with unjs/defu.

Each item in extends is a string that can be either an absolute or relative path to the current config file pointing to a config file for extending or the directory containing the config file. If it starts with either github:, gitlab:, bitbucket:, or https:, yajuu automatically clones it.

For custom merging strategies, you can directly access each layer with layers property.

Example:

// config.ts
export default {
  colors: {
    primary: "user_primary",
  },
  extends: ["./theme"],
};
// config.dev.ts
export default {
  dev: true,
};
// theme/config.ts
export default {
  extends: "../base",
  colors: {
    primary: "theme_primary",
    secondary: "theme_secondary",
  },
};
// base/config.ts
export default {
  colors: {
    primary: "base_primary",
    text: "base_text",
  },
};

The loaded configuration would look like this:

const config = {
  dev: true,
  colors: {
    primary: "user_primary",
    secondary: "theme_secondary",
    text: "base_text",
  },
};

Layers:

[
 { config: /* theme config */, configFile: /* path/to/theme/config.ts */, cwd: /* path/to/theme */ },
 { config: /* base  config */, configFile: /* path/to/base/config.ts  */, cwd: /* path/to/base */ },
 { config: /* dev   config */, configFile: /* path/to/config.dev.ts  */, cwd: /* path/ */ },
]

Environment-specific configuration

Users can define environment-specific configuration using these config keys:

  • $test: {...}
  • $development: {...}
  • $production: {...}
  • $env: { [env]: {...} }

yajuu tries to match envName and override environment config if specified.

Note: Environment will be applied when extending each configuration layer. This way layers can provide environment-specific configuration.

Example:

{
  // Default configuration
  logLevel: 'info',

  // Environment overrides
  $test: { logLevel: 'silent' },
  $development: { logLevel: 'warning' },
  $production: { logLevel: 'error' },
  $env: {
    staging: { logLevel: 'debug' }
  }
}

Watching Configuration

you can use watchConfig instead of loadConfig to load config and watch for changes, add and removals in all expected configuration paths and auto reload with new config.

Lifecycle hooks

  • onWatch: This function is always called when config is updated, added, or removed before attempting to reload the config.
  • acceptHMR: By implementing this function, you can compare old and new functions and return true if a full reload is not needed.
  • onUpdate: This function is always called after the new config is updated. If acceptHMR returns true, it will be skipped.
import { watchConfig } from "yajuu";

const config = watchConfig({
  cwd: ".",
  // chokidarOptions: {}, // Default is { ignoreInitial: true }
  // debounce: 200 // Default is 100. You can set it to false to disable debounced watcher
  onWatch: (event) => {
    console.log("[watcher]", event.type, event.path);
  },
  acceptHMR({ oldConfig, newConfig, getDiff }) {
    const diff = getDiff();
    if (diff.length === 0) {
      console.log("No config changed detected!");

      return true; // No changes!
    }
  },
  onUpdate({ oldConfig, newConfig, getDiff }) {
    const diff = getDiff();
    console.log(`Config updated:\n${diff.map((i) => i.toJSON()).join("\n")}`);
  },
});

console.log("watching config files:", config.watchingFiles);
console.log("initial config", config.config);

// Stop watcher when not needed anymore
// await config.unwatch();

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

📝 License

MIT. Made with ❤️ by Ray

yajuu's People

Contributors

renovate[bot] avatar so1ve avatar autofix-ci[bot] avatar

Stargazers

Lockinwize Lolite avatar  avatar

Watchers

 avatar

yajuu's Issues

🍳 Renovate Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: File contents are invalid JSON but parse using JSON5. Support for this will be removed in a future release so please change to a support .json5 file name or ensure correct JSON syntax.

Pending Approval

These branches will be created by Renovate only once you click their checkbox below.

  • chore(deps): update actions/checkout action to v4
  • chore(deps): update actions/setup-node action to v4
  • chore(deps): update dependency @so1ve/eslint-config to v2
  • chore(deps): update dependency @types/node to v20
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency tsx to v4
  • chore(deps): update dependency unbuild to v2
  • chore(deps): update dependency vite to v5
  • chore(deps): update dependency vitest to v1
  • chore(deps): update dessant/lock-threads action to v5
  • chore(deps): update pnpm to v9
  • 🔐 Create all pending approval PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/autofix-ci.yml
  • actions/checkout v3
  • actions/setup-node v3
  • autofix-ci/action ea32e3a12414e6d3183163c3424a7d7a8631ad84
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • actions/setup-node v3
.github/workflows/lock.yml
  • dessant/lock-threads v4
.github/workflows/release.yml
  • actions/checkout v3
  • actions/setup-node v3
npm
package.json
  • chokidar ^3.5.3
  • defu ^6.1.2
  • dotenv ^16.0.3
  • giget ^1.1.2
  • kazuya ^0.3.1
  • mlly ^1.2.0
  • ohash ^1.1.2
  • pathe ^1.1.0
  • perfect-debounce ^1.0.0
  • pkg-types ^1.0.3
  • rc9 ^2.1.0
  • @antfu/ni ^0.21.3
  • @babel/types ^7.21.5
  • @so1ve/eslint-config ^0.110.0
  • @types/node ^18.16.4
  • @vitest/coverage-c8 ^0.32.0
  • bumpp ^9.1.0
  • eslint ^8.39.0
  • expect-type ^0.15.0
  • prettier ^2.8.8
  • tsx ^3.12.7
  • typescript ^5.0.4
  • unbuild ^1.2.1
  • vite ^4.3.4
  • vitest ^0.32.0
  • pnpm 8.15.6

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.