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

'use strict';

const editor = require('editor');
const {tmpdir, homedir} = require('os');
const fs = require('fs');
const storj = require('storj-lib');
const path = require('path');
const mkdirp = require('mkdirp');
const config = require('../lib/config/daemon');
const storjshare_create = require('commander');

storjshare_create
  .option('-a, --sjcx <addr>', 'specify the sjcx address (required)')
  .option('-s, --storage <path>', 'specify the storage path')
  .option('-l, --logfile <path>', 'specify the logfile path')
  .option('-k, --privkey <privkey>', 'specify the private key')
  .option('-o, --outfile <writepath>', 'write config to path')
  .parse(process.argv);

if (!storjshare_create.sjcx) {
  console.error('\n  no payment address was given, try --help');
  process.exit(1);
}

if (!storjshare_create.privkey) {
  storjshare_create.privkey = storj.KeyPair().getPrivateKey();
}

if (!storjshare_create.storage) {
  storjshare_create.storage = path.join(
    homedir(),
    '.config/storjshare/shares',
    storj.KeyPair(storjshare_create.privkey).getNodeID()
  );
  mkdirp.sync(storjshare_create.storage);
}

if (!storjshare_create.logfile) {
  storjshare_create.logfile = path.join(
    homedir(),
    '.config/storjshare/logs',
    storj.KeyPair(storjshare_create.privkey).getNodeID() + '.log'
  );
}

let isWritingToTemp = false;

if (!storjshare_create.outfile) {
  isWritingToTemp = true;
  storjshare_create.outfile = path.join(
    tmpdir(),
    storj.KeyPair(storjshare_create.privkey).getNodeID() + '.json'
  );
}

let exampleConfigPath = path.join(__dirname, '../example/farmer.config.json');
let exampleConfigString = fs.readFileSync(exampleConfigPath).toString();

function replaceEmptyConfig(prop, value) {
  exampleConfigString = exampleConfigString.replace(
    `"${prop}": ""`,
    `"${prop}": "${value}"`
  );
}

replaceEmptyConfig('paymentAddress', storjshare_create.sjcx);
replaceEmptyConfig('networkPrivateKey', storjshare_create.privkey);
replaceEmptyConfig('storagePath', path.normalize(storjshare_create.storage));
replaceEmptyConfig('loggerOutputFile',
                   path.normalize(storjshare_create.logfile));

let outfile = isWritingToTemp || path.isAbsolute(storjshare_create.outfile) ?
                path.normalize(storjshare_create.outfile) :
                path.join(process.cwd(), storjshare_create.outfile);

try {
  fs.writeFileSync(outfile, exampleConfigString);
} catch (err) {
  console.log (`\n  failed to write config, reason: ${err.message}`);
  process.exit(1);
}

console.log(`\n  * configuration written to ${outfile}`);
console.log('  * opening in your favorite editor to tweak before running');
editor(outfile, () => {
  console.log('  ...');
  console.log(`  * use new config: storjshare start --config ${outfile}`);
});
<commit_msg>hack windows paths into JSON str replace<commit_after>#!/usr/bin/env node

'use strict';

const editor = require('editor');
const {tmpdir, homedir} = require('os');
const fs = require('fs');
const storj = require('storj-lib');
const path = require('path');
const mkdirp = require('mkdirp');
const config = require('../lib/config/daemon');
const storjshare_create = require('commander');

storjshare_create
  .option('-a, --sjcx <addr>', 'specify the sjcx address (required)')
  .option('-s, --storage <path>', 'specify the storage path')
  .option('-l, --logfile <path>', 'specify the logfile path')
  .option('-k, --privkey <privkey>', 'specify the private key')
  .option('-o, --outfile <writepath>', 'write config to path')
  .parse(process.argv);

if (!storjshare_create.sjcx) {
  console.error('\n  no payment address was given, try --help');
  process.exit(1);
}

if (!storjshare_create.privkey) {
  storjshare_create.privkey = storj.KeyPair().getPrivateKey();
}

if (!storjshare_create.storage) {
  storjshare_create.storage = path.join(
    homedir(),
    '.config/storjshare/shares',
    storj.KeyPair(storjshare_create.privkey).getNodeID()
  );
  mkdirp.sync(storjshare_create.storage);
}

if (!storjshare_create.logfile) {
  storjshare_create.logfile = path.join(
    homedir(),
    '.config/storjshare/logs',
    storj.KeyPair(storjshare_create.privkey).getNodeID() + '.log'
  );
}

let isWritingToTemp = false;

if (!storjshare_create.outfile) {
  isWritingToTemp = true;
  storjshare_create.outfile = path.join(
    tmpdir(),
    storj.KeyPair(storjshare_create.privkey).getNodeID() + '.json'
  );
}

let exampleConfigPath = path.join(__dirname, '../example/farmer.config.json');
let exampleConfigString = fs.readFileSync(exampleConfigPath).toString();

function replaceEmptyConfig(prop, value) {
  value = value.split('\\').join('\\\\'); // NB: Hack windows paths into JSON
  exampleConfigString = exampleConfigString.replace(
    `"${prop}": ""`,
    `"${prop}": "${value}"`
  );
}

replaceEmptyConfig('paymentAddress', storjshare_create.sjcx);
replaceEmptyConfig('networkPrivateKey', storjshare_create.privkey);
replaceEmptyConfig('storagePath', path.normalize(storjshare_create.storage));
replaceEmptyConfig('loggerOutputFile',
                   path.normalize(storjshare_create.logfile));

let outfile = isWritingToTemp || path.isAbsolute(storjshare_create.outfile) ?
                path.normalize(storjshare_create.outfile) :
                path.join(process.cwd(), storjshare_create.outfile);

try {
  fs.writeFileSync(outfile, exampleConfigString);
} catch (err) {
  console.log (`\n  failed to write config, reason: ${err.message}`);
  process.exit(1);
}

console.log(`\n  * configuration written to ${outfile}`);
console.log('  * opening in your favorite editor to tweak before running');
editor(outfile, () => {
  console.log('  ...');
  console.log(`  * use new config: storjshare start --config ${outfile}`);
});

{'fraction_non_alphanumeric': 0.16450441100155683, 'fraction_numerical': 0.0006919218128351496, 'mean_word_length': 4.640975609756097, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '4976753', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1977, 'n_words': 372}