Skip to content

Commit

Permalink
Lint some scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
sapegin committed Dec 20, 2017
1 parent 9557d0e commit fcfcf1e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 100 deletions.
135 changes: 67 additions & 68 deletions bin/browse
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#!/usr/bin/env node

// Magically opens a site in a browser (creates dev server if necessary).
//
//
// Supported engines:
// - Wordpress
//
//
// Author: Artem Sapegin, sapegin.me
// License: MIT
// https://github.com/sapegin/dotfiles
//

'use strict';

var fs = require('fs');
var path = require('path');
var util = require('util');
var chalk = require('chalk');
var execSync = require('child_process').execSync;
const fs = require('fs');
const path = require('path');
const util = require('util');
const chalk = require('chalk');
const execSync = require('child_process').execSync;

function run() {
cdToProjectRoot();

var kind = getProjectType();
var browse;
const kind = getProjectType();
let browse;
console.log('Project type:', kind);
switch (kind) {
case 'wordpress':
Expand All @@ -47,11 +47,11 @@ function ok() {
}

function readFile(filepath) {
return fs.readFileSync(filepath, {encoding: 'utf8'});
return fs.readFileSync(filepath, { encoding: 'utf8' });
}

function cdToProjectRoot() {
var tries = 20;
let tries = 20;
while (tries && !fs.existsSync('.git')) {
process.chdir('..');
tries -= 1;
Expand All @@ -62,11 +62,7 @@ function cdToProjectRoot() {
}

function appendSystemFile(filepath, contents) {
execSync(
'echo "\n\n' +
contents + '\n' +
'" | sudo tee -a ' + filepath + ' > /dev/null'
);
execSync('echo "\n\n' + contents + '\n' + '" | sudo tee -a ' + filepath + ' > /dev/null');
}

function getProjectName() {
Expand All @@ -83,7 +79,7 @@ function getProjectType() {
}

function isStringInFile(filepath, string) {
var contents = readFile(filepath);
const contents = readFile(filepath);
return contents.indexOf(string) !== -1;
}

Expand All @@ -92,7 +88,11 @@ function isWordpress() {
}

function isKoken() {
return fs.existsSync(getPath('api.php')) && fs.existsSync(getPath('dl.php')) && fs.existsSync(getPath('i.php'));
return (
fs.existsSync(getPath('api.php')) &&
fs.existsSync(getPath('dl.php')) &&
fs.existsSync(getPath('i.php'))
);
}

function getPath(filepath) {
Expand All @@ -102,21 +102,19 @@ function getPath(filepath) {
return filepath;
}


/**
* Base class
*/
function Browse() {
}
function Browse() {}

Browse.prototype = {
open: function() {
open() {
this.hostName = getProjectName();
console.log('Opening ' + this.hostName + '...');
this.runCommands();
},

runCommands: function() {
runCommands() {
this.addVirtualHost();
this.addHost();
this.startApache();
Expand All @@ -125,99 +123,101 @@ Browse.prototype = {
this.openBrowser();
},

addVirtualHost: function() {
var vhostsConfPath = '/etc/apache2/extra/httpd-vhosts.conf';
if (isStringInFile(vhostsConfPath, 'ServerName ' + this.hostName + '\n')) return;
addVirtualHost() {
const vhostsConfPath = '/etc/apache2/extra/httpd-vhosts.conf';
if (isStringInFile(vhostsConfPath, 'ServerName ' + this.hostName + '\n')) {
return;
}

process.stdout.write('Adding virtual host... ');

appendSystemFile(vhostsConfPath, [
'<VirtualHost *:80>',
' DocumentRoot ' + process.cwd(),
' ServerName ' + this.hostName,
'</VirtualHost>'
].join('\n'));
appendSystemFile(
vhostsConfPath,
[
'<VirtualHost *:80>',
' DocumentRoot ' + process.cwd(),
' ServerName ' + this.hostName,
'</VirtualHost>',
].join('\n')
);

// Restart Apache
execSync('sudo apachectl graceful');

ok();
},

addHost: function() {
var hostsFilePath = '/etc/hosts';
if (isStringInFile(hostsFilePath, '127.0.0.1 ' + this.hostName + '\n')) return;
addHost() {
const hostsFilePath = '/etc/hosts';
if (isStringInFile(hostsFilePath, '127.0.0.1 ' + this.hostName + '\n')) {
return;
}

process.stdout.write('Adding host to /etc/hosts... ');

appendSystemFile(hostsFilePath,
'127.0.0.1 ' + this.hostName + '\n'
);
appendSystemFile(hostsFilePath, '127.0.0.1 ' + this.hostName + '\n');

// Flush DNS cache
execSync('dscacheutil -flushcache');

ok();
},

startApache: function() {
startApache() {
process.stdout.write('Starting Apache... ');

var result = execSync('ps ax | grep httpd | wc -l');
if (parseInt(result.toString()) <= 2) { // Apache not started
const result = execSync('ps ax | grep httpd | wc -l');
if (parseInt(result.toString()) <= 2) {
// Apache not started
execSync('sudo apachectl start');
}

ok();
},

startMysql: function() {
startMysql() {
process.stdout.write('Starting MySQL... ');

var result = execSync('mysql.server start');
const result = execSync('mysql.server start');
if (result.toString().indexOf('SUCCESS!') !== -1) {
ok();
}
else {
} else {
console.log();
console.log(result.toString());
}
},

createDatabase: function() {
var dbParams = this.getDatabaseParams();
var prefix = 'MYSQL_PWD=' + dbParams.password;
var connection = '-u' + dbParams.username + ' -h ' + dbParams.host;
var dbName = 'wp_' + this.hostName;
createDatabase() {
const dbParams = this.getDatabaseParams();
const prefix = 'MYSQL_PWD=' + dbParams.password;
const connection = '-u' + dbParams.username + ' -h ' + dbParams.host;
const dbName = 'wp_' + this.hostName;
try {
execSync(prefix + ' mysql -Bb -e \'\' ' + connection + ' ' + dbName, {stdio: 'ignore'});
}
catch (e) {
execSync(prefix + " mysql -Bb -e '' " + connection + ' ' + dbName, { stdio: 'ignore' });
} catch (e) {
// Database not exists
process.stdout.write('Creating database... ');
execSync(prefix + ' mysqladmin ' + connection + ' create ' + dbName);
ok();
}
},

openBrowser: function() {
openBrowser() {
execSync('open "http://' + this.hostName + '/"');
}
},
};


/**
* Wordpress
*/
function BrowseWordpress() {
}
function BrowseWordpress() {}

util.inherits(BrowseWordpress, Browse);

BrowseWordpress.prototype.getDatabaseParams = function() {
var configFile = readFile(getPath('wp-config.php'));
var params = {};
var m;
const configFile = readFile(getPath('wp-config.php'));
const params = {};
let m;

// User name
m = configFile.match(/define\('DB_USER', '([^']*)'/);
Expand All @@ -226,26 +226,25 @@ BrowseWordpress.prototype.getDatabaseParams = function() {
// Password
m = configFile.match(/define\('DB_PASSWORD', '([^']*)'/);
params.password = m[1];

// Host
m = configFile.match(/define\('DB_HOST', '([^']*)'/);
params.host = m[1];

return params;
};

/**
* Koken
*/
function BrowseKoken() {
}
function BrowseKoken() {}

util.inherits(BrowseKoken, Browse);

BrowseKoken.prototype.getDatabaseParams = function() {
var configFile = readFile(getPath('storage/configuration/database.php'));
var params = {};
var m;
const configFile = readFile(getPath('storage/configuration/database.php'));
const params = {};
let m;

// User name
m = configFile.match(/'username' => '([^']*)'/);
Expand Down
17 changes: 8 additions & 9 deletions bin/morn-list-for-alfred
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const glob = require('glob');

const DIR = `${userHome}/Dropbox/Projects/Morning.photos/morning.photos/source`;

var files = glob.sync(`${DIR}/**/*.{md,txt}`);
const files = glob.sync(`${DIR}/**/*.{md,txt}`);

var query = process.argv[2];
var regexp = new RegExp(query, 'i');
var items = files
const query = process.argv[2];
const regexp = new RegExp(query, 'i');
let items = files
.map(file => {
var contents = fs.readFileSync(file, 'utf8');
var title = contents.match(/^title: [\x22\x27]?(.*?)[\x22\x27]?$/im);
var date = contents.match(/^date: (.*?)$/m);
const contents = fs.readFileSync(file, 'utf8');
const title = contents.match(/^title: [\x22\x27]?(.*?)[\x22\x27]?$/im);
const date = contents.match(/^date: (.*?)$/m);
return {
title: title ? title[1] : '<***>',
date: date ? date[1] : path.basename(file),
Expand All @@ -42,8 +42,7 @@ var items = files
type: 'fileicon',
path: doc.file,
},
}))
;
}));

if (!items.length) {
items = [
Expand Down
Loading

0 comments on commit fcfcf1e

Please sign in to comment.