diff --git a/integration/dummy-generic/dummy-generic.js b/integration/dummy-generic/dummy-generic.js index 6fb987e..ed31616 100755 --- a/integration/dummy-generic/dummy-generic.js +++ b/integration/dummy-generic/dummy-generic.js @@ -1,148 +1,160 @@ +#!/usr/bin/env node + const wppusApi = require('./wppus-api'); -const commands = { - // ### CHECKING THE PACKAGE STATUS ### +wppusApi.on('ready', function (api) { - status: function () { + const commands = { - if (true === wppusApi.isInstalled()) { - console.log("Status: Installed"); - } else if (false === wppusApi.isInstalled()) { - console.log("Status: Not installed"); - } else { - console.log("Status: Unknown"); - } - }, + // ### CHECKING THE PACKAGE STATUS ### - // ### INSTALLING THE PACKAGE ### + status: async function () { - 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); + if (true === api.is_installed()) { + console.log("Status: Installed"); + } else if (false === api.is_installed()) { + console.log("Status: Not installed"); + } else { + console.log("Status: Unknown"); + } + }, - console.log("Installed"); - } else { - console.log("Failed to install"); - } - }, + // ### INSTALLING THE PACKAGE ### + install: async function (licenseKey) { + // If the command is "install", the script is not installed, and the license key is not empty + if (false === api.is_installed() && '' !== licenseKey) { + // Install the script + api.install(licenseKey); - // ### UNINSTALLING THE PACKAGE ### + console.log("Installed"); + } else { + console.log("Failed to install"); + } + }, - 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"); - } - }, + // ### UNINSTALLING THE PACKAGE ### - // ### ACTIVATING THE LICENSE ### + uninstall: async function () { + // If the command is "uninstall" and the script is installed + if (true === api.is_installed()) { + // Uninstall the script + api.uninstall(); - 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("Uninstalled"); + } else { + console.log("Failed to uninstall"); + } + }, - console.log("Activated"); - } else { - console.log("The package is not installed"); - } - }, + // ### ACTIVATING THE LICENSE ### - // ### DEACTIVATING THE LICENSE ### + activate: async function () { + // If the command is "activate", the script is installed, and the license key is not empty + if (true === api.is_installed()) { + // Activate the license + await api.activate_license(); - deactivate: function () { - // If the command is "deactivate" and the script is installed - if (true === wppusApi.isInstalled()) { - // Deactivate the license - wppusApi.deactivate(); + console.log("Activated"); + } else { + console.log("The package is not installed"); + } + }, - console.log("Deactivated"); - } else { - console.log("The package is not installed"); - } - }, + // ### DEACTIVATING THE LICENSE ### - // ### GETTING UPDATE INFORMATION ### + deactivate: async function () { + // If the command is "deactivate" and the script is installed + if (true === api.is_installed()) { + // Deactivate the license + api.deactivate_license(); - 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("Deactivated"); + } else { + console.log("The package is not installed"); + } + }, + + // ### GETTING UPDATE INFORMATION ### + + get_update_info: async function () { + // If the command is "get_update_info" and the script is installed + if (true === api.is_installed()) { + // Get the update information + const info = await api.get_update_info(); + // Get the current version + const version = api.get_version(); + // Get the remote version + const newVersion = info.version; + + console.log(""); + console.log(`current ${version} vs. remote ${newVersion}`); + console.log(""); + + if (newVersion > version) { + console.log("---------"); + console.log(""); + console.log("Update available !!! Run the \"update\" command!"); + console.log(""); + } + + console.log("---------"); + console.log(""); + console.log(info); + } else { + console.log("The package is not installed"); + } + }, - console.log(`current ${version} vs. remote ${newVersion}`); + // ### UPDATING THE PACKAGE ### - if (newVersion > version) { - console.log("Update available !!! Run the \"update\" command!"); - } + update: async function () { + // If the command is "update" and the script is installed + if (true === api.is_installed()) { + // Get the update information + api.update(); - console.log("---------"); - console.log(info); - } else { - console.log("The package is not installed"); + console.log("Updated"); + console.log(api.get_update_info()); + } 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."); } - }, + }; - // ### UPDATING THE PACKAGE ### + // ### MAIN ### - update: function () { - // If the command is "update" and the script is installed - if (true === wppusApi.isInstalled()) { - // Get the update information - wppusApi.update(); + (async function () { + const command = process.argv[2] || ''; + const license = process.argv[3] || ''; - 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); + if (typeof commands[command] === 'function') { + + if (command === 'install') { + await commands[command](license); + } else { + await commands[command](); + } } else { - commands[command](); + commands.usage(); } - } else { - commands.usage(); - } - process.exit(); -})(); \ No newline at end of file + 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 d92c189..117b60e 100755 --- a/integration/dummy-generic/wppus-api.js +++ b/integration/dummy-generic/wppus-api.js @@ -59,32 +59,32 @@ async function main() { // define the current version of the package from the wppus.json file let version = config.packageData.Version; // define license_key from the wppus.json file - let license_key = config.licenseKey; + let license_key = config.licenseKey ? config.licenseKey : ''; // define license_signature from the wppus.json file - let license_signature = config.licenseSignature; + let license_signature = config.licenseSignature ? config.licenseSignature : ''; // define the domain let domain = ""; if ("Darwin" === os.type()) { - domain = machineIdSync(); + domain = machineIdSync().replace(/\n+$/, "");; } else if ("Linux" === os.type()) { - domain = fs.readFileSync('/var/lib/dbus/machine-id', 'utf8'); + domain = fs.readFileSync('/var/lib/dbus/machine-id', 'utf8').replace(/\n+$/, "");; } // ### INSTALLING THE PACKAGE ### - const install = function () { + const install = async function (license_key) { // add the license key to wppus.json config.licenseKey = license_key; // add a file '.installed' in current directory fs.writeFileSync(path.join(__dirname, '.installed'), ''); // write the new wppus.json file - fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config)); + fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config, null, 4)); }; // ### UNINSTALLING THE PACKAGE ### - const uninstall = function () { + const uninstall = async function () { // remove the license key from wppus.json config.licenseKey = ""; // remove the license signature from wppus.json @@ -92,7 +92,7 @@ async function main() { // remove the file '.installed' from current directory fs.unlinkSync(path.join(__dirname, '.installed')); // write the new wppus.json file - fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config)); + fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config, null, 4)); license_signature = ""; }; @@ -108,7 +108,7 @@ async function main() { const send_api_request = function (endpoint, args) { // build the request url - let full_url = url + '/' + endpoint + '/?' + querystring.stringify(args); + let full_url = url.replace(/\/$/, "") + '/' + endpoint + '/?' + querystring.stringify(args); // make the request return new Promise((resolve, reject) => { let response = ''; @@ -161,12 +161,12 @@ async function main() { // make the request let response = await send_api_request(endpoint, args); // get the signature from the response - let signature = decodeURIComponent(response.license_signature); + let signature = JSON.parse(decodeURIComponent(response)).license_signature; // add the license signature to wppus.json config.licenseSignature = signature; // write the new wppus.json file - fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config)); + fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config, null, 4)); license_signature = signature; }; @@ -187,7 +187,7 @@ async function main() { // remove the license signature from wppus.json config.licenseSignature = ""; // write the new wppus.json file - fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config)); + fs.writeFileSync(path.join(__dirname, 'wppus.json'), JSON.stringify(config, null, 4)); license_signature = ""; }; @@ -230,7 +230,7 @@ async function main() { // check for updates let response = await check_for_updates(); // get the version from the response - let new_version = JSON.parse(response).version; + let new_version = JSON.parse(decodeURIComponent(response)).version; if (compareVersions(version, new_version) < 0) { // download the update @@ -286,7 +286,7 @@ async function main() { const get_update_info = async function () { // get the update information - return await check_for_updates(); + return JSON.parse(decodeURIComponent(await check_for_updates())); }; return { diff --git a/integration/dummy-generic/wppus-api.php b/integration/dummy-generic/wppus-api.php index 19ed020..1ac30eb 100644 --- a/integration/dummy-generic/wppus-api.php +++ b/integration/dummy-generic/wppus-api.php @@ -43,10 +43,10 @@ public static function init() { # define the domain if ( 'Darwin' === PHP_OS ) { # macOS - self::$domain = exec( 'ioreg -rd1 -c IOPlatformExpertDevice | awk -F\'"\' \'/IOPlatformUUID/{print $4}\'' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec, WordPress.WP.GlobalVariablesOverride.Prohibited + self::$domain = rtrim( exec( 'ioreg -rd1 -c IOPlatformExpertDevice | awk -F\'"\' \'/IOPlatformUUID/{print $4}\'' ), "\n" ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec, WordPress.WP.GlobalVariablesOverride.Prohibited } elseif ( 'Linux' === PHP_OS ) { # Ubuntu - self::$domain = exec( 'cat /var/lib/dbus/machine-id' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec, WordPress.WP.GlobalVariablesOverride.Prohibited + self::$domain = rtrim( exec( 'cat /var/lib/dbus/machine-id' ), "\n" ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec, WordPress.WP.GlobalVariablesOverride.Prohibited } } diff --git a/integration/dummy-generic/wppus-api.sh b/integration/dummy-generic/wppus-api.sh index 9b75070..837d04a 100755 --- a/integration/dummy-generic/wppus-api.sh +++ b/integration/dummy-generic/wppus-api.sh @@ -31,10 +31,10 @@ license_signature=$(jq -r '.licenseSignature' "$(cd "$(dirname "$0")"; pwd -P)/w # define the domain if [[ "$(uname)" == "Darwin" ]]; then # macOS - domain=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}') + domain=$(echo "$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}')" | tr -d '\n') elif [[ "$(uname)" == "Linux" ]]; then # Ubuntu - domain=$(cat /var/lib/dbus/machine-id) + domain=$(echo "$(cat /var/lib/dbus/machine-id)" | tr -d '\n') fi ### INSTALLING THE PACKAGE ###