-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from pipocadigital/refactor/gulp
Refactoring the gulp tasks
- Loading branch information
Showing
16 changed files
with
381 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
var gulp = require('gulp'), | ||
browserSync = require('browser-sync'); | ||
const gulp = require('gulp'); | ||
const browserSync = require('browser-sync'); | ||
|
||
// Browser Sync | ||
gulp.task('browser-sync', function() { | ||
browserSync({ | ||
server: { | ||
baseDir: gulp.paths.basePath | ||
} | ||
}); | ||
const baseDir = gulp.paths.basePath; | ||
|
||
browserSync({server: { baseDir }}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
var gulp = require('gulp'), | ||
del = require('del'); | ||
const gulp = require('gulp'); | ||
const del = require('del'); | ||
|
||
// Clean | ||
gulp.task('clean', function() { | ||
return del(gulp.paths.pagesDest); | ||
}); | ||
gulp.task('clean', () => del(gulp.paths.pagesDest)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
var gulp = require('gulp'), | ||
connect = require('gulp-connect-php'), | ||
browserSync = require('browser-sync'); | ||
const gulp = require('gulp'); | ||
const connect = require('gulp-connect-php'); | ||
const browserSync = require('browser-sync'); | ||
|
||
// Connect Sync | ||
gulp.task('connect-sync', function() { | ||
connect.server({ | ||
port: gulp.config.port, | ||
base: gulp.paths.basePath, | ||
livereload: true | ||
}, function (){ | ||
browserSync({ | ||
proxy: gulp.config.localhost+':'+gulp.config.port | ||
}); | ||
}); | ||
const proxy = gulp.config.localhost + ':' + gulp.config.port; | ||
const serverConfig = { | ||
base: gulp.paths.basePath, | ||
port: gulp.config.port, | ||
livereload: true | ||
}; | ||
|
||
connect.server(serverConfig, () => browserSync({ proxy })); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
var gulp = require('gulp'), | ||
browserSync = require('browser-sync'); | ||
const gulp = require('gulp'); | ||
const browserSync = require('browser-sync'); | ||
|
||
// Fonts | ||
gulp.task('fonts', function(){ | ||
gulp.src(gulp.paths.fonts) | ||
.pipe(gulp.dest(gulp.paths.fontsDest)) | ||
.pipe(browserSync.reload({stream:true})); | ||
gulp.task('fonts', function() { | ||
return gulp.src(gulp.paths.fonts) | ||
.pipe(gulp.dest(gulp.paths.fontsDest)) | ||
.pipe(browserSync.reload({stream: true})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,158 +1,153 @@ | ||
var fs = require('fs'), | ||
slugify = require('slugify'), | ||
request = require('request'); | ||
|
||
// Helpers | ||
function helpers() { | ||
function fileExists(filePath) { | ||
try { | ||
return fs.statSync(filePath).isFile(); | ||
} catch (err) { | ||
return false; | ||
} | ||
// TODO: refactor this file ASAP. | ||
const fs = require('fs'); | ||
const slugify = require('slugify'); | ||
const request = require('request'); | ||
|
||
const wpStyleFile = './src/style.css'; | ||
const wpConfigFile = './wp-config.php'; | ||
const bowerJsonFile = './bower.json'; | ||
const packageJsonFile = './package.json'; | ||
|
||
function rewriteProjectName(name) { | ||
if (name === '') { | ||
log('Please, give us a project name using the `--p` param.', 'danger'); | ||
process.exit(); | ||
} | ||
|
||
function log(message, type) { | ||
var date = new Date().toTimeString().split(' ')[0]; | ||
var dateFormated = '[\x1b[90m' + date + '\x1b[0m] >> '; | ||
var color = ''; | ||
var log = ''; | ||
|
||
switch (type) { | ||
case 'danger': | ||
color = '\x1b[31m'; | ||
break; | ||
case 'success': | ||
color = '\x1b[32m'; | ||
break; | ||
default: | ||
color = '\x1b[34m'; | ||
} | ||
|
||
log += dateFormated + color + message; | ||
log += '\x1b[0m'; | ||
|
||
console.log(log); | ||
} | ||
|
||
function rewriteProjectName(name) { | ||
if (name === '') { | ||
this.log('Please, give us a project name using the `--p` param.', 'danger'); | ||
process.exit(); | ||
} | ||
|
||
this.updatePackageJson(name); | ||
this.updateBowerJson(name); | ||
} | ||
updatePackageJson(name); | ||
updateBowerJson(name); | ||
} | ||
|
||
function updatePackageJson(name) { | ||
var packageJsonFile = './package.json'; | ||
var slug = slugify(name).toLowerCase(); | ||
var that = this; | ||
function updatePackageJson(name) { | ||
const slug = getSlug(name); | ||
|
||
that.log('Configuring package.json', 'success'); | ||
log('Configuring package.json...', 'success'); | ||
|
||
fs.readFile(packageJsonFile, 'utf8', function (err, data) { | ||
var updatedPackageJson; | ||
fs.readFile(packageJsonFile, 'utf8', function (error, data) { | ||
checkErrorsAndExit(error); | ||
|
||
checkErrorsWhenIsReading(err); | ||
const updatedPackageJson = JSON.parse(data); | ||
updatedPackageJson.version = '0.0.0'; | ||
updatedPackageJson.name = slug; | ||
updatedPackageJson.title = name; | ||
updatedPackageJson.description = name; | ||
|
||
updatedPackageJson = JSON.parse(data); | ||
writeOn(packageJsonFile, JSON.stringify(updatedPackageJson, null, ' ')); | ||
}); | ||
} | ||
|
||
updatedPackageJson.version = '0.0.0'; | ||
updatedPackageJson.name = slug; | ||
updatedPackageJson.title = name; | ||
updatedPackageJson.description = name; | ||
function updateBowerJson(name) { | ||
const slug = getSlug(name); | ||
|
||
writeOn(packageJsonFile, JSON.stringify(updatedPackageJson, null, ' ')); | ||
}); | ||
} | ||
log('Configuring bower.json', 'success'); | ||
|
||
function updateBowerJson(name) { | ||
var bowerJsonFile = './bower.json'; | ||
var slug = slugify(name).toLowerCase(); | ||
fs.readFile(bowerJsonFile, 'utf8', function (error, data) { | ||
checkErrorsAndExit(error); | ||
|
||
this.log('Configuring bower.json', 'success'); | ||
writeOn(bowerJsonFile, data.replace(/grao-de-milho/g, slug)); | ||
}); | ||
} | ||
|
||
fs.readFile(bowerJsonFile, 'utf8', function (err, data) { | ||
checkErrorsWhenIsReading(err); | ||
function updateWpStyle(name) { | ||
if (fileExists(wpStyleFile)) { | ||
fs.readFile(wpStyleFile, 'utf8', function (error, data) { | ||
checkErrorsAndExit(error); | ||
|
||
writeOn(bowerJsonFile, data.replace(/grao-de-milho/g, slug)); | ||
writeOn(wpStyleFile, data.replace(/Grão de Milho/g, name)); | ||
}); | ||
} | ||
} | ||
|
||
function updateWpStyle(name) { | ||
var wpStyleFile = './src/style.css'; | ||
|
||
if (fileExists(wpStyleFile)) { | ||
fs.readFile(wpStyleFile, 'utf8', function (err, data) { | ||
checkErrorsWhenIsReading(err); | ||
|
||
writeOn(wpStyleFile, data.replace(/Grão de Milho/g, name)); | ||
}); | ||
} | ||
} | ||
function updateWpConfig(dbOptions) { | ||
fs.readFile(wpConfigFile, 'utf8', function (error, data) { | ||
checkErrorsAndExit(error); | ||
|
||
function updateWpConfig(dbOptions) { | ||
var wpConfigDefaultUrl = './wp-config.php'; | ||
var wpConfigUrl = './wordpress/wp-config.php'; | ||
const newWpConfigFile = './wordpress/wp-config.php'; | ||
|
||
fs.readFile(wpConfigDefaultUrl, 'utf8', function (err, data) { | ||
checkErrorsWhenIsReading(err); | ||
data = data.replace(/database_name_here/g, dbOptions.name); | ||
data = data.replace(/username_here/g, dbOptions.user); | ||
data = data.replace(/password_here/g, dbOptions.pass); | ||
data = data.replace(/host_here/g, dbOptions.host); | ||
|
||
data = data.replace(/database_name_here/g, dbOptions.name); | ||
data = data.replace(/username_here/g, dbOptions.user); | ||
data = data.replace(/password_here/g, dbOptions.pass); | ||
data = data.replace(/host_here/g, dbOptions.host); | ||
writeOn(newWpConfigFile, data); | ||
}); | ||
} | ||
|
||
writeOn(wpConfigUrl, data); | ||
}); | ||
} | ||
function updateWpKeys() { | ||
const secretKeyUrl = 'https://api.wordpress.org/secret-key/1.1/salt/'; | ||
|
||
function updateWpKeys() { | ||
var wpConfigUrl = './wordpress/wp-config.php', | ||
url = 'https://api.wordpress.org/secret-key/1.1/salt/'; | ||
log('Generating authentication keys', 'success'); | ||
|
||
this.log('Generating authentication keys', 'success'); | ||
request | ||
.get(secretKeyUrl, function (error, response, body) { | ||
checkErrorsAndExit(error); | ||
|
||
request.get(url, function (err, response, body) { | ||
checkErrorsWhenIsReading(err); | ||
const wpConfigUrl = './wordpress/wp-config.php'; | ||
|
||
fs.readFile(wpConfigUrl, 'utf8', function (err, data) { | ||
checkErrorsWhenIsReading(err); | ||
checkErrorsAndExit(err); | ||
|
||
data = data.replace(/AUTHENTICATION_KEY/gi, body); | ||
|
||
writeOn(wpConfigUrl, data); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function writeOn(file, content) { | ||
fs.writeFile(file, content, 'utf8', function (err) { | ||
checkErrorsWhenIsReading(err); | ||
}); | ||
function getSlug(name) { | ||
return slugify(name).toLowerCase(); | ||
} | ||
|
||
function checkErrorsAndExit(error) { | ||
if (error) { | ||
log(error, 'danger'); | ||
process.exit(1) | ||
} | ||
} | ||
|
||
function writeOn(file, content) { | ||
fs.writeFile(file, content, 'utf8', err => checkErrorsAndExit(err)); | ||
} | ||
|
||
function checkErrorsWhenIsReading(error) { | ||
if (error) { | ||
this.log(error, 'danger'); | ||
process.exit(1) | ||
} | ||
function fileExists(filePath) { | ||
try { | ||
return fs.statSync(filePath).isFile(); | ||
} catch (err) { | ||
// TODO: handle this | ||
return false; | ||
} | ||
} | ||
|
||
function log(message, type) { | ||
const date = new Date().toTimeString(); | ||
const dateFormated = dateTermnialOutputFormat(date.split(' ')[0]); | ||
const color = colorByAlertType(type); | ||
const log = dateFormated + color + message + '\x1b[0m'; | ||
|
||
console.log(log); | ||
} | ||
|
||
function dateTermnialOutputFormat(date) { | ||
return '[\x1b[90m' + date + '\x1b[0m] >> '; | ||
} | ||
|
||
return { | ||
slugify: slugify, | ||
fileExists: fileExists, | ||
log: log, | ||
writeOn: writeOn, | ||
rewriteProjectName: rewriteProjectName, | ||
updateWpConfig: updateWpConfig, | ||
updatePackageJson: updatePackageJson, | ||
updateBowerJson: updateBowerJson, | ||
updateWpStyle: updateWpStyle, | ||
updateWpKeys: updateWpKeys | ||
}; | ||
function colorByAlertType(type) { | ||
switch (type) { | ||
case 'danger': | ||
return '\x1b[31m'; | ||
case 'success': | ||
return '\x1b[32m'; | ||
default: | ||
return '\x1b[34m'; | ||
} | ||
} | ||
|
||
module.exports = helpers(); | ||
module.exports = { | ||
log, | ||
writeOn, | ||
fileExists, | ||
updateWpConfig, | ||
rewriteProjectName, | ||
updateWpStyle, | ||
updateWpKeys | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,8 @@ | ||
var gulp = require('gulp'), | ||
plumber = require('gulp-plumber'), | ||
cache = require('gulp-cache'), | ||
imagemin = require('gulp-imagemin'), | ||
browserSync = require('browser-sync'); | ||
const gulp = require('gulp'); | ||
const browserSync = require('browser-sync'); | ||
|
||
// Images | ||
gulp.task('images', function() { | ||
return gulp.src(gulp.paths.images) | ||
.pipe(cache(imagemin({ | ||
optimizationLevel: 3, | ||
progressive: true, | ||
interlaced: true | ||
}))) | ||
.pipe(gulp.dest(gulp.paths.imagesDest)) | ||
.pipe(browserSync.reload({stream:true})); | ||
return gulp.src(gulp.paths.images) | ||
.pipe(gulp.dest(gulp.paths.imagesDest)) | ||
.pipe(browserSync.reload({stream: true})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.