Skip to content

Commit

Permalink
Asset pipeline and 404 generation now work
Browse files Browse the repository at this point in the history
  • Loading branch information
astronomersiva committed Jul 29, 2018
1 parent 500e5d7 commit 322a899
Show file tree
Hide file tree
Showing 13 changed files with 1,844 additions and 153 deletions.
5 changes: 4 additions & 1 deletion lib/lego.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ module.exports = async function(args) {
const taskList = [
'cleanBuild',
'copyCNAME',
'convertMarkdown'
'generate404Page',
'generatePostsFromMarkdown',
'copyMinorStaticAssets',
'copyMajorStaticAssets'
];

for (const task of taskList) {
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/cleanBuild.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const fs = require('fs-extra');
const { BUILD } = require('../utils/constants');

module.exports = async function() {
try {
await fs.remove('build');
await fs.remove(BUILD);
} catch (error) {
throw error;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/tasks/copyCNAME.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const fs = require('fs-extra');
const path = require('path');

const { BUILD } = require('../utils/constants');

module.exports = async function() {
try {
await fs.copy('CNAME', 'build/CNAME');
await fs.copy('CNAME', path.join(BUILD, 'CNAME'));
} catch (error) {
throw error;
}
Expand Down
41 changes: 41 additions & 0 deletions lib/tasks/copyMajorStaticAssets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const postcss = require('postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const uglifyJS = require('uglify-es');

const filterFileList = require('../utils/filterFileList');
const { STATIC, BUILD } = require('../utils/constants');

module.exports = async function() {
try {
let assets = glob.sync(`${STATIC}/+(js|css)/**/*`);
assets = filterFileList(assets, ['js', 'css']);

for (const asset of assets) {
let destination = path.join(BUILD, asset);
let destinationDir = path.join(BUILD, path.dirname(asset));

if (!fs.existsSync(destinationDir)) {
fs.mkdirpSync(destinationDir);
}

let contents = fs.readFileSync(asset).toString();
let extension = path.extname(asset);

if (extension === '.js') {
let result = uglifyJS.minify(contents);
fs.writeFileSync(destination, result.code);
} else if (extension === '.css') {
let result = await postcss([cssnano, autoprefixer])
.process(contents, { from: asset, to: destination });

fs.writeFileSync(destination, result.css);
}
}
} catch (error) {
throw error;
}
}
21 changes: 21 additions & 0 deletions lib/tasks/copyMinorStaticAssets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs-extra');
const path = require('path');

const cleanFileList = require('../utils/cleanFileList');
const { STATIC, BUILD } = require('../utils/constants');

module.exports = async function() {
try {
let staticFiles = cleanFileList(fs.readdirSync(STATIC));
let minorStaticFiles = staticFiles.filter((path) => !['css', 'js'].includes(path));

for (const asset of minorStaticFiles) {
fs.copySync(
path.join(STATIC, asset),
path.join(BUILD, STATIC, asset)
);
}
} catch (error) {
throw error;
}
}
23 changes: 23 additions & 0 deletions lib/tasks/generate404Page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs-extra');
const path = require('path');
const Liquid = require('liquid-node');
const engine = new Liquid.Engine();

const { LAYOUTS, BUILD } = require('../utils/constants');

// needed for `include` statements
engine.registerFileSystem(new Liquid.LocalFileSystem(LAYOUTS));

module.exports = async function() {
try {
const pathTo404 = path.join(LAYOUTS, '404.html');
const destination = path.join(BUILD, '404.html');

const layout = fs.readFileSync(pathTo404).toString();
const rendered404 = await engine.parseAndRender(layout);

fs.writeFileSync(destination, rendered404);
} catch (error) {
throw error;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const fs = require('fs-extra');
const path = require('path');
const md = require('markdown-it')();

const filterFileList = require('../utils/filterFileList');
const { BUILD, PAGES } = require('../utils/constants');

module.exports = async function() {
try {
const PAGES = 'pages';
const BUILD = 'legobuild';

const pages = fs.readdirSync(PAGES, { encoding: 'utf8' });
const filteredPages = filterFileList(pages, 'md');

if (!fs.existsSync(BUILD)) {
fs.mkdirSync(BUILD);
}

for (const page of filteredPages) {
const pageContents = fs.readFileSync(path.join(PAGES, page)).toString();
const htmlPath = path.join(BUILD, path.parse(page).name);
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/cleanFileList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function(files) {
return files.filter((file) => {
// Filters hidden files
return !((/(^|\/)\.[^/.]/g).test(file));
});
}
6 changes: 6 additions & 0 deletions lib/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
BUILD: 'build',
PAGES: 'pages',
STATIC: 'static',
LAYOUTS: 'layouts'
}
20 changes: 16 additions & 4 deletions lib/utils/filterFileList.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const path = require('path');
const cleanFileList = require('./cleanFileList');

// syntax sugar
const extensions = {
js: '.js',
css: '.css',
md: '.md'
};

module.exports = function(files, extension) {
return files.filter((file) => {
// Checks if the file has the given extension, also filter hidden files
return (path.extname(file) === `.${extension}`) &&
!((/(^|\/)\.[^/.]/g).test(file));
let cleanedFileList = cleanFileList(files);
return cleanedFileList.filter((file) => {
// Checks if the file has the given extension
if (Array.isArray(extension)) {
return Object.values(extensions).includes(path.extname(file));
}

return (path.extname(file) === extensions[extension]);
});
}
4 changes: 4 additions & 0 deletions lib/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const {

class Logger {
refresh() {
if (process.env.DEBUG) {
return;
}

clear();

let { message, stack } = this;
Expand Down
Loading

0 comments on commit 322a899

Please sign in to comment.