<commit_before>#!/usr/bin/env node

/**
 * Headless Chromium-based web performance metrics collector and monitoring tool
 *
 * Run "node phantomas.js" to get help
 *
 * @see https://github.com/macbre/phantomas
 */
"use strict";

var phantomas = require(".."),
  debug = require("debug")("phantomas:cli"),
  program = require("optimist-config-file"),
  options = {},
  url = "";

// parse options
program
  .usage(
    "Headless Chromium-based web performance metrics collector and monitoring tool\n\nphantomas <url> [options]"
  )
  // mandatory
  .header("General options")
  .describe("url", "Set URL to work with")
  .string("url")
  // version / help
  .describe("version", "Show version number and quit")
  .boolean("version")
  .alias("version", "V")
  .describe("help", "This help text")
  .boolean("help")
  .alias("help", "h")
  .describe("verbose", "print debug messages to the console")
  .boolean("verbose")
  .alias("verbose", "v")
  .describe("debug", "run phantomas in debug mode")
  .default("debug")
  .describe("modules", "run selected modules only [moduleOne],[moduleTwo],...")
  .describe(
    "include-dirs",
    "load modules from specified directories [dirOne],[dirTwo],..."
  )
  .describe("skip-modules", "skip selected modules [moduleOne],[moduleTwo],...")
  .describe(
    "config",
    "uses JSON or YAML-formatted config file to set parameters"
  )
  .string("config")
  // optional params
  .header("Client options")
  // --engine=[webkit|gecko]
  .describe(
    "engine",
    "[experimental] select engine used to run the phantomas [webkit|gecko]"
  )
  .string("engine")
  .describe("phone", "force viewport and user agent of a mobile phone")
  .describe("tablet", "force viewport and user agent of a tablet")
  .describe(
    "viewport",
    "viewport dimensions [width]x[height [default: 1280x1024]"
  )
  .describe("user-agent", "provide a custom user agent")
  .header("HTTP options")
  .describe("auth-user", "sets the user name used for HTTP authentication")
  .describe("auth-pass", "sets the password used for HTTP authentication")
  .describe(
    "cookie",
    'document.cookie formatted string for setting a single cookie (e.g. "bar=foo;domain=url")'
  )
  .describe(
    "cookies-file",
    "specifies the file name to store the persistent Cookies"
  )
  .describe(
    "ignore-ssl-errors",
    "ignores SSL errors, such as expired or self-signed certificate errors"
  )
  .describe(
    "proxy",
    "specifies the proxy server to use (e.g. --proxy=192.168.1.42:8080)"
  )
  .describe(
    "proxy-auth",
    "specifies the authentication information for the proxy (e.g. --proxy-auth=username:password)"
  )
  .describe(
    "proxy-type",
    "specifies the type of the proxy server [http|socks5|none]"
  )
  .describe(
    "ssl-protocol",
    "sets the SSL protocol for secure connections [sslv3|sslv2|tlsv1|any]"
  )
  .default("ssl-protocol", "any")
  .header("Runtime options")
  .describe(
    "allow-domain",
    "allow requests to given domain(s) - aka whitelist [domain],[domain],..."
  )
  .describe(
    "block-domain",
    "disallow requests to given domain(s) - aka blacklist [domain],[domain],..."
  )
  .describe("disable-js", "disable JavaScript on the page that will be loaded")
  .boolean("disable-js")
  .describe("no-externals", "block requests to 3rd party domains")
  .boolean("no-externals")
  .describe("post-load-delay", "wait X seconds before generating a report")
  .describe("scroll", "scroll down the page when it's loaded")
  .boolean("scroll")
  .describe("spy-eval", "report calls to eval()")
  .boolean("spy-eval")
  .describe("stop-at-onload", "stop phantomas immediately after onload event")
  .boolean("stop-at-onload")
  .describe("timeout", "timeout for phantomas run")
  .default("timeout", 15)
  .describe(
    "wait-for-event",
    "wait for a given phantomas event before generating a report"
  )
  .describe(
    "wait-for-selector",
    "wait for an element matching given CSS selector before generating a report"
  )
  .describe("scroll", "scroll down the page when it's loaded")
  .boolean("scroll")
  .describe("socket", "[experimental] use provided UNIX socket for IPC")
  .header("Output and reporting")
  .describe("analyze-css", "[experimental] emit in-depth CSS metrics")
  .boolean("analyze-css")
  .describe("colors", "forces ANSI colors even when output is piped")
  .boolean("colors")
  .describe(
    "film-strip",
    "register film strip when page is loading (a comma separated list of milliseconds can be passed)"
  )
  .boolean("film-strip")
  .describe(
    "film-strip-dir",
    "folder path to output film strip (default is ./filmstrip directory)"
  )
  .describe("har", "save HAR to a given file")
  .describe("log", "log to a given file")
  .describe("page-source", "[experimental] save page source to file")
  .boolean("page-source")
  .describe(
    "page-source-dir",
    "[experimental] folder path to output page source (default is ./html directory)"
  )
  .describe("reporter", "output format / reporter")
  .default("reporter", "plain")
  .alias("reporter", "R")
  .alias("reporter", "format")
  .describe("screenshot", "render fully loaded page to a given file")
  .describe("silent", "don't write anything to the console")
  .boolean("silent");

