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

"use strict";

const os = require("os");
const fs = require("fs");
const path = require("path");
const meow = require("meow");
const execa = require("execa");
const dedent = require("dedent");
const inquirer = require("inquirer");

// Global variables

const configDir = path.join(os.homedir(), "/.config/");
const configPath = path.join(configDir, "sshfs-box.json");

// CLI

const cli = meow(
  `
  Small CLI tool to simply mount/unmount remote sshfs directories

  Usage
    $ sshfs-box

  Options
    --config, -c  Configure remote & local paths to connect
                  Config stored in ~/.config/sshfs-box.json
`,
  {
    alias: {
      c: "config",
      configure: "config"
    }
  }
);

// Main

let configString;
try {
  configString = fs.readFileSync(configPath, "utf8");
} catch (e) {}

if (cli.flags.config) {
  promptEditConfig(configString).then(promptSshfs);
} else {
  if (configString) {
    const validation = validateConfigString(configString); // true ok, otherwise returns error string
    if (validation === true) {
      promptSshfs(JSON.parse(configString));
    } else {
      process.stderr.write(
        `${configPath} does not contain valid config, please fix it`
      );
      promptEditConfig(configString).then(promptSshfs);
    }
  } else {
    process.stderr.write(
      `Cant open config on ${configPath}, creating new config`
    );
    promptEditConfig().then(promptSshfs);
  }
}

// Prompt functions

function promptEditConfig(defaultConfigOverride) {
  const defaultConfig = dedent`
    {
      "urls": [
        "user@host1:dir",
        "user@host2:dir"
      ],
      "folder": "${path.join(os.homedir(), "/remote")}"
    }
  `;

  const promptSettings = {
    type: "editor",
    name: "config",
    message: "Configure sshfs-box",
    default: defaultConfigOverride || defaultConfig,
    validate: validateConfigString
  };

  return inquirer.prompt(promptSettings).then(response => {
    const configString = response.config;
    const config = JSON.parse(configString);
    try {
      fs.mkdirSync(configDir);
    } catch (e) {
      // pass
    }

    fs.writeFileSync(configPath, configString, "utf8");

    return config;
  });
}

function promptSshfs(config) {
  const mounted = execa.shellSync("mount").stdout.split("\n");

  function isMounted(remote, local) {
    return mounted.some(mount => mount.startsWith(remote + " on " + local));
  }

  const data = config.urls.map(remote => {
    // user@host:/dir/dir -> user@host-.dir.div
    const local = path.join(
      config.folder,
      remote.replace(":", "-").replace("/", ".")
    );

    const isChecked = isMounted(remote, local);

    return {
      name: `${remote} ↔ ${local}`,
      checked: isChecked,
      remote: remote,
      local: local
    };
  });

  inquirer
    .prompt({
      type: "checkbox",<commit_msg>Fix folder special chars replacement<commit_after>#!/usr/bin/env node

"use strict";

const os = require("os");
const fs = require("fs");
const path = require("path");
const meow = require("meow");
const execa = require("execa");
const dedent = require("dedent");
const inquirer = require("inquirer");

// Global variables

const configDir = path.join(os.homedir(), "/.config/");
const configPath = path.join(configDir, "sshfs-box.json");

// CLI

const cli = meow(
  `
  Small CLI tool to simply mount/unmount remote sshfs directories

  Usage
    $ sshfs-box

  Options
    --config, -c  Configure remote & local paths to connect
                  Config stored in ~/.config/sshfs-box.json
`,
  {
    alias: {
      c: "config",
      configure: "config"
    }
  }
);

// Main

let configString;
try {
  configString = fs.readFileSync(configPath, "utf8");
} catch (e) {}

if (cli.flags.config) {
  promptEditConfig(configString).then(promptSshfs);
} else {
  if (configString) {
    const validation = validateConfigString(configString); // true ok, otherwise returns error string
    if (validation === true) {
      promptSshfs(JSON.parse(configString));
    } else {
      process.stderr.write(
        `${configPath} does not contain valid config, please fix it`
      );
      promptEditConfig(configString).then(promptSshfs);
    }
  } else {
    process.stderr.write(
      `Cant open config on ${configPath}, creating new config`
    );
    promptEditConfig().then(promptSshfs);
  }
}

// Prompt functions

function promptEditConfig(defaultConfigOverride) {
  const defaultConfig = dedent`
    {
      "urls": [
        "user@host1:dir",
        "user@host2:dir"
      ],
      "folder": "${path.join(os.homedir(), "/remote")}"
    }
  `;

  const promptSettings = {
    type: "editor",
    name: "config",
    message: "Configure sshfs-box",
    default: defaultConfigOverride || defaultConfig,
    validate: validateConfigString
  };

  return inquirer.prompt(promptSettings).then(response => {
    const configString = response.config;
    const config = JSON.parse(configString);
    try {
      fs.mkdirSync(configDir);
    } catch (e) {
      // pass
    }

    fs.writeFileSync(configPath, configString, "utf8");

    return config;
  });
}

function promptSshfs(config) {
  const mounted = execa.shellSync("mount").stdout.split("\n");

  function isMounted(remote, local) {
    return mounted.some(mount => mount.startsWith(remote + " on " + local));
  }

  const data = config.urls.map(remote => {
    // user@host:/dir/dir => user@host--dir-div
    const local = path.join(
      config.folder,
      remote.replace(/:/g, "-").replace(/\//g, "-")
    );

    const isChecked = isMounted(remote, local);

    return {
      name: `${remote} ↔ ${local}`,
      checked: isChecked,
      remote: remote,
      local: local
    };
  });

  inquirer
    .prompt({
      type: "checkbox",
{'fraction_non_alphanumeric': 0.14866735894773278, 'fraction_numerical': 0.0013845621322256837, 'mean_word_length': 2.939331970006817, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '4746874', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1820, 'n_words': 417}