Skip to content

Commit

Permalink
Committing and pushing before I screw up a path and delete my whole FS
Browse files Browse the repository at this point in the history
  • Loading branch information
Ændrew Rininsland committed Aug 5, 2016
1 parent c09ddbe commit b3750d3
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 20 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@
"dependencies": {
"es6-promisify": "^4.1.0",
"glob": "^7.0.5",
"koa-manifest-rev": "^0.1.0",
"koa-mount": "^2.0.0",
"koa-router": "^7.0.1",
"koa-static": "^3.0.0",
"rev-file": "^2.0.0"
"rev-file": "^2.0.0",
"sander": "^0.5.1"
}
}
9 changes: 7 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import Koa from 'koa';
import serve from 'koa-static';
import mount from 'koa-mount';
import convert from 'koa-convert';
import revManifest from 'koa-manifest-rev';
import koaBS from 'koa-browser-sync'; // Optional alternative: https://www.npmjs.com/package/koa-liveload
import chalk from 'chalk';
import { resolve } from 'path';

import router from './routes';
import devTasks from './tasks/dev';
import revTask from './tasks/rev';

const app = new Koa();
const port = process.env.PORT || 5555;
Expand All @@ -38,10 +39,14 @@ if (app.env === 'development') {
ui: process.argv.includes('--bsui'),
ghostMode: process.argv.includes('--ghost'),
})));

app.use(devTasks);
// app.use(revTask);
}

app.use(revManifest({
manifest: resolve(__dirname, '../', 'dist/', 'rev-manifest.json'),
}));

app.use(router.routes());
app.use(router.allowedMethods());

Expand Down
13 changes: 11 additions & 2 deletions server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ import buildHtml from './tasks/templates';
const routes = new Router();

routes.get('/', async (ctx, next) => {
await next();

const filters = [
{
name: 'rev',
func: ctx.state.manifest,
},
];

try {
ctx.body = await buildHtml();
ctx.body = await buildHtml(undefined, filters);
} catch (e) {
console.error('Error building HTML:');
console.error(e);
}
next();
});

export default routes;
2 changes: 0 additions & 2 deletions server/deps.js → server/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

import p from 'es6-promisify';
import globOriginal from 'glob';
import { writeFile as writeFileOriginal } from 'fs';
import { render } from 'node-sass';
import inlineSource from 'inline-source';

export const glob = p(globOriginal);
export const writeFile = p(writeFileOriginal);
export const sassRender = p(render);
export const inline = p(inlineSource);
13 changes: 12 additions & 1 deletion server/tasks/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { watch } from 'chokidar';

import buildSass from './styles';
import buildScripts from './scripts';
import revAssets from './rev';

const distFiles = resolve(__dirname, '../../client/**/*.{js,scss}');

Expand All @@ -17,6 +18,7 @@ watch(distFiles).on('all', async (evt, path) => {
try {
await buildSass(path);
} catch (e) {
console.error('Error generating styles:');
console.error(e);
}
break;
Expand All @@ -25,16 +27,25 @@ watch(distFiles).on('all', async (evt, path) => {
try {
await buildScripts(path);
} catch (e) {
console.error('Error building bundle:');
console.error(e);
}
break;

default:
break;
}

revAssets(path);
});

// This might eventually do something? ¯\_(ツ)_/¯
export default function devTasks(ctx, next) {
try {
revAssets(null, true);
} catch (e) {
console.error('Error revving assets:');
console.error(e);
}

next();
}
56 changes: 51 additions & 5 deletions server/tasks/rev.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,56 @@
*
*/

import rev from 'rev-file';
import { glob } from '../deps';
import { sync as rev } from 'rev-file';
import { unlink } from 'fs';
import { resolve } from 'path';
import { copyFile, writeFile } from 'sander';
import { glob } from '../shared';

export default async function (ctx, next) {
await glob();
next();
const manifestPath = resolve(__dirname, '../../', 'dist/', 'rev-manifest.json');

export async function clearAllRevved() {
const allFiles = await glob('dist/**/*.{js,css,html}');
allFiles.forEach(filePath => {
if (filePath.match(/.+?-[a-z0-9]{10}\.(?:js|css|html)$/)) {
unlink(filePath, (err) => {
console.error('Error clearing rev cache:');
if (err) console.error(err);
});
}
});
}

export default async function (path, all = false) {
if (all || !path) {
clearAllRevved();

try {
const allFiles = await glob('dist/**/*.{js,css,html}');
const manifest = allFiles.reduce((last, curr) => {
if (!curr.match(/.+?-[a-z0-9]{10}\.(?:js|css|html)$/)) {
last[curr.replace('dist/', '')] = rev(curr).replace('dist/', '');
}

return last;
}, {});

Object.keys(manifest).forEach(async key => {
try {
await copyFile(resolve(__dirname, '../../dist', key))
.to(resolve(__dirname, '../../dist', manifest[key]));
} catch (e) {
console.error('Error copying revv\'d asset:');
console.error(e);
}
});

writeFile(manifestPath, JSON.stringify(manifest));
} catch (e) {
console.log('Error revving:');
console.error(e);
}
} else {
// Do something with individual files here? /shrug
}
}
2 changes: 1 addition & 1 deletion server/tasks/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import browserify from 'browserify';
import { resolve, basename } from 'path';
import { writeFile } from '../deps';
import { writeFile } from 'sander';
import p from 'es6-promisify';

const BROWSERIFY_TRANSFORMS = [
Expand Down
6 changes: 4 additions & 2 deletions server/tasks/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
*/

import { resolve, basename } from 'path';
import { writeFile, sassRender } from '../deps';
import { writeFile } from 'sander';
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';

import { sassRender } from '../shared';

const AUTOPREFIXER_BROWSERS = [
'ie >= 8',
'ff >= 30',
Expand All @@ -31,7 +33,7 @@ export default async function (path = resolve(__dirname, '../../client/styles/ma
// Next run autoprefixer...
const processed = await prefixer.process(compiledSass.css.toString());

return writeFile(outPath, processed);
return writeFile(undefined, outPath, processed);
} catch (e) {
return e;
}
Expand Down
14 changes: 10 additions & 4 deletions server/tasks/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@
import p from 'es6-promisify';
import min from 'htmlmin';
import { resolve, basename } from 'path';
import { writeFile } from 'sander';

import { configure as env } from '../../views';
import { writeFile, inline } from '../deps';
import { configure } from '../../views';
import { inline } from '../shared';

// I'm not sure if this will work due to `require` caching.
import data from '../../config/article';

const defaultPath = resolve(__dirname, '../..', 'client/index.html');

/**
* Render a template using Nunjucks.
* Returns the rendered template to Koa and writes it to `dist/.`
*
* @param {String} path Absolute path of file to render
* @return {Promise<string>} Rendered version of Nunjucks template.
*/
export default async function (path = resolve(__dirname, '../..', 'client/index.html')) {
export default async function (path = defaultPath, ...filters) {
const outPath = resolve(__dirname, `../../dist/${basename(path)}`);
const render = p(env().render, env());
const env = configure();
const render = p(env.render, env);

filters.forEach(filter => env.addFilter(filter.name, filter.func));

try {
// Render from Nunjucks...
Expand Down

0 comments on commit b3750d3

Please sign in to comment.