Source: cli.js

#!/usr/bin/env node

/**
 * @description The entry point for the __CLI__ version of ipynb2web. 
 * 
 * Install:
 * ```
 * npm install ipynb2web
 * ``` 
 * Usage: 
 * ```
 * ipynb2web <COMMAND> <SAVETO> <FROM/or/SitemapName>
 * ```
 * 
 * Get help: 
 * ```
 * ipynb2web help
 * ```
 * 
 * It provides a command line interface function that processes given arguments and calls [createSitemap](module-prerender.html#.createSitemap), 
 * [createAudio](module-prerender.html#.createAudio), or [cli_nbs2html](module-prerender.html#.cli_nbs2html), based on the first argument.
 * @module Ipynb2web:cli
 * @exports cli
 * @author Charles Karpati
 */

import { createAudio, createSitemap, cli_nbs2html } from './prerender.mjs';


/**
 * Displays documentation on how to use the CLI when 'help' argument is provided.
 * @memberof module:Ipynb2web:cli
 */
function help() {
  console.log(`Usage: ipynb2web <COMMAND> <SAVETO> <FROM/or/SitemapName> [PathPrefix] [Domain]
    
Commands:
  sitemap      Create a sitemap.
  audio        Create audio assets.
  help         Display this help message.

For sitemap command:
  PathPrefix   Optional prefix to add to all URLs (e.g., '/docs')
               Example: ipynb2web sitemap ./ ./sitemap.txt /docs

  Domain       Optional domain to prefix all sitemap URLs (e.g., 'https://example.com')
               Example: ipynb2web sitemap ./ ./sitemap.txt /docs example.com

For processing notebooks (non-sitemap, non-audio commands):
  AssetsDir    Optional directory path for saving static assets separately
               instead of inlining them. When provided, images and other 
               assets will be saved as separate files in this directory.
               Example: ipynb2web notebooks ./output ./input '' ./assets
               
Examples:
  ipynb2web help
  ipynb2web sitemap ./ ./sitemap.txt /docs
  ipynb2web sitemap ./ ./sitemap.txt /docs example.com
  ipynb2web audio ./input ./output
  ipynb2web notebooks ./output ./input
  ipynb2web notebooks ./output ./input '' ./static-assets
`);
}

/**
 * Command line interface function that processes given arguments and calls the appropriate function based on the first argument.
 *
 * @param {string[]} args - An array of command line arguments.
 * - args[0]: 'Command' - Enter ['sitemap', 'audio'] to create these assets. If neither, it will the value be appended to the SAVETO and FROM paths for processing nb2json on.
 * - args[1]: 'SAVETO' - This directory path, used as a target directory for saving files.
 * - args[2]: 'FROM' - This directory path, used as an output directory for processing files (Whenever args[0] is NOT 'sitemap').
 * - args[2]: 'sitemapFile' - The file path for saving the sitemap (ONLY when args[0] is 'sitemap').
 * - args[3]: 'pathPrefix' - Optional prefix to add to all URLs in the sitemap (ONLY when args[0] is 'sitemap').
 * - args[4]: 'domain' - Optional domain to prefix all URLs in the sitemap (ONLY when args[0] is 'sitemap').
 * - args[4]: 'assetsDir' - Optional directory path for saving static assets separately instead of inlining them (NOT applicable for 'sitemap' and 'audio' commands).
 * @memberof module:Ipynb2web:cli
 */
function cli(args) {
    const directory = args[0] || '';
    const SAVETO = args[1] || false;
    const FROM = args[2] || false;
    const sitemapFile = args[2] || false;
    const pathPrefix = args[3] || '';
   const domain = args[4] || '';
   const assetsDir = args[4] || null;

    console.log('CLI RECEIVED ARGS:'); //, { directory, SAVETO, FROM, sitemapFile, assetsDir });

    /**
     * Based on the first argument, call the appropriate function.
     * If 'sitemap', call createSitemap.
     * If 'audio', call createAudio.
     * Otherwise, call cli_nbs2html.
     */ 
    if (directory === 'sitemap') {
      // New signature:
      //   ipynb2web sitemap <searchDir> <sitemapFile> [pathPrefix] [domain]
      // Legacy signature (kept for compatibility with older usage):
      //   ipynb2web sitemap '' <sitemapFile> <searchDir> <pathPrefix> [domain]
      let searchDir = SAVETO || './';
      let sitemapOutFile = sitemapFile || './sitemap.txt';
      let sitemapPathPrefix = pathPrefix || '';
      let sitemapDomain = domain || '';

      if ((SAVETO === '' || SAVETO === false) && typeof pathPrefix === 'string' && pathPrefix.trim().startsWith('.') && args[4]) {
        // Your legacy call style puts searchDir in args[3] and pathPrefix in args[4]
        searchDir = args[3] || './';
        sitemapPathPrefix = args[4] || '';
        sitemapDomain = args[5] || '';
      }

      createSitemap(searchDir, sitemapOutFile, sitemapPathPrefix, sitemapDomain);
    }
    else if (directory === 'audio') { createAudio(FROM, SAVETO); }
    else { cli_nbs2html(FROM, directory, SAVETO, true, assetsDir); }
}

/**
 * CJS: If this module is the main module (i.e., the script being run), call the cli or help function with the command line arguments.
 */
/*
if (require.main === module) {  }
*/

if (import.meta.url.includes('ipynb2web')) {
    const args = process.argv.slice(2);
    if (args[0] === 'help' || args.length === 0) {
        help();
    } else {
        cli(args);
    }
}


// MJS
export default cli;