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

Boosting CLI Capabilities: New options and robust configuration in main module #11

Merged
merged 18 commits into from
Jul 17, 2024

Conversation

mitsuki31
Copy link
Owner

@mitsuki31 mitsuki31 commented Jul 17, 2024

Overview

This pull request introduces a series of enhancements, and new features aimed at improving the command-line (CLI) functionality, maintainability, and usability of the main module and related components. The changes include the addition of new helper functions, configuration parsing capabilities, and enhancements to the argument parsing and main function logic, and also introduced new command-line options for easier configuring both download and audio conversion process instead using the programmatic way. See the supported and available options on Description section below.

Description

New Features

  • Command Line Argument Parsing
    Introduced a new function, initParser, to handle various command-line arguments more efficiently, this function relying to argparse module. Added a filterOptions function to process and resolve command-line arguments, returning a read-only object with the filtered options. The supported command-line options are as follows:

    • Download Options

      Option Name Args Description
      --cwd DIR Set the current working directory to DIR, used to resolve the outDir path. Defaults to the curent directory.
      -f | --file | --batch FILE Path to a file containing a list of YouTube URLs for batch downloading.
      -c | --config FILE Path to a configuration file containing the downloadOptions object to configure both the download options and audio converter options.
      -o | --outDir | --out-dir DIR Specify the output directory for downloaded files. Defaults to the current directory.
      -C | --convertAudio | --convert-audio - Enable audio conversion to a specific format (requires FFmpeg).
      -q | --quiet - Suppress all output messages. Use it twice (-qq) to also suppress the audio conversion progress.
    • Audio Converter Options

      Option Name Args Description
      --format FMT Convert the audio to the specified format. Requires to enable --convertAudio option.
      --codec | --encoding CODEC Specify the codec for the converted audio. Requires to enable --convertAudio option.
      --bitrate N Set the bitrate for the converted audio in kbps. Requires to enable --convertAudio option.
      --freq | --frequency N Set the audio sampling frequency for the converted audio in Hertz (Hz). Requires to enable --convertAudio option.
      --channels N Specify the audio channels for the converted audio. Requires to enable --convertAudio option.
      --deleteOld | --delete-old | --overwrite - Delete the old audio file after the audio conversion is done. Requires to enable --convertAudio option.
    • Miscellaneous Options

      Option Name Description
      -h | -? | --help Display the help message and exit.
      -V | --version Display the module version and exit. Use it twice (-VV) to also display all dependencies' version (not including devDependencies).
      --copyright Display the copyright information and exit.
      --print-config Display the configuration that being used and exit. Useful for debugging.
  • Configuration Resolving
    Added a new module called config within lib directory, which offer some useful functions to parse and resolve configuration files.

    • resolveConfig
      Added a resolveConfig internal function to parse and resolve configuration files, supporting both JSON and JS formats.

    • parseConfig
      Introduced a parseConfig internal function that can handle both synchronous and asynchronous imports, along with a configChecker function for validation.

  • Helper Functions Addition
    Added utility functions isNullOrUndefined, isObject, and dropNullAndUndefined to check types and clean objects. These functions are now exported from utils module for broader usage.

  • Add Main Function
    Consolidates the main code block into a driver function for improved readability and maintainability.

Feature Enhancements

  • resolveOptions Enhancements
    Added a useDefault parameter to the resolveOptions function. Setting this parameter to false ensures that unspecified or null options are set to undefined instead of their default values. The function is now exported for easier internal development.

Dependency Addition

  • Add argparse Dependency
    Includes the argparse module for robust command-line argument parsing.

Documentation

  • Typedef Visibility
    Changed the visibility of DownloadOptions and ConvertAudioOptions typedefs to global for improved documentation clarity.

  • Function Documentation
    Provided detailed documentation for new and modified functions, including examples and parameter descriptions.

Todo List

Summary

This pull request refines the main module's structure and functionality, enhancing both the developer and user experience. Key improvements include more robust configuration handling, clearer argument parsing, addition of helpful utility functions, and the introduction of new command-line options. These changes lay the groundwork for future enhancements and ensure the module is more maintainable and easier to use.

mitsuki31 added 17 commits July 6, 2024 16:17
Set this parameter to false will make the function to set the unspecified or null options to undefined instead the default value. Additionally, the `resolveOptions` are now exported to ease the internal development.
For easier code maintenance within internal project.
This module is useful to parse command-line options.
This function will perform filtering and resolving given options from command-line argument parser and returns a read-only object with the processed and filtered options.
Fixed ESLint error for rule: no-extra-boolean-cast
* Removed unnecessary escape on regular expression
* Disabled error for camelcase and prefer-const rules for specific code
* Changed the double quotes to single quote strings
* Added new option to argument parser with options as following:
  - '-c'
  - '--config'
* Implemented a new function called `resolveConfig` to resolve and parse the configuration file along with the documentation
* Added a typedef called `YTMP3_Config` used for references of the configuration file
* Removed the `getInput()` function from main module
All code within block `if (require,main === module)` has been reallocated to main function and refactored.
* Added `isNullOrUndefined` function to check whether the given value is null or undefined
* Added `isObject` function to check whether the given value is an object and not null, an array, a `RegExp` instance, neither a URL instance
* Introduced a new module named `config.js` offering the configuration resolver and parser
* Added `parseConfig` function to import and parse the configuration file with support both JSON and JS file
* Added `configChecker` function to check and validate the configuration object and throw an error if specific condition is met
* Added an alias named `importConfig` that aliased from `parseConfig` function with `resolve` argument set to true
* Moved the `resolveConfig` from main module to `config` module and refactored it to supports current module environments
Changed the visibility of two typedefs (`DownloadOptions` and `ConvertAudioOptions`) to global.
This helper function easily removes `null` and `undefined` properties from an object, useful for removing unnecessary properties from an object.
The option was implemented on change `714235c` but it was not handled properly. So, this change we added to resolve and parse the configuration file along with the documentation, including defined a typedef `FilteredOptions` and removed the `YTMP3_Config` typedef (which is now are defined in `lib/config` module).
This option to show the configuration that being used during download and audio conversion process. Useful for debugging.
* Fixed the logic on if statements due to unresolved null `url`
* Added a warning message when user provide multiple URLs in a single command (`ytmp3 <url1> <url2> <url3>`). Because we still developing this feature of multiple downloads using URLs
* Display the stack error if an error occurred on main function, this improving readability and make it easier to troubleshoot the problem
* Minor refinement to the top-level module documentation
@mitsuki31 mitsuki31 added the refactor Refactor and enhancement changes label Jul 17, 2024
@mitsuki31 mitsuki31 self-assigned this Jul 17, 2024
My bad, forgot to run lint first before pushing branch
@mitsuki31 mitsuki31 merged commit 0b89a06 into master Jul 17, 2024
8 checks passed
@mitsuki31 mitsuki31 deleted the refactor/enhance-main-module branch July 17, 2024 16:30
mitsuki31 added a commit that referenced this pull request Jul 24, 2024
Users can now supply multiple YouTube URLs in command line arguments without having to create a temporary file containing those URLs. This change gives more further flexibility and convenience for CLI-based users. For instance:

  $ ytmp3 -o downloads --convertAudio --codec libmp3lame --format mp3 \
  > https://www.youtube.com/watch?v=<VIDEO_ID> https://www.youtube.com/watch?v=<VIDEO_ID>

Resolves #11 task.
@mitsuki31 mitsuki31 added the feature Bring new features label Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Bring new features refactor Refactor and enhancement changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant