Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
- adds serve command that starts up an http server and serves content from the site directory
- adds --watch option that will start a watch process that will build on any changes to the site, excluding the site directory
  - if --watch is used in tandem with serve javascript will executed in the body to refresh the page in case of any builds happen
- adds --port option that allows the serve command to use a specified port, by default this is random
- adds a help command
- if config.output is set, generate will favor that instead of the default which is {site}/site
- if config.output is set serve will use that instead of the default which is {site}/site
if config.output is set when built the console output will reflect the built directory
- parses code blocks correctly
- adds a new command to bootstrap basic template in the cwd
  • Loading branch information
gabrielcsapo committed Nov 24, 2017
1 parent c97a8d5 commit 8677c57
Show file tree
Hide file tree
Showing 22 changed files with 266 additions and 53 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
script:
- npm install lcov-server -g
- npm run lint
- npm run coverage
- cat coverage/lcov.info | lcov-server --upload https://lcov-server.gabrielcsapo.com
node_js:
- "8"
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# 0.0.2 (11/21/2017)

- adds `serve` command that starts up an http server and serves content from the site directory
- adds `--watch` option that will start a watch process that will build on any changes to the site, excluding the `site` directory
- if `--watch` is used in tandem with `serve` javascript will executed in the body to refresh the page in case of any builds happen
- adds `--port` option that allows the serve command to use a specified port, by default this is random
- adds a `help` command
- if `config.output` is set, generate will favor that instead of the default which is `{site}/site`
- if `config.output` is set `serve` will use that instead of the default which is `{site}/site`
if `config.output` is set when built the console output will reflect the built directory
- parses code blocks correctly
- adds a `new` command to bootstrap basic template in the cwd

# 0.0.1 (11/21/2017)

- adds `build` command can be run as `build` or `build --directory=./some/directory`
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# sweeney

> 💈 a blog-aware, static site generator
> 💈 a blog aware, static site generator
[![Npm Version](https://img.shields.io/npm/v/sweeney.svg)](https://www.npmjs.com/package/sweeney)
[![Build Status](https://travis-ci.org/gabrielcsapo/sweeney.svg?branch=master)](https://travis-ci.org/gabrielcsapo/sweeney)
Expand All @@ -18,8 +18,19 @@ npm install sweeney -g

## Usage

> work in progress!
```
Usage: sweeney [options]
Commands:
## Philosophy 🧠
new [name] bootstrap a new project with in the directory named
build build and output static files to site directory
serve generates a http server to serve content from the site directory
help displays this screen
> a zero dependency tool
Options:
--port={Number} overrides the randomized port for serve
--directory={Path} overrides the default path which is the current working directory
--watch will watch the directory used to generate site and build when changes are made. If this is used in tandem with serve, it will inject javascript to reload the page when changes were made.
```
3 changes: 0 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
- [ ] support markdown footnotes
- [ ] add `serve` command to be able to generate a http server to show realtime changes
- [ ] add `new` command to bootstrap a template application in the given directory
- [ ] add `build` command which should use the sweeney.js file to build the site
116 changes: 114 additions & 2 deletions bin/sweeney.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,144 @@
#!/usr/bin/env node
const http = require('http');
const fs = require('fs');
const path = require('path');
const qs = require('qs');

const bootstrap = require('../lib/bootstrap');
const generate = require('../lib/generate');

const args = process.argv.slice(2);

// this is used when establishing when a build occured
let build = Date.now();
const options = {};
args.filter((a) => a.indexOf('--') > -1).forEach((a) => Object.assign(options, qs.parse(a)));

switch(args[0]) {
case 'help':
console.log(``+ // eslint-disable-line
`
Usage: sweeney [options]
Commands:
new [name] bootstrap a new project with in the directory named
build build and output static files to site directory
serve generates a http server to serve content from the site directory
help displays this screen
Options:
--port={Number} overrides the randomized port for serve
--directory={Path} overrides the default path which is the current working directory
--watch will watch the directory used to generate site and build when changes are made. If this is used in tandem with serve, it will inject javascript to reload the page when changes were made.
`);
break;
case 'new':
(async function() {
try {
const name = args[1];
const directory = path.resolve(process.cwd(), name);

await bootstrap(directory);
console.log(`application bootstrapped in ${directory}`); // eslint-disable-line
} catch(ex) {
console.log(`uhoh something happened \n ${ex.toString()}`); // eslint-disable-line
}
}());
break;
case 'build':
(async function() {
try {
const directory = path.resolve(process.cwd(), options['--directory'] || './');
const config = require(path.resolve(directory, 'sweeney.js'));

await generate(directory, config);
console.log(`site built at ${path.resolve(process.cwd(), options['--directory'] || './')}`); // eslint-disable-line
console.log(`site built at ${path.resolve(process.cwd(), options['--directory'] || './', config.output || 'site')}`); // eslint-disable-line
} catch(ex) {
console.log(`uhoh something happened \n ${ex.toString()}`); // eslint-disable-line
}
}());
break;
case 'serve':

try {
const directory = path.resolve(process.cwd(), options['--directory'] || './');
const config = require(path.resolve(directory, 'sweeney.js'));

const server = http.createServer((req, res) => {
if(req.url === '/__api/update') {
res.statusCode = 200;
return res.end(build.toString());
}
let file = req.url || '/index.html';
if(file === '/') file = '/index.html';
let ext = file.substr(file.lastIndexOf('.') + 1, file.length);

try {
// removing the leading / from the file name
let contents = fs.readFileSync(path.resolve(directory, (config.output || 'site'), file.substr(1, file.length))).toString('utf8');
// inject javascript into the page to refresh it in the case that a new build occurs
if(ext == 'html' && options['--watch'] !== undefined) {
contents = contents.replace('</body>', `<script>
(function() {
var build = "${build}";
var d = document.createElement('div');
d.innerHTML = '💈 you are currently developing this site, any changes will trigger a refresh';
d.style.padding = '10px';
d.style.textAlign = 'center';
document.body.prepend(d);
setInterval(function() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if(this.responseText !== build) {
location.reload();
}
}
};
xhttp.open("GET", "/__api/update", true);
xhttp.send();
}, 500)
}());
</script></body>`);
}
res.end(contents);
} catch(ex) {
res.statusCode = 500;
res.end();
}
}).listen(options['--port'], () => {
console.log(`sweeney listening on http://localhost:${server.address().port}`); // eslint-disable-line
});
} catch(ex) {
console.log(`uhoh something happened \n ${ex.toString()}`); // eslint-disable-line
}
break;
default:
console.log(`sorry the command ${args[0]} is not supported`); // eslint-disable-line
break;
}

if(options['--watch'] !== undefined) {
const directory = path.resolve(process.cwd(), options['--directory'] || './');
let config = require(path.resolve(directory, 'sweeney.js'));

console.log(`watching ${path.resolve(process.cwd(), options['--directory'] || './')}`); // eslint-disable-line
fs.watch(directory, {
recursive: true
}, async function(ev, file) {
// refresh the require cache in the case config has updated
if(file.indexOf('sweeney.js') > -1) {
delete require.cache[require.resolve(path.resolve(directory, 'sweeney.js'))];
config = require(path.resolve(directory, 'sweeney.js'));
}
// we don't want to rebuild the output directory because this is expected to change
if(path.dirname(file).indexOf((config.output || 'site')) === -1) {
console.log(`rebuilding because of ${ev} of ${file}`); // eslint-disable-line
await generate(directory, config);
build = Date.now();
}
});
}
13 changes: 11 additions & 2 deletions docs/2017-11-20-welcome-to-sweeney.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width initial-scale=1" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>template</title> <link rel="stylesheet" href="./site.css"> <meta name="description" content="this is a template site"> </head> <body style="display: flex; min-height: 100vh; flex-direction: column; margin: 0 auto;"> <header class="site-header"> <div class="wrapper"> <a class="site-title" href="./index.html">template</a> <nav class="site-nav"> <a href="#" class="menu-icon"> <svg viewBox="0 0 18 15"> <path fill="#424242" d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.031C17.335,0,18,0.665,18,1.484L18,1.484z"/> <path fill="#424242" d="M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0c0-0.82,0.665-1.484,1.484-1.484 h15.031C17.335,6.031,18,6.696,18,7.516L18,7.516z"/> <path fill="#424242" d="M18,13.516C18,14.335,17.335,15,16.516,15H1.484C0.665,15,0,14.335,0,13.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.031C17.335,12.031,18,12.696,18,13.516L18,13.516z"/> </svg> </a> <div class="trigger"> <a class="page-link" href="./about.html">About</a> <a class="page-link" href="http://github.com/gabrielcsapo/sweeney">Source</a> </div> </nav> </div> </header> <div class="page-content"> <div class="wrapper"> <div class="post"> <header class="post-header"> <h1 class="post-title">Welcome to Sweeney!</h1> <p class="post-meta">Mon Nov 20 2017 12:58:29 GMT-0800 (PST) </p> <small class="post-tags">sweeney, example</small> </header> <article class="post-content"> <br/><p>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width initial-scale=1" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>template</title> <link rel="stylesheet" href="./site.css"> <meta name="description" content="this is a template site"> </head> <body style="display: flex; min-height: 100vh; flex-direction: column; margin: 0 auto;"> <header class="site-header"> <div class="wrapper"> <a class="site-title" href="./index.html">template</a> <nav class="site-nav"> <a href="#" class="menu-icon"> <svg viewBox="0 0 18 15"> <path fill="#424242" d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.031C17.335,0,18,0.665,18,1.484L18,1.484z"/> <path fill="#424242" d="M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0c0-0.82,0.665-1.484,1.484-1.484 h15.031C17.335,6.031,18,6.696,18,7.516L18,7.516z"/> <path fill="#424242" d="M18,13.516C18,14.335,17.335,15,16.516,15H1.484C0.665,15,0,14.335,0,13.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.031C17.335,12.031,18,12.696,18,13.516L18,13.516z"/> </svg> </a> <div class="trigger"> <a class="page-link" href="./about.html">About</a> <a class="page-link" href="./projects.html">Projects</a> <a class="page-link" href="http://github.com/gabrielcsapo/sweeney">Source</a> </div> </nav> </div> </header> <div class="page-content"> <div class="wrapper"> <div class="post"> <header class="post-header"> <h1 class="post-title">Welcome to Sweeney!</h1> <p class="post-meta">Mon Nov 20 2017 12:58:29 GMT-0800 (PST) </p> <small class="post-tags">sweeney, example</small> </header> <article class="post-content"> <br/><p>
You’ll find this post in your <code>_posts</code> 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 <code>sweeney serve --watch</code>, which launches a web server and auto-regenerates your site when a file is updated.
</p><br/><br/><p>
To add new posts, simply add a file in the <code>_posts</code> directory that follows the convention <code>YYYY-MM-DD-name-of-post.ext</code> and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.
To add new posts, simply add a file in the <code>_posts</code> directory that follows the convention <code>YYYY-MM-DD-name-of-post.ext</code> and includes the necessary options block.
</p><br/><br/><pre><code>---
{
"layout": "post",
"title": "Welcome to Sweeney!",
"date": "2017-11-20 12:58:29",
"tags": ["sweeney", "example"]
}
---</code></pre><p>
Take a look at the source for this post to get an idea about how it works.
</p><br/><br/><table>
<tr>
<th><p>
Expand Down
Loading

0 comments on commit 8677c57

Please sign in to comment.