{"size": 1133, "ext": "js", "max_stars_repo_path": "lib/prettier.js", "max_stars_repo_name": "lyrgithub/ff-cms-cli", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/prettier.js", "max_issues_repo_name": "lyrgithub/ff-cms-cli", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/prettier.js", "max_forks_repo_name": "lyrgithub/ff-cms-cli", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.1224489796, "max_line_length": 79, "alphanum_fraction": 0.6213592233}
const path = require("path");
const chalk = require("chalk");
const fs = require("fs-extra");
const exists = require("fs").existsSync;
const format = require("prettier-eslint");

const cwd = process.cwd();

module.exports = function(args) {
  const file = args[3];
  if (!file) {
    console.log(
      chalk.yellow("Need to appoint a javascript file path: `pro p ./test.js`")
    );
  }

  let eslintConfig;
  try {
    eslintConfig = fs.readJsonSync(path.join(cwd, "./.eslintrc"));
  } catch (e) {
    try {
      eslintConfig = fs.readJsonSync(path.join(cwd, "./.eslintrc.json"));
    } catch (e) {
      throw new Error(
        "no found .eslintrc[.json], please run `pro p` in project root path."
      );
    }
  }

  const source = fs.readFileSync(path.join(cwd, file), "utf8");

  const options = {
    text: source,
    eslintConfig,
    prettierOptions: {
      bracketSpacing: true
    },
    fallbackPrettierOptions: {
      singleQuote: true
    }
  };

  const formattedCode = format(options);

  fs.writeFileSync(path.join(cwd, file), formattedCode, "utf8");

  console.log(chalk.green("Format file success :)"));
};
