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

require('dotenv').config();

const pkg = require('../package.json');
const exportResume = require('./export-resume');
const serve = require('./serve');
const init = require('./init');
const test = require('./test');
const getResume = require('./pre-flow/get-resume');
const program = require('commander');
const chalk = require('chalk');
const path = require('path');

const normalizeTheme = (value, defaultValue) => {
  const theme = value || defaultValue;
  // TODO - This is not great, but bypasses this function if it is a relative path
  if (theme[0] === '.') {
    return theme;
  }
  return theme.match('jsonresume-theme-.*')
    ? theme
    : `jsonresume-theme-${theme}`;
};

(async () => {
  program
    .usage('[command] [options]')
    .version(pkg.version)
    .option(
      '-F, --force',
      'Used by `publish` and `export` - bypasses schema testing.',
    )
    .option(
      '-t, --theme <theme name>',
      'Specify theme used by `export` and `serve` or specify a path starting with . (use . for current directory or ../some/other/dir)',
      normalizeTheme,
      'jsonresume-theme-even',
    )
    .option('-f, --format <file type extension>', 'Used by `export`.')
    .option(
      '-r, --resume <resume filename>',
      'path to the resume in json format',
      path.join(process.cwd(), 'resume.json'),
    )
    .option('-p, --port <port>', 'Used by `serve` (default: 4000)', 4000)
    .option(
      '-s, --silent',
      'Used by `serve` to tell it if open browser auto or not.',
      false,
    )
    .option(
      '-d, --dir <path>',
      'Used by `serve` to indicate a public directory path.',
      'public',
    )
    .option(
      '--schema <relativePath>',
      'Used by `test` to validate against a custom schema.',
    );

  program
    .command('init')
    .description('Initialize a resume.json file')
    .action(() => {
      init();
    });

  program
    .command('test')
    .description('Schema validation test your resume.json')
    .action(async () => {
      const resume = await getResume({ resumePath: program.resume });
      await test(resume);
    });

  program
    .command('export [fileName]')
    .description(
      'Export locally to .html or .pdf. Supply a --format <file format> flag and argument to specify export format.',
    )
    .action(async (fileName) => {
      const resume = await getResume({ resumePath: program.resume });
      exportResume(
        { resume, fileName, ...program },
        (err, fileName, format) => {
          console.log(
            chalk.green(
              '\nDone! Find your new',
              format,
              'resume at:\n',
              path.resolve(process.cwd(), fileName + format),
            ),
          );
        },
      );
    });

  program
    .command('serve')
    .description('Serve resume at http://localhost:4000/')
    .action(async () => {
      const resume = await getResume({
        validate: false,
        resumePath: program.resume,
      });
      serve({
        ...program,
        resume,
      });
    });

  await program.parseAsync(process.argv);

  const validCommands = program.commands.map((cmd) => {
    return cmd._name;
  });

  // https://github.com/tj/commander.js/blob/master/CHANGELOG.md#testing-for-no-arguments
  if (program.rawArgs.length < 3) {
    console.log(chalk.cyan('resume-cli:'), 'https://jsonresume.org', '\n');
    program.help();
  } else if (validCommands.indexOf(process.argv[2]) === -1) {
    console.log(chalk.red('Invalid argument:'), process.argv[2]);
    console.log(chalk.cyan('resume-cli:'), 'https://jsonresume.org', '\n');<commit_msg>fixup! refactor: removing pre-flow. getting resume only when needed (#440) (#447)<commit_after>#!/usr/bin/env node

require('dotenv').config();

const pkg = require('../package.json');
const exportResume = require('./export-resume');
const serve = require('./serve');
const init = require('./init');
const test = require('./test');
const getResume = require('./pre-flow/get-resume');
const program = require('commander');
const chalk = require('chalk');
const path = require('path');

const normalizeTheme = (value, defaultValue) => {
  const theme = value || defaultValue;
  // TODO - This is not great, but bypasses this function if it is a relative path
  if (theme[0] === '.') {
    return theme;
  }
  return theme.match('jsonresume-theme-.*')
    ? theme
    : `jsonresume-theme-${theme}`;
};

(async () => {
  program
    .usage('[command] [options]')
    .version(pkg.version)
    .option(
      '-F, --force',
      'Used by `publish` and `export` - bypasses schema testing.',
    )
    .option(
      '-t, --theme <theme name>',
      'Specify theme used by `export` and `serve` or specify a path starting with . (use . for current directory or ../some/other/dir)',
      normalizeTheme,
      'jsonresume-theme-even',
    )
    .option('-f, --format <file type extension>', 'Used by `export`.')
    .option(
      '-r, --resume <resume filename>',
      'path to the resume in json format',
      path.join(process.cwd(), 'resume.json'),
    )
    .option('-p, --port <port>', 'Used by `serve` (default: 4000)', 4000)
    .option(
      '-s, --silent',
      'Used by `serve` to tell it if open browser auto or not.',
      false,
    )
    .option(
      '-d, --dir <path>',
      'Used by `serve` to indicate a public directory path.',
      'public',
    )
    .option(
      '--schema <relativePath>',
      'Used by `test` to validate against a custom schema.',
    );

  program
    .command('init')
    .description('Initialize a resume.json file')
    .action(() => {
      init();
    });

  program
    .command('test')
    .description('Schema validation test your resume.json')
    .action(async () => {
      const resume = await getResume({ resumePath: program.resume });
      await test(resume);
    });

  program
    .command('export [fileName]')
    .description(
      'Export locally to .html or .pdf. Supply a --format <file format> flag and argument to specify export format.',
    )
    .action(async (fileName) => {
      const resume = await getResume({ resumePath: program.resume });
      exportResume(
        { ...program, resume, fileName },
        (err, fileName, format) => {
          console.log(
            chalk.green(
              '\nDone! Find your new',
              format,
              'resume at:\n',
              path.resolve(process.cwd(), fileName + format),
            ),
          );
        },
      );
    });

  program
    .command('serve')
    .description('Serve resume at http://localhost:4000/')
    .action(async () => {
      serve({
        ...program,
        resumeFilename: program.resume,
      });
    });

  await program.parseAsync(process.argv);

  const validCommands = program.commands.map((cmd) => {
    return cmd._name;
  });

  // https://github.com/tj/commander.js/blob/master/CHANGELOG.md#testing-for-no-arguments
  if (program.rawArgs.length < 3) {
    console.log(chalk.cyan('resume-cli:'), 'https://jsonresume.org', '\n');
    program.help();
  } else if (validCommands.indexOf(process.argv[2]) === -1) {
    console.log(chalk.red('Invalid argument:'), process.argv[2]);
    console.log(chalk.cyan('resume-cli:'), 'https://jsonresume.org', '\n');
{'fraction_non_alphanumeric': 0.17378800328677074, 'fraction_numerical': 0.005477951246233908, 'mean_word_length': 2.4743101807802095, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 33, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3159384', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2252, 'n_words': 586}