Skip to content

Commit

Permalink
Merge pull request #23 from kodeflex/wp-config
Browse files Browse the repository at this point in the history
Add more of `wp-config.php` settings #5 1
  • Loading branch information
thinkholic committed Mar 5, 2017
2 parents 8bc27e3 + 497e19c commit bb83ee9
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 326 deletions.
409 changes: 252 additions & 157 deletions lib/actions/artisan.js

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions lib/actions/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,26 @@ module.exports = {
params: {}
});

const config = JSON.parse(Utils.readFileSync(`./${env.templates.config}`, 'utf-8'));
// [TODO]
// Here, only allowing to update Databse configs.
// Need to improve this [PIORITY]

const config = JSON.parse(Utils.readFileSync(`./${env.defaults.config}`, 'utf-8'));
config.db[key] = val;

// update `site.json`
Utils.writeJSONSync(`${env.templates.config}`, config);
Utils.writeJSONSync(`${env.defaults.config}`, config);

// [TODO]
// Improve the logic here

const newConfig = config.db;
newConfig.siteName = config.siteName;
newConfig.cliVersion = config.cliVersion;

// update `wp-config.php`
Utils.writeFileSync(
`./${env.init.dir}/${env.templates.wpConfig}`,
Utils.getTemplatePath(env.templates.wpConfig),
`./${env.defaults.installDir}/wp-config.php`,
Utils.getTemplatePath('wp-config.php'),
newConfig
);

Expand Down
30 changes: 15 additions & 15 deletions lib/actions/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const Logs = require('./logs');
module.exports = {
create(config, cb) {
const dbConn = mysql.createConnection({
host: config.mysqlDbHost,
user: config.mysqlDbUser,
password: config.mysqlDbPwd,
port: config.mysqlDbPort
host: config.MYSQL_HOST,
user: config.MYSQL_USER,
password: config.MYSQL_PASSWORD,
port: config.MYSQL_PORT
});
dbConn.connect();

dbConn.query(`CREATE DATABASE ${config.mysqlDbName}`, (err, res) => {
dbConn.query(`CREATE DATABASE ${config.NAME}`, (err, res) => {
if (err) {
cb(err, null);
} else {
Expand All @@ -24,14 +24,14 @@ module.exports = {

drop(config, cb) {
const dbConn = mysql.createConnection({
host: config.mysqlDbHost,
user: config.mysqlDbUser,
password: config.mysqlDbPwd,
port: config.mysqlDbPort
host: config.MYSQL_HOST,
user: config.MYSQL_USER,
password: config.MYSQL_PASSWORD,
port: config.MYSQL_PORT
});
dbConn.connect();

dbConn.query(`DROP DATABASE ${config.mysqlDbName}`, (err, res) => {
dbConn.query(`DROP DATABASE ${config.NAME}`, (err, res) => {
if (err) {
cb(err, null);
} else {
Expand All @@ -44,11 +44,11 @@ module.exports = {
queryExec(config, query, callBack) {
// Setup the database connection
const dbConn = mysql.createConnection({
host: config.mysqlDbHost,
user: config.mysqlDbUser,
password: config.mysqlDbPwd,
port: config.mysqlDbPort,
database: config.mysqlDbName
host: config.MYSQL_HOST,
user: config.MYSQL_USER,
password: config.MYSQL_PASSWORD,
port: config.MYSQL_PORT,
database: config.NAME
});
dbConn.connect();

Expand Down
4 changes: 2 additions & 2 deletions lib/actions/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ module.exports = {

// Add basic colors to errors and warnings
if (log && log.type) {
if (log.type === 'error') {
if (log.type === 'Error') {
message = `${chalk.bold.underline.red('Error:')} ${chalk.red(message)}`;
} else if (log.type === 'warn') {
} else if (log.type === 'Warn') {
message = `${chalk.bold.underline.yellow('Warning:')} ${chalk.yellow(message)}`;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/actions/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {

// set `wpCli` args
const args = {
path: env.init.dir
path: env.defaults.installDir
};

// install the plugin
Expand Down Expand Up @@ -77,7 +77,7 @@ module.exports = {

// set `wpCli` args
const args = {
path: env.init.dir
path: env.defaults.installDir
};

// install the plugin
Expand Down
16 changes: 9 additions & 7 deletions lib/actions/theme.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint max-len: 0 */

const inquirer = require('inquirer');

const Utils = require('./utils');
Expand Down Expand Up @@ -45,33 +47,33 @@ module.exports = {
});

// create theme directory
Utils.mkdirSync(`${Utils.getWorkingDir()}/${env.init.dir}/wp-content/themes/${themeName}`);
Utils.mkdirSync(`${Utils.getWorkingDir()}/${env.defaults.installDir}/wp-content/themes/${themeName}`);

// add `style.css`
Utils.writeFileSync(
`${Utils.getWorkingDir()}/${env.init.dir}/wp-content/themes/${themeName}/style.css`,
Utils.getTemplatePath(env.templates.styleFile),
`${Utils.getWorkingDir()}/${env.defaults.installDir}/wp-content/themes/${themeName}/style.css`,
Utils.getTemplatePath('theme/style.css'),
config
);

// add `index.php`
Utils.writeFileSync(
`${Utils.getWorkingDir()}/${env.init.dir}/wp-content/themes/${themeName}/index.php`,
Utils.getTemplatePath(env.templates.index),
`${Utils.getWorkingDir()}/${env.defaults.installDir}/wp-content/themes/${themeName}/index.php`,
Utils.getTemplatePath('theme/index.php'),
null
);

// copy `screenshot.png`
Utils.copyFileSync(
`${Utils.getResourcePath(env.resources.screenshot)}`,
`./${env.init.dir}/wp-content/themes/${themeName}/`
`./${env.defaults.installDir}/wp-content/themes/${themeName}/`
);

Logs.show('theme-init-success', {
type: 'Info',
message: null,
params: {
themeDir: `${Utils.getWorkingDir()}/${env.init.dir}/wp-content/themes/${themeName}/`
themeDir: `${Utils.getWorkingDir()}/${env.defaults.installDir}/wp-content/themes/${themeName}/`
}
});
}
Expand Down
69 changes: 1 addition & 68 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
/* eslint no-shadow: 0, no-param-reassign: 0 */

const program = require('commander');
const inquirer = require('inquirer');
const _ = require('lodash');

const actions = require('./actions');
const pkg = require('../package.json');
const env = require('./env');
const promptSchema = require('./promptSchema');

function updateSchema(schema, config) {
schema.forEach((obj) => {
if (config[obj.name]) {
obj.default = config[obj.name];
}
});

return schema;
}

program
.version(pkg.version);
Expand All @@ -28,58 +12,7 @@ program
.option('-f, --force', 'Overwrites existing files, if have.')
.description('wp init [path] -f')
.action((path, options) => {
// show welcome
let docRoot = `${actions.utils.getWorkingDir()}/`;
if (path) {
docRoot += `${path}/`;
}
actions.logs.show('welcome', {
type: 'Info',
message: null,
params: {
cliVersion: pkg.version,
docRoot
}
});

// check if already initialized a project
if (actions.artisan.isInitialized(path)) {
actions.logs.show('project-init-failed', {
type: 'Error',
message: null,
params: {}
});
return;
}

// prompt for user inputs
actions.logs.show('prompt-site-config', {
type: 'Info',
message: null,
params: {}
});

inquirer
.prompt(promptSchema.config)
.then((config) => {
// update configs.
config.cliVersion = pkg.version;
config.siteUrl = `http://${env.server.host}:${env.server.defaultPort}`;

// Prompt for install configs.
if (config.doInstall) {
inquirer
.prompt(updateSchema(promptSchema.installConfig, config))
.then((installConfig) => {
_.merge(config, installConfig);
actions.artisan.initialize(path, config, options);
}
);
} else {
actions.artisan.initialize(path, config, options);
}
}
);
actions.artisan.initialize(path, options);
}
);

Expand Down
30 changes: 12 additions & 18 deletions lib/env.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
module.exports = {
init: {
dir: 'site'
defaults: {
installDir: 'site',
config: 'site.json',
server: {
host: 'localhost',
port: '8080'
}
},
config: {
allowUpdate: [
'mysqlDbHost',
'mysqlDbPort',
'mysqlDbUser',
'mysqlDbPwd',
'mysqlDbName'
'MYSQL_HOST',
'MYSQL_PORT',
'MYSQL_USER',
'MYSQL_PASSWORD',
'NAME'
]
},
templates: {
config: 'site.json',
wpConfig: 'wp-config.php',
gitignore: '.gitignore',
styleFile: 'theme/style.css',
index: 'theme/index.php'
},
resources: {
screenshot: 'screenshot.png'
},
server: {
host: 'localhost',
defaultPort: '8080'
},
deleteList: [
'<siteDir>/wp-config-sample.php',
'<siteDir>/readme.html',
Expand Down
22 changes: 11 additions & 11 deletions lib/promptSchema/config.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
module.exports = [
{
type: 'input',
name: 'siteName',
name: 'site.name',
message: 'Enter the site name'
},
{
type: 'input',
name: 'mysqlDbHost',
name: 'db.MYSQL_HOST',
message: 'MYSQL Database Host',
default: '127.0.0.1'
},
{
type: 'input',
name: 'mysqlDbPort',
name: 'db.MYSQL_PORT',
message: 'MYSQL Database Port',
default: '3306'
},
{
type: 'input',
name: 'mysqlDbUser',
name: 'db.MYSQL_USER',
message: 'MYSQL Database User',
default: 'root'
},
{
type: 'password',
name: 'mysqlDbPwd',
name: 'db.MYSQL_PASSWORD',
message: 'MYSQL Database Password',
default: ''
},
{
type: 'input',
name: 'mysqlDbName',
name: 'db.NAME',
message: 'MYSQL Database Name',
validate(value) {
if (!value || value.trim() === '') {
Expand All @@ -41,25 +41,25 @@ module.exports = [
},
{
type: 'input',
name: 'mysqlDbTblPrefix',
name: 'db.TBL_PREFIX',
message: 'MYSQL Database Table Prefix',
default: 'wp_'
},
{
type: 'input',
name: 'serverHost',
message: 'Enter the site url',
name: 'server.host',
message: 'Enter the url host',
default: 'localhost'
},
{
type: 'input',
name: 'serverPort',
name: 'server.port',
message: 'Enter the the port number',
default: '8080'
},
{
type: 'confirm',
name: 'doInstall',
name: 'install.confirm',
message: 'Run Install script?',
default: true
}
Expand Down
Loading

0 comments on commit bb83ee9

Please sign in to comment.