Skip to content

Commit

Permalink
Format all source files
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Mindru authored and danmindru committed Feb 23, 2020
1 parent d950d1d commit 6ab7b8d
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 207 deletions.
10 changes: 6 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ module.exports = {
commonjs: true,
es6: true
},
extends: "standard",
extends: ['standard', 'prettier'],
globals: {
Atomics: "readonly",
SharedArrayBuffer: "readonly"
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018
},
rules: {}
rules: {
semi: 0
}
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/node_modules/
/tmp/
.DS_Store
/dist
/dist
IDEAS.md
69 changes: 37 additions & 32 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';
const gulp = require('gulp');
const plumber = require('gulp-plumber');

const gulp = require('gulp'),
fs = require('fs'),
plumber = require('gulp-plumber');
const {
SOURCE,
DIST,
WORKING_DIR,
CONFIGURATION_FILE
} = require('./constants');

const options = {
source: 'templates',
dist: 'dist',
workingDir: 'tmp',
source: SOURCE,
dist: DIST,
workingDir: WORKING_DIR,
configurationFile: CONFIGURATION_FILE,
src: function plumbedSrc() {
return gulp.src.apply(gulp, arguments).pipe(plumber());
}
Expand All @@ -16,35 +21,35 @@ const options = {
/**
* Load tasks from the '/tasks' directory.
*/
const build = require('./tasks/build')(options);
const checkDeps = require('./tasks/check-deps')(options);
const dupe = require('./tasks/dupe')(options);
const less = require('./tasks/less')(options);
const lint = require('./tasks/lint')(options);
const postcss = require('./tasks/postcss')(options);
const sass = require('./tasks/sass')(options);
require('./tasks/build')(options);
require('./tasks/dupe')(options);
require('./tasks/less')(options);
require('./tasks/lint')(options);
require('./tasks/postcss')(options);
require('./tasks/sass')(options);
require('./tasks/check-deps')(options);

/* Runs the entire pipeline once. */
gulp.task('run-pipeline', gulp.series('dupe', 'less', 'sass', 'postcss', 'lint', 'build'));
gulp.task(
'run-pipeline',
gulp.series('dupe', 'less', 'sass', 'postcss', 'lint', 'build')
);

/* By default templates will be built into '/dist'. */
gulp.task(
'default',
gulp.series(
'run-pipeline',
() => {
/* gulp will watch for changes in '/templates'. */
gulp.watch(
[
options.source + '/**/*.html',
options.source + '/**/*.css',
options.source + '/**/*.scss',
options.source + '/**/*.less',
options.source + '/**/conf.json'
],
{ delay: 500 },
gulp.series('run-pipeline')
)
}
)
gulp.series('run-pipeline', () => {
/* gulp will watch for changes in '/templates'. */
gulp.watch(
[
options.source + '/**/*.html',
options.source + '/**/*.css',
options.source + '/**/*.scss',
options.source + '/**/*.less',
options.source + '/**/conf.json'
],
{ delay: 500 },
gulp.series('run-pipeline')
);
})
);
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

177 changes: 95 additions & 82 deletions tasks/build.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,67 @@
'use strict';

const gulp = require('gulp'),
inlineCss = require('gulp-inline-css'),
minifyHTML = require('gulp-minify-html'),
minifyInline = require('gulp-minify-inline'),
preprocess = require('gulp-preprocess'),
rename = require('gulp-rename'),
klaw = require('klaw'),
fs = require('fs'),
del = require('del'),
jsonlint = require('jsonlint'),
inlineimg = require('gulp-inline-images-no-http'),
path = require('path');
const gulp = require('gulp');
const inlineCss = require('gulp-inline-css');
const minifyHTML = require('gulp-minify-html');
const minifyInline = require('gulp-minify-inline');
const preprocess = require('gulp-preprocess');
const rename = require('gulp-rename');
const klaw = require('klaw');
const fs = require('fs');
const del = require('del');
const inlineimg = require('gulp-inline-images-no-http');
const path = require('path');

function buildTask(options) {
const { configurationFile } = options;

// Requires: 'dupe', 'less', 'sass', 'postcss', 'lint'.
gulp.task(
'build',
function build(done) {
/**
* Makes templates for a given directory & its configurations.
*
* @function makeTemplates
* @param {String} dir Directory to make templates from.
* @param {Array} confItems A list of configurations objects (usually persons) to make templates from.
*/
function makeTemplates(dir, confItems) {
confItems.forEach(function handleConf(conf) {
var cwd = options.workingDir + '/' + dir;
var stylesheets = [];
gulp.task('build', function build(done) {
/**
* Makes templates for a given directory & its configurations.
*
* @function makeTemplates
* @param {String} dir Directory to make templates from.
* @param {Array} confItems A list of configurations objects (usually persons) to make templates from.
*/
function makeTemplates(dir, confItems) {
return confItems.map(conf => {
const cwd = options.workingDir + '/' + dir;
const files = [];

/**
* Find stylesheets relative to the CWD & generate <link> tags.
* This way we can automagically inject them into <head>.
*/
/**
* Find stylesheets relative to the CWD & generate <link> tags.
* This way we can automagically inject them into <head>.
*/
return new Promise(resolve => {
klaw(cwd)
.on('readable', function walkTemplateDir() {
var stylesheet;
let file;

while ((stylesheet = this.read())) {
var relativePath = __dirname.substring(0, __dirname.lastIndexOf('/')) + '/tmp/' + dir;
stylesheets.push(stylesheet.path.replace(relativePath, ''));
while ((file = this.read())) {
const relativePath =
__dirname.substring(0, __dirname.lastIndexOf('/')) +
'/tmp/' +
dir;
files.push(file.path.replace(relativePath, ''));
}
})
.on('end', function finishedTemplateDirWalk() {
const context = Object.assign(
conf,
{
stylesheets: stylesheets
.filter(function filterFiles (file) {
/* Read only CSS files. */
return file.match(/.*\.css/) ? true : false;
})
.reduce(function (prev, current, index, acc) {
var cssPath = path.win32.basename(current);
return (prev += '<link rel="stylesheet" href="' + cssPath + '">');
/**
* Creates a context of stylesheets to inject them into HTML files later.
* Generates a list of <link> tags with the found stylesheets.
*/
const context = Object.assign(conf, {
stylesheets: files
.filter(file => !!file.match(/.*\.css/)) // Read only CSS files.
.reduce((acc, cur) => {
const cssPath = path.win32.basename(cur);
return (acc +=
'<link rel="stylesheet" href="' + cssPath + '">');
}, '')
}
);
});

options
.src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html'])
.pipe(
preprocess({ context })
)
.pipe(preprocess({ context }))
.pipe(inlineimg())
.pipe(
inlineCss({
Expand All @@ -83,43 +81,58 @@ function buildTask(options) {
})
)
.pipe(gulp.dest(options.dist));

resolve();
});
});
}
});
}

/* Clean up & then read 'src' to generate templates (build entry point). */
return del(options.dist)
.then(function buildStart() {
/**
* Loop through dirs and load their conf files.
* Promisify all 'makeTemplate' calls and when resolved, let gulp know we're done.
*/
var files = [];
var promises = [];
/*
* Clean up & then read from workingDir to generate templates.
* For each found config, a template group will be generated through `makeTemplates`.
*/
return del(options.dist)
.then(function buildStart() {
/**
* Loop through dirs and load their conf files.
* Promisify all 'makeTemplate' calls and when resolved, let gulp know we're done.
*/
const promises = [];

fs.readdirSync('./' + options.workingDir).forEach(function readConfigurations(dir) {
/** NB: For 'watch' to properly work, the cache needs to be deleted before each require. */
var confPath = '../tmp/' + dir + '/conf.json';
var current = null;
var confItems;
fs.readdirSync(options.workingDir).forEach(function readConfigurations(
dir
) {
const confPath = `../tmp/${dir}/${configurationFile}`;

delete require.cache[require.resolve(confPath)];
current = require(confPath);
if (current && current.length) {
confItems = [...current];
} else {
confItems = [current];
}
/** Exit with warn if no configuration file found. */
if (!fs.existsSync(path.resolve(options.workingDir, confPath))) {
return console.warn(
`Missing configuration in "${dir}". Did you remember to create "${dir}/${configurationFile}"?`
);
}

promises.push(makeTemplates(dir, confItems));
});
let current = null;
let confItems;

Promise.all(promises);
})
.then(() => done())
.catch((err) => console.log(err));
}
);
delete require.cache[require.resolve(confPath)]; // NB: For 'watch' to properly work, the cache needs to be deleted before each require.
current = require(confPath);

// Handle single objects or arrays of configs.
if (current && current.length) {
confItems = [...current];
} else {
confItems = [current];
}

promises.push(makeTemplates(dir, confItems));
});

Promise.all(promises);
})
.then(() => done())
.catch(err => console.log(err));
});
}

module.exports = buildTask;
14 changes: 5 additions & 9 deletions tasks/check-deps.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict';
const gulp = require('gulp');
const david = require('gulp-david');

const gulp = require('gulp'),
david = require('gulp-david');

function checkDepsTask(){
gulp.task('check-deps', function checkDeps(){
gulp
.src('package.json')
.pipe(david());
function checkDepsTask() {
gulp.task('check-deps', function checkDeps() {
gulp.src('package.json').pipe(david());
});
}

Expand Down
10 changes: 4 additions & 6 deletions tasks/dupe.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';
const gulp = require('gulp');
const del = require('del');

const gulp = require('gulp'),
del = require('del');

function dupeTask(options){
gulp.task('dupe', function(){
function dupeTask(options) {
gulp.task('dupe', function() {
del.sync([options.workingDir]);

return options
Expand Down
Loading

0 comments on commit 6ab7b8d

Please sign in to comment.