Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.0.4 #25

Merged
merged 70 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
3791575
Rename class and file 'install' to 'Installer'
erikyo Dec 8, 2023
45ec2e0
Add Database.js file with upload and dump methods
erikyo Dec 8, 2023
daa727a
Refactor package installation classes into separate files
erikyo Dec 8, 2023
ac8af1d
Add Updater.js with update methods for plugins, themes and WordPress
erikyo Dec 8, 2023
ec94d27
Refactor utils.js, moving functions into newly created utils/index.js
erikyo Dec 8, 2023
0aeb047
Refactor code and enhance database functionality
erikyo Dec 8, 2023
41fd51d
Update package version and add new dependencies
erikyo Dec 8, 2023
0426834
Add typings for WPMMconfig object properties
erikyo Dec 8, 2023
9b2d987
Refactor getConfig function for better typing
erikyo Dec 8, 2023
29655bf
Improve code documentation and implement proper naming
erikyo Dec 8, 2023
ccca816
Add comprehensive JSDoc comments and refactor variable names
erikyo Dec 8, 2023
33fba9c
Refactor codebase for clarity and add JSDoc comments
erikyo Dec 8, 2023
e68283e
Add user locale function and improve code readability
erikyo Dec 9, 2023
fa8c1ec
Create WordPress configuration utility functions
erikyo Dec 9, 2023
d3b28f6
Refactor Dump class for WordPress configuration
erikyo Dec 9, 2023
d75d1e9
Correct typo in Initialize.js comments
erikyo Dec 9, 2023
96386a7
Refactor WordPress installer in Installer.js
erikyo Dec 9, 2023
c34e7e2
Refactor Package class and remove Plugin/Theme specific classes
erikyo Dec 9, 2023
15e6a95
Refactor WpPackage setup and configuration
erikyo Dec 9, 2023
bc80770
Refactor WordPress paths utility and action functions
erikyo Dec 9, 2023
0282cdc
Update package version and dependencies in package-lock.json
erikyo Dec 9, 2023
25421ce
: Simplify wp-package.json and reorganize tests
erikyo Dec 9, 2023
ebfb655
Refactor and simplify utility test file imports
erikyo Dec 9, 2023
6e54259
Provides an example for the new database related functions
erikyo Dec 9, 2023
b19665a
Add post-install command runner function
erikyo Dec 9, 2023
263a661
chore
erikyo Dec 9, 2023
6ee55c1
: Implement remote config loading
erikyo Dec 9, 2023
d1484b4
Update baseFolder for WordPress installation
erikyo Dec 9, 2023
86c4c55
Add lib/constants.js file for WordPress install
erikyo Dec 9, 2023
1036322
Refactor WordPress config generation in Initialize.js
erikyo Dec 9, 2023
99afe09
Implement WordPress package update functionality
erikyo Dec 9, 2023
9b2bf81
Refactor Dump.js and improve WordPress version handling
erikyo Dec 9, 2023
44cd7f6
Add husky for git hooks and update scripts
erikyo Dec 9, 2023
b08953c
Implement Updater class and adjust update functionality
erikyo Dec 9, 2023
b854a04
Refactor update method with Updater class and 'updateObject'
erikyo Dec 9, 2023
7b46e00
Refactor updater logic by moving it to Updater class
erikyo Dec 9, 2023
adee258
Update and organize Initialize class methods
erikyo Dec 9, 2023
b02291d
Refactor dependency paths and constructor in Installer class
erikyo Dec 9, 2023
3d64e4e
Refactor lib/utils module and update Installer class
erikyo Dec 9, 2023
255e610
Refactor utility functions and update Installer constructor
erikyo Dec 9, 2023
caa1dee
Remove `wp-package.json` and refactor utility imports
erikyo Dec 9, 2023
f82e789
Refactor code to consolidate utility imports
erikyo Dec 9, 2023
a6ae21a
Update base folder definition and refactor file imports
erikyo Dec 9, 2023
2a795b4
Refactor function name and add new tests to utils.test.js
erikyo Dec 9, 2023
c78c061
Add WordPress configuration parsing functionality and related tests
erikyo Dec 9, 2023
33fa39f
Add initConfig function to data.js
erikyo Dec 10, 2023
06758fe
Refactor getConfig function and connection settings in data.js
erikyo Dec 10, 2023
f71f110
Add axiosFetch function to wordpress.js
erikyo Dec 10, 2023
0841b18
Replace axios.get with axiosFetch in getLastWp function
erikyo Dec 10, 2023
5af2e3c
Add getWpConfigContent function in wordpress.js
erikyo Dec 10, 2023
575bc94
Fix constant names and improve function parsers in wordpress.js
erikyo Dec 10, 2023
9d79a14
Refactor database dump method and improve error handling
erikyo Dec 10, 2023
1b4993f
Update Dump class in lib/Dump.js
erikyo Dec 10, 2023
130c25c
Update casing of constant declarations in lib/constants.js
erikyo Dec 10, 2023
120d1ad
Adapt lib/index.js to handle asynchronous configuration loading
erikyo Dec 10, 2023
c3bfc09
Refactor code to use async functions and rename constants
erikyo Dec 10, 2023
719d2c0
Update parseWpConfig tests and move test assets to fixtures
erikyo Dec 10, 2023
c63a8d1
Add directory existence validation to scanDirectory method
erikyo Dec 10, 2023
3b633b7
Refactor WordPress configuration definitions and simplify action invo…
erikyo Dec 10, 2023
57cbbb7
Add actions.js file with WordPress management functionalities
erikyo Dec 10, 2023
98f741d
Refactor actions in actions.js and index.js
erikyo Dec 10, 2023
c982e82
Refactor code for clarity and error handling in data.js
erikyo Dec 10, 2023
aabc72d
Refactor code and update documentation
erikyo Dec 10, 2023
6a29c76
Update project dependencies and introduce TypeScript
erikyo Dec 10, 2023
9973f23
Refactor and optimize WordPress installation configuration
erikyo Dec 11, 2023
bb8be61
Add utility functions for WordPress configuration parsing and modific…
erikyo Dec 11, 2023
3f8eb28
Add utility prompts for WordPress configuration and dump options
erikyo Dec 11, 2023
c635334
Refactor utility imports and update test configurations
erikyo Dec 11, 2023
da2a62d
Modify typescript target, update eslint script and add dependencies
erikyo Dec 11, 2023
0999c8a
Update configuration handling and refactor prompts
erikyo Dec 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor and optimize WordPress installation configuration
The commit encompasses several important changes targeted towards WordPress package installation and configuration. It first optimizes the WpPackage.js file for more streamlined and efficient handling of WordPress installations. It also includes smarter parsing and generation of WordPress and other package config files. Furthermore, it enhances the database.js file to use better defined configurations. Major changes involve movement of parser-related functions to a new utils/parsers.js file, and inclusion of user prompts related to WordPress installation in a new utils/prompts.js file.
  • Loading branch information
