UNPKG

@nomadmystic/wordpress-scaffold-cli

Version:

This project is created to speed up WordPress development

84 lines (83 loc) 2.77 kB
import 'dotenv/config'; import path from 'path'; import fs from 'fs-extra'; import colors from 'colors'; import DebugUtils from './debug-utils.js'; export default class PathUtils { static whereAmI = async () => { const isDebugFullMode = await DebugUtils.isDebugFullMode(); if (isDebugFullMode) { const wordPressDebugPath = process.env?.WORDPRESS_PATH ?? ''; return path.resolve(wordPressDebugPath); } else { return path.resolve(process.cwd()); } }; static isWordpressInstall = async () => { try { return fs.pathExistsSync(`${await this.whereAmI()}/wp-admin/admin-ajax.php`); } catch (err) { console.log('PathUtils.isWordpressInstall()'); console.error(err); } }; static getThemesFolderPath = async () => { try { return path.resolve(`${await this.whereAmI()}/wp-content/themes`); } catch (err) { console.log('PathUtils.getThemesFolderPath()'); console.error(err); } }; static getPluginsFolderPath = async () => { try { return path.resolve(`${await this.whereAmI()}/wp-content/plugins`); } catch (err) { console.log('PathUtils.getPluginsFolderPath()'); console.error(err); } }; static checkForWordPressInstall = async () => { try { const isDebugMode = await DebugUtils.isDebugMode(); const isInstalled = await PathUtils.isWordpressInstall(); if (!isInstalled && !isDebugMode) { console.log(colors.yellow('Your path is not at the root of your WordPress install.')); console.log(colors.yellow(`You are located at ${this.whereAmI}`)); console.log(colors.yellow('Please move to the root WordPress install folder.')); process.exit(1); } } catch (err) { console.log('PathUtils.checkForWordPressInstall()'); console.error(err); } }; static validateIsPathWithDisplay = async (path, message, exit = false) => { try { if (fs.existsSync(path)) { console.log(colors.red(message)); if (exit) { process.exit(0); } } } catch (err) { console.log('PathUtils.validateIsPathWithDisplay()'); console.error(err); } }; static validateIsPath = async (path) => { try { return fs.existsSync(path); } catch (err) { console.log('PathUtils.validateIsPath'); console.error(err); } }; }