@seliseblocks/next-forms
Version: 
Next.js Generic Packages for Forms
53 lines (47 loc) • 1.45 kB
JavaScript
const fs = require('fs');
const path = require('path');
const execSync = require('child_process');
const NEXT_TRANSPILE_KEY = 'transpilePackages';
const PROJECT_PATH = __dirname.split('node_modules')[0];
const LIB_PATH = path.resolve(__dirname, '..');
const PACKAGE_JSON_PATH = path.resolve(LIB_PATH, 'package.json');
const CONFIG_PATH = path.resolve(PROJECT_PATH, 'next.config.js');
/**
 * @description Reads a JSON file and returns the JSON object.
 * @param {string} path The path of the JSON file.
 * @returns {object} The JSON object.
 */
const readJson = (path) => {
  try {
    return JSON.parse(fs.readFileSync(path, 'utf-8'));
  } catch (e) {
    console.error(`Error reading ${path.split('/').pop()} from your project!`, e);
    return null;
  }
};
/**
 * @description Writes a JSON object to a file.
 * @param configObj
 * @param configPath
 * @returns {void}
 */
const writeFile = (configObj, configPath) => {
  fs.writeFileSync(configPath, `module.exports = ${JSON.stringify(configObj, null, 2)}`);
  execSync.execSync(`prettier --write ${configPath}`, { encoding: 'utf-8' });
};
/**
 * @description Constants used in the scripts.
 * @type {{NEXT_TRANSPILE_KEY: string, PROJECT_PATH: string, LIB_PATH: string, PACKAGE_JSON_PATH: string, CONFIG_PATH: string}}
 */
const CONSTANTS = {
  NEXT_TRANSPILE_KEY,
  PROJECT_PATH,
  LIB_PATH,
  PACKAGE_JSON_PATH,
  CONFIG_PATH,
};
module.exports = {
  readJson,
  writeFile,
  CONSTANTS,
};