@nomadmystic/wordpress-scaffold-cli
Version:
This project is created to speed up WordPress development
92 lines (91 loc) • 4.23 kB
JavaScript
import fs from 'fs';
import path from 'path';
import fse from 'fs-extra';
import { glob } from 'glob';
import { packageRootDir } from '../../utils/package-root.js';
export default class UpdateTypeFiles {
static copyFiles = async (foldersToCopy) => {
try {
for (let copyFolder of foldersToCopy) {
fse.copySync(`${path.join(`${packageRootDir}/${copyFolder.source}`)}`, copyFolder.destination, {
overwrite: false
});
}
}
catch (err) {
console.log('UpdateTypeFiles.copyFiles()');
console.error(err);
}
};
static updateFiles = async (values, updateObjectsArray) => {
try {
for (let update = 0; update < updateObjectsArray.length; update++) {
if (updateObjectsArray[update] && typeof updateObjectsArray[update] !== 'undefined') {
await this.updateFile(values.finalPath, updateObjectsArray[update].fileName, updateObjectsArray[update].stringToUpdate, updateObjectsArray[update].updateString);
}
}
}
catch (err) {
console.log('UpdateTypeFiles.updateFiles()');
console.error(err);
}
};
static updateFile = async (updatePath, fileName, stringToUpdate, updateString) => {
try {
let updatedContent = '';
if (fs.existsSync(`${updatePath}/${fileName}`)) {
let fileContents = fs.readFileSync(`${updatePath}/${fileName}`, 'utf8');
let reg = new RegExp(stringToUpdate, 'gm');
updatedContent = fileContents.replaceAll(reg, updateString);
fs.writeFileSync(`${updatePath}/${fileName}`, updatedContent);
}
}
catch (err) {
console.log('UpdateTypeFiles.updateScaffoldFile()');
console.error(err);
}
};
static updateClassFiles = async (values) => {
try {
let updateObjectsArray = [];
const phpFiles = glob.sync(`${values.finalPath}/classes/**/*.php`, {
nodir: true,
});
let classFileUpdates = [];
if (phpFiles && typeof phpFiles !== 'undefined' && phpFiles.length > 0) {
for (let classPath = 0; classPath < phpFiles.length; classPath++) {
if (phpFiles[classPath] && typeof phpFiles[classPath] !== 'undefined') {
let classObject = {};
const afterLastSlash = phpFiles[classPath].substring(phpFiles[classPath].lastIndexOf('/') + 1);
const beforeLastSlash = phpFiles[classPath].match(/^(.*[\\\/])/);
classObject.updatePath = beforeLastSlash ? beforeLastSlash[0].slice(0, -1) : values.finalPath + '/classes';
classObject.fileName = afterLastSlash;
classObject.stringToUpdate = 'PASCAL_NAME';
classObject.updateString = values.namespace;
classFileUpdates.push(classObject);
}
}
updateObjectsArray.push(...classFileUpdates);
}
for (let update = 0; update < updateObjectsArray.length; update++) {
if (updateObjectsArray[update] && typeof updateObjectsArray[update] !== 'undefined') {
await this.updateFile(updateObjectsArray[update].updatePath, updateObjectsArray[update].fileName, updateObjectsArray[update].stringToUpdate, updateObjectsArray[update].updateString);
}
}
}
catch (err) {
console.log('UpdateTypeFiles.updateClassFiles()');
console.error(err);
}
};
static updateWebpack = async (values, type = 'theme') => {
try {
let webpackContent = fs.readFileSync(`${packageRootDir}/src/scaffold/mocks/mock-${type}-webpack.txt`, 'utf8');
await this.updateFile(values.finalPath, 'webpack.config.js', 'WEBPACK_PATH', webpackContent);
}
catch (err) {
console.log('UpdateTypeFiles.updateWebpack()');
console.error(err);
}
};
}