From b5f80be5fec50b4eb709d18e92002af1700b1ea7 Mon Sep 17 00:00:00 2001 From: Alexandre Froger Date: Wed, 10 Jan 2024 06:35:52 +0800 Subject: [PATCH] fix moved paths + generic nodeJS MVP ; next is python --- integration/dummy-generic/dummy-generic.js | 149 ++++++++++++++++++++- integration/dummy-generic/wppus-api.js | 16 +++ integration/dummy-generic/wppus-api.php | 4 +- integration/dummy-generic/wppus-api.sh | 6 +- 4 files changed, 170 insertions(+), 5 deletions(-) diff --git a/integration/dummy-generic/dummy-generic.js b/integration/dummy-generic/dummy-generic.js index d813c40..6fb987e 100755 --- a/integration/dummy-generic/dummy-generic.js +++ b/integration/dummy-generic/dummy-generic.js @@ -1 +1,148 @@ -const wppusApi = require('./wppus-api'); \ No newline at end of file +const wppusApi = require('./wppus-api'); +const commands = { + + // ### CHECKING THE PACKAGE STATUS ### + + status: function () { + + if (true === wppusApi.isInstalled()) { + console.log("Status: Installed"); + } else if (false === wppusApi.isInstalled()) { + console.log("Status: Not installed"); + } else { + console.log("Status: Unknown"); + } + }, + + // ### INSTALLING THE PACKAGE ### + + install: function (licenseKey) { + // If the command is "install", the script is not installed, and the license key is not empty + if (false === wppusApi.isInstalled() && '' !== licenseKey) { + // Install the script + wppusApi.install(licenseKey); + + console.log("Installed"); + } else { + console.log("Failed to install"); + } + }, + + + // ### UNINSTALLING THE PACKAGE ### + + uninstall: function () { + // If the command is "uninstall" and the script is installed + if (true === wppusApi.isInstalled()) { + // Uninstall the script + wppusApi.uninstall(); + + console.log("Uninstalled"); + } else { + console.log("Failed to uninstall"); + } + }, + + // ### ACTIVATING THE LICENSE ### + + activate: function () { + // If the command is "activate", the script is installed, and the license key is not empty + if (true === wppusApi.isInstalled()) { + // Activate the license + wppusApi.activate(); + + console.log("Activated"); + } else { + console.log("The package is not installed"); + } + }, + + // ### DEACTIVATING THE LICENSE ### + + deactivate: function () { + // If the command is "deactivate" and the script is installed + if (true === wppusApi.isInstalled()) { + // Deactivate the license + wppusApi.deactivate(); + + console.log("Deactivated"); + } else { + console.log("The package is not installed"); + } + }, + + // ### GETTING UPDATE INFORMATION ### + + get_update_info: function () { + // If the command is "get_update_info" and the script is installed + if (true === wppusApi.isInstalled()) { + // Get the update information + const info = wppusApi.getUpdateInfo(); + // Get the current version + const version = wppusApi.getVersion(); + // Get the remote version + const newVersion = info.version; + + console.log(`current ${version} vs. remote ${newVersion}`); + + if (newVersion > version) { + console.log("Update available !!! Run the \"update\" command!"); + } + + console.log("---------"); + console.log(info); + } else { + console.log("The package is not installed"); + } + }, + + // ### UPDATING THE PACKAGE ### + + update: function () { + // If the command is "update" and the script is installed + if (true === wppusApi.isInstalled()) { + // Get the update information + wppusApi.update(); + + console.log("Updated"); + console.log(wppusApi.getUpdateInfo()); + } else { + console.log("The package is not installed"); + } + }, + + // ### USAGE ### + + usage: function () { + console.log("Usage: ./dummy-generic.js [command] [arguments]"); + console.log("Commands:"); + console.log(" install [license] - install the package"); + console.log(" uninstall - uninstall the package"); + console.log(" activate - activate the license"); + console.log(" deactivate - deactivate the license"); + console.log(" get_update_info - output information about the remote package update"); + console.log(" update - update the package if available"); + console.log(" status - output the package status"); + console.log("Note: this package assumes it needs a license."); + } +}; + +// ### MAIN ### + +(function () { + const command = process.argv[2] || ''; + const license = process.argv[3] || ''; + + if (typeof commands[command] === 'function') { + + if (command === 'install') { + commands[command](license); + } else { + commands[command](); + } + } else { + commands.usage(); + } + + process.exit(); +})(); \ No newline at end of file diff --git a/integration/dummy-generic/wppus-api.js b/integration/dummy-generic/wppus-api.js index c2910c0..d92c189 100755 --- a/integration/dummy-generic/wppus-api.js +++ b/integration/dummy-generic/wppus-api.js @@ -255,6 +255,22 @@ async function main() { let file = files[i]; if (file !== script_name) { + let destinationPath = path.join(__dirname, file); + + if (fs.existsSync(destinationPath)) { + + if (parseInt(process.version.slice(1).split('.')[0]) >= 14) { + fs.rmSync(destinationPath, { recursive: true, force: true }); + } else { + + if (fs.lstatSync(destinationPath).isDirectory()) { + fs.rmdirSync(destinationPath, { recursive: true }); + } else { + fs.unlinkSync(destinationPath); + } + } + } + fs.renameSync('/tmp/' + package_name + '/' + file, path.join(__dirname, file)); } } diff --git a/integration/dummy-generic/wppus-api.php b/integration/dummy-generic/wppus-api.php index 76e3d0f..19ed020 100644 --- a/integration/dummy-generic/wppus-api.php +++ b/integration/dummy-generic/wppus-api.php @@ -221,9 +221,11 @@ public static function update() { # move the updated main script to the current directory ; # the updated main script is in charge of overriding the update # scripts by moving files around after update + $t_info = PATHINFO_FILENAME; + foreach ( glob( '/tmp/' . self::$package_name . '/*' ) as $file ) { - if ( ! is_dir( $file ) && pathinfo( $file, PATHINFO_FILENAME ) !== pathinfo( self::$package_script, PATHINFO_FILENAME ) ) { + if ( pathinfo( $file, $t_info ) !== pathinfo( self::$package_script, $t_info ) ) { rename( $file, dirname( self::$package_script ) . '/' . basename( $file ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename } } diff --git a/integration/dummy-generic/wppus-api.sh b/integration/dummy-generic/wppus-api.sh index 9aa4122..9b75070 100755 --- a/integration/dummy-generic/wppus-api.sh +++ b/integration/dummy-generic/wppus-api.sh @@ -253,11 +253,11 @@ if [ "$1" == "update" ]; then # set the permissions of the new file to the permissions of the old file chmod $OCTAL_MODE /tmp/$package_name/$package_name.sh - # move all the files except the wppus-api (all languages) to the + # move all the files and directories except the wppus-api (all languages) to the # current directory ; the updated main script is in charge of # overriding the update scripts by moving files around after update - for file in /tmp/$package_name *; do - if [[ ! -d "$file" ]] && [[ "${file%.*}" != "${script_name%.*}" ]]; then + for file in /tmp/$package_name/*; do + if [[ "${file%.*}" != "${script_name%.*}" ]]; then mv /tmp/$package_name/$file $(dirname "$0") fi done