Comparing version 1.1.0 to 1.2.0
@@ -17,1 +17,2 @@ # Underscore Reference | ||
- `_body` This is the main contents of the file (the part outside the front-matter). | ||
- `_path` This is the path of the HTML or Markdown file, relative to `src/`. Note that extensions are preserved, i.e. `index.md` is a possible value. |
31
index.js
'use strict'; | ||
var p = require('thenify'); | ||
var fs = require('fs-extra'); | ||
var pfs = { | ||
access: p(fs.access), | ||
outputFile: p(fs.outputFile), | ||
readFile: p(fs.readFile), | ||
}; | ||
var path = require('path-extra'); | ||
@@ -47,9 +52,7 @@ var globby = require('globby'); | ||
// Else, run through middleware: | ||
return middleware(data, path.extname(filePath)) | ||
return middleware(data) | ||
.then(getDefaults) | ||
.then(function (data) { | ||
return getDefaults(data, filePath); | ||
}) | ||
.then(function (data) { | ||
// If _layout, render it: | ||
if (data._layout) return render(data, filePath); | ||
if (data._layout) return render(data); | ||
// Else, return _body: | ||
@@ -62,3 +65,3 @@ else return data._body; | ||
// Output using fs-extra: | ||
return p(fs.outputFile)(writePath, html); | ||
return pfs.outputFile(writePath, html); | ||
}); | ||
@@ -72,3 +75,3 @@ }); | ||
function loadFile(name) { | ||
return p(fs.readFile)(path.join(src, name), 'utf8') | ||
return pfs.readFile(path.join(src, name), 'utf8') | ||
.then(function (text) { | ||
@@ -80,2 +83,3 @@ return matter(text); | ||
data._body = file.content; | ||
data._path = name; | ||
return data; | ||
@@ -87,5 +91,5 @@ }); | ||
// Returns Promise(data object) | ||
function middleware(data, ext) { | ||
function middleware(data) { | ||
// Check path's ext: | ||
switch (ext) { | ||
switch (path.extname(data._path)) { | ||
case '.html': | ||
@@ -115,3 +119,3 @@ // noop: | ||
// Render with consolidate.js: | ||
return p(cons[engine])(layout, data); | ||
return engine(layout, data); | ||
}); | ||
@@ -126,7 +130,6 @@ } | ||
function setConf(eng, conf) { | ||
var access = p(fs.access); | ||
// Check that src & layouts exists: | ||
return Promise.all([ | ||
access(conf.src), | ||
access(conf.layouts), | ||
pfs.access(conf.src), | ||
pfs.access(conf.layouts), | ||
new Promise(function (resolve) { | ||
@@ -145,3 +148,3 @@ // Check that engine is a string: | ||
layouts = conf.layouts; | ||
engine = eng; | ||
engine = p(cons[eng]); | ||
devMode = conf.devMode; | ||
@@ -148,0 +151,0 @@ // setConf in helper modules: |
'use strict'; | ||
var p = require('thenify'); | ||
var fs = require('fs-extra'); | ||
var readFile = require('thenify')(require('fs-extra').readFile); | ||
var path = require('path-extra'); | ||
var globby = require('globby'); | ||
var yaml = require('js-yaml'); | ||
var cache = {}; | ||
// Only load a subset of lodash: | ||
@@ -13,10 +11,10 @@ var _ = { | ||
}; | ||
var cache = {}; | ||
// Config vars: | ||
var src; | ||
module.exports = function getDefaults(data, filePath) { | ||
module.exports = function (data) { | ||
// Must have src/ in the path so we know when to stop: | ||
var dirPath = path.join(src, path.dirname(filePath)); | ||
var arr = [load(dirPath)]; | ||
recurse(dirPath); | ||
return Promise.all(arr) | ||
return recurse(path.join(src, data._path)) | ||
.then(function (arr) { | ||
@@ -28,40 +26,26 @@ // Combine defaults: | ||
}); | ||
function recurse(dirPath) { | ||
// If we have reached src/, stop: | ||
if (path.normalizeTrim(dirPath) === path.normalizeTrim(src)) return; | ||
else { | ||
let newPath = path.dirname(dirPath); | ||
arr.push(load(newPath)); | ||
return recurse(newPath); | ||
} | ||
} | ||
}; | ||
module.exports.setConf = function (conf) { | ||
src = conf.src; | ||
}; | ||
function recurse (p) { | ||
var arr = []; | ||
do { | ||
p = path.dirname(p); | ||
arr.push(load(p)); | ||
} while (path.normalizeTrim(p) !== path.normalizeTrim(src)); | ||
return Promise.all(arr); | ||
} | ||
function load(dirPath) { | ||
if (cache[dirPath]) return cache[dirPath]; | ||
else { | ||
var val = loadFromFile(dirPath); | ||
cache[dirPath] = val; | ||
return val; | ||
} | ||
} | ||
function loadFromFile(dirPath) { | ||
return globby(path.join(dirPath, '_defaults.*')) | ||
return cache[dirPath] = globby(path.join(dirPath, '_defaults.@(yaml|yml|json)')) | ||
.then(function (res) { | ||
var defaults = {}; | ||
if (!res[0]) return defaults; | ||
switch (path.extname(res[0])) { | ||
case '.yaml': | ||
case '.yml': | ||
defaults = p(fs.readFile)(res[0], 'utf8').then(d => yaml.safeLoad(d)); | ||
break; | ||
case '.json': | ||
defaults = p(fs.readJson)(res[0]); | ||
} | ||
defaults = readFile(res[0], 'utf8').then(d => yaml.safeLoad(d)); | ||
return defaults; | ||
}); | ||
} | ||
module.exports.setConf = function (conf) { | ||
src = conf.src; | ||
}; |
{ | ||
"name": "onessg", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "The Static Site Generator that does only one thing: compile your html and markdown.", | ||
@@ -48,7 +48,7 @@ "main": "index.js", | ||
"ejs": "^2.5.1", | ||
"eslint": "~3.10.0", | ||
"eslint": "~3.13.0", | ||
"mocha": "^3.0.2", | ||
"nyc": "^8.1.0", | ||
"nyc": "^10.0.0", | ||
"suppose": "^0.6.1" | ||
} | ||
} |
@@ -28,3 +28,4 @@ /* eslint no-console: "off" */ | ||
devMode: devMode, | ||
}, function () { | ||
}, function (err) { | ||
if (err) return done(err); | ||
// Assert that dist/ & expected/ are equal: | ||
@@ -141,2 +142,5 @@ try { | ||
}); | ||
test('_path is set automatically', function (done) { | ||
assert.fixture('_path', done); | ||
}); | ||
}); | ||
@@ -143,0 +147,0 @@ suite('_defaults file', function () { |
Sorry, the diff of this file is not supported yet
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
65372
105
0
483