// handle --config (issue #209)
program.setConfigFile("config");

// handle env variables (issue #685)
program.setReplacementVars(process.env);

// parse it
options = program.parse(process.argv);

// show version number
if (options.version === true) {
  console.log("phantomas v%s", phantomas.version);
  process.exit(0);
}

// show help
if (options.help === true) {
  program.showHelp();
  process.exit(0);
}

// handle URL passed without --url option (#249)
if (typeof options._[2] === "string") {
  options.url = options._[2];
}

// --url is mandatory -> show help
if (typeof options.url !== "string" && typeof options.config === "undefined") {
  program.showHelp();
  process.exit(255);
}

url = options.url;

delete options.url;
delete options._;
delete options.$0;

// handle --no-foo options
options["no-externals"] = options.externals === false;<commit_msg>bin/phantomas.js: basic handling of command line options via commander<commit_after>#!/usr/bin/env node

/**
 * Headless Chromium-based web performance metrics collector and monitoring tool
 *
 * Run "node phantomas.js" to get help
 *
 * @see https://github.com/macbre/phantomas
 */
"use strict";

const { program } = require("commander"),
  phantomas = require(".."),
  debug = require("debug")("phantomas:cli");

var url = "";

// parse options
program
  .description(
    "Headless Chromium-based web performance metrics collector and monitoring tool"
  )
  .version(phantomas.version)
  .usage("--url <url> [options]")

  //
  // https://www.npmjs.com/package/commander#common-option-types-boolean-and-value
  //

  // mandatory
  .option("--url <url>", "Set URL to work with");
/**
  .option("verbose", "print debug messages to the console")
  .boolean("verbose")
  .alias("verbose", "v")
  .option("debug", "run phantomas in debug mode")
  .default("debug")
  .option("modules", "run selected modules only [moduleOne],[moduleTwo],...")
  .option(
    "include-dirs",
    "load modules from specified directories [dirOne],[dirTwo],..."
  )
  .option("skip-modules", "skip selected modules [moduleOne],[moduleTwo],...")
  .option(
    "config",
    "uses JSON or YAML-formatted config file to set parameters"
  )
  .string("config")
  // optional params
  .header("Client options")
  .option("phone", "force viewport and user agent of a mobile phone")
  .option("tablet", "force viewport and user agent of a tablet")
  .option(
    "viewport",
    "viewport dimensions [width]x[height [default: 1280x1024]"
  )
  .option("user-agent", "provide a custom user agent")
  .header("HTTP options")
  .option("auth-user", "sets the user name used for HTTP authentication")
  .option("auth-pass", "sets the password used for HTTP authentication")
  .option(
    "cookie",
    'document.cookie formatted string for setting a single cookie (e.g. "bar=foo;domain=url")'
  )
  .option(
    "cookies-file",
    "specifies the file name to store the persistent Cookies"
  )
  .option(
    "ignore-ssl-errors",
    "ignores SSL errors, such as expired or self-signed certificate errors"
  )
  .option(
    "proxy",
    "specifies the proxy server to use (e.g. --proxy=192.168.1.42:8080)"
  )
  .option(
    "proxy-auth",
    "specifies the authentication information for the proxy (e.g. --proxy-auth=username:password)"
  )
  .option(
    "proxy-type",
    "specifies the type of the proxy server [http|socks5|none]"
  )
  .option(
    "ssl-protocol",
    "sets the SSL protocol for secure connections [sslv3|sslv2|tlsv1|any]"
  )
  .default("ssl-protocol", "any")
  .header("Runtime options")
  .option(
    "allow-domain",
    "allow requests to given domain(s) - aka whitelist [domain],[domain],..."
  )
  .option(
    "block-domain",
    "disallow requests to given domain(s) - aka blacklist [domain],[domain],..."
  )
  .option("disable-js", "disable JavaScript on the page that will be loaded")
  .boolean("disable-js")
  .option("no-externals", "block requests to 3rd party domains")
  .boolean("no-externals")
  .option("post-load-delay", "wait X seconds before generating a report")
  .option("scroll", "scroll down the page when it's loaded")
  .boolean("scroll")
  .option("spy-eval", "report calls to eval()")
  .boolean("spy-eval")
  .option("stop-at-onload", "stop phantomas immediately after onload event")
  .boolean("stop-at-onload")
  .option("timeout", "timeout for phantomas run")
  .default("timeout", 15)
  .option(
    "wait-for-event",
    "wait for a given phantomas event before generating a report"
  )
  .option(
    "wait-for-selector",
    "wait for an element matching given CSS selector before generating a report"
  )
  .option("scroll", "scroll down the page when it's loaded")
  .boolean("scroll")
  .option("socket", "[experimental] use provided UNIX socket for IPC")
  .header("Output and reporting")
  .option("analyze-css", "[experimental] emit in-depth CSS metrics")
  .boolean("analyze-css")
  .option("colors", "forces ANSI colors even when output is piped")
  .boolean("colors")
  .option(
    "film-strip",
    "register film strip when page is loading (a comma separated list of milliseconds can be passed)"
  )
  .boolean("film-strip")
  .option(
    "film-strip-dir",
    "folder path to output film strip (default is ./filmstrip directory)"
  )
  .option("har", "save HAR to a given file")
  .option("log", "log to a given file")
  .option("page-source", "[experimental] save page source to file")
  .boolean("page-source")
  .option(
    "page-source-dir",
    "[experimental] folder path to output page source (default is ./html directory)"
  )
  .option("reporter", "output format / reporter")
  .default("reporter", "plain")
  .alias("reporter", "R")
  .alias("reporter", "format")
  .option("screenshot", "render fully loaded page to a given file")
  .option("silent", "don't write anything to the console")
  .boolean("silent");
 */
// handle --config (issue #209)
//program.setConfigFile("config");

// handle env variables (issue #685)
//program.setReplacementVars(process.env);

// parse it
program.parse(process.argv);
var options = program.opts();

debug("argv: %j", process.argv);
debug("opts: %j", options);

// handle URL passed without --url option (#249)
if (typeof options.url === "undefined" && process.argv.length >= 3) {
  options.url = process.argv[2];
}

// --url is mandatory -> show help
if (typeof options.url !== "string" && typeof options.config === "undefined") {
  debug("URL not provided - show help and leave");
  console.log(program.helpInformation());
  process.exit(255);
}

url = options.url;
debug("url: %s", url);

delete options.url;

// handle --no-foo options
options["no-externals"] = options.externals === false;
{'fraction_non_alphanumeric': 0.1597315436241611, 'fraction_numerical': 0.007298657718120805, 'mean_word_length': 3.7645883293365308, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 9, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1150917', 'n_tokens_mistral': 3853, 'n_tokens_neox': 3667, 'n_words': 1124}