Comparing version 0.2.0 to 0.3.0
27
cli.js
#!/usr/bin/env node | ||
/* eslint no-console: "off" */ | ||
var argv = require('yargs') | ||
.usage('$0 -e ejs \n\nOverride Defaults: \n$0 -e ejs -s source/ -d _output -l templates') | ||
.demand(['e']) | ||
.usage('$0 <template_engine> \n' + | ||
'$0 <template_engine> [-s <source_dir>] [-d <output_dir>] [-l <layout_dir>]') | ||
.demand(1, 1, 'Error: You must specify an template engine') | ||
.default({ | ||
s: 'src/', | ||
d: 'dist/', | ||
l: 'layouts/' | ||
l: 'layouts/', | ||
}) | ||
.string(['e', 's', 'd', 'l']) | ||
.string(['s', 'd', 'l']) | ||
.alias({ | ||
e: 'engine', | ||
s: 'src', | ||
d: 'dist', | ||
l: 'layouts' | ||
l: 'layouts', | ||
}) | ||
.describe({ | ||
e: 'Set the valid consolidate.js template engine to use when parsing layouts', | ||
s: 'Set the src directory', | ||
d: 'Set the dist directory', | ||
l: 'Set the layouts directory' | ||
l: 'Set the layouts directory', | ||
}) | ||
.help() | ||
.version() | ||
.example('$0 ejs\n' + | ||
'$0 ejs -s posts/ -d output/ -l templates/') | ||
.epilog('A list of supported template engines may be found at: https://github.com/tj/consolidate.js/#supported-template-engines.') | ||
.argv; | ||
var ssg = require('./index.js'); | ||
ssg(argv.e, argv.s, argv.d, argv.l); | ||
var onessg = require('./index.js'); | ||
onessg(argv._[0], argv.s, argv.d, argv.l, function (err) { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
94
index.js
var fs = require('fs-extra'); | ||
var path = require('path'); | ||
var replaceExt=require('replace-ext'); | ||
var fm = require('front-matter'); | ||
@@ -7,22 +8,23 @@ var cons = require('consolidate'); | ||
var yaml = require('js-yaml'); | ||
var marked = require('marked'); | ||
var _ = require('lodash'); | ||
module.exports = function (engine, src, dist, layouts) { | ||
module.exports = function (engine, src, dist, layouts, cb) { | ||
// SANITY CHECKS | ||
// Check that src exists: | ||
fs.access(src, function (err) { | ||
if (err) throw err; | ||
if (err) return cb(err); | ||
}); | ||
// Check that engine is a string: | ||
if (typeof engine !== 'string' || engine === '') throw new Error('Please pass a valid engine parameter'); | ||
if (typeof engine !== 'string' || engine === '') return cb(new Error('Please pass a valid engine parameter')); | ||
// Check that engine is supported by consolidate.js: | ||
if (typeof cons[engine] !== 'function') throw new Error(engine+' is not a valid consolidate.js template engine'); | ||
if (typeof cons[engine] !== 'function') return cb(new Error(engine+' is not a valid consolidate.js template engine')); | ||
// MAIN CODE | ||
// For each file in src: | ||
// For each html file in src: | ||
forGlob(path.join(src, '**/*.html'), function (filePath) { | ||
// Load and parse FM: | ||
loadFile(filePath, function (err, data) { | ||
if (err) throw err; | ||
if (err) return cb(err); | ||
// Render it: | ||
render(data, filePath, function (err, html) { | ||
if (err) throw err; | ||
if (err) return cb(err); | ||
// Get path to write to: | ||
@@ -32,3 +34,3 @@ var writePath=path.join(dist, filePath.replace(src, '')); | ||
fs.outputFile(writePath, html, function (err) { | ||
if (err) throw err; | ||
if (err) return cb(err); | ||
}); | ||
@@ -40,2 +42,27 @@ }); | ||
}); | ||
// For each markdown file in src: | ||
forGlob(path.join(src, '**/*.@(md|markdown)'), function (filePath) { | ||
// Load and parse FM: | ||
loadFile(filePath, function (err, data) { | ||
if (err) return cb(err); | ||
marked(data.body, function (err, body) { | ||
if (err) return cb(err); | ||
// Overwrite markdown with html: | ||
data.body=body; | ||
// Render it: | ||
render(data, filePath, function (err, html) { | ||
if (err) return cb(err); | ||
// Get path to write to: | ||
var writePath=replaceExt(path.join(dist, filePath.replace(src, '')), '.html'); | ||
// Output using fs-extra: | ||
fs.outputFile(writePath, html, function (err) { | ||
if (err) return cb(err); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}, function () { | ||
// Empty for now, since forGlob() is sync | ||
}); | ||
// IN-SCOPE HELPER FUNCTIONS | ||
// Declare render() inside the main function for access to var engine | ||
@@ -53,5 +80,3 @@ function render(data, filePath, cb) { | ||
// Glob doesn't throw an error if the layout path doesn't exist, so we do: | ||
if (!layout) { | ||
cb(new Error('The file: '+path.join(layouts, data.attributes._layout)+'.'+engine+' does not exist')) | ||
} | ||
if (!layout) cb(new Error('The file: '+path.join(layouts, data.attributes._layout)+'.'+engine+' does not exist')); | ||
var locals=data.attributes; | ||
@@ -61,32 +86,23 @@ locals._body=data.body; | ||
cons[engine](layout, locals, cb); | ||
} else { | ||
// Else, return body | ||
cb(null, data.body) | ||
} | ||
} else cb(null, data.body); // Else, return body | ||
}); | ||
} | ||
// Declate getDefaults inside the main function for access to var src | ||
// Declare getDefaults inside the main function for access to var src | ||
function getDefaults(filePath, cb, defaults) { | ||
glob(path.join(path.dirname(filePath), '_defaults.*'), function (err, res) { | ||
if (!defaults) { | ||
defaults={}; | ||
} | ||
if (err) return cb(err); | ||
if (!res[0]) { | ||
return recurse(); | ||
} | ||
if (!defaults) defaults={}; | ||
if (!res[0]) return recurse(); | ||
var ext=path.extname(res[0]); | ||
if (ext === '.yaml' || ext === '.yml') { | ||
try { | ||
try { | ||
switch (ext) { | ||
case '.yaml': | ||
case '.yml': | ||
_.defaultsDeep(defaults, yaml.safeLoad(fs.readFileSync(res[0], 'utf8'))); | ||
} catch (e) { | ||
return cb(e); | ||
} | ||
} else if (ext === '.json') { | ||
try { | ||
// Use fs-extra: | ||
break; | ||
case '.json': | ||
_.defaultsDeep(defaults, fs.readJsonSync(res[0])); | ||
} catch (e) { | ||
return cb(e); | ||
} | ||
} catch (e) { | ||
return cb(e); | ||
} | ||
@@ -96,5 +112,4 @@ recurse(); | ||
function recurse() { | ||
if (path.dirname(filePath) === src.replace(path.sep, '')) { | ||
return cb(null, defaults); | ||
} else { | ||
if (path.dirname(filePath) === src.replace(path.sep, '')) return cb(null, defaults); | ||
else { | ||
var newPath=path.dirname(filePath); | ||
@@ -110,6 +125,7 @@ return getDefaults(newPath, cb, defaults); | ||
fs.readFile(name, 'utf8', function (err, res) { | ||
if (err) { return cb(err); } | ||
if (err) return cb(err); | ||
var json; | ||
// Use try...catch for sync front-matter: | ||
try { | ||
var json=fm(res); | ||
json=fm(res); | ||
} catch (e) { | ||
@@ -123,4 +139,4 @@ return cb(e); | ||
function forGlob(pattern, iter, cb) { | ||
glob(pattern, function (err, res) { | ||
if (err) { return cb(err); } | ||
glob(pattern, {nodir: true}, function (err, res) { | ||
if (err) return cb(err); | ||
res.forEach(iter); | ||
@@ -127,0 +143,0 @@ cb(null); |
{ | ||
"name": "onessg", | ||
"version": "0.2.0", | ||
"description": "The Static Site Generator that does only one thing: compile your html.", | ||
"version": "0.3.0", | ||
"description": "The Static Site Generator that does only one thing: compile your html and markdown.", | ||
"main": "index.js", | ||
@@ -10,3 +10,6 @@ "bin": { | ||
"scripts": { | ||
"test": "mocha --ui tdd" | ||
"test": "npm run lint && npm run mocha", | ||
"coverage": "npm run mocha && nyc report --reporter lcov", | ||
"mocha": "nyc mocha --ui tdd", | ||
"lint": "eslint --ignore-path .gitignore '**/*.js'" | ||
}, | ||
@@ -18,3 +21,4 @@ "keywords": [ | ||
"simple", | ||
"layouts" | ||
"layouts", | ||
"markdown" | ||
], | ||
@@ -33,8 +37,12 @@ "author": "Ryan Zimmerman <opensrc@ryanzim.com> (http://ryanzim.com)", | ||
"lodash": "^4.14.0", | ||
"yargs": "^4.8.1" | ||
"marked": "^0.3.6", | ||
"replace-ext": "^1.0.0", | ||
"yargs": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"ejs": "^2.5.1", | ||
"mocha": "^2.5.3" | ||
"eslint": "~3.3.0", | ||
"mocha": "^3.0.2", | ||
"nyc": "^7.1.0" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # onessg | ||
onessg (One Static Site Generator) is the Static Site Generator that does only one thing: compile your html. It won't minify your JS, concat your CSS, or optimize your images. Why? You most likely already have a favorite tool for doing that. | ||
onessg (One Static Site Generator) is the Static Site Generator that does only one thing: compile your html and markdown. It won't minify your JS, concat your CSS, or optimize your images. Why? You most likely already have a favorite tool for doing that. | ||
@@ -35,3 +35,3 @@ > The Javascript pendulum has swung from restrictive, monolithic frameworks to modular, boilerplate-hindered libraries. | ||
| ├── _defaults.yaml | ||
| └── page-one.html | ||
| └── page-one.md | ||
├── layouts/ | ||
@@ -44,5 +44,5 @@ | └── page.ejs | ||
All HTML files can include front-matter (yaml or json). | ||
All files can include front-matter (yaml or json). | ||
**src/page-one.html**: | ||
**src/page-one.md**: | ||
```html | ||
@@ -53,3 +53,3 @@ --- | ||
--- | ||
<!-- Your HTML --> | ||
Hello World! | ||
``` | ||
@@ -85,3 +85,3 @@ Notice the underscore before `layout`. _Anything prefixed with an underscore is reserved word for onessg._ All keys in the front-matter will be passed as a local to your templates. | ||
```bash | ||
onessg -e ejs | ||
onessg ejs | ||
``` | ||
@@ -95,3 +95,3 @@ (Substitute ejs with the name of your template engine) | ||
| ├── _defaults.yaml | ||
| └── page-one.html | ||
| └── page-one.md | ||
├── layouts/ | ||
@@ -113,3 +113,3 @@ | └── page.ejs | ||
<body> | ||
<!-- Your HTML --> | ||
<p>Hello World!</p> | ||
</body> | ||
@@ -128,3 +128,3 @@ </html> | ||
| ├── _defaults.yaml | ||
| └── page-one.html | ||
| └── page-one.md | ||
├── layouts/ | ||
@@ -158,3 +158,3 @@ | └── page.ejs | ||
```bash | ||
onessg -e ejs | ||
onessg ejs | ||
``` | ||
@@ -170,3 +170,3 @@ | ||
| ├── _defaults.yaml | ||
| └── page-one.html | ||
| └── page-one.md | ||
├── layouts/ | ||
@@ -202,10 +202,6 @@ | └── page.ejs | ||
``` | ||
onessg -e ejs | ||
onessg <template_engine> | ||
onessg <template_engine> [-s <source_dir>] [-d <output_dir>] [-l <layout_dir>] | ||
Override Defaults: | ||
onessg -e ejs -s source/ -d _output -l templates | ||
Options: | ||
-e, --engine Set the valid consolidate.js template engine to use when | ||
parsing layouts [string] [required] | ||
-s, --src Set the src directory [string] [default: "src/"] | ||
@@ -215,2 +211,10 @@ -d, --dist Set the dist directory [string] [default: "dist/"] | ||
--help Show help [boolean] | ||
--version Show version number [boolean] | ||
Examples: | ||
onessg ejs | ||
onessg ejs -s posts/ -d output/ -l templates/ | ||
A list of supported template engines may be found at: | ||
https://github.com/tj/consolidate.js/#supported-template-engines. | ||
``` | ||
@@ -220,3 +224,3 @@ | ||
Contributions welcome; please discuss before making significant changes. All new features should be tested. Run `npm test` to run the tests. | ||
Contributions welcome; please discuss before making significant changes. All new features should be tested. Run `npm test` to run the tests. You can generate a code coverage report by running `npm run coverage`. The report will be found at `coverage/lcov-report/index.html`. | ||
@@ -223,0 +227,0 @@ For bugs :beetle:, feature requests :bulb:, and questions :speech_balloon:, please file an issue! |
var execSync=require('child_process').execSync; | ||
var exec=require('child_process').exec; | ||
var fs=require('fs-extra'); | ||
var path=require('path'); | ||
var assert=require('assert'); | ||
var replaceExt=require('replace-ext'); | ||
var onessg=require('../index.js'); | ||
assert.file=function (fileName) { | ||
fileName=replaceExt(fileName, '.html'); | ||
var expected=fs.readFileSync(path.join('test/expected', fileName), 'utf8'); | ||
@@ -14,3 +18,3 @@ var actual=fs.readFileSync(path.join('test/dist', fileName), 'utf8'); | ||
// Build with cli: | ||
execSync('./../cli.js -e ejs', {cwd: 'test'}); | ||
execSync('./../cli.js ejs', {cwd: 'test'}); | ||
// Tests: | ||
@@ -28,2 +32,16 @@ suite('plain html', function () { | ||
}); | ||
suite('markdown', function () { | ||
test('empty file', function () { | ||
assert.file('empty-md.md'); | ||
}); | ||
test('text', function () { | ||
assert.file('text-md.md'); | ||
}); | ||
test('advanced markdown', function () { | ||
assert.file('markdown.md'); | ||
}); | ||
test('.markdown extention', function () { | ||
assert.file('text-markdown.markdown'); | ||
}); | ||
}); | ||
suite('layouts & front-matter', function () { | ||
@@ -68,1 +86,25 @@ test('basic layout', function () { | ||
}); | ||
suite('errors', function () { // NOTE: This suite should be run last! | ||
test('invalid src', function (done) { | ||
onessg('ejs', 'noop', 'test/dist', 'test/layouts', function (e) { | ||
done(assert(e)); | ||
}); | ||
}); | ||
test('invalid type for engine', function (done) { | ||
onessg(0, 'test/src', 'test/dist', 'test/layouts', function (e) { | ||
done(assert(e)); | ||
}); | ||
}); | ||
test('unsupported engine', function (done) { | ||
onessg('noop', 'test/src', 'test/dist', 'test/layouts', function (e) { | ||
done(assert(e)); | ||
}); | ||
}); | ||
test('cli returns errors', function (done) { | ||
this.timeout(5000); | ||
this.retries(4); | ||
exec('./../cli.js ejs -s noop', {cwd: 'test'}, function (e) { | ||
return done(assert(e)); | ||
}); | ||
}); | ||
}); |
@@ -8,2 +8,3 @@ # Tests | ||
- `assert.file('text.html');` will throw an error if `dist/text.html` is not equal to `expected/text.html`, failing the test. | ||
- When a markdown file is passed to `assert.file()`, it compares the corresponding HTML files in `dist/` and `expected/`. | ||
@@ -15,1 +16,3 @@ ## Guidelines for writing tests: | ||
- All front-matter and `_defaults` files should be written in yaml, except for the purpose of testing other configuration formats. | ||
- Write basic tests in HTML, not markdown. | ||
- Markdown files should use the `.md` extension, except for the purpose of testing `.markdown` extension support. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39093
56
310
221
9
4
5
2
+ Addedmarked@^0.3.6
+ Addedreplace-ext@^1.0.0
+ Addedmarked@0.3.19(transitive)
+ Addedreplace-ext@1.0.1(transitive)
+ Addedyargs@5.0.0(transitive)
+ Addedyargs-parser@3.2.0(transitive)
- Removedyargs@4.8.1(transitive)
- Removedyargs-parser@2.4.1(transitive)
Updatedyargs@^5.0.0