metalsmith
Advanced tools
Comparing version 0.11.0 to 1.0.0
1.0.0 - September 29, 2014 | ||
-------------------------- | ||
* add generator support for node 0.11 | ||
* change `#join` to `#path` and use `path.resolve` | ||
* add support for absolute `source` and `directory` paths | ||
* add `#directory` getter and setter method | ||
* add `#readFile` method to expose the core reading logic | ||
* add `#writeFile` method to expose the core writing logic | ||
* fix default `clean` setting when running the cli | ||
0.11.0 - September 12, 2014 | ||
@@ -3,0 +13,0 @@ --------------------------- |
207
lib/index.js
var assert = require('assert'); | ||
var clone = require('clone'); | ||
var each = require('async').each; | ||
var front = require('front-matter'); | ||
var fs = require('fs-extra'); | ||
var fs = require('co-fs-extra'); | ||
var Mode = require('stat-mode'); | ||
var noop = function(){}; | ||
var path = require('path'); | ||
var readdir = require('recursive-readdir'); | ||
var rm = require('rimraf').sync; | ||
var rm = require('rimraf'); | ||
var thunkify = require('thunkify'); | ||
var unyield = require('unyield'); | ||
var utf8 = require('is-utf8'); | ||
@@ -15,18 +16,26 @@ var Ware = require('ware'); | ||
/** | ||
* Expose `Metalsmith`. | ||
* Thunks. | ||
*/ | ||
readdir = thunkify(readdir); | ||
rm = thunkify(rm); | ||
/** | ||
* Export `Metalsmith`. | ||
*/ | ||
module.exports = Metalsmith; | ||
/** | ||
* Initialize a new `Metalsmith` builder with a working `dir`. | ||
* Initialize a new `Metalsmith` builder with a working `directory`. | ||
* | ||
* @param {String} dir | ||
* @param {String} directory | ||
*/ | ||
function Metalsmith(dir){ | ||
if (!(this instanceof Metalsmith)) return new Metalsmith(dir); | ||
this.dir = path.resolve(dir); | ||
function Metalsmith(directory){ | ||
if (!(this instanceof Metalsmith)) return new Metalsmith(directory); | ||
assert(directory, 'You must pass a working directory path.'); | ||
this.ware = new Ware(); | ||
this.data = {}; | ||
this.directory(directory); | ||
this.metadata({}); | ||
this.source('src'); | ||
@@ -51,2 +60,15 @@ this.destination('build'); | ||
/** | ||
* Get or set the working `directory`. | ||
* | ||
* @param {Object} directory | ||
* @return {Object or Metalsmith} | ||
*/ | ||
Metalsmith.prototype.directory = function(directory){ | ||
if (!arguments.length) return path.resolve(this._directory); | ||
this._directory = directory; | ||
return this; | ||
}; | ||
/** | ||
* Get or set the global `metadata` to pass to templates. | ||
@@ -59,4 +81,4 @@ * | ||
Metalsmith.prototype.metadata = function(metadata){ | ||
if (!arguments.length) return this.data; | ||
this.data = clone(metadata); | ||
if (!arguments.length) return this._metadata; | ||
this._metadata = clone(metadata); | ||
return this; | ||
@@ -73,4 +95,4 @@ }; | ||
Metalsmith.prototype.source = function(path){ | ||
if (!arguments.length) return this.join(this._src); | ||
this._src = path; | ||
if (!arguments.length) return this.path(this._source); | ||
this._source = path; | ||
return this; | ||
@@ -87,4 +109,4 @@ }; | ||
Metalsmith.prototype.destination = function(path){ | ||
if (!arguments.length) return this.join(this._dest); | ||
this._dest = path; | ||
if (!arguments.length) return this.path(this._destination); | ||
this._destination = path; | ||
return this; | ||
@@ -101,3 +123,3 @@ }; | ||
if (!arguments.length) return this._clean; | ||
this._clean = clean; | ||
this._clean = !! clean; | ||
return this; | ||
@@ -115,3 +137,3 @@ }; | ||
if (!arguments.length) return this._frontmatter; | ||
this._frontmatter = frontmatter; | ||
this._frontmatter = !! frontmatter; | ||
return this; | ||
@@ -121,12 +143,12 @@ }; | ||
/** | ||
* Join path `strs` with the working directory. | ||
* Resolve `paths` relative to the root directory. | ||
* | ||
* @param {String} strs... | ||
* @param {String} paths... | ||
* @return {String} | ||
*/ | ||
Metalsmith.prototype.join = function(){ | ||
var strs = [].slice.call(arguments); | ||
strs.unshift(this.dir); | ||
return path.join.apply(path, strs); | ||
Metalsmith.prototype.path = function(){ | ||
var paths = [].slice.call(arguments); | ||
paths.unshift(this.directory()); | ||
return path.resolve.apply(path, paths); | ||
}; | ||
@@ -137,25 +159,16 @@ | ||
* | ||
* @param {Function} fn | ||
* @return {Object} | ||
*/ | ||
Metalsmith.prototype.build = function(fn){ | ||
fn = fn || noop; | ||
var self = this; | ||
Metalsmith.prototype.build = unyield(function*(){ | ||
var clean = this.clean(); | ||
var dest = this.destination(); | ||
var clean = this.clean(); | ||
if (clean) yield rm(dest); | ||
if (clean) rm(dest); | ||
var files = yield this.read(); | ||
files = yield this.run(files); | ||
yield this.write(files); | ||
return files; | ||
}); | ||
this.read(function(err, files){ | ||
if (err) return fn(err); | ||
self.run(files, function(err, files){ | ||
if (err) return fn(err); | ||
self.write(files, function(err){ | ||
fn(err, files); | ||
}); | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -165,8 +178,10 @@ * Run a set of `files` through the middleware stack. | ||
* @param {Object} files | ||
* @param {Function} fn | ||
* @return {Object} | ||
*/ | ||
Metalsmith.prototype.run = function(files, fn){ | ||
this.ware.run(files, this, fn); | ||
}; | ||
Metalsmith.prototype.run = unyield(function*(files){ | ||
var run = thunkify(this.ware.run.bind(this.ware)); | ||
var res = yield run(files, this); | ||
return res[0]; | ||
}); | ||
@@ -176,43 +191,44 @@ /** | ||
* | ||
* @param {Function} fn | ||
* @api private | ||
* @return {Object} | ||
*/ | ||
Metalsmith.prototype.read = function(fn){ | ||
var files = {}; | ||
Metalsmith.prototype.read = unyield(function*(){ | ||
var src = this.source(); | ||
var parse = this.frontmatter(); | ||
var read = this.readFile.bind(this); | ||
var paths = yield readdir(src); | ||
var files = yield paths.map(read); | ||
readdir(src, function(err, arr){ | ||
if (err) return fn(err); | ||
each(arr, read, function(err){ | ||
fn(err, files); | ||
}); | ||
}); | ||
return paths.reduce(function(memo, file, i){ | ||
file = path.relative(src, file); | ||
memo[file] = files[i]; | ||
return memo; | ||
}, {}); | ||
}); | ||
function read(file, done){ | ||
var name = path.relative(src, file); | ||
fs.stat(file, function(err, stats){ | ||
if (err) return done(err); | ||
fs.readFile(file, function(err, buffer){ | ||
if (err) return done(err); | ||
var file = {}; | ||
/** | ||
* Read a single file by file `path`. | ||
* | ||
* @param {String} path | ||
* @return {Object} | ||
*/ | ||
if (parse && utf8(buffer)) { | ||
var parsed = front(buffer.toString()); | ||
file = parsed.attributes; | ||
file.contents = new Buffer(parsed.body); | ||
} else { | ||
file.contents = buffer; | ||
} | ||
Metalsmith.prototype.readFile = unyield(function*(path){ | ||
var frontmatter = this.frontmatter(); | ||
var stats = yield fs.stat(path); | ||
var buffer = yield fs.readFile(path); | ||
var file = {}; | ||
file.mode = Mode(stats).toOctal(); | ||
file.stats = stats; | ||
files[name] = file; | ||
done(); | ||
}); | ||
}); | ||
if (frontmatter && utf8(buffer)) { | ||
var parsed = front(buffer.toString()); | ||
file = parsed.attributes; | ||
file.contents = new Buffer(parsed.body); | ||
} else { | ||
file.contents = buffer; | ||
} | ||
}; | ||
file.mode = Mode(stats).toOctal(); | ||
file.stats = stats; | ||
return file; | ||
}); | ||
/** | ||
@@ -222,20 +238,23 @@ * Write a dictionary of `files` to the dest directory. | ||
* @param {Object} files | ||
* @param {Function} fn | ||
* @api private | ||
*/ | ||
Metalsmith.prototype.write = function(files, fn){ | ||
var dest = this.destination(); | ||
Metalsmith.prototype.write = unyield(function*(files){ | ||
var write = this.writeFile.bind(this); | ||
yield Object.keys(files).map(function(file){ | ||
return write(file, files[file]); | ||
}); | ||
}); | ||
each(Object.keys(files), write, fn); | ||
/** | ||
* Write a `file` with `data` to the destination directory. | ||
* | ||
* @param {String} file | ||
* @param {Object} data | ||
*/ | ||
function write(file, done){ | ||
var data = files[file]; | ||
var out = path.join(dest, file); | ||
return fs.outputFile(out, data.contents, function(err){ | ||
if (err) done(err); | ||
if (!data.mode) return done(); | ||
fs.chmod(out, data.mode, done); | ||
}); | ||
} | ||
}; | ||
Metalsmith.prototype.writeFile = unyield(function*(file, data){ | ||
var dest = this.destination(); | ||
var out = path.resolve(dest, file); | ||
yield fs.outputFile(out, data.contents); | ||
if (data.mode) yield fs.chmod(out, data.mode); | ||
}); |
{ | ||
"name": "metalsmith", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"repository": "git://github.com/segmentio/metalsmith.git", | ||
"version": "0.11.0", | ||
"license": "MIT", | ||
"description": "An extremely simple, pluggable static site generator.", | ||
"keywords": [ | ||
"static", | ||
"file", | ||
"site", | ||
"website", | ||
"blog", | ||
"generator", | ||
"markdown", | ||
"jekyll", | ||
"wintersmith", | ||
"blacksmith" | ||
], | ||
"main": "lib/index.js", | ||
"bin": { | ||
@@ -30,2 +17,3 @@ "metalsmith": "bin/metalsmith" | ||
"clone": "^0.1.11", | ||
"co-fs-extra": "0.0.2", | ||
"front-matter": "~0.2.0", | ||
@@ -37,11 +25,26 @@ "fs-extra": "~0.10.0", | ||
"stat-mode": "^0.2.0", | ||
"ware": "~0.3.0" | ||
"thunkify": "^2.1.2", | ||
"unyield": "0.0.1", | ||
"ware": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1.x", | ||
"fs-readdir-recursive": "0.0.2", | ||
"assert-dir-equal": "~1.0.1", | ||
"co-mocha": "^1.0.1", | ||
"gnode": "^0.1.0", | ||
"metalsmith-drafts": "0.0.1", | ||
"metalsmith-markdown": "0.2.1" | ||
} | ||
"metalsmith-markdown": "0.2.1", | ||
"mocha": "^1.21.4" | ||
}, | ||
"keywords": [ | ||
"static", | ||
"file", | ||
"site", | ||
"website", | ||
"blog", | ||
"generator", | ||
"markdown", | ||
"jekyll", | ||
"wintersmith", | ||
"blacksmith" | ||
] | ||
} |
Sorry, the diff of this file is not supported yet
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
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
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17698
8
218
1
1
13
6
1
+ Addedco-fs-extra@0.0.2
+ Addedthunkify@^2.1.2
+ Addedunyield@0.0.1
+ Addedco@3.1.0(transitive)
+ Addedco-from-stream@0.0.0(transitive)
+ Addedco-fs@1.2.0(transitive)
+ Addedco-fs-extra@0.0.2(transitive)
+ Addedco-read@0.0.1(transitive)
+ Addedfs-extra@0.12.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@2.4.0(transitive)
+ Addedncp@0.6.0(transitive)
+ Addedthunkify@0.0.12.1.2(transitive)
+ Addedunyield@0.0.1(transitive)
+ Addedware@1.3.0(transitive)
+ Addedwrap-fn@0.1.5(transitive)
- Removedware@0.3.0(transitive)
Updatedware@^1.2.0