UNPKG

@nomadmystic/wordpress-scaffold-cli

Version:

This project is created to speed up WordPress development

237 lines (236 loc) 10 kB
import fs from 'fs'; import path from 'path'; import fse from "fs-extra"; import StringUtils from '../../utils/string-utils.js'; import PathUtils from '../../utils/path-utils.js'; import MessagingUtils from '../../utils/messaging-utils.js'; import { packageRootDir } from '../../utils/package-root.js'; import { scaffoldInternal } from './scaffold-internal.js'; import DebugUtils from "../../utils/debug-utils.js"; const updateInternalJson = async (filePath, json, isPlugin = false) => { try { await scaffoldInternal(); const dashedValues = [ 'project-name', 'active-theme', ]; const disallowedKeys = [ 'database-setup', 'database-name', 'database-password', 'database-username', 'site-admin-password', 'site-admin-user', 'admin-email', ]; let jsonFile = fs.readFileSync(filePath, 'utf-8'); if (!jsonFile || typeof jsonFile === 'undefined' || jsonFile === '') { return ''; } let jsonFileParsed = JSON.parse(jsonFile); let property; for (property in json) { if (Object.hasOwn(json, property) && property && typeof property !== 'undefined') { let dashedProperty = await StringUtils.camelCaseToDash(property); if (json[property] && typeof json[property] === 'undefined' && json[property] !== '') { continue; } if (json[property] && typeof json[property] !== 'undefined' && typeof json[property] === 'string' && dashedValues.includes(dashedProperty)) { jsonFileParsed[`${dashedProperty}`] = await StringUtils.addDashesToString(json[property].trim()); if (dashedProperty === 'project-name') { jsonFileParsed['project-namespace'] = await StringUtils.pascalCaseString(jsonFileParsed['project-name']); continue; } continue; } if (typeof json[property] !== 'undefined' && !disallowedKeys.includes(dashedProperty)) { jsonFileParsed[`${dashedProperty}`] = json[property]; } } } fs.writeFileSync(filePath, JSON.stringify(jsonFileParsed)); return JSON.parse(fs.readFileSync(filePath, 'utf-8')); } catch (err) { console.log('updateInternalJson()'); console.error(err); } }; export class ProjectJson { static isDebugFullMode = false; static whereAmI = ''; static configFilePath = ''; static dashedValues = [ 'project-name', 'active-theme', ]; static disallowedKeys = [ 'database-setup', 'database-name', 'database-password', 'database-username', 'site-admin-password', 'site-admin-user', 'admin-email', 'plugin-name', 'plugin-path', 'plugin-description', 'plugin-front-end-framework', ]; static update = async (configUpdates, type = '') => { try { this.whereAmI = await PathUtils.whereAmI(); this.isDebugFullMode = await DebugUtils.isDebugFullMode(); await this.scaffoldInternal(); this.configFilePath = `${this.whereAmI}/internal/project/project-config.json`; let projectConfigObject = await this.getProjectConfigObject(); projectConfigObject = await this.performRootJsonUpdate(projectConfigObject, configUpdates); if (type === 'plugin') { projectConfigObject = await this.performPluginJsonUpdate(projectConfigObject, configUpdates); } await this.saveFile(projectConfigObject); return projectConfigObject; } catch (err) { console.log('ProjectJson.update()'); console.error(err); } }; static saveFile = async (projectConfigObject) => { try { fs.writeFileSync(this.configFilePath, JSON.stringify(projectConfigObject)); await MessagingUtils.displayColoredMessage('The internal project config file has been saved.', 'green'); } catch (err) { console.log('ProjectJson.saveFile()'); console.error(err); } }; static scaffoldInternal = async () => { try { if (!fs.existsSync(`${this.whereAmI}/internal`)) { fse.copySync(`${path.join(packageRootDir + '/scaffolding/internal')}`, `${this.whereAmI}/internal`, { overwrite: false }); } } catch (err) { console.log('ProjectJson.scaffoldInternal()'); console.error(err); } }; static getProjectConfigObject = async () => { try { let jsonFile = fs.readFileSync(this.configFilePath, 'utf-8'); if (!jsonFile || typeof jsonFile === 'undefined' || jsonFile === '') { return ''; } return JSON.parse(jsonFile); } catch (err) { console.log('ProjectJson.getProjectConfigObject'); console.error(err); } }; static performRootJsonUpdate = async (projectConfigObject, configUpdates) => { try { let property; for (property in configUpdates) { if (Object.hasOwn(configUpdates, property) && property && typeof property !== 'undefined') { let dashedProperty = await StringUtils.camelCaseToDash(property); if (configUpdates[property] && typeof configUpdates[property] === 'undefined' && configUpdates[property] !== '') { continue; } if (configUpdates[property] && typeof configUpdates[property] !== 'undefined' && typeof configUpdates[property] === 'string' && this.dashedValues.includes(dashedProperty)) { projectConfigObject[`${dashedProperty}`] = await StringUtils.addDashesToString(configUpdates[property].trim()); if (dashedProperty === 'project-name') { projectConfigObject['project-namespace'] = await StringUtils.pascalCaseString(projectConfigObject['project-name']); continue; } continue; } if (typeof configUpdates[property] !== 'undefined' && !this.disallowedKeys.includes(dashedProperty)) { projectConfigObject[`${dashedProperty}`] = configUpdates[property]; } } } return JSON.parse(fs.readFileSync(this.configFilePath, 'utf-8')); } catch (err) { console.log('ProjectJson.performRootJsonUpdate()'); console.error(err); } }; static performPluginJsonUpdate = async (projectConfigObject, configUpdates) => { try { let activePlugins = projectConfigObject['active-plugins']; const alreadyExists = await this.cleanUpPluginArray(activePlugins, configUpdates); if (!alreadyExists) { activePlugins.push(configUpdates); } else { console.log(`Plugin ${configUpdates['plugin-name']} already exists in the schema. Please try another name.`); process.exit(1); } projectConfigObject['active-plugins'] = activePlugins; if (this.isDebugFullMode) { console.log('ProjectJson.performPluginJsonUpdate()'); console.log('projectConfigObject'); console.log(projectConfigObject); console.log('configUpdates'); console.log(configUpdates); } return projectConfigObject; } catch (err) { console.log('ProjectJson.performPluginJsonUpdate()'); console.error(err); } }; static cleanUpPluginArray = async (activePlugins, configUpdates) => { try { let alreadyExists = false; for (let plugin = 0; plugin < activePlugins.length; plugin++) { if (activePlugins[plugin] && typeof activePlugins[plugin] !== 'undefined') { const currentPlugin = await this.deleteUnusedPluginProperties(activePlugins[plugin]); if (!currentPlugin || typeof currentPlugin === 'undefined' || Object.keys(currentPlugin).length === 0) { activePlugins.splice(plugin, 1); } if (activePlugins[plugin]?.['plugin-name'] && activePlugins[plugin]?.['plugin-name'] === configUpdates['plugin-name']) { if (fs.existsSync(activePlugins[plugin]?.['plugin-path'])) { alreadyExists = true; } } } } return alreadyExists; } catch (err) { console.log('ProjectJson.cleanUpPluginArray()'); console.error(err); } }; static deleteUnusedPluginProperties = async (plugin) => { try { let currentPlugin = plugin; if (!fs.existsSync(currentPlugin?.['plugin-path'])) { delete currentPlugin['plugin-name']; delete currentPlugin['plugin-path']; delete currentPlugin['plugin-description']; delete currentPlugin['plugin-front-end-framework']; } return currentPlugin; } catch (err) { console.log('ProjectJson.deleteUnusedPluginProperties()'); console.error(err); } }; } export default updateInternalJson;