-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
c97a8d5
commit 8677c57
Showing
22 changed files
with
266 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.