Skip to content

Commit

Permalink
Merge ce06b50 into 5a3d557
Browse files Browse the repository at this point in the history
  • Loading branch information
Zearin committed Jun 17, 2018
2 parents 5a3d557 + ce06b50 commit 77ffd5b
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 34 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ This document follows the guidelines of [Keep A Changelog](http://keepachangelog
### Added
* ESLint (...finally!) (#305) (@Zearin)


### Removed
* Dependency `absolute` (save ≈21k!) (c05f9e2) (@Zearin)
* **Remove Dependencies:**
- `absolute` (save ≈21k!) (c05f9e2) (@Zearin)
- `is` (save ≈74k!) (7eaac9e2, 54dba0c1) (@Zearin)

### Changed
* When a `Metalsmith` setter receives an invalid argument, it now throws
a `TypeError` instead of an `AssertionError` (but error messages
are unchanged) (@Zearin)

### Updated
Thanks to ESLint, we’ve tidied up Metalsmith’s source code a bit.
Expand All @@ -30,6 +35,8 @@ Thanks to ESLint, we’ve tidied up Metalsmith’s source code a bit.


### Security
* Use `Buffer.from()` in supporting versions of Node.js possible (@Zearin)

**The new `npm audit` command is awesome.** You should use it in your own projects!

#### `npm audit` vulnerability fixes
Expand Down
11 changes: 11 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## License

The MIT License (MIT)

Copyright &copy; Segment \<friends@segment.com\>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 1 addition & 10 deletions Readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,8 @@ You have three options:
3. Use Metalsmith v1.7. Put `"metalsmith": "^1.7.0"` in your `package.json` and `npm install` that version.


## License
## [License](LICENSE.md)

The MIT License (MIT)

Copyright &copy; Segment \<friends@segment.com\>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[npm-badge]: https://img.shields.io/npm/v/metalsmith.svg
[npm-url]: https://www.npmjs.com/package/metalsmith
Expand Down
25 changes: 14 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var isObject = function(o) {return o !== null && typeof o === 'object'}
var isString = function(s) {return typeof s === 'string'}




/**
* Export `Metalsmith`.
*/
Expand Down Expand Up @@ -77,7 +79,7 @@ Metalsmith.prototype.use = function(plugin){

Metalsmith.prototype.directory = function(directory){
if (!arguments.length) return path.resolve(this._directory)
if (!isString(directory)) throw new TypeError('You must pass a directory path string.')
assert(isString(directory), 'You must pass a directory path string.')
this._directory = directory
return this
}
Expand All @@ -91,7 +93,7 @@ Metalsmith.prototype.directory = function(directory){

Metalsmith.prototype.metadata = function(metadata){
if (!arguments.length) return this._metadata
if (!isObject(metadata)) throw new TypeError('You must pass a metadata object.')
assert(isObject(metadata), 'You must pass a metadata object.')
this._metadata = clone(metadata)
return this
}
Expand All @@ -105,7 +107,7 @@ Metalsmith.prototype.metadata = function(metadata){

Metalsmith.prototype.source = function(path){
if (!arguments.length) return this.path(this._source)
if (!isString(path)) throw new TypeError('You must pass a source path string.')
assert(isString(path), 'You must pass a source path string.')
this._source = path
return this
}
Expand All @@ -119,7 +121,7 @@ Metalsmith.prototype.source = function(path){

Metalsmith.prototype.destination = function(path){
if (!arguments.length) return this.path(this._destination)
if (!isString(path)) throw new TypeError('You must pass a destination path string.')
assert(isString(path), 'You must pass a destination path string.')
this._destination = path
return this
}
Expand All @@ -133,7 +135,7 @@ Metalsmith.prototype.destination = function(path){

Metalsmith.prototype.concurrency = function(max){
if (!arguments.length) return this._concurrency
if (!isNumber(max)) throw new TypeError('You must pass a number for concurrency.')
assert(isNumber(max), 'You must pass a number for concurrency.')
this._concurrency = max
return this
}
Expand All @@ -146,7 +148,7 @@ Metalsmith.prototype.concurrency = function(max){
*/
Metalsmith.prototype.clean = function(clean){
if (!arguments.length) return this._clean
if (!isBoolean(clean)) throw new TypeError('You must pass a boolean.')
assert(isBoolean(clean), 'You must pass a boolean.')
this._clean = clean
return this
}
Expand All @@ -160,7 +162,7 @@ Metalsmith.prototype.clean = function(clean){

Metalsmith.prototype.frontmatter = function(frontmatter){
if (!arguments.length) return this._frontmatter
if (!isBoolean(frontmatter)) throw new TypeError('You must pass a boolean.')
assert(isBoolean(frontmatter), 'You must pass a boolean.')
this._frontmatter = frontmatter
return this
}
Expand Down Expand Up @@ -295,13 +297,14 @@ Metalsmith.prototype.readFile = unyield(function*(file){
err.code = 'invalid_frontmatter'
throw err
}

ret = parsed.data
// Use `Buffer.from` whenever possible
// Use `Buffer.from` if possible.
//
// See:
// - https://nodejs.org/api/buffer.html#buffer_new_buffer_string_encoding
// - https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding
ret.contents = (Buffer.hasOwnProperty('from'))
//
ret = parsed.data
ret.contents = (Buffer.hasOwnProperty('from'))
? Buffer.from(parsed.content)
: new Buffer(parsed.content)
} else {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/read-invalid-frontmatter/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---whatever
abc: xyz
---
Body
1 change: 1 addition & 0 deletions test/fixtures/write-file/expected/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body
109 changes: 98 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ var path = require('path')
var rm = require('rimraf').sync
var fixture = path.resolve.bind(path, __dirname, 'fixtures')


/* Use `Buffer.from` whenever possible.
* See:
* - https://nodejs.org/api/buffer.html#buffer_new_buffer_string_encoding
* - https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding
*/
function getBuffer(arg) {
if (Buffer.hasOwnProperty('from')){
return Buffer.from(arg)
}
return new Buffer(arg)
}

describe('Metalsmith', function(){
beforeEach(function(){
rm('test/tmp')
Expand Down Expand Up @@ -59,6 +72,12 @@ describe('Metalsmith', function(){
m.ignore('dirfile')
assert(1 == m.ignores.length)
})

it('should return the ignore list without arguments', function() {
var m = Metalsmith('test/tmp')
m.ignore('dirfile')
assert(m.ignore()[0] === 'dirfile')
})
})

describe('#directory', function(){
Expand Down Expand Up @@ -156,6 +175,18 @@ describe('Metalsmith', function(){
var m = Metalsmith('test/tmp')
assert.equal(m.concurrency(), Infinity)
})

it('should error on non-number', function() {
var m = Metalsmith('test/tmp')
var badArgs = [NaN, 'hi', process, false, '1', '2', '3']
badArgs.forEach( function(bad) {
assert.throws(function() {
m.concurrency(bad),
TypeError
})
})
})

})

describe('#clean', function(){
Expand Down Expand Up @@ -237,7 +268,7 @@ describe('Metalsmith', function(){
assert.deepEqual(files, {
'index.md': {
title: 'A Title',
contents: new Buffer('body'),
contents: getBuffer('body'),
mode: stats.mode.toString(8).slice(-4),
stats: stats
}
Expand All @@ -256,7 +287,7 @@ describe('Metalsmith', function(){
assert.deepEqual(files, {
'dir/index.md': {
title: 'A Title',
contents: new Buffer('body'),
contents: getBuffer('body'),
mode: stats.mode.toString(8).slice(-4),
stats: stats
}
Expand All @@ -274,7 +305,7 @@ describe('Metalsmith', function(){
assert.deepEqual(files, {
'index.md': {
title: 'A Title',
contents: new Buffer('body'),
contents: getBuffer('body'),
mode: stats.mode.toString(8).slice(-4),
stats: stats
}
Expand All @@ -290,7 +321,7 @@ describe('Metalsmith', function(){
if (err) return done(err)
assert.deepEqual(files, {
'bin': {
contents: new Buffer('echo test'),
contents: getBuffer('echo test'),
mode: stats.mode.toString(8).slice(-4),
stats: stats
}
Expand Down Expand Up @@ -338,7 +369,7 @@ describe('Metalsmith', function(){
'index.md': {
date: new Date('2013-12-02'),
title: 'A Title',
contents: new Buffer('body'),
contents: getBuffer('body'),
mode: stats.mode.toString(8).slice(-4),
stats: stats
}
Expand All @@ -360,7 +391,7 @@ describe('Metalsmith', function(){
'index.md': {
date: new Date('2013-12-02'),
title: 'A Title',
contents: new Buffer('body'),
contents: getBuffer('body'),
mode: stats.mode.toString(8).slice(-4),
stats: stats
}
Expand All @@ -370,10 +401,46 @@ describe('Metalsmith', function(){
})
})

describe('#readFile', function() {

it('should read non-absolute files', function(done) {
var m = Metalsmith(fixture('read'))
var stats = fs.statSync(fixture('read/src/index.md'))
var expected = {
title: 'A Title',
contents: getBuffer('body'),
mode: stats.mode.toString(8).slice(-4),
stats: stats
}
m.readFile('index.md', function(err, file) {
if (err) return done(err)
assert.deepEqual(file, expected)
done()
})
})

it('should error when reading invalid frontmatter', function(done) {
var m = Metalsmith(fixture('read-invalid-frontmatter'))
m.frontmatter(true)
m.readFile('index.md', function(err, file) {
if (err) {
assert(err instanceof Error)
assert.throws(
function () { throw err },
/invalid frontmatter/i
)
return file
}
throw new Error('This should not execute!')
})
done()
})
})

describe('#write', function(){
it('should write to a destination directory', function(done){
var m = Metalsmith(fixture('write'))
var files = { 'index.md': { contents: new Buffer('body') }}
var files = { 'index.md': { contents: getBuffer('body') }}
m.write(files, function(err){
if (err) return done(err)
equal(fixture('write/build'), fixture('write/expected'))
Expand All @@ -383,7 +450,7 @@ describe('Metalsmith', function(){

it('should write to a provided directory', function(done){
var m = Metalsmith(fixture('write-dir'))
var files = { 'index.md': { contents: new Buffer('body') }}
var files = { 'index.md': { contents: getBuffer('body') }}
var dir = fixture('write-dir/out')
m.write(files, dir, function(err){
if (err) return done(err)
Expand All @@ -398,7 +465,7 @@ describe('Metalsmith', function(){
var m = Metalsmith(fixture('write-mode'))
var files = {
'bin': {
contents: new Buffer('echo test'),
contents: getBuffer('echo test'),
mode: '0777'
}
}
Expand All @@ -424,6 +491,26 @@ describe('Metalsmith', function(){
})
})

describe('#writeFile', function() {
it('should write non-absolute files', function(done) {
var m = Metalsmith(fixture('write-file'))
var file = 'index.md'
var data = { contents: getBuffer('body') }

var expected = fixture('write-file/expected')

m.writeFile(file, data, function(err) {
if (err) return done(err)
equal(fixture('write-file/build'), expected)
assert.equal(
fs.readFileSync(fixture('write-file/build/index.md'), 'utf8'),
fs.readFileSync(fixture('write-file/expected/index.md'), 'utf8')
)
done()
})
})
})

describe('#run', function(){
it('should apply a plugin', function(done){
var m = Metalsmith('test/tmp')
Expand Down Expand Up @@ -494,7 +581,7 @@ describe('Metalsmith', function(){
.use(function(files, metalsmith, done){
Object.keys(files).forEach(function(file){
var data = files[file]
data.contents = new Buffer(data.title)
data.contents = getBuffer(data.title)
})
done()
})
Expand Down Expand Up @@ -539,7 +626,7 @@ describe('Metalsmith', function(){
.use(function(files, metalsmith, done){
Object.keys(files).forEach(function(file){
var data = files[file]
data.contents = new Buffer(data.title)
data.contents = getBuffer(data.title)
})
done()
})
Expand Down

0 comments on commit 77ffd5b

Please sign in to comment.