Skip to content

Commit

Permalink
Merge pull request #20 from asharirfan/feature/lightning-support
Browse files Browse the repository at this point in the history
Add Local Lightning support
  • Loading branch information
asharirfan authored Sep 24, 2021
2 parents 210a4da + 1c8eed6 commit 412b117
Show file tree
Hide file tree
Showing 13 changed files with 567 additions and 674 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
node_modules
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{
"env": {
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"sourceType": "module"
"ecmaVersion": 2018
},
"rules": {
"camelcase": "off",
"indent": [
"error",
"tab"
Expand Down
6 changes: 0 additions & 6 deletions .vscode/settings.json

This file was deleted.

20 changes: 11 additions & 9 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
# ⚡ local-wpcli

A CLI to configure WP-CLI with the locally hosted websites of [Local by Flywheel](https://local.getflywheel.com/).

> This CLI does not support Local Lightning — latest major release — yet. Related issue for more information: [#13](https://github.com/asharirfan/local-wpcli/issues/13)
> The CLI now works with Local Lightning! 🎉
## 🚀 Getting Started!

Use the following command to install `local-wpcli` globally.

```node
npm install -g local-wpcli
```

## ✅ Usage

To use `local-wpcli`,

1. Open terminal and go to the root of the website folder.<br> `cd ~/local-sites/local-wp/`
2. Type `local-wpcli` & press enter.
3. Before creating the configuration files, it will ask for:
1. Remote Host (IP Address)
2. Remote Port
1. Open terminal and go to the root of the website folder.<br>`cd ~/local-sites/wordpress/`
2. Type `local-wpcli` & press Enter (⏎).
3. Enter path to **Socket** file of the website & press Enter (⏎).

👉 *Note: You can find `Remote Host` & `Port` in the `DATABASE` tab of the website in Local by Flywheel's desktop app. Please refer to the screenshot below for help.*
👉 *Note: You can find `Socket` in the `DATABASE` tab of the website in Local by Flywheel's desktop app. Please refer to the following screenshot below for help.*

![](https://i.imgur.com/W9JhPvI.png "Local by Flywheel Screenshot")
!["Local by Flywheel Screenshot"](https://i.imgur.com/raRKqTv.jpg "Local by Flywheel Screenshot")

## 🎩 License & Attribution

MIT &copy; [Ashar Irfan](https://asharirfan.com).

This project is inspired by [Create Guten Block](https://github.com/ahmadawais/create-guten-block). Thank you [Ahmad Awais](https://twitter.com/MrAhmadAwais) for Create Guten Block. It rocks 🤘
This project is inspired by [Create Guten Block](https://github.com/ahmadawais/create-guten-block).
16 changes: 16 additions & 0 deletions app/exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* CLI exit.
*/

const end = require('exit-cli');
const pkgJson = require('../package.json');

const exit = async () => {

await end({
github: 'https://github.com/asharirfan/local-wpcli',
twitter: 'https://twitter.com/MrAsharIrfan',
pkgJSON: pkgJson
});
};
module.exports = exit;
4 changes: 2 additions & 2 deletions app/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module.exports = {
return fs.existsSync(file);
},

local_php: (ip, port) => {
local_php: (socket) => {
return `<?php
define('DB_HOST', '${ip}:${port}');
define('DB_HOST', 'localhost:${socket}');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
Expand Down
28 changes: 13 additions & 15 deletions app/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@ const userInput = (name, input_type, message, error) => {
message: message
};

if (name === 'remoteHostIP') {
// Set default IP if the question is for Remote Host.
question.default = '192.168.95.100';
} else if (name === 'remoteHostPort') {
// Set error message if the question is for Remote IP.
question.validate = function (value) {
if (value.length) {
return true;
} else {
return error;
}
};
}
// Set error message if the question is for Remote IP.
question.validate = function (value) {
if (value.length) {
return true;
} else {
return error;
}
};

// Return the question object.
return question;
Expand All @@ -50,8 +45,11 @@ module.exports = {
*/
askWebsiteDetails: () => {
const questions = [
userInput('remoteHostIP', 'input', 'Remote Host:', 'Please enter the IP address of the Remote Host.'), // Remote Host.
userInput('remoteHostPort', 'input', 'Remote Port:', 'Please enter the Port of the Remote Host.'), // Remote IP.
userInput(
'socket',
'input',
'Socket:', 'Please enter the socket of the database.'
), // Socket.
];
return inquirer.prompt(questions);
}
Expand Down
5 changes: 4 additions & 1 deletion app/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ const fs = require('fs');
const files = require('./files');

module.exports = (config_files, data, force) => {

config_files.forEach(function (filename) {

// Set file path.
const file = `${process.cwd()}/${filename}`;

if (!files.fileExists(file) || force) {

if (filename === 'wp-cli.local.php') {
fs.writeFile(
file,
files.local_php(data.remoteHostIP, data.remoteHostPort),
files.local_php(data.socket),
(err) => {
if (err) throw err;
}
Expand Down
13 changes: 10 additions & 3 deletions app/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const inquirer = require('./inquirer');
const install = require('./install');
const test = require('./test');
const pgjson = require('../package.json');
const exit = require('./exit');

/**
* Set the options for the CLI.
Expand All @@ -26,7 +27,7 @@ program
.version(pgjson.version, '-v, --version')
.usage('[options]')
.option('-f, --force', 'Overide existing WP-CLI configuration files.')
.parse(process.argv)
.parse(process.argv);

// If force install option is set, then set it to true.
let forceInstall = false;
Expand All @@ -35,8 +36,11 @@ if (program.force) {
}

module.exports = async () => {

// Welcome message.
console.log(chalk.green('Setting up config files...\n'));
console.log( // eslint-disable-line no-console
chalk.green('Setting up config files...\n')
);

// Config files array.
const config_files = ['wp-cli.local.php', 'wp-cli.local.yml'];
Expand All @@ -54,4 +58,7 @@ module.exports = async () => {

// Test the CLI by running a WP-CLI command.
await test();
}

// Exit CLI.
await exit();
};
31 changes: 24 additions & 7 deletions app/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,33 @@

'use strict';

const { exec } = require('child_process');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const chalk = require('chalk');

module.exports = async () => {
await exec('wp option get siteurl', (err, stdout, stderr) => {
if (err) {
console.log(`\n❌ ${stderr}`);
return;
}

console.log(chalk.green(`\n✅ Successfully connected to ${stdout}`));
const testResult = await exec(
'wp option get siteurl'
).catch(({ stderr }) => {

// Test command failed due to some reason.
console.error(`\n❌ ${stderr}`); // eslint-disable-line no-console
return false;
});

if (false === testResult) {
return;
}

const { stdout, stderr } = testResult;

if (stderr) {
console.error(`\n❌ ${stderr}`); // eslint-disable-line no-console
return;
}

console.log( // eslint-disable-line no-console
chalk.green(`\n✅ Successfully connected to ${stdout}`)
);
};
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const major = semVer[0];

// If below Node 8.
if (major < 8) {
console.error(
console.error( // eslint-disable-line no-console
chalk.red(
`⚠️ You are running Node ${nodeVersion}
Local-WPCLI requires Node 8 or higher.
Expand All @@ -27,8 +27,11 @@ Kindly, update your version of Node.`
}

// Crash the script on unhandled rejections.
process.on('unhandledRejection', err => {
throw err;
process.on('unhandledRejection', (reason) => {
console.log( // eslint-disable-line no-console
'Unhandled Rejection due to reason:',
reason
);
});

/**
Expand Down
Loading

0 comments on commit 412b117

Please sign in to comment.