<commit_before>const fs = require('fs-extra');
const paths = require('./paths');
const chokidar = require('chokidar');
const EventEmitter = require('events');

const config = new EventEmitter();

let appRegistry = 'rekit/app-registry';
let pluginRegistry = 'rekit/plugin-registry';
let appType;
function getPkgJson(noCache, prjRoot) {
  const pkgJsonPath = prjRoot ? paths.join(prjRoot, 'package.json') : paths.map('package.json');
  if (!fs.existsSync(pkgJsonPath)) return null;
  if (noCache) delete require.cache[pkgJsonPath];
  return require(pkgJsonPath);
}

let rekitConfig = null;
let rekitConfigWatcher = null;
function getRekitConfig(noCache, prjRoot) {
  const rekitConfigFile = prjRoot ? paths.join(prjRoot, 'rekit.json') : paths.map('rekit.json');
  const pkgJsonPath = prjRoot ? paths.join(prjRoot, 'package.json') : paths.map('package.json');
  if (!rekitConfigWatcher && !global.__REKIT_NO_CONFIG_WATCH) {
    rekitConfigWatcher = chokidar.watch([rekitConfigFile, pkgJsonPath], { persistent: true });
    rekitConfigWatcher.on('all', () => {
      rekitConfig = null;
      config.emit('change');
    });
  }

  if (rekitConfig) return rekitConfig;

  if (fs.existsSync(rekitConfigFile)) {
    try {
      rekitConfig = fs.readJsonSync(rekitConfigFile);
    } catch (err) {
      throw new Error('Config file broken: failed to parse rekit.json');
    }
  } else {
    const pkgJson = getPkgJson(true, prjRoot);
    rekitConfig = pkgJson.rekit;
  }

  const c = rekitConfig || {};
  c.appType = appType || c.appType;
  return c;
}

function setAppType(_appType) {
  appType = _appType;
}

function setAppRegistry(reg) {
  appRegistry = reg;
}

function getAppRegistry() {
  return appRegistry;
}

function setPluginRegistry(reg) {
  pluginRegistry = reg;
}

function getPluginRegistry() {
  return pluginRegistry;
}

// Load rekit configuration from package.json
Object.assign(config, {
  css: 'less',
  style: 'less',
  getPkgJson,<commit_msg>Support project with no package.json<commit_after>const fs = require('fs-extra');
const path = require('path');
const paths = require('./paths');
const chokidar = require('chokidar');
const EventEmitter = require('events');

const config = new EventEmitter();

let appRegistry = 'rekit/app-registry';
let pluginRegistry = 'rekit/plugin-registry';
let appType;
function getPkgJson(noCache, prjRoot) {
  const pkgJsonPath = prjRoot ? paths.join(prjRoot, 'package.json') : paths.map('package.json');
  if (!fs.existsSync(pkgJsonPath)) return null;
  if (noCache) delete require.cache[pkgJsonPath];
  return require(pkgJsonPath);
}

let rekitConfig = null;
let rekitConfigWatcher = null;
function getRekitConfig(noCache, prjRoot) {
  const rekitConfigFile = prjRoot ? paths.join(prjRoot, 'rekit.json') : paths.map('rekit.json');
  const pkgJsonPath = prjRoot ? paths.join(prjRoot, 'package.json') : paths.map('package.json');
  if (!rekitConfigWatcher && !global.__REKIT_NO_CONFIG_WATCH) {
    rekitConfigWatcher = chokidar.watch([rekitConfigFile, pkgJsonPath], { persistent: true });
    rekitConfigWatcher.on('all', () => {
      rekitConfig = null;
      config.emit('change');
    });
  }

  if (rekitConfig) return rekitConfig;

  if (fs.existsSync(rekitConfigFile)) {
    try {
      rekitConfig = fs.readJsonSync(rekitConfigFile);
    } catch (err) {
      throw new Error('Config file broken: failed to parse rekit.json');
    }
  } else {
    const pkgJson = getPkgJson(true, prjRoot);
    rekitConfig = pkgJson && pkgJson.rekit || {};
  }

  const c = rekitConfig || {};
  c.appType = appType || c.appType;
  return c;
}

function getAppName() {
  const pkgJson = getPkgJson();
  return pkgJson ? pkgJson.name : path.basename(__dirname);
}

function setAppType(_appType) {
  appType = _appType;
}

function setAppRegistry(reg) {
  appRegistry = reg;
}

function getAppRegistry() {
  return appRegistry;
}

function setPluginRegistry(reg) {
  pluginRegistry = reg;
}

function getPluginRegistry() {
  return pluginRegistry;
}

// Load rekit configuration from package.json
Object.assign(config, {
  css: 'less',
  style: 'less',
  getAppName,
  getPkgJson,
{'fraction_non_alphanumeric': 0.1357125515901918, 'fraction_numerical': 0.0, 'mean_word_length': 4.522788203753351, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1864646', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1375, 'n_words': 301}