{"size": 2704, "ext": "js", "max_stars_repo_path": "bin/cli.js", "max_stars_repo_name": "rjmunro/bookmarklet", "max_stars_repo_stars_event_min_datetime": "2015-01-15T02:33:42.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-30T19:50:24.000Z", "max_issues_repo_path": "bin/cli.js", "max_issues_repo_name": "rjmunro/bookmarklet", "max_issues_repo_issues_event_min_datetime": "2015-02-07T16:56:22.000Z", "max_issues_repo_issues_event_max_datetime": "2021-04-06T14:28:09.000Z", "max_forks_repo_path": "bin/cli.js", "max_forks_repo_name": "rjmunro/bookmarklet", "max_forks_repo_forks_event_min_datetime": "2015-02-07T06:30:01.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-25T08:48:05.000Z"}
{"max_stars_count": 161, "max_issues_count": 16, "max_forks_count": 38, "avg_line_length": 19.7372262774, "max_line_length": 77, "alphanum_fraction": 0.5980029586}
#!/usr/bin/env node

const path = require('path');
const fs = require('fs');
const bookmarklet = require('../bookmarklet');

//
// Input parsing
//

let args = process.argv.slice(2);

if (['-V', '--version'].some(flag => args.indexOf(flag) !== -1)) {
  console.log(bookmarklet.version.join('.'));
  process.exit(0);
}

function help() {
  console.error(`
Bookmarklet v${bookmarklet.version.join('.')}

Usage: bookmarklet [options] source [destination]
  source       path to file to read from or - for stdin
  destination  path to file to write to

Options:
  -d, --demo   generate a demo HTML page

More info: https://github.com/mrcoles/bookmarklet
  `);
}

function die(msg) {
  msg && console.error(`[ERROR] bookmarklet: ${msg}`);
  process.exit(1);
}

function warn(msg) {
  console.error(`[WARN] bookmarklet: ${msg}`);
}

// flags

const _isArgDemo = arg => arg === '-d' || arg === '--demo';

const makeDemo = args.some(_isArgDemo);

args = args.filter(arg => !_isArgDemo(arg));

// help

if (args.length == 0 || args.some(arg => arg === '-h' || arg === '--help')) {
  help();
  process.exit(0);
}

// file paths

if (args.length > 2) {
  die('invalid arguments, run with --help to see usage.\n\n');
}

let source = args[0];
let destination = args[1];

const readStdin = source === '-';

if (source && source[0] !== '/' && !readStdin) {
  source = path.join(process.cwd(), source);
}

if (destination) {
  if (destination[0] !== '/') {
    destination = path.join(process.cwd(), destination);
  }
  let isDirectory = destination.endsWith('/');
  if (!isDirectory) {
    try {
      let destStat = fs.statSync(destination);
      isDirectory = destStat.isDirectory();
    } catch (e) {}
  }

  if (isDirectory) {
    if (readStdin) {
      die('must name output file if reading from stdin\n\n');
    }

    let filename = path.basename(source);
    destination = path.join(destination, filename);
  }
}

//
// Main
//

function dataCallback(e, data) {
  if (e) {
    die(e.message);
  }

  data = bookmarklet.parseFile(data);

  if (data.errors) {
    die(data.errors.join('\n'));
  }

  return bookmarklet
    .convert(data.code, data.options)
    .then(code => {
      if (makeDemo) {
        code = bookmarklet.makeDemo(code, data.options);
      }

      if (destination) {
        fs.writeFileSync(destination, code);
      } else {
        console.log(code);
      }
    })
    .catch(e => {
      die(e);
    });
}

if (source !== '-') {
  fs.readFile(source, 'utf8', dataCallback);
} else {
  process.stdin.resume();
  process.stdin.setEncoding('utf8');

  var buffer = '';
  process.stdin.on('data', data => (buffer += data));
  process.stdin.on('end', () => dataCallback(false, buffer));
}
