Skip to content

Commit

Permalink
fix generic node, trim all generic domain var
Browse files Browse the repository at this point in the history
  • Loading branch information
froger-me committed Jan 10, 2024
1 parent b5f80be commit 171e360
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 137 deletions.
250 changes: 131 additions & 119 deletions integration/dummy-generic/dummy-generic.js
Original file line number Diff line number Diff line change
@@ -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();
})();
process.exit();
})();
});
28 changes: 14 additions & 14 deletions integration/dummy-generic/wppus-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,40 @@ 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
config.licenseSignature = "";
// 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 = "";
};
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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;
};
Expand All @@ -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 = "";
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions integration/dummy-generic/wppus-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
Loading

0 comments on commit 171e360

Please sign in to comment.