Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global config support and cross-platform enhancements #45

Merged
merged 21 commits into from
Sep 5, 2024

Conversation

mitsuki31
Copy link
Owner

@mitsuki31 mitsuki31 commented Sep 4, 2024

Overview

This pull request introduces significant enhancements to the YTMP3-JS module, focusing on adding support for global configuration files and ensuring consistent cross-platform behavior. The changes involve refactoring existing utilities, improving error handling, expanding test coverage, and enhancing the command-line interface (CLI). Additionally, the PR includes fixes for path resolution issues on Windows and POSIX platforms, making the module more robust and user-friendly.

Detailed Description

Global Configuration Support

  • findGlobalConfig(): A new function was introduced to locate the global configuration file, with an added searchDir parameter that allows specifying a custom directory for the search.
  • parseGlobalConfig(): A corresponding parser function for validating and importing the global configuration file.
  • Integrated the lsfnd npm package to assist in locating configuration files within the specified directory.

Error Handling Enhancements

  • Custom Error Classes: New InvalidTypeError and GlobalConfigParserError classes were added to provide more specific error messages and detailed context for errors related to invalid types and configuration parsing.

Cross-Platform Path Resolution

  • Windows and POSIX Support: The parseConfig function was refactored to correctly handle path separators and resolution across both Windows and POSIX platforms, ensuring paths are properly interpreted regardless of the operating system.

Refactorings and Improvements

  • Utilities: Refactored various utility functions, including improvements to object type checks (isObject, isPlainObject) and the introduction of the getType utility.
  • CLI Enhancements: Improved CLI argument parsing behavior with better support for intermixed arguments and added the ability to invert enabled options (e.g., --no-convertAudio).
  • New CLI Option Addition:
    • Introduced a new command-line option to disable global and/or specified configuration parsing, called --no-config.
    • Enhanced the global configuration parsing logic to conditionally apply settings only when --no-config is not specified.
    • Improved overall flexibility by allowing users to opt out of configuration file overrides, ensuring more precise control over the CLI's behavior.
  • Configuration Extensions: Updated the supported configuration file extensions, deprecating older extensions and introducing .config variants for future use.

Testing Enhancements

  • Comprehensive Test Suites: New test cases and suites were added to cover the newly introduced features, as well as to improve the coverage of existing modules.

Deprecated Features

This pull request introduces some deprecations that users should be aware of:

Deprecated Configuration File Extensions

  • The following configuration file extensions are deprecated and will be removed in the next major release:
    • .js
    • .mjs
    • .cjs
  • Users are encouraged to migrate to the new .config.js, .config.mjs, or .config.cjs extensions to ensure future compatibility.

Summary

This update introduces several enhancements to command-line usage, including support for global configuration, the ability to invert enabled options, and the addition of new command-line options.

How to Set Up Global Configuration?

The home directory for ytmp3 is typically located relative to the user's home directory, specifically in a directory named ".ytmp3-js". This equates to "$HOME/.ytmp3-js" on POSIX systems and "%USERPROFILE%\.ytmp3-js" on Windows.

To set up global configuration, simply create a YTMP3-JS configuration file in this directory. The system supports both CommonJS and EcmaScript Module formats, and also JSON format.

Note

Please note, the system parser will only detect the global configuration named ytmp3-js.*, otherwise nothing will be parsed.

Once configured, you can verify your global configuration by running the following command:

ytmp3 --print-config

If you prefer not to use any configuration files -- whether global or specified by the --config option -- you can disable them easily with the --no-config flag:

ytmp3 --no-config

Important

Please note that the .js, .mjs, and .cjs file extensions for configuration files are deprecated and will be removed in the next major update. To ensure future compatibility, use .config.js, .config.mjs, or .config.cjs instead.

This deprecation does not affect configuration files with the .json extension.


By consolidating and enhancing the project's capabilities, this update aims to provide more robust and flexible configuration management, improved error handling, and better cross-platform support.

- Introduced `YTMP3_HOMEDIR` constant to standardize the home directory path across the utility functions.
- Refactored `LOGDIR` to utilize the new `YTMP3_HOMEDIR` constant for better maintainability.
- Improved the `isObject` function to strictly check for non-null objects while ensuring compatibility with objects created from null prototypes.
- Added a new `isPlainObject` function to specifically identify plain objects (`{}`) with an `Object` prototype.
- Implemented a `getType` utility function to return a detailed string representation of the type for given values.
- Updated module exports to include the new utilities (`YTMP3_HOMEDIR`, `isPlainObject`, and `getType`).
- Added `InvalidTypeError` class to handle cases where values do not match expected types. Includes additional properties for `actualType`, `expectedType`, and `cause`, which provide more context for type-related errors.
- Added `GlobalConfigParserError` class to handle errors related to invalid or unprocessable configuration files. It includes properties such as `errno`, `code`, `syscall`, and `path` to offer detailed information about the underlying error.
- Both new error classes extend the base `Error` class and utilize the `isPlainObject` utility function to validate options.
- Updated module exports to include the new error classes alongside the existing ones.
- Updated `KNOWN_CONFIG_EXTS` to include new `.config` variants for configuration files, marking older extensions as deprecated. These deprecated extensions (`.js`, `.mjs`, `.cjs`) will be removed in the next major update.
- Modified `parseConfig` function to handle the new configuration file extensions and adjusted logic for resolving and importing configuration files. Improved error message formatting to list supported extensions more clearly.
- Updated JSDoc comments to reflect changes in configuration extensions.

Deprecation Note:
The following extensions are now deprecated and will be removed in the next major version: `.js`, `.mjs`, `.cjs`. Please transition to using `.config.js`, `.config.mjs`, or `.config.cjs` for future compatibility.
- Added a JSDoc for `YTMP3_HOMEDIR` constant
- Improve the JSDoc of other constants
- Replaced `TypeError` with `InvalidTypeError` in `configChecker` and `parseConfig` functions for better error specificity when invalid types are encountered.
- Enhanced `configChecker` to include `actualType` and `expectedType` in error details when an invalid configuration object is detected.
- Exported `KNOWN_OPTIONS`, `KNOWN_CONFIG_EXTS`, and `ERR_FORMAT` constants for improved modularity and testing purposes.
- Updated JSDoc to reflect the change from `TypeError` to `InvalidTypeError`.
- Improved JSDoc examples for configuration files by specifying the expected filenames (e.g., `ytmp3-js.config.cjs`).
…arch

- Introduced `findGlobalConfig()` to locate the most appropriate global configuration file in the user's home directory.
- Added `parseGlobalConfig()` to validate and parse the global configuration file, with optional parser settings.
- Integrated the `lsfnd` npm package to assist in locating configuration files.
- Updated JSDoc comments to document the new functionality.
- Separated the main index file to create a distinct entry point for API-related functionality.
- Introduced a new module called `bin/cli.js` specifically designed to handle command-line interface (CLI) operations.
- Moved command line arguments parser functionality to a module called `argparser` in "bin" directory.
- Adjusted the `bin` field in `package.json` to use `./bin/cli.js` instead of `./index.js` for the `ytmp3` command.
- Added the "bin" directory to `include` field in `eslint.config.js` file
- Added the "bin" directory to `ignores` field in `eslint.config.test.js` file
Now it uses `utils.isPlainObject` instead `utils.isObject` to check a plain object
- Replaced instances of `TypeError` with `InvalidTypeError` in the `config` module test suite.
- Adjusted test descriptions to align with the updated error handling in the `config` module.
- Ensured that `InvalidTypeError` is correctly imported and used in assertions for better type validation.
- Added new test cases for `isObject` to ensure it returns `true` for non-array objects and objects with null prototypes.
- Introduced a separate test suite for `isPlainObject` to validate that it correctly identifies plain objects, including those with null prototypes.
- Updated test assertions for better clarity and accuracy.
- Updated `argparser.js` to suppress default values for options when not provided, ensuring cleaner output and reducing unnecessary clutter in the parsed arguments.
- Switched to `parse_intermixed_args()` to support intermixed positional and optional arguments, improving usability and flexibility in the command-line interface.

This change enhances the overall argument parsing behavior, making the CLI tool more intuitive and user-friendly.
- Introduced a new `--noConfig` (`--no-config`) CLI option, allowing users to disable the loading of global or specified configuration files.
- Updated the `filterOptions` function to respect the `--no-config` flag, preventing the merging of global configuration with command-line options when this flag is set.
- Enhanced the global configuration parsing logic to conditionally apply settings only when `--no-config` is not specified.
- Improved overall flexibility by allowing users to opt out of configuration file overrides, ensuring more precise control over the CLI's behavior.
- Added the ability to inverse the behavior of enabled options by using `BooleanOptionalAction` in `argparse`.
- Updated `--convertAudio` and `--deleteOld` options to use `BooleanOptionalAction`, allowing users to disable these options by using their inverse form (e.g., `--no-convertAudio` or `--no-deleteOld`).
- Introduced a new `--noQuiet` option to reset the `quiet` level, enabling suppressed output messages even when `quiet` is enabled.
- Added `searchDir` parameter to `findGlobalConfig` function, allowing the search directory for the global configuration file to be specified. Defaults to `utils.YTMP3_HOMEDIR`.
- Implemented directory existence and type checks to ensure the specified `searchDir` is a valid directory.
- Updated the function  to return `null` if the `searchDir` does not exist or is not a directory, enhancing error handling and flexibility in configuration management.
* Added new test suites for new functions (`findGlobalConfig` and `parseGlobalConfig`) that search and parse the known global configuration file of YTMP3-JS.
* Added new 3 test cases for `parseConfig` function.
* Several improvements to other test suites and code coverage.
Refactored the `parseConfig` function to properly handle path resolution across different platforms. This change ensures that paths are correctly resolved on both Windows and POSIX systems by replacing incorrect path separators and using platform-specific logic.

- On Windows, '/' is replaced with the appropriate Windows separator.
- On POSIX systems, '\' is replaced with the appropriate POSIX separator.
- Applied `path.resolve` to ensure the paths are absolute.

This change enhances cross-platform compatibility and resolves issues related to incorrect path handling in configuration file imports.
@mitsuki31 mitsuki31 added refactor Refactor and enhancement changes feature Bring new features minor Minor changes labels Sep 4, 2024
@mitsuki31 mitsuki31 self-assigned this Sep 4, 2024
* Added homepage and GitHub repo link to the epilog of CLI documentation.
@mitsuki31 mitsuki31 merged commit 0112f47 into master Sep 5, 2024
18 checks passed
@mitsuki31 mitsuki31 deleted the feat/add-global-config-support branch September 5, 2024 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Bring new features minor Minor changes refactor Refactor and enhancement changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant