From 17990052e64e0448545dee08c572ddd88a1b31db Mon Sep 17 00:00:00 2001 From: "Gabriel J. Csapo" Date: Sun, 2 Sep 2018 20:59:59 -0700 Subject: [PATCH] 1.4.0 - add support for parsing of .sweeney.js and .sweeney file paths. - adds a plugin interface - fix issue with generating uneeded files to disk --- CHANGELOG.md | 6 ++++++ bin/sweeney.js | 11 +++++++---- .../example/2017-11-20-welcome-to-sweeney.html | 2 +- docs/example/about.html | 2 +- docs/example/index.html | 2 +- docs/example/posts.html | 2 +- docs/example/projects.html | 2 +- docs/index.html | 9 +++++---- example/{.sweeney => .sweeney.js} | 0 lib/defaultPlugins.js | 18 +++++++++++------- lib/plugin.js | 6 ++++++ lib/site.js | 2 +- lib/util.js | 5 ++--- package.json | 2 +- test/util.js | 2 +- 15 files changed, 45 insertions(+), 26 deletions(-) rename example/{.sweeney => .sweeney.js} (100%) create mode 100644 lib/plugin.js diff --git a/CHANGELOG.md b/CHANGELOG.md index fcb5a9a..2abcedf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 1.4.0 (09/02/2018) + +- add support for parsing of `.sweeney.js` and `.sweeney` file paths. +- adds a plugin interface +- fix issue with generating unneeded files to disk + # 1.3.2 (05/23/2018) - cleans up timing logic diff --git a/bin/sweeney.js b/bin/sweeney.js index eba3520..08a0d64 100755 --- a/bin/sweeney.js +++ b/bin/sweeney.js @@ -8,7 +8,7 @@ const { version } = require('../package.json') const Site = require('../lib/site') const Serve = require('../lib/serve/serve') -const { time, getConfig, copyDirectory, renderSubDepends } = require('../lib/util') +const { time, getConfig, copyDirectory, renderSubDepends, ensureDirectoryExists } = require('../lib/util') process.on('unhandledRejection', (error) => { console.error(`Error: \n ${error.stack}`); // eslint-disable-line @@ -50,9 +50,12 @@ const program = woof(` }, output: { type: 'string', - validate: function (value) { - const stats = fs.statSync(value) - return !stats.isDirectory() ? `please provide a valid directory path. \n ${value} is not a valid path.` : true// ensure that this is a directory + validate: async function (value) { + const fullPath = path.resolve(__dirname, value) + await ensureDirectoryExists(fullPath) + const stats = fs.statSync(fullPath) + + return !stats.isDirectory() ? `please provide a valid directory path. \n ${fullPath} is not a valid path.` : true// ensure that this is a directory } }, port: { diff --git a/docs/example/2017-11-20-welcome-to-sweeney.html b/docs/example/2017-11-20-welcome-to-sweeney.html index 1a0ad90..32dbffd 100644 --- a/docs/example/2017-11-20-welcome-to-sweeney.html +++ b/docs/example/2017-11-20-welcome-to-sweeney.html @@ -1 +1 @@ - Welcome to Sweeney! Sweeney

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run sweeney serve --watch, which launches a web server and auto-regenerates your site when a file is updated.

To add new posts, simply add a file in the _posts directory that follows the convention YYYY-MM-DD-name-of-post.ext and includes the necessary options block.

--- {   "layout": "post",   "title": "Welcome to Sweeney!",   "date": "2017-11-20 12:58:29",   "tags": ["sweeney", "example"] } --- 

Take a look at the source for this post to get an idea about how it works.

before after
hard easy

There is also an option to override defaults with a sweeney.js in the root directory.

module.exports = {   source: './',   output: '../../docs',   ... // anything extra added to this file will be accessible via templates }; 

Check out the Sweeney docs for more info on how to get the most out of Sweeney. File all bugs/feature requests at Sweeney’s GitHub repo.

\ No newline at end of file + Welcome to Sweeney! Sweeney!

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run sweeney serve --watch, which launches a web server and auto-regenerates your site when a file is updated.

To add new posts, simply add a file in the _posts directory that follows the convention YYYY-MM-DD-name-of-post.ext and includes the necessary options block.

--- {   "layout": "post",   "title": "Welcome to Sweeney!",   "date": "2017-11-20 12:58:29",   "tags": ["sweeney", "example"] } --- 

Take a look at the source for this post to get an idea about how it works.

before after
hard easy

There is also an option to override defaults with a sweeney.js in the root directory.

module.exports = {   source: './',   output: '../../docs',   ... // anything extra added to this file will be accessible via templates }; 

Check out the Sweeney docs for more info on how to get the most out of Sweeney. File all bugs/feature requests at Sweeney’s GitHub repo.

\ No newline at end of file diff --git a/docs/example/about.html b/docs/example/about.html index be9b3b5..a5b0c29 100644 --- a/docs/example/about.html +++ b/docs/example/about.html @@ -1 +1 @@ - About Sweeney

This is an about page!

here is some info
\ No newline at end of file + About Sweeney!

This is an about page!

here is some info
\ No newline at end of file diff --git a/docs/example/index.html b/docs/example/index.html index d8e124b..d20dae8 100644 --- a/docs/example/index.html +++ b/docs/example/index.html @@ -1 +1 @@ - Home Sweeney

Welcome to your fresh cut site!

Check out some of the posts!

\ No newline at end of file + Home Sweeney!

Welcome to your fresh cut site!

Check out some of the posts!

\ No newline at end of file diff --git a/docs/example/posts.html b/docs/example/posts.html index ef0164a..0bd65a6 100644 --- a/docs/example/posts.html +++ b/docs/example/posts.html @@ -1 +1 @@ - Posts Sweeney

Here are some posts

\ No newline at end of file + Posts Sweeney!

Here are some posts

\ No newline at end of file diff --git a/docs/example/projects.html b/docs/example/projects.html index 0de444b..00d88e6 100644 --- a/docs/example/projects.html +++ b/docs/example/projects.html @@ -1 +1 @@ - Projects Sweeney

Projects

here are some projects

  • sweeney 💈 a blog aware, static site generator
\ No newline at end of file + Projects Sweeney!

Projects

here are some projects

  • sweeney 💈 a blog aware, static site generator
\ No newline at end of file diff --git a/docs/index.html b/docs/index.html index b47ce4b..3d830f8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -268,9 +268,10 @@

A static site generator that cuts the way you wa
 const path = require('path');
+const Plugin = require('sweeney/lib/plugin');
 
-module.exports.include = {
-  parse: async function(filePath, content) {
+module.exports.include = class IncludePlugin extends Plugin {
+  static async parse(filePath, content) {
     // we are looking for the pattern {{-- includes ... }} in the template
     // this could be any pattern, it does not need to look like the handlebars syntax it could be [[== includes ... ==]]
     // that is the extensibility of the plugin architecture
@@ -295,8 +296,8 @@ 

A static site generator that cuts the way you wa }; } return false; - }, - render: async function(plugins, filePath, content, templates, data, found) { + } + static async render(plugins, filePath, content, templates, data, found) { const start = process.hrtime(); let ext = found.substring(found.lastIndexOf('.') + 1, found.length); let name = found.substring(found.lastIndexOf('/') + 1, found.length - 3); diff --git a/example/.sweeney b/example/.sweeney.js similarity index 100% rename from example/.sweeney rename to example/.sweeney.js diff --git a/lib/defaultPlugins.js b/lib/defaultPlugins.js index 42a1fba..51d97cf 100644 --- a/lib/defaultPlugins.js +++ b/lib/defaultPlugins.js @@ -1,9 +1,9 @@ const path = require('path') - +const Plugin = require('./plugin') const { render, parse } = require('./util') -module.exports.editable = { - async parse (filePath, content) { +module.exports.editable = class EditablePlugin extends Plugin { + static async parse (filePath, content) { const reg = /{{-- editable (.+?) (string|number|color) --}}/g if (content.match(reg)) { @@ -44,8 +44,8 @@ module.exports.editable = { } } -module.exports.includes = { - async parse (filePath, content) { +module.exports.includes = class IncludePlugin extends Plugin { + static async parse (filePath, content) { const reg = /{{-- includes (.+?) --}}/g if (content.match(reg)) { @@ -64,8 +64,10 @@ module.exports.includes = { } } return false - }, - async render (plugins, filePath, content, templates, data, found) { + } + static async render (plugins, filePath, content, templates, data, found) { + let shouldRender = true + const start = process.hrtime() const ext = found.substring(found.lastIndexOf('.') + 1, found.length) const name = found.substring(found.lastIndexOf('/') + 1, found.length - 3) @@ -96,6 +98,7 @@ module.exports.includes = { depends.push(output) if (_template.type === 'css') { + shouldRender = false content = content.replace(`{{-- includes ${found} --}}`, ``) } else { content = content.replace(`{{-- includes ${found} --}}`, output.rendered) @@ -104,6 +107,7 @@ module.exports.includes = { return { depends, + shouldRender, content } } diff --git a/lib/plugin.js b/lib/plugin.js new file mode 100644 index 0000000..df4167a --- /dev/null +++ b/lib/plugin.js @@ -0,0 +1,6 @@ +class Plugin { + async parse () {} + async render () {} +} + +module.exports = Plugin diff --git a/lib/site.js b/lib/site.js index 52a99d9..d9df80a 100644 --- a/lib/site.js +++ b/lib/site.js @@ -140,7 +140,7 @@ class Site { rendered: options.rendered }) - if (!inMemory) await writeFile(page.outputPath, options.rendered) + if (!inMemory && options.shouldRender) await writeFile(page.outputPath, options.rendered) } if (typeof this.onBuild === 'function') { diff --git a/lib/util.js b/lib/util.js index 15223bd..1438d24 100644 --- a/lib/util.js +++ b/lib/util.js @@ -78,8 +78,6 @@ async function copyDirectory (source, destination) { */ async function getConfig (directory) { try { - await stat(`${directory}/.sweeney`) - const config = require(`${directory}/.sweeney`) switch (typeof config) { @@ -90,7 +88,7 @@ async function getConfig (directory) { } } catch (ex) { // propogate message to the user, this is something with the config - if (ex.message.indexOf('no such file or directory') === -1) { + if (ex.message.indexOf('Cannot find module') === -1) { throw ex } return {} @@ -313,6 +311,7 @@ async function render (plugins, templates, template, additional = {}) { filePath, depends: output, rendered: output.rendered, + shouldRender: true, time: process.hrtime(start)[1] / 1000000 } } else { diff --git a/package.json b/package.json index dd7e6f9..7b88705 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sweeney", - "version": "1.3.2", + "version": "1.4.0", "description": "💈 A static site generator that cuts the way you want it to", "author": "Gabriel J. Csapo ", "license": "Apache-2.0", diff --git a/test/util.js b/test/util.js index 10c385f..ff03568 100644 --- a/test/util.js +++ b/test/util.js @@ -521,7 +521,7 @@ test('util', (t) => { const files = await readdir(destination) t.deepEqual(files, [ - '.sweeney', + '.sweeney.js', 'about.sy', 'index.sy', 'layouts',