From a7619551a87561833d239fdb2e1b9f9afee33692 Mon Sep 17 00:00:00 2001 From: Werner Glinka Date: Mon, 7 Feb 2022 16:02:34 -0600 Subject: [PATCH] run format and changelog --- CHANGELOG.md | 22 +++ Readme.md | 25 +-- lib/index.js | 156 +++++++++-------- .../deep-nested/src/path/path/data.yaml | 1 - tests/OLD-fixtures/duplicate/src/data.json | 3 - tests/OLD-fixtures/duplicate/src/data2.json | 3 - tests/OLD-fixtures/json/src/data.json | 3 - tests/OLD-fixtures/malformed/src/data.json | 3 - tests/OLD-fixtures/nested/src/path/data.yaml | 1 - tests/OLD-fixtures/yaml/src/data.yaml | 1 - tests/OLD-index.js | 91 ---------- .../build/data/folder-test/etiam.json | 4 +- .../build/data/folder-test/fringilla.json | 4 +- .../build/data/folder-test/sollicitudin.json | 4 +- tests/fixtures/build/data/json-test.json | 2 +- tests/fixtures/external/ext-json-test.json | 2 +- .../fixtures/external/folder-test/etiam.json | 4 +- .../external/folder-test/fringilla.json | 4 +- .../external/folder-test/sollicitudin.json | 4 +- .../fixtures/src/data/folder-test/etiam.json | 4 +- .../src/data/folder-test/fringilla.json | 4 +- .../src/data/folder-test/sollicitudin.json | 4 +- tests/fixtures/src/data/json-test.json | 2 +- tests/index.js | 161 ++++++++++-------- 24 files changed, 231 insertions(+), 281 deletions(-) create mode 100644 CHANGELOG.md delete mode 100644 tests/OLD-fixtures/deep-nested/src/path/path/data.yaml delete mode 100644 tests/OLD-fixtures/duplicate/src/data.json delete mode 100644 tests/OLD-fixtures/duplicate/src/data2.json delete mode 100644 tests/OLD-fixtures/json/src/data.json delete mode 100644 tests/OLD-fixtures/malformed/src/data.json delete mode 100644 tests/OLD-fixtures/nested/src/path/data.yaml delete mode 100644 tests/OLD-fixtures/yaml/src/data.yaml delete mode 100644 tests/OLD-index.js diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..192eee4 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +### Changelog + +All notable changes to this project will be documented in this file. Dates are displayed in UTC. + +Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). + +#### Unreleased + +- Nested config files [`#19`](https://github.com/wernerglinka/metalsmith-metadata/pull/19) +- Don't attempt to parse a file if the key already exists in the metadata, even if the file is missing [`#9`](https://github.com/wernerglinka/metalsmith-metadata/pull/9) +- Revert 9e2224d2e0dd99661f2174f788a337f1add9d823 [`#16`](https://github.com/wernerglinka/metalsmith-metadata/pull/16) +- Normalizing path options for Windows compatibility. [`#7`](https://github.com/wernerglinka/metalsmith-metadata/pull/7) +- Update dependencies [`#8`](https://github.com/wernerglinka/metalsmith-metadata/pull/8) +- Allow .yml as file extension for YAML as well [`#12`](https://github.com/wernerglinka/metalsmith-metadata/pull/12) +- added tests [`e6ad84a`](https://github.com/wernerglinka/metalsmith-metadata/commit/e6ad84a533c1658ac749b35887ad73fb9a5f6ac3) +- first commit [`48bcefc`](https://github.com/wernerglinka/metalsmith-metadata/commit/48bcefcb049d896158aedd340149417a600d4c4c) +- don't try and parse if the key already exists in the metadata... [`c3353d8`](https://github.com/wernerglinka/metalsmith-metadata/commit/c3353d83f8eed5645e9178e5556c6dabf9d23e4e) +- Add tests for nested and deep-nested path [`e525a11`](https://github.com/wernerglinka/metalsmith-metadata/commit/e525a11bce359214afab15d3fa5ddedea88eadcb) +- more tests [`6b0cf69`](https://github.com/wernerglinka/metalsmith-metadata/commit/6b0cf696cea1321348fe5dd3d9c0b5f9f5043762) +- Add regex for normalizing path [`c9cf34a`](https://github.com/wernerglinka/metalsmith-metadata/commit/c9cf34a199a42f0449647729bb7f4010c2c2470f) +- bump version number [`cc8b115`](https://github.com/wernerglinka/metalsmith-metadata/commit/cc8b11597719811f8fd7043d75d549e6903d6eb1) +- fix test name [`965a9b5`](https://github.com/wernerglinka/metalsmith-metadata/commit/965a9b5d6d5d1f0b7a54381c0719c0a404b2f3dc) diff --git a/Readme.md b/Readme.md index d201bf0..960ab1a 100644 --- a/Readme.md +++ b/Readme.md @@ -1,9 +1,8 @@ - # metalsmith-metadata - A metalsmith plugin to load global metadata from files. +A metalsmith plugin to load global metadata from files. - Supports `.json` and `.yaml` data. +Supports `.json` and `.yaml` data. ## Installation @@ -11,7 +10,7 @@ ## CLI Usage - Install via npm and then add the `metalsmith-metadata` key to your `metalsmith.json` plugins. Each key in the dictionary of options will be the key mixed into the global metadata, like so: +Install via npm and then add the `metalsmith-metadata` key to your `metalsmith.json` plugins. Each key in the dictionary of options will be the key mixed into the global metadata, like so: ```json { @@ -26,17 +25,19 @@ ## Javascript Usage - Pass the options to `Metalsmith#use`: +Pass the options to `Metalsmith#use`: ```js -var metadata = require('metalsmith-metadata'); - -metalsmith.use(metadata({ - authors: './path/to/authors.json', - categories: './path/to/categories.yaml' -})); +var metadata = require("metalsmith-metadata"); + +metalsmith.use( + metadata({ + authors: "./path/to/authors.json", + categories: "./path/to/categories.yaml", + }) +); ``` ## License - MIT \ No newline at end of file +MIT diff --git a/lib/index.js b/lib/index.js index 97d3004..701fe86 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,12 @@ -'use strict'; +"use strict"; -const {promises: {readFile, readdir}} = require('fs'); -const path = require('path'); +const { + promises: { readFile, readdir }, +} = require("fs"); +const path = require("path"); const extension = path.extname; -const yaml = require('js-yaml'); -const toml = require('toml'); - +const yaml = require("js-yaml"); +const toml = require("toml"); /** * @typedef Options @@ -13,14 +14,14 @@ const toml = require('toml'); */ /** @type {Options} */ -const defaults = {} +const defaults = {}; /** * Normalize plugin options * @param {Options} [options] * @returns {Object} */ - function normalizeOptions(options) { +function normalizeOptions(options) { return Object.assign({}, defaults, options || {}); } @@ -53,23 +54,23 @@ function tomlToJSON(string) { /** * getExternalFile * Reads file content in either .json, .yaml, .yml or .toml format - * @param {*} filePath + * @param {*} filePath * @returns Content of the file in .json */ async function getExternalFile(filePath) { const fileExtension = extension(filePath); const fileBuffer = await readFile(filePath); let fileContent; - + switch (fileExtension) { - case ".yaml" : - case ".yml" : + case ".yaml": + case ".yml": fileContent = yamlToJSON(fileBuffer); break; - case ".toml" : + case ".toml": fileContent = tomlToJSON(fileBuffer); break; - case ".json" : + case ".json": fileContent = JSON.parse(fileBuffer.toString()); // remove line breaks etc from the filebuffer break; default: @@ -77,83 +78,78 @@ async function getExternalFile(filePath) { } return fileContent; -}; +} /** * getDirectoryFiles - * @param {*} directoryPath + * @param {*} directoryPath * @returns List of all files in the directory */ async function getDirectoryFiles(directoryPath) { - const fileList = await readdir(directoryPath); + const fileList = await readdir(directoryPath); return await getDirectoryFilesContent(directoryPath, fileList); - -}; +} /** * getDirectoryFilesContent - * @param {*} directoryPath - * @param {*} fileList + * @param {*} directoryPath + * @param {*} fileList * @returns The content of all files in a directory */ async function getDirectoryFilesContent(directoryPath, fileList) { - const fileContent = await fileList.map(async file => { - return await getExternalFile(path.join(path.join(directoryPath, file))); + const fileContent = await fileList.map(async (file) => { + return await getExternalFile(path.join(path.join(directoryPath, file))); }); return await Promise.all(fileContent); -}; +} /** * getFileObject - * @param {*} filePath - * @param {*} optionKey - * @param {*} allMetadata + * @param {*} filePath + * @param {*} optionKey + * @param {*} allMetadata * @returns promise to push metafile object to metalsmith metadata object */ async function getFileObject(filePath, optionKey, allMetadata) { - return getExternalFile(filePath) - .then(fileBuffer => { - allMetadata[optionKey] = fileBuffer; - }); + return getExternalFile(filePath).then((fileBuffer) => { + allMetadata[optionKey] = fileBuffer; + }); } /** * getDirectoryObject - * @param {*} directoryPath - * @param {*} optionKey - * @param {*} allMetadata + * @param {*} directoryPath + * @param {*} optionKey + * @param {*} allMetadata * @returns promise to push concatenated metafile object of all directory files to metalsmith metadata object */ async function getDirectoryObject(directoryPath, optionKey, allMetadata) { return getDirectoryFiles(directoryPath) - .then(fileBuffers => { + .then((fileBuffers) => { const groupMetadata = []; - fileBuffers.forEach(fileBuffer => { - groupMetadata.push(JSON.parse(JSON.stringify(fileBuffer))); - }) + fileBuffers.forEach((fileBuffer) => { + groupMetadata.push(JSON.parse(JSON.stringify(fileBuffer))); + }); if (groupMetadata.length) { allMetadata[optionKey] = groupMetadata; - } - else { + } else { console.log(`No files found in this directory "${key}"`); } - }) - .catch(error => { + .catch((error) => { console.error(error.message); process.exit(1); }); -}; - +} /** * A Metalsmith plugin to read files with metadata - * + * * Files containing metadata must be located in the Metalsmith root directory. * Content of files located in the Metalsmith source directory (local files) is readily available * in the files object while files outside the source directory (external files) are read fropm disk. - * + * * Files are specified via option entries like: site: "./data/siteMetadata.json" * The resulting meta object will then be something like this: * { @@ -164,19 +160,19 @@ async function getDirectoryObject(directoryPath, optionKey, allMetadata) { * "siteURL":"https://newmsnunjucks.netlify.app/", * ... * } - * + * * Directories may also be specified like this: example: "./data/example". In this case * the plugin will read all files in the directory and concatenate them into a single file object. - * - * + * + * * @param {Options} options * @returns {import('metalsmith').Plugin} */ -function initMetameta(options){ +function initMetameta(options) { options = normalizeOptions(options); - return function metameta(files, metalsmith, done){ + return function metameta(files, metalsmith, done) { const allMetadata = metalsmith.metadata(); // array to hold all active promises during external file reads. Will be @@ -184,18 +180,17 @@ function initMetameta(options){ const allPromises = []; // loop over all options/metadata files/directories - Object.keys(options).forEach(function(optionKey) { - + Object.keys(options).forEach(function (optionKey) { // check if file is located inside the metalsmith source directory const metaFilePath = options[optionKey]; // convention: "./" inside, "../" outside of metasmith source const isLocal = metaFilePath.startsWith("./"); const isExternal = metaFilePath.startsWith("../"); - + // flag to be reset when valid filepath is detected let validFilepath = false; - + /* * if file or directory is local we can get the metadata from the metalsmith file object */ @@ -206,8 +201,13 @@ function initMetameta(options){ // check if the optionKey element has a file exension const fileExtension = extension(metaFilePath); - if ( fileExtension ) { - if ( fileExtension === ".json" || fileExtension === ".yaml" || fileExtension === ".yml" || fileExtension === ".toml") { + if (fileExtension) { + if ( + fileExtension === ".json" || + fileExtension === ".yaml" || + fileExtension === ".yml" || + fileExtension === ".toml" + ) { // get the data from file object try { metadata = files[key].contents.toString(); @@ -216,16 +216,14 @@ function initMetameta(options){ return done(error); } - if ( fileExtension === ".yaml" || fileExtension === ".yml" ) { + if (fileExtension === ".yaml" || fileExtension === ".yml") { metadata = JSON.stringify(yamlToJSON(metadata)); } - if ( fileExtension === ".toml" ) { + if (fileExtension === ".toml") { metadata = JSON.stringify(toml.parse(metadata)); } - - // to temp meta object allMetadata[optionKey] = JSON.parse(metadata); // ... and remove this file from the metalsmith build process @@ -235,10 +233,10 @@ function initMetameta(options){ validFilepath = true; } } else { - // assume this is a directory, all files in this directory will be concatenated into one + // assume this is a directory, all files in this directory will be concatenated into one // metadata object const groupMetadata = []; - Object.keys(files).forEach(function(file) { + Object.keys(files).forEach(function (file) { if (file.includes(key)) { groupMetadata.push(JSON.parse(files[file].contents.toString())); } @@ -246,8 +244,7 @@ function initMetameta(options){ if (groupMetadata.length) { allMetadata[optionKey] = groupMetadata; - } - else { + } else { console.log(`No files found in this directory "${key}"`); } @@ -265,24 +262,37 @@ function initMetameta(options){ // check if the optionKey has a file exension const fileExtension = extension(metaFilePath); - if ( fileExtension ) { - if ( fileExtension === ".json" || fileExtension === ".yaml" || fileExtension === ".yml" || fileExtension === ".toml") { + if (fileExtension) { + if ( + fileExtension === ".json" || + fileExtension === ".yaml" || + fileExtension === ".yml" || + fileExtension === ".toml" + ) { // read external file content and store in metadata object const filePath = path.join(metalsmith._directory, key); - const extFilePromise = getFileObject(filePath, optionKey, allMetadata) + const extFilePromise = getFileObject( + filePath, + optionKey, + allMetadata + ); // add this promise to allPromises array. Will be later used with Promise.allSettled to invoke done() allPromises.push(extFilePromise); // indicate filepath is valid validFilepath = true; - } + } } else { // assume this is a directory // get content of all files in this directory, concatenated into one metadata object const directoryPath = path.join(metalsmith._directory, key); - const extDirectoryPromise = getDirectoryObject(directoryPath, optionKey, allMetadata); - + const extDirectoryPromise = getDirectoryObject( + directoryPath, + optionKey, + allMetadata + ); + // add this promise to allPromises array. Will be later used with Promise.allSettled to invoke done() allPromises.push(extDirectoryPromise); @@ -296,10 +306,10 @@ function initMetameta(options){ done(error); } }); - + // Promise.allSettled is used to invoke done() Promise.allSettled(allPromises).then(() => done()); }; } -module.exports = initMetameta; \ No newline at end of file +module.exports = initMetameta; diff --git a/tests/OLD-fixtures/deep-nested/src/path/path/data.yaml b/tests/OLD-fixtures/deep-nested/src/path/path/data.yaml deleted file mode 100644 index 37f188c..0000000 --- a/tests/OLD-fixtures/deep-nested/src/path/path/data.yaml +++ /dev/null @@ -1 +0,0 @@ -string: 'string' \ No newline at end of file diff --git a/tests/OLD-fixtures/duplicate/src/data.json b/tests/OLD-fixtures/duplicate/src/data.json deleted file mode 100644 index 86f08b1..0000000 --- a/tests/OLD-fixtures/duplicate/src/data.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "string": "string" -} \ No newline at end of file diff --git a/tests/OLD-fixtures/duplicate/src/data2.json b/tests/OLD-fixtures/duplicate/src/data2.json deleted file mode 100644 index 9711098..0000000 --- a/tests/OLD-fixtures/duplicate/src/data2.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "string": "string2" -} \ No newline at end of file diff --git a/tests/OLD-fixtures/json/src/data.json b/tests/OLD-fixtures/json/src/data.json deleted file mode 100644 index 86f08b1..0000000 --- a/tests/OLD-fixtures/json/src/data.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "string": "string" -} \ No newline at end of file diff --git a/tests/OLD-fixtures/malformed/src/data.json b/tests/OLD-fixtures/malformed/src/data.json deleted file mode 100644 index d0defa0..0000000 --- a/tests/OLD-fixtures/malformed/src/data.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "str -} \ No newline at end of file diff --git a/tests/OLD-fixtures/nested/src/path/data.yaml b/tests/OLD-fixtures/nested/src/path/data.yaml deleted file mode 100644 index 37f188c..0000000 --- a/tests/OLD-fixtures/nested/src/path/data.yaml +++ /dev/null @@ -1 +0,0 @@ -string: 'string' \ No newline at end of file diff --git a/tests/OLD-fixtures/yaml/src/data.yaml b/tests/OLD-fixtures/yaml/src/data.yaml deleted file mode 100644 index 37f188c..0000000 --- a/tests/OLD-fixtures/yaml/src/data.yaml +++ /dev/null @@ -1 +0,0 @@ -string: 'string' \ No newline at end of file diff --git a/tests/OLD-index.js b/tests/OLD-index.js deleted file mode 100644 index d6fe9ff..0000000 --- a/tests/OLD-index.js +++ /dev/null @@ -1,91 +0,0 @@ - -var assert = require('assert'); -var exists = require('fs').existsSync; -var Metalsmith = require('metalsmith'); -var metadata = require('../lib'); - -describe('metalsmith-metadata', function(){ - it('should error for malformed data', function(done){ - var m = Metalsmith('test/fixtures/malformed').use(metadata({ file: 'data.json' })); - m.build(function(err){ - assert(err); - assert(~err.message.indexOf('malformed data')); - assert(!exists('test/fixtures/malformed/build')); - done(); - }); - }); - - it('should parse JSON', function(done){ - var m = Metalsmith('test/fixtures/json').use(metadata({ file: 'data.json' })); - m.build(function(err){ - if (err) return done(err); - assert.deepEqual(m.metadata().file, { string: 'string' }); - assert(!exists('test/fixtures/json/build')); - done(); - }); - }); - - it('should not parse a file if the key already exists in the metadata', function(done){ - var m = Metalsmith('test/fixtures/json') - .use(metadata({ file: 'data.json' })) - .use(metadata({ file: 'missing.json' })); - m.build(function(err){ - if (err) return done(err); - assert.deepEqual(m.metadata().file, { string: 'string' }); - assert(!exists('test/fixtures/json/build')); - done(); - }); - }); - - it('should parse a file even if the key exists if the file is in the bundle', function(done){ - var m = Metalsmith('test/fixtures/duplicate') - .use(metadata({ file: 'data.json' })) - .use(metadata({ file: 'data2.json' })); - m.build(function(err){ - if (err) return done(err); - assert.deepEqual(m.metadata().file, { string: 'string2' }); - assert(!exists('test/fixtures/json/build')); - done(); - }); - }); - - it('should parse YAML', function(done){ - var m = Metalsmith('test/fixtures/yaml').use(metadata({ file: 'data.yaml' })); - m.build(function(err){ - if (err) return done(err); - assert.deepEqual(m.metadata().file, { string: 'string' }); - assert(!exists('test/fixtures/yaml/build')); - done(); - }); - }); - - it('should parse nested path', function(done){ - var m = Metalsmith('test/fixtures/nested').use(metadata({ file: 'path/data.yaml' })); - m.build(function(err){ - if (err) return done(err); - assert.deepEqual(m.metadata().file, { string: 'string' }); - assert(!exists('test/fixtures/nested/build')); - done(); - }); - }); - - it('should parse nested path with backslash', function(done){ - var m = Metalsmith('test/fixtures/nested').use(metadata({ file: 'path\\data.yaml' })); - m.build(function(err){ - if (err) return done(err); - assert.deepEqual(m.metadata().file, { string: 'string' }); - assert(!exists('test/fixtures/nested/build')); - done(); - }); - }); - - it('should parse deep nested path', function(done){ - var m = Metalsmith('test/fixtures/deep-nested').use(metadata({ file: 'path/path/data.yaml' })); - m.build(function(err){ - if (err) return done(err); - assert.deepEqual(m.metadata().file, { string: 'string' }); - assert(!exists('test/fixtures/deep-nested/build')); - done(); - }); - }); -}); \ No newline at end of file diff --git a/tests/fixtures/build/data/folder-test/etiam.json b/tests/fixtures/build/data/folder-test/etiam.json index 41ca819..a8620b2 100644 --- a/tests/fixtures/build/data/folder-test/etiam.json +++ b/tests/fixtures/build/data/folder-test/etiam.json @@ -1,3 +1,3 @@ { - "title": "Fringilla Etiam Sollicitudin" -} \ No newline at end of file + "title": "Fringilla Etiam Sollicitudin" +} diff --git a/tests/fixtures/build/data/folder-test/fringilla.json b/tests/fixtures/build/data/folder-test/fringilla.json index 96e6dec..456f97d 100644 --- a/tests/fixtures/build/data/folder-test/fringilla.json +++ b/tests/fixtures/build/data/folder-test/fringilla.json @@ -1,3 +1,3 @@ { - "title": "Pharetra Egestas Mollis" -} \ No newline at end of file + "title": "Pharetra Egestas Mollis" +} diff --git a/tests/fixtures/build/data/folder-test/sollicitudin.json b/tests/fixtures/build/data/folder-test/sollicitudin.json index a894ddf..c327ac0 100644 --- a/tests/fixtures/build/data/folder-test/sollicitudin.json +++ b/tests/fixtures/build/data/folder-test/sollicitudin.json @@ -1,3 +1,3 @@ { - "title": "Sem Venenatis Tortor" -} \ No newline at end of file + "title": "Sem Venenatis Tortor" +} diff --git a/tests/fixtures/build/data/json-test.json b/tests/fixtures/build/data/json-test.json index 67369ab..3f0303a 100644 --- a/tests/fixtures/build/data/json-test.json +++ b/tests/fixtures/build/data/json-test.json @@ -8,4 +8,4 @@ { "title": "Tortor Mattis Amet Ullamcorper" } -] \ No newline at end of file +] diff --git a/tests/fixtures/external/ext-json-test.json b/tests/fixtures/external/ext-json-test.json index a30905a..bb153f0 100644 --- a/tests/fixtures/external/ext-json-test.json +++ b/tests/fixtures/external/ext-json-test.json @@ -8,4 +8,4 @@ { "title": "Pellentesque Malesuada Ullamcorper" } -] \ No newline at end of file +] diff --git a/tests/fixtures/external/folder-test/etiam.json b/tests/fixtures/external/folder-test/etiam.json index 41ca819..a8620b2 100644 --- a/tests/fixtures/external/folder-test/etiam.json +++ b/tests/fixtures/external/folder-test/etiam.json @@ -1,3 +1,3 @@ { - "title": "Fringilla Etiam Sollicitudin" -} \ No newline at end of file + "title": "Fringilla Etiam Sollicitudin" +} diff --git a/tests/fixtures/external/folder-test/fringilla.json b/tests/fixtures/external/folder-test/fringilla.json index 96e6dec..456f97d 100644 --- a/tests/fixtures/external/folder-test/fringilla.json +++ b/tests/fixtures/external/folder-test/fringilla.json @@ -1,3 +1,3 @@ { - "title": "Pharetra Egestas Mollis" -} \ No newline at end of file + "title": "Pharetra Egestas Mollis" +} diff --git a/tests/fixtures/external/folder-test/sollicitudin.json b/tests/fixtures/external/folder-test/sollicitudin.json index a894ddf..c327ac0 100644 --- a/tests/fixtures/external/folder-test/sollicitudin.json +++ b/tests/fixtures/external/folder-test/sollicitudin.json @@ -1,3 +1,3 @@ { - "title": "Sem Venenatis Tortor" -} \ No newline at end of file + "title": "Sem Venenatis Tortor" +} diff --git a/tests/fixtures/src/data/folder-test/etiam.json b/tests/fixtures/src/data/folder-test/etiam.json index 41ca819..a8620b2 100644 --- a/tests/fixtures/src/data/folder-test/etiam.json +++ b/tests/fixtures/src/data/folder-test/etiam.json @@ -1,3 +1,3 @@ { - "title": "Fringilla Etiam Sollicitudin" -} \ No newline at end of file + "title": "Fringilla Etiam Sollicitudin" +} diff --git a/tests/fixtures/src/data/folder-test/fringilla.json b/tests/fixtures/src/data/folder-test/fringilla.json index 96e6dec..456f97d 100644 --- a/tests/fixtures/src/data/folder-test/fringilla.json +++ b/tests/fixtures/src/data/folder-test/fringilla.json @@ -1,3 +1,3 @@ { - "title": "Pharetra Egestas Mollis" -} \ No newline at end of file + "title": "Pharetra Egestas Mollis" +} diff --git a/tests/fixtures/src/data/folder-test/sollicitudin.json b/tests/fixtures/src/data/folder-test/sollicitudin.json index a894ddf..c327ac0 100644 --- a/tests/fixtures/src/data/folder-test/sollicitudin.json +++ b/tests/fixtures/src/data/folder-test/sollicitudin.json @@ -1,3 +1,3 @@ { - "title": "Sem Venenatis Tortor" -} \ No newline at end of file + "title": "Sem Venenatis Tortor" +} diff --git a/tests/fixtures/src/data/json-test.json b/tests/fixtures/src/data/json-test.json index 67369ab..3f0303a 100644 --- a/tests/fixtures/src/data/json-test.json +++ b/tests/fixtures/src/data/json-test.json @@ -8,4 +8,4 @@ { "title": "Tortor Mattis Amet Ullamcorper" } -] \ No newline at end of file +] diff --git a/tests/index.js b/tests/index.js index 3f4a84c..1cf554b 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,17 +1,17 @@ /* global describe, it */ -'use strict'; - -const chai = require('chai'); -const metalsmith = require('metalsmith'); -const mdMeta = require('../lib'); -const inplace = require('metalsmith-in-place'); -const layouts = require('@metalsmith/layouts'); -const fs = require('fs'); -const path = require('path'); +"use strict"; + +const chai = require("chai"); +const metalsmith = require("metalsmith"); +const mdMeta = require("../lib"); +const inplace = require("metalsmith-in-place"); +const layouts = require("@metalsmith/layouts"); +const fs = require("fs"); +const path = require("path"); const expect = chai.expect; -const fixture = path.resolve.bind(path, __dirname, 'fixtures'); +const fixture = path.resolve.bind(path, __dirname, "fixtures"); const templateConfig = { engineOptions: { @@ -20,151 +20,174 @@ const templateConfig = { }; function file(_path) { - return fs.readFileSync(fixture(_path), 'utf8'); + return fs.readFileSync(fixture(_path), "utf8"); } -describe('metalsmith-metadata', () => { - - it('should parse local JSON', done => { - +describe("metalsmith-metadata", () => { + it("should parse local JSON", (done) => { metalsmith(fixture()) - .use(mdMeta({ + .use( + mdMeta({ localJSON: "./data/json-test.json", - })) + }) + ) .use(inplace(templateConfig)) .use(layouts(templateConfig)) - .build( err => { + .build((err) => { if (err) { return done(err); } - expect(file('build/json-test.html')).to.be.eql(file('expected/json-test.html')); - + expect(file("build/json-test.html")).to.be.eql( + file("expected/json-test.html") + ); + done(); }); }); - it('should parse local YAML', done => { - + it("should parse local YAML", (done) => { metalsmith(fixture()) - .use(mdMeta({ + .use( + mdMeta({ localYAML: "./data/yaml-test.yaml", - })) + }) + ) .use(inplace(templateConfig)) .use(layouts(templateConfig)) - .build( err => { + .build((err) => { if (err) { return done(err); } - expect(file('build/yaml-test.html')).to.be.eql(file('expected/yaml-test.html')); - + expect(file("build/yaml-test.html")).to.be.eql( + file("expected/yaml-test.html") + ); + done(); }); }); - it('should parse local TOML', done => { - + it("should parse local TOML", (done) => { metalsmith(fixture()) - .use(mdMeta({ + .use( + mdMeta({ localTOML: "./data/toml-test.toml", - })) + }) + ) .use(inplace(templateConfig)) .use(layouts(templateConfig)) - .build( err => { + .build((err) => { if (err) { return done(err); } - expect(file('build/toml-test.html')).to.be.eql(file('expected/toml-test.html')); - + expect(file("build/toml-test.html")).to.be.eql( + file("expected/toml-test.html") + ); + done(); }); }); - it('should parse local files in a folder', done => { - + it("should parse local files in a folder", (done) => { metalsmith(fixture()) - .use(mdMeta({ + .use( + mdMeta({ localFolder: "./data/folder-test", - })) + }) + ) .use(inplace(templateConfig)) .use(layouts(templateConfig)) - .build( err => { + .build((err) => { if (err) { return done(err); } - expect(file('build/local-folder-test.html')).to.be.eql(file('expected/local-folder-test.html')); - + expect(file("build/local-folder-test.html")).to.be.eql( + file("expected/local-folder-test.html") + ); + done(); }); }); - it('should parse external JSON', done => { - + it("should parse external JSON", (done) => { metalsmith(fixture()) - .use(mdMeta({ + .use( + mdMeta({ externalJSON: "../external/ext-json-test.json", - })) + }) + ) .use(inplace(templateConfig)) .use(layouts(templateConfig)) - .build( err => { + .build((err) => { if (err) { return done(err); } - expect(file('build/external-json-test.html')).to.be.eql(file('expected/external-json-test.html')); - + expect(file("build/external-json-test.html")).to.be.eql( + file("expected/external-json-test.html") + ); + done(); }); }); - it('should parse external TOML', done => { - + it("should parse external TOML", (done) => { metalsmith(fixture()) - .use(mdMeta({ + .use( + mdMeta({ externalTOML: "../external/ext-toml-test.toml", - })) + }) + ) .use(inplace(templateConfig)) .use(layouts(templateConfig)) - .build( err => { + .build((err) => { if (err) { return done(err); } - expect(file('build/external-toml-test.html')).to.be.eql(file('expected/external-toml-test.html')); - + expect(file("build/external-toml-test.html")).to.be.eql( + file("expected/external-toml-test.html") + ); + done(); }); }); - it('should parse external YAML', done => { - + it("should parse external YAML", (done) => { metalsmith(fixture()) - .use(mdMeta({ + .use( + mdMeta({ externalYAML: "../external/ext-yaml-test.yaml", - })) + }) + ) .use(inplace(templateConfig)) .use(layouts(templateConfig)) - .build( err => { + .build((err) => { if (err) { return done(err); } - expect(file('build/external-yaml-test.html')).to.be.eql(file('expected/external-yaml-test.html')); - + expect(file("build/external-yaml-test.html")).to.be.eql( + file("expected/external-yaml-test.html") + ); + done(); }); }); - it('should parse files in an external folder', done => { - + it("should parse files in an external folder", (done) => { metalsmith(fixture()) - .use(mdMeta({ + .use( + mdMeta({ externalFolder: "../external/folder-test", - })) + }) + ) .use(inplace(templateConfig)) .use(layouts(templateConfig)) - .build( err => { + .build((err) => { if (err) { return done(err); } - expect(file('build/external-folder-test.html')).to.be.eql(file('expected/external-folder-test.html')); - + expect(file("build/external-folder-test.html")).to.be.eql( + file("expected/external-folder-test.html") + ); + done(); }); });