UNPKG

@nomadmystic/wordpress-scaffold-cli

Version:

This project is created to speed up WordPress development

120 lines (119 loc) 5.32 kB
#!/usr/bin/env node import 'dotenv/config'; import colors from 'colors'; import InquirerCli from '../cli/inquirer-cli.js'; import AbstractScaffold from '../abstract/AbstractScaffold.js'; import PathUtils from '../utils/path-utils.js'; import DebugUtils from '../utils/debug-utils.js'; import StringUtils from '../utils/string-utils.js'; import updateInternalJson from '../scaffold/common/update-internal-json.js'; import getThemeOptions from '../config/theme-options.js'; import scaffoldTheme from '../scaffold/theme/scaffold-theme.js'; import scaffoldThemeRoot from '../scaffold/theme/scaffold-root.js'; import updateScaffoldClasses from '../scaffold/theme/scaffold-classes.js'; class ScaffoldTheme extends AbstractScaffold { static isDebugFullMode = false; static whereAmI = ''; static initializeScaffolding = async () => { try { this.whereAmI = await PathUtils.whereAmI(); this.isDebugFullMode = await DebugUtils.isDebugFullMode(); await PathUtils.checkForWordPressInstall(); const answers = await InquirerCli.performPromptsTasks(await getThemeOptions()).catch((err) => console.error(err)); await this.scaffoldFiles(answers); } catch (err) { console.log('ScaffoldTheme.performScaffolding()'); console.error(err); } }; static scaffoldFiles = async (answers) => { try { const themeValues = await this.buildValueObject(answers); await this.scaffoldTheme(themeValues); await this.scaffoldThemeRoot(themeValues); await this.updateScaffoldClasses(themeValues); console.log(colors.green(`Your ${themeValues.name} theme has been scaffold.`)); console.log(colors.yellow(`Check: ${themeValues.themesPath}/${themeValues.safeThemeName}`)); } catch (err) { console.log('ScaffoldTheme.scaffoldFiles()'); console.error(err); } }; static buildValueObject = async (answers) => { try { const configFilePath = `${this.whereAmI}/internal/project/project-config.json`; const themesPath = await PathUtils.getThemesFolderPath(); const projectName = answers.projectName ? answers.projectName : ''; const themeName = answers.themeName ? answers.themeName.trim() : ''; const themeDescription = answers.themeDescription ? answers.themeDescription.trim() : ''; const frontEndFramework = answers.frontEndFramework ? answers.frontEndFramework : ''; const siteUrl = answers.siteUrl ? answers.siteUrl : ''; const devSiteUrl = answers.devSiteUrl ? answers.devSiteUrl : ''; const safeThemeName = await StringUtils.addDashesToString(themeName); const newThemePath = `${themesPath}/${safeThemeName}`; const capAndSnakeCaseTheme = await StringUtils.capAndSnakeCaseString(safeThemeName); let configUpdates = { 'active-theme': safeThemeName, 'active-theme-path': newThemePath, 'absolute-project-folder': this.whereAmI, 'absolute-themes-folder': themesPath, 'theme-description': themeDescription, 'front-end-framework': frontEndFramework, 'site-url': siteUrl, 'dev-site-url': devSiteUrl, }; if (projectName && typeof projectName !== 'undefined') { configUpdates['project-name'] = projectName; configUpdates['project-namespace'] = await StringUtils.pascalCaseString(projectName); } configUpdates = await updateInternalJson(configFilePath, configUpdates); return { projectName: configUpdates['project-name'], name: themeName, themesPath: themesPath, finalPath: newThemePath, themeDescription: themeDescription, frontEndFramework: frontEndFramework, siteUrl: siteUrl, devSiteUrl: devSiteUrl, safeThemeName: safeThemeName, capAndSnakeCaseTheme: capAndSnakeCaseTheme, projectNamespace: configUpdates['project-namespace'], }; } catch (err) { console.log('ScaffoldTheme.buildValueObject()'); console.error(err); } }; static scaffoldTheme = async (themeValues) => { try { await scaffoldTheme(themeValues); } catch (err) { console.log('ScaffoldTheme.scaffoldTheme()'); console.error(err); } }; static scaffoldThemeRoot = async (themeValues) => { try { await scaffoldThemeRoot(themeValues); } catch (err) { console.log('ScaffoldTheme.scaffoldThemeRoot()'); console.error(err); } }; static updateScaffoldClasses = async (themeValues) => { try { await updateScaffoldClasses(themeValues); } catch (err) { console.log('ScaffoldTheme.updateScaffoldClasses()'); console.error(err); } }; } ScaffoldTheme.initializeScaffolding().catch(err => console.error(err));