erikyo committed Dec 11, 2023
commit 9973f23732329700248d531ebda964112915adad
12 changes: 8 additions & 4 deletions lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Database {
*/
dbDumpFilename = () => {
const date = new Date().toDateString().replace(" ", "-");
return `${this.config.wordpress.config.DB_NAME}-${date}.sql.gz`;
return `${this.config.wordpress.WP_config.DB_NAME}-${date}.sql.gz`;
};

/**
Expand All @@ -49,7 +49,11 @@ class Database {
return new Error('Database configuration not found');
}

const databaseConnectionConfig = getConnectionSettings(this.config.wordpress.config);
/**
* the connection settings for the database
* @type {mysql.ConnectionOptions} databaseConnectionConfig - The connection settings for the database.
*/
const databaseConnectionConfig = getConnectionSettings(this.config.wordpress.WP_config);

/**
* @type {import('mysql2/promise').Connection} connection - The MySQL connection object.
Expand Down Expand Up @@ -95,7 +99,7 @@ class Database {

console.log(`✳️ Dumping database to ${dumpFile}...`);

if (!this.config.wordpress.config) {
if (!this.config.wordpress.WP_config) {
return new Error('🔴 Database configuration not found');
}

Expand All @@ -104,7 +108,7 @@ class Database {
*
* @type {mysqldump.ConnectionOptions} databaseConnectionConfig - The connection settings for the database.
*/
const databaseConnectionConfig = getConnectionSettings(this.config.wordpress.config);
const databaseConnectionConfig = getConnectionSettings(this.config.wordpress.WP_config);

if (databaseConnectionConfig.host === 'localhost') {
console.log('⚠️ Warning: You are using localhost as your database host. This may not work correctly.');
Expand Down
49 changes: 29 additions & 20 deletions lib/Dump.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require('fs');
const path = require('path');
const { getCurrentWpInfo, getUserLocale, getLastWp } = require("./utils/wordpress.js");
const {parseWpConfig, getWpConfigContent} = require("./utils/wordpress");
const {getCurrentWpInfo} = require("./utils/wordpress.js");
const {getWpConfigContent} = require("./utils/wordpress.js");
const {parseWpConfig} = require("./utils/parsers.js");

/**
* Represents a Dump class for WordPress configuration.
Expand All @@ -13,50 +14,51 @@ class Dump {
* Constructor for the class.
*
* Initializes the class with the necessary folders for WordPress.
* @param {import("./constants").WPMMpaths} paths - The object containing the paths for the WordPress installation.
*/
constructor (paths) {
this.wpFolder = paths.baseFolder;
this.baseFolder = paths.baseFolder;
this.themeFolder = paths.themeFolder;
this.pluginsFolder = paths.pluginsFolder;
}

/**
* Initializes the function by logging the `wpFolder` and `themeFolder` properties,
* Initializes the function by logging the `baseFolder` and `themeFolder` properties,
* scanning the theme and plugins directories, retrieving the website name from the
* `wpFolder` path, getting the WordPress version from `wp-includes/version.php`,
* `baseFolder` path, getting the WordPress version from `wp-includes/version.php`,
* determining the language using `Intl.DateTimeFormat().resolvedOptions().locale`,
* and saving the result to a JSON file.
*
* @return {void}
* @return {Promise<import('./constants').WPMMconfig|{}>} - The configuration object.
*/
init() {
async init() {
const themes = this.scanDirectory(this.themeFolder);
const plugins = this.scanDirectory(this.pluginsFolder);

// the website name
const name = path.basename(this.wpFolder);
const name = path.basename(this.baseFolder);

console.log(`🔍️ Scanning ${this.wpFolder}`);
console.log(`🔍️ Scanning ${this.baseFolder}`);
/**
* @type {{locale: string|null, version: string|null}}
*/
const wpInfo = getCurrentWpInfo(this.wpFolder);
const wpInfo = getCurrentWpInfo(this.baseFolder);

const wpConfigData = parseWpConfig(
getWpConfigContent(this.wpFolder)
getWpConfigContent(this.baseFolder)
);

const language = wpInfo.locale || getUserLocale();
const version = wpInfo.version || getLastWp()?.version || 'latest';
const version = wpInfo.version || 'latest';
const language = wpInfo.locale || 'en_US';

const result = {
name,
wordpress: {
name,
version,
language,
config: {
...wpConfigData.constants,
...wpConfigData.variables
WP_config: {
...wpConfigData?.constants,
...wpConfigData?.variables
}
},
themes,
Expand All @@ -67,13 +69,15 @@ class Dump {

fs.writeFileSync(outputPath, JSON.stringify(result, null, 2));
console.log(`🆗 Wordpress configuration Dump completed. Configuration saved to ${outputPath}`);

return result;
}

/**
* Scans a directory and returns an array of objects containing the name and version of each item found.
*
* @param {string} directory - The path of the directory to scan.
* @return {Array} - An array of objects with the name and version of each item found.
* @return {import('./constants').WPMMconfigPkg[]|null} - An array of objects with the name and version of each item found.
*/
scanDirectory (directory) {
// Check if the directory exists
Expand All @@ -83,7 +87,12 @@ class Dump {
}

const items = fs.readdirSync(directory);
const result = [];
/**
* The array of parsed items
*
* @type {import('./constants').WPMMconfigPkg[]} result
*/
let result= [];

for (const item of items) {
const fullPath = path.join(directory, item);
Expand Down Expand Up @@ -113,7 +122,7 @@ class Dump {
}
}

return result;
return result.length ? result : null;
}

/**
Expand Down
15 changes: 7 additions & 8 deletions lib/Initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class Initialize {
/**
* Generates the configuration file for WordPress.
*
* @param {{version ?: string, language?: string}=} options - The options for the initialization.
* @return {Promise<import('./constants.js').WPMMconfig>} The configuration object.
*/
async generateConfig() {
async generateConfig(options) {

// check if the output path exists
if (this.hasConfig()) {
Expand All @@ -48,15 +49,13 @@ class Initialize {
}

const name = this.wpFolder.split(path.sep).pop() || DefaultWpInstallFolder;
const lastWp = await getLastWp() ?? { version: 'latest' };
const userLocale = getUserLocale();

return {
name,
wordpress: {
version: lastWp?.version || 'latest',
language: userLocale,
config: DefaultWpConfig
name,
version: options?.version || (await getLastWp())?.version || 'latest',
language: options?.language || getUserLocale(),
'WP_config': DefaultWpConfig
},
database: {
type: DefaultWpDatabaseType,
Expand All @@ -71,7 +70,7 @@ class Initialize {
writeConfig = (/** @type {import('./constants.js').WPMMconfig} */ result) => {
// write the config to the output path
fs.writeFileSync(this.outputPath, JSON.stringify(result, null, 2));
console.log(`🆗 Wordpress configuration file created. Configuration saved to ${this.outputPath}`);
console.log(`🆗 WordPress configuration file created. Configuration saved to ${this.outputPath}`);
};
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ class Installer {

// Install WordPress
if (wordpress) {
const wp = new WpPackage(this.config, 'wordpress', this.paths);
const wp = new WpPackage(this.config.wordpress, 'wordpress', this.paths);
await wp.install();
}

if (plugins) {
const pluginPackages = plugins.map((plugin) => new Package(plugin, 'plugin', this.paths));
promises.push(...pluginPackages.map((pluginPackage) => pluginPackage.install().then(() => {
wpJson.plugins[pluginPackage.pkgInfo.name] = pluginPackage.pkgInfo;
wpJson.plugins.push(pluginPackage.pkgInfo);
})));
}

if (themes) {
const themePackages = themes.map((theme) => new Package(theme, 'theme', this.paths));
promises.push(...themePackages.map((themePackage) => themePackage.install().then(() => {
wpJson.themes[themePackage.pkgInfo.name] = themePackage.pkgInfo;
wpJson.themes.push(themePackage.pkgInfo);
})));
}

Expand Down
5 changes: 3 additions & 2 deletions lib/Package.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ const {
* Represents a package and provides methods to download and install it.
*
* @class Package
* @template Package
*/
class Package {
/**
* Constructs a new instance of the class.
*
* @param {import('./constants.js').WPMMconfigPkg} pkgConfig - the configuration object
* @param {string} packageType - the type of package
* @param {{baseFolder: *}} paths - the object containing the paths
* @param {import('./constants.js').WPMMpaths} paths - the object containing the paths
*/
constructor (pkgConfig, packageType, paths) {
this.pkgInfo = pkgConfig;
Expand Down Expand Up @@ -53,7 +54,7 @@ class Package {
* @param {import('./constants.js').WPMMpaths} paths - An array of paths.
* @param {string} packageType - The type of package ('plugin' or 'theme').
*
* @return {string|undefined} The destination folder path.
* @return {string} The destination folder path.
*/
getDestFolder (paths, packageType) {
if (packageType === 'plugin') {
Expand Down
43 changes: 18 additions & 25 deletions lib/WpPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,35 @@ const fs = require('fs');
const { renameFolder } = require('./utils/fs.js');

const Package = require('./Package.js');
const { replaceDbConstant, replaceDbConstantBool, replaceEmptySalts } = require('./utils/wordpress.js');
const { replaceDbConstant, replaceDbConstantBool, replaceEmptySalts } = require('./utils/parsers.js');
const { getWordPressDownloadUrl } = require('./utils/data.js');

/**
* Represents a WordPress package that can be installed and configured.
*
* @class WpPackage
* @extends Package
* @extends Package<WpPackage> pkgConfig - the configuration object
*/
class WpPackage extends Package {

/**
* the constructor of the class
* Constructs a new instance of the class.
* @param {import('./constants.js').WordpressPkg} pkgConfig - the configuration object
* @param {string} packageType - the type of package
* @param {import('./constants.js').WPMMpaths} paths - the object containing the paths
*/
constructor (pkgConfig, paths) {
/**
* @type {import('./constants.js').WordpressPkg} pkgConfig - the configuration object
*/
super(
pkgConfig,
'wordpress',
{
...paths,
baseFolder: paths.baseFolder
}
);
constructor(pkgConfig, packageType, paths) {
super(pkgConfig, 'wordpress', paths);
this.pkgInfo = pkgConfig;
}

/**
* Installs WordPress with the specified version and language.
*
* @param {string} version - The version of WordPress to install.
* @param {string} language - The language of WordPress to install.
* @param {string=} version - The version of WordPress to install.
* @param {string=} language - The language of WordPress to install.
*/
async installWordPress (version, language) {
async installWordPress (version = 'latest', language = 'en_US') {
const downloadUrl = getWordPressDownloadUrl(version, language);

try {
Expand Down Expand Up @@ -83,13 +75,14 @@ class WpPackage extends Package {
let configContent = fs.readFileSync(configPath, 'utf8');

// Update database name, username, password, and other settings based on user-defined config
configContent = replaceDbConstant(configContent, 'DB_NAME', this.pkgInfo.wordpress.config.DB_NAME);
configContent = replaceDbConstant(configContent, 'DB_USER', this.pkgInfo.wordpress.config.DB_USER);
configContent = replaceDbConstant(configContent, 'DB_PASSWORD', this.pkgInfo.wordpress.config.DB_PASSWORD);
configContent = replaceDbConstant(configContent, 'DB_HOST', this.pkgInfo.wordpress.config.DB_HOST);
configContent = replaceDbConstant(configContent, 'DB_CHARSET', this.pkgInfo.wordpress.config.DB_CHARSET);
configContent = replaceDbConstant(configContent, 'DB_NAME', this.pkgInfo.WP_config.DB_NAME);
configContent = replaceDbConstant(configContent, 'DB_USER', this.pkgInfo.WP_config.DB_USER);
configContent = replaceDbConstant(configContent, 'DB_PASSWORD', this.pkgInfo.WP_config.DB_PASSWORD);
configContent = replaceDbConstant(configContent, 'DB_HOST', this.pkgInfo.WP_config.DB_HOST);
configContent = replaceDbConstant(configContent, 'DB_CHARSET', this.pkgInfo.WP_config.DB_CHARSET);

configContent = replaceDbConstantBool(configContent, 'WP_DEBUG', this.pkgInfo.WP_config.WP_DEBUG);

configContent = replaceDbConstantBool(configContent, 'WP_DEBUG', this.pkgInfo.wordpress.config.WP_DEBUG);

configContent = replaceEmptySalts(configContent);

Expand All @@ -108,7 +101,7 @@ class WpPackage extends Package {
* @returns {Promise<void>} A Promise that resolves when the installation and configuration are complete.
*/
async install () {
const { version, language } = this.pkgInfo.wordpress;
const { version, language } = this.pkgInfo;
await this.installWordPress(version, language);
await this.setupWordPressConfig();
}
Expand Down
12 changes: 7 additions & 5 deletions lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,29 @@ function actions({config, paths}) {
/** Dumps the current WordPress installation data. This function creates a new Dump instance and initializes it.*/
dump: () => {
const dump = new Dump(paths);
dump.init();
dump.init().then(() => {
console.log('🚀 All data dumped successfully.');
});
},

/** Dump the current WordPress database. */
'dump-db': async () => {

const db = new Database(config);
await db.dumpDatabase(paths.baseFolder);
console.log(`🚀 Database dumped successfully.`);
},

/** Dump the current WordPress database, plugins and themes setup. */
'dump-all': () => {
wpmmActions["dump-db"]() || console.log('🚀 WP Database dumped successfully.');
wpmmActions.dump() || console.log('🚀 All data dumped successfully.');
wpmmActions["dump-db"]() &&
wpmmActions.dump() &&
console.log('🚀 All data dumped successfully.');
},

/** Upload a database by executing SQL queries from a specified file. */
'upload-db': () => {
const db = new Database(config);
db.uploadDatabase(config.wordpress.config.DB_NAME).then(() => {
db.uploadDatabase(config.wordpress.WP_config.DB_NAME).then(() => {
console.log('🚀 Database uploaded successfully.');
});
},
Expand Down
Loading