Created
December 27, 2018 18:17
-
-
Save johnReeve/db7cebebed1e26f03d36a4d48260d379 to your computer and use it in GitHub Desktop.
A sample gulpfile and package.json for wordpress themes
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
var CONFIG = { | |
PROJECT_URL: 'https://nib.local', | |
SOURCE: { | |
scripts: 'source/js/**/*.js', | |
styles: 'source/scss/**/*.scss', | |
images: 'source/images/*', | |
php: '**/*.php' | |
}, | |
ASSETS: { | |
styles: 'assets/styles/', | |
scripts: 'assets/js/', | |
images: 'assets/images/', | |
all: 'assets/' | |
}, | |
JSHINT_CONFIG: { | |
"node": true, | |
"esversion": 6, | |
"globals": { | |
"document": true, | |
"jQuery": true | |
} | |
}, | |
TRANSLATION: { | |
"pot_filename": 'functions/translation/NIB-theme.pot', | |
"original_language": 'en', | |
"target_languages": [ | |
'es', | |
'zh' | |
] | |
}, | |
AUTOPREFIXER_CONFIG: { | |
browsers: ['last 2 versions', '> 5%', 'Firefox ESR'] | |
} | |
}; | |
var gulp = require('gulp'), | |
chalk = require('chalk'), | |
_ = require('lodash'), | |
gettextParser = require("gettext-parser"), | |
AWS = require('aws-sdk'), | |
pa11y = require('pa11y'); | |
plugin = require('gulp-load-plugins')(); | |
// GULP FUNCTIONS | |
// JSHint, concat, and minify JavaScript | |
gulp.task('scripts', function () { | |
return gulp.src(CONFIG.SOURCE.scripts, {since: gulp.lastRun('scripts')}) | |
.pipe(plugin.plumber({errorHandler: reportError})) | |
.pipe(plugin.sourcemaps.init()) | |
.pipe(plugin.babel({ | |
presets: ["@babel/preset-env"], | |
compact: true, | |
ignore: [] | |
})) | |
// Only lint modified files | |
.pipe(plugin.cached('scripts')) | |
.pipe(plugin.jshint(CONFIG.JSHINT_CONFIG)) | |
.pipe(plugin.jshint.reporter('jshint-stylish')) | |
.pipe(plugin.remember('scripts')) | |
.pipe(plugin.concat('scripts.js')) | |
.pipe(plugin.uglify()) | |
.pipe(plugin.sourcemaps.write('.')) // Creates sourcemap for minified JS | |
.pipe(gulp.dest(CONFIG.ASSETS.scripts)) | |
}); | |
// Compile Sass, Autoprefix and minify | |
gulp.task('styles', function () { | |
return gulp.src(CONFIG.SOURCE.styles) | |
.pipe(plugin.plumber({errorHandler: reportError})) | |
.pipe(plugin.sourcemaps.init()) | |
.pipe(plugin.sass()) | |
.pipe(plugin.autoprefixer({ | |
browsers: CONFIG.AUTOPREFIXER_CONFIG.browsers, | |
cascade: false | |
})) | |
.pipe(plugin.cssnano()) | |
.pipe(plugin.sourcemaps.write('.')) | |
.pipe(gulp.dest(CONFIG.ASSETS.styles)); | |
}); | |
// Optimize images, move into assets directory | |
gulp.task('images', function () { | |
return gulp.src(CONFIG.SOURCE.images) | |
.pipe(plugin.plumber({errorHandler: reportError})) | |
.pipe(plugin.imagemin([], { | |
verbose: true | |
})) | |
.pipe(gulp.dest(CONFIG.ASSETS.images)) | |
}); | |
// Automatically update the translation file | |
gulp.task('translate', function () { | |
return gulp.src(CONFIG.SOURCE.php) | |
.pipe(plugin.wpPot({ | |
domain: 'materiell-themes', | |
package: 'NIB-theme' | |
})) | |
.pipe(gulp.dest(CONFIG.TRANSLATION.pot_filename)); | |
}); | |
// Automatically prettify the PHP | |
gulp.task('translate', function () { | |
return gulp.src(CONFIG.SOURCE.php) | |
.pipe(plugin.wpPot({ | |
domain: 'materiell-themes', | |
package: 'NIB-theme' | |
})) | |
.pipe(gulp.dest(CONFIG.TRANSLATION.pot_filename)); | |
}); | |
gulp.task('pa11y', (done) => { | |
pa11y({ | |
url: CONFIG.PROJECT_URL, | |
// failOnError: true, // fail the build on error | |
showFailedOnly: false, // show errors only and override reporter | |
reporter: 'console' | |
}).then((result) => { | |
const color_wt = chalk.whiteBright; | |
const color_lo = chalk.red; | |
const color_hi = chalk.redBright; | |
console.log(chalk.green.bold(`PA11Y: Ran pa11y for ${result.documentTitle}`)); | |
console.log(chalk.green(`PA11Y: URL:: ${result.pageUrl}`)); | |
if (result.issues.length > 0) { | |
console.log(chalk.redBright(`PA11Y: ISSUES FOUND`)); | |
result.issues.forEach((i, e) => { | |
console.log(color_wt(`Context: ${i.context}`)); | |
console.log(color_hi(`Message: ${i.message}`)); | |
console.log(color_lo(`Code: ${i.code}`)); | |
console.log(color_lo(`Selector: ${i.selector}`)); | |
console.log(chalk.blueBright(`************************************`)); | |
}); | |
} else { | |
console.log(chalk.green.bold(`PA11Y: No Issues Detected`)); | |
} | |
done(); | |
}); | |
} | |
); | |
// Watch files for changes (without Browser-Sync) | |
gulp.task('watch', function () { | |
// Watch .scss files | |
gulp.watch(CONFIG.SOURCE.styles, gulp.parallel('styles')); | |
// Watch scripts files | |
gulp.watch(CONFIG.SOURCE.scripts, gulp.parallel('scripts')); | |
// Watch images files | |
gulp.watch(CONFIG.SOURCE.images, gulp.parallel('images')); | |
// Watch pho files for translation | |
gulp.watch(CONFIG.SOURCE.images, gulp.parallel('translate')); | |
}); | |
// Run styles, scripts, etc | |
gulp.task('default', gulp.parallel('styles', 'scripts', 'images')); | |
/** | |
* Helpers: | |
* | |
*/ | |
// outputs the errors from plumber | |
function reportError (error) { | |
console.log(chalk.red(error.message)); | |
this.emit('end'); | |
}; | |
/** | |
* TODO: Create autotranslate method using AWS-Translate | |
* | |
* https://github.com/smhg/gettext-parser creates mo files and parses (maybe both) po/pot files | |
* | |
*/ | |
// Automatically create translations | |
gulp.task('auto_translate', function (done) { | |
const language_jobs = generateTranslation(CONFIG.TRANSLATION.pot_filename); | |
language_jobs.then((values) => { | |
console.log("values:"); | |
console.dir(values); | |
console.log("auto_translate done"); | |
done() | |
}); | |
}); | |
const generateTranslation = (pot_filename) => { | |
/** | |
* SETUP: | |
*/ | |
console.log(chalk.greenBright.bold("Started Translating Assets")); | |
console.log(chalk.green.bold(`POT_FILE: ${pot_filename}`)); | |
process.env.AWS_PROFILE = "jreeve-mat-us-east-1"; | |
AWS.config.update({region: 'us-east-1'}); | |
const translate = new AWS.Translate(); | |
const input = require('fs').readFileSync(pot_filename); | |
const po = gettextParser.po.parse(input); | |
/** | |
* PER LANGUAGE: | |
*/ | |
let language_jobs = []; | |
CONFIG.TRANSLATION.target_languages.forEach((lang_code) => { | |
console.log("translating: " + lang_code); | |
// console.log("BEFORE:"); | |
// console.dir(po, { depth: null }); | |
/*** | |
* Get a bunch of translated strings | |
*/ | |
let translation_jobs = []; | |
// first by context: | |
_.each(po.translations, (translation_objects, context_key) => { | |
_.each(translation_objects, (translation_string, translation_key) => { | |
const params = { | |
SourceLanguageCode: CONFIG.TRANSLATION.original_language, | |
TargetLanguageCode: lang_code, | |
Text: translation_string.msgid, | |
MyParam: "SHAZAM" | |
}; | |
if ("" === translation_string.msgid) { | |
// console.log("NO STRING"); | |
} else { | |
const translation_promise = | |
translate.translateText(params, (err, data) => { | |
// console.log(chalk.white.bold("msgid: ") + chalk.white(translation_string.msgid)); | |
if (err) console.log(err, err.stack); // an error occurred | |
else { | |
// successful response | |
// console.log(data); | |
translation_string.msgstr = data.TranslatedText; | |
translation_objects[translation_key] = translation_string; | |
} | |
// console.log("()()()()()()("); | |
// console.dir(translation_objects); | |
// console.log("()()()()()()("); | |
}).promise(); | |
translation_jobs.push(translation_promise); | |
} | |
}); | |
}); | |
// console.log(translation_jobs); | |
language_jobs.push( | |
Promise.all(translation_jobs) | |
); | |
/*** | |
* When they are done, write them out: | |
*/ | |
// Save .po and .mo | |
// var output = gettextParser.po.compile(data); | |
// require('fs').writeFileSync(output); | |
// | |
// var output = gettextParser.mo.compile(data); | |
// require('fs').writeFileSync(output); | |
// _.each(po.translations, (translation_objects, context_key) => { | |
// | |
// | |
// data.translations[context_key] = get_translation_for_context(lang_code, translation_objects); | |
// }); | |
}); | |
console.dir(language_jobs); | |
return Promise.all(language_jobs); | |
}; | |
function get_translation_for_context(lang_code, translation_objects) { | |
const translate = new AWS.Translate(); | |
_.each(translation_objects, (el, i) => { | |
const params = { | |
SourceLanguageCode: CONFIG.TRANSLATION.original_language, | |
TargetLanguageCode: lang_code, | |
Text: el.msgid | |
}; | |
if ("" === el.msgid) { | |
// console.log("NO STRING"); | |
} else { | |
// console.log("HERE WE ARE TRANSLATING:"); | |
// console.dir(el); | |
// console.dir(i); | |
// el.msgstr = "translation will go here"; | |
// translation_objects[i] = el; | |
// translate.translateText(params, (err, data) => { | |
// console.log(chalk.white.bold("msgid: ") + chalk.white(el.msgid)); | |
// if (err) console.log(err, err.stack); // an error occurred | |
// else { | |
// // successful response | |
// console.log(data); | |
// el.msgstr = data.TranslatedText; | |
// translation_objects[i] = el; | |
// } | |
// }); | |
} | |
}); | |
} |
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
{ | |
"name": "mat_nib", | |
"version": "1.0.0", | |
"description": "A bespoke theme for NIB", | |
"author": "Materiell", | |
"license": "GPL-2.0", | |
"private": true, | |
"devDependencies": { | |
"@babel/core": "^7.1.0", | |
"@babel/preset-env": "^7.1.0", | |
"chalk": "^2.4.1", | |
"focus-within": "^2.0.0", | |
"gulp": "github:gulpjs/gulp#v4.0.0", | |
"gulp-autoprefixer": "^6.0.0", | |
"gulp-babel": "^8.0.0", | |
"gulp-cached": "^1.1.1", | |
"gulp-concat": "^2.5.2", | |
"gulp-cssnano": "^2.1.1", | |
"gulp-filter": "^5.0.0", | |
"gulp-imagemin": "^4.1.0", | |
"gulp-jshint": "^2.1.0", | |
"gulp-load-plugins": "^1.4.0", | |
"gulp-newer": "^1.3.0", | |
"gulp-plumber": "^1.0.1", | |
"gulp-prettier": "^2.0.0", | |
"gulp-remember": "^1.0.1", | |
"gulp-sass": "^4.0.1", | |
"gulp-sourcemaps": "^2.6.4", | |
"gulp-uglify": "^3.0.1", | |
"gulp-util": "^3.0.5", | |
"gulp-wp-pot": "^2.0.4", | |
"jshint": "^2.9.6", | |
"jshint-stylish": "^2.0.0", | |
"lodash": "^4.17.11", | |
"pa11y": "^5.0.4" | |
}, | |
"scripts": { | |
"build": "gulp", | |
"scripts": "gulp scripts", | |
"styles": "gulp styles", | |
"auto_translate": "gulp auto_translate", | |
"translate": "gulp translate", | |
"images": "gulp images", | |
"pa11y": "gulp pa11y", | |
"watch": "gulp watch" | |
}, | |
"dependencies": { | |
"aws-sdk": "^2.324.0", | |
"gettext-parser": "^2.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment