{"size": 1823, "ext": "js", "max_stars_repo_path": "packages/spire/lib/create-core.js", "max_stars_repo_name": "sbekrin/spire", "max_stars_repo_stars_event_min_datetime": "2019-02-13T10:52:41.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-03T18:23:01.000Z", "max_issues_repo_path": "packages/spire/lib/create-core.js", "max_issues_repo_name": "sbekrin/spire", "max_issues_repo_issues_event_min_datetime": "2019-04-12T10:10:04.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-08T02:48:45.000Z", "max_forks_repo_path": "packages/spire/lib/create-core.js", "max_forks_repo_name": "sbekrin/spire", "max_forks_repo_forks_event_min_datetime": "2019-04-12T09:52:22.000Z", "max_forks_repo_forks_event_max_datetime": "2019-04-12T09:52:22.000Z"}
{"max_stars_count": 22, "max_issues_count": 49, "max_forks_count": 1, "avg_line_length": 25.3194444444, "max_line_length": 66, "alphanum_fraction": 0.6505759737}
const { join } = require('path');
const {
  pathExists,
  readJson,
  writeJSON,
  readFile,
  writeFile,
} = require('fs-extra');
const detectIndent = require('detect-indent');
const gitRemoteOriginUrl = require('git-remote-origin-url');

function createCore({ cwd, logger }, { setState, getState }) {
  async function hasFile(file) {
    return pathExists(join(cwd, file));
  }
  async function getFile(file) {
    return readFile(join(cwd, file));
  }
  async function setFile(file, content) {
    return writeFile(join(cwd, file), content);
  }
  async function getPackageProp(prop) {
    const pkgPath = join(cwd, 'package.json');
    return (await readJson(pkgPath))[prop];
  }
  async function hasPackageProp(prop) {
    return (await getPackageProp(prop)) !== undefined;
  }
  async function setPackageProp(prop, value) {
    const pkgPath = join(cwd, 'package.json');
    const fileContents = await readFile(pkgPath, 'UTF-8');
    const spaces = detectIndent(fileContents).indent;
    const currentContents = JSON.parse(fileContents);
    const nextContents = { ...currentContents, [prop]: value };
    await writeJSON(pkgPath, nextContents, { spaces });
  }
  async function getProvider() {
    let remoteUrl;
    try {
      remoteUrl = await gitRemoteOriginUrl(cwd);
    } catch (error) {
      logger.warn(
        'Unable to determine git provider. Using "none" instead.',
        error
      );
      return 'none';
    }

    if (remoteUrl.includes('github.com')) {
      return 'github';
    } else if (remoteUrl.includes('gitlab.com')) {
      return 'gitlab';
    }

    return 'none';
  }

  return {
    setState,
    getState,
    hasFile,
    readFile: getFile,
    writeFile: setFile,
    getPackageProp,
    hasPackageProp,
    setPackageProp,
    getProvider,
  };
}

module.exports = createCore;
