From fcfcf1ebc02d1cdd2b9d929006ac833f407d9f23 Mon Sep 17 00:00:00 2001 From: Artem Sapegin Date: Wed, 20 Dec 2017 18:37:08 +0100 Subject: [PATCH] Lint some scripts --- bin/browse | 135 +++++++++++++++++++-------------------- bin/morn-list-for-alfred | 17 +++-- bin/repo | 39 +++++------ bin/stripfrontmatter | 13 ++-- 4 files changed, 104 insertions(+), 100 deletions(-) diff --git a/bin/browse b/bin/browse index 0ac75246..c061ccdd 100755 --- a/bin/browse +++ b/bin/browse @@ -1,10 +1,10 @@ #!/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 @@ -12,17 +12,17 @@ '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': @@ -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; @@ -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() { @@ -83,7 +79,7 @@ function getProjectType() { } function isStringInFile(filepath, string) { - var contents = readFile(filepath); + const contents = readFile(filepath); return contents.indexOf(string) !== -1; } @@ -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) { @@ -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(); @@ -125,18 +123,23 @@ 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, [ - '', - ' DocumentRoot ' + process.cwd(), - ' ServerName ' + this.hostName, - '' - ].join('\n')); + appendSystemFile( + vhostsConfPath, + [ + '', + ' DocumentRoot ' + process.cwd(), + ' ServerName ' + this.hostName, + '', + ].join('\n') + ); // Restart Apache execSync('sudo apachectl graceful'); @@ -144,15 +147,15 @@ Browse.prototype = { 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'); @@ -160,39 +163,38 @@ Browse.prototype = { 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); @@ -200,24 +202,22 @@ Browse.prototype = { } }, - 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', '([^']*)'/); @@ -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' => '([^']*)'/); diff --git a/bin/morn-list-for-alfred b/bin/morn-list-for-alfred index 3919f370..d8876754 100755 --- a/bin/morn-list-for-alfred +++ b/bin/morn-list-for-alfred @@ -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), @@ -42,8 +42,7 @@ var items = files type: 'fileicon', path: doc.file, }, - })) -; + })); if (!items.length) { items = [ diff --git a/bin/repo b/bin/repo index fd7e92c1..0661b057 100755 --- a/bin/repo +++ b/bin/repo @@ -1,7 +1,7 @@ #!/usr/bin/env node // Magic project opener: opens repository using fuzzy search. -// +// // Should be used via Bash alias like this: // function proj { cd "$("$HOME/dotfiles/bin/repo" $1)"; } // @@ -12,27 +12,33 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); +const fs = require('fs'); +const path = require('path'); -var PROJECTS_DIR = path.join(process.env.HOME, 'Dropbox/Projects'); -var SKIP_PREFIXES = ['_Repos', 'Misc']; +const PROJECTS_DIR = path.join(process.env.HOME, 'Dropbox/Projects'); +const SKIP_PREFIXES = ['_Repos', 'Misc']; -var args = process.argv.splice(2); +const args = process.argv.splice(2); if (args.length !== 1) { console.log('Usage: repo '); process.exit(1); } -var repo = find(getRepos(PROJECTS_DIR), args[0]); +const repo = find(getRepos(PROJECTS_DIR), args[0]); if (repo) { console.log(path.join(PROJECTS_DIR, repo)); } function find(repos, name) { // Fuzzy search - var nameRegexp = new RegExp(name.replace(/[^a-z0-9]/gi, '').split('').join('.*'), 'i'); - var filterRegexp = new RegExp('^(' + SKIP_PREFIXES.join('|') + ')/', 'i'); + const nameRegexp = new RegExp( + name + .replace(/[^a-z0-9]/gi, '') + .split('') + .join('.*'), + 'i' + ); + const filterRegexp = new RegExp('^(' + SKIP_PREFIXES.join('|') + ')/', 'i'); repos = repos.filter(function(repo) { repo = repo.replace(filterRegexp, ''); return nameRegexp.test(repo); @@ -45,7 +51,7 @@ function find(repos, name) { } // Order by repo name’s length - var length = function(repo) { + const length = function(repo) { return path.basename(repo).length; }; repos.sort(function(a, b) { @@ -56,13 +62,12 @@ function find(repos, name) { return repos[0]; } - function getRepos(dir) { - var projects = getDirs(dir); - var repos = []; + const projects = getDirs(dir); + const repos = []; projects.forEach(function(projectName) { - var projectDir = path.join(dir, projectName); - var subDirs = getDirs(projectDir); + const projectDir = path.join(dir, projectName); + const subDirs = getDirs(projectDir); subDirs.forEach(function(subDirName) { if (fs.existsSync(path.join(projectDir, subDirName, '.git'))) { repos.push(path.join(projectName, subDirName)); @@ -74,7 +79,5 @@ function getRepos(dir) { } function getDirs(dir) { - return fs.readdirSync(dir).filter( - file => fs.statSync(path.join(dir, file)).isDirectory() - ); + return fs.readdirSync(dir).filter(file => fs.statSync(path.join(dir, file)).isDirectory()); } diff --git a/bin/stripfrontmatter b/bin/stripfrontmatter index 0a23bc3f..d922dd5b 100755 --- a/bin/stripfrontmatter +++ b/bin/stripfrontmatter @@ -18,11 +18,14 @@ const pattern = process.argv[2]; const files = glob.sync(pattern); files.forEach(file => { - console.log(`${file}...`); - let contents = fs.readFileSync(file, 'utf8'); - const frontmatter = fastmatter(contents); - fs.writeFileSync(file, `# ${frontmatter.attributes.title} + console.log(`${file}...`); + const contents = fs.readFileSync(file, 'utf8'); + const frontmatter = fastmatter(contents); + fs.writeFileSync( + file, + `# ${frontmatter.attributes.title} ${frontmatter.body} -`); +` + ); });