From 3bc59f39805edf36057d0e425dcc4da5e44ca600 Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Sun, 20 May 2018 14:42:44 -0700 Subject: [PATCH 01/16] rename --- example/index.js | 8 +++++--- readme.md | 2 +- test.js | 20 ++++++++++---------- transform.js | 8 ++++---- utils/file.js | 14 +++++++------- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/example/index.js b/example/index.js index 7cb539f..fc6fff1 100644 --- a/example/index.js +++ b/example/index.js @@ -1,14 +1,16 @@ +var nanocontent = require('../') var css = require('sheetify') -var hypha = require('hypha') var choo = require('choo') -css('./src/design/index.js') -var site = hypha.readSiteSync('./content', { +var site = nanocontent.readSiteSync('./content', { parent: '/content' }) var app = choo() +// design +css('./src/design/index.js') + // content and routes app.use(require('./src/stores/content')(site)) app.route('*', require('./src/views/notfound')) diff --git a/readme.md b/readme.md index 06f696e..bb27c43 100644 --- a/readme.md +++ b/readme.md @@ -63,7 +63,7 @@ var site = nanocontent.readSiteSync('./content') Each directory becomes a path containing a sub-object of the content in your text file. -``` +```js { '/': { }, '/about': { }, diff --git a/test.js b/test.js index 8449756..cf642b7 100644 --- a/test.js +++ b/test.js @@ -1,38 +1,38 @@ var test = require('ava') -var hypha = require('.') +var nanocontent = require('.') test('readPageSync works', function (t) { - var page = hypha.readPageSync('example/content/about') + var page = nanocontent.readPageSync('example/content/about') t.is(page.title, 'About') t.is(page.view, 'custom') }) test('readPage works', async function (t) { - var page = await hypha.readPage('example/content/about') + var page = await nanocontent.readPage('example/content/about') t.is(page.title, 'About') t.is(page.view, 'custom') }) test('readPageSync and readPage outputs are the same', async function (t) { - var syncPage = hypha.readPageSync('example/content/about') - var asyncPage = await hypha.readPage('example/content/about') + var syncPage = nanocontent.readPageSync('example/content/about') + var asyncPage = await nanocontent.readPage('example/content/about') t.deepEqual(syncPage, asyncPage) }) test('readSiteSync works', function (t) { - var site = hypha.readSiteSync('example/content') + var site = nanocontent.readSiteSync('example/content') t.is(site['/example/content'].title, 'Example') t.is(site['/example/content/about'].title, 'About') }) test('readSiteSync and readSite outputs are the same', async function (t) { - var syncSite = hypha.readSiteSync('example/content') - var asyncSite = await hypha.readSite('example/content') + var syncSite = nanocontent.readSiteSync('example/content') + var asyncSite = await nanocontent.readSite('example/content') t.deepEqual(syncSite, asyncSite) }) test('readSiteSync and readSite outputs are the same with parent option', async function (t) { - var syncSite = hypha.readSiteSync('example/content', { parent: true }) - var asyncSite = await hypha.readSite('example/content', { parent: true }) + var syncSite = nanocontent.readSiteSync('example/content', { parent: true }) + var asyncSite = await nanocontent.readSite('example/content', { parent: true }) t.deepEqual(syncSite, asyncSite) }) diff --git a/transform.js b/transform.js index be02b01..f075b4e 100644 --- a/transform.js +++ b/transform.js @@ -2,7 +2,7 @@ var staticModule = require('static-module') var through = require('through2') var path = require('path') -var hypha = require('.') +var nanocontent = require('.') module.exports = transform @@ -15,13 +15,13 @@ function transform (filename) { } var sm = staticModule({ - hypha: { + nanocontent: { readPageSync: function (pathPage, opts) { opts = opts || { } opts.pathRoot = opts.pathRoot || vars.__dirname var pathDir = path.isAbsolute(pathPage) ? pathPage : path.join(vars.__dirname, pathPage) - var pageSync = hypha.readPageSync(pathDir, opts) + var pageSync = nanocontent.readPageSync(pathDir, opts) var stream = through() stream.push(JSON.stringify(pageSync, { }, 2)) @@ -37,7 +37,7 @@ function transform (filename) { } var pathDir = path.isAbsolute(pathSite) ? pathSite : path.join(vars.__dirname, pathSite) - var siteSync = hypha.readSiteSync(pathDir, opts) + var siteSync = nanocontent.readSiteSync(pathDir, opts) var stream = through() stream.push(JSON.stringify(siteSync, { }, 2)) diff --git a/utils/file.js b/utils/file.js index dc1d121..6eb9317 100644 --- a/utils/file.js +++ b/utils/file.js @@ -1,6 +1,6 @@ var objectKeys = require('object-keys') -var assert = require('assert') var slash = require('normalize-path') +var assert = require('assert') var path = require('path') module.exports = { @@ -49,16 +49,16 @@ function getFileType (extension, filetypes) { } function isFile (pathFile) { - assert.equal(typeof pathFile, 'string', 'enoki: arg1 pathFile must be type string') + assert.equal(typeof pathFile, 'string', 'arg1 pathFile must be type string') return path.extname(pathFile) !== '' } function getFileMeta (opts) { - assert.equal(typeof opts, 'object', 'enoki: arg1 opts must be type object') - assert.equal(typeof opts.pathFile, 'string', 'enoki: arg1 opts.pathFile must be type string') - assert.equal(typeof opts.pathParent, 'string', 'enoki: arg1 opts.pathParent must be type string') - assert.equal(typeof opts.pathRoot, 'string', 'enoki: arg1 opts.pathRoot must be type string') - assert.equal(typeof opts.filetypes, 'object', 'enoki: arg1 opts.filetypes must be type string') + assert.equal(typeof opts, 'object', 'arg1 opts must be type object') + assert.equal(typeof opts.pathFile, 'string', 'arg1 opts.pathFile must be type string') + assert.equal(typeof opts.pathParent, 'string', 'arg1 opts.pathParent must be type string') + assert.equal(typeof opts.pathRoot, 'string', 'arg1 opts.pathRoot must be type string') + assert.equal(typeof opts.filetypes, 'object', 'arg1 opts.filetypes must be type string') var output = { } var ext = path.extname(opts.pathFile) From 977b721ea645ea6922b957bd7382c5c7d5874fc0 Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Sun, 20 May 2018 15:10:07 -0700 Subject: [PATCH 02/16] check for pages --- example/package.json | 2 +- lib/readFile.js | 11 +++++------ lib/readFileSync.js | 11 +++++------ utils/page.js | 11 +++++++++++ 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/example/package.json b/example/package.json index c23f003..d84d875 100644 --- a/example/package.json +++ b/example/package.json @@ -1,5 +1,5 @@ { - "name": "hypha-example", + "name": "nanocontent-example", "version": "1.0.0", "description": "nice", "main": "index.js", diff --git a/lib/readFile.js b/lib/readFile.js index 61cb880..3588f70 100644 --- a/lib/readFile.js +++ b/lib/readFile.js @@ -1,6 +1,6 @@ var assert = require('assert') -var utilFile = require('../utils/file') +var utilPage = require('../utils/page') var readPage = require('./readPage') module.exports = readFile @@ -10,9 +10,8 @@ async function readFile (pathFile, opts) { assert.equal(typeof opts, 'object', 'arg2: opts must be type object') assert.equal(typeof opts.fs, 'object', 'arg2: opts.fs must be type object') - if (!utilFile.isFile(pathFile)) { - return readPage(pathFile, opts) - } else { - return false - } + // pages + if (utilPage.isPage(pathFile)) return readPage(pathFile, opts) + + return false } diff --git a/lib/readFileSync.js b/lib/readFileSync.js index af09883..10f1986 100644 --- a/lib/readFileSync.js +++ b/lib/readFileSync.js @@ -1,7 +1,7 @@ var assert = require('assert') var readPageSync = require('./readPageSync') -var utilFile = require('../utils/file') +var utilPage = require('../utils/page') module.exports = readFileSync @@ -10,9 +10,8 @@ function readFileSync (pathFile, opts) { assert.equal(typeof opts, 'object', 'arg2: opts must be type object') assert.equal(typeof opts.fs, 'object', 'arg2: opts.fs must be type object') - if (!utilFile.isFile(pathFile)) { - return readPageSync(pathFile, opts) - } else { - return false - } + // pages + if (utilPage.isPage(pathFile)) return readPageSync(pathFile, opts) + + return false } diff --git a/utils/page.js b/utils/page.js index e69de29..37eca7a 100644 --- a/utils/page.js +++ b/utils/page.js @@ -0,0 +1,11 @@ +var assert = require('assert') +var path = require('path') + +module.exports = { + isPage: isPage +} + +function isPage (pathPage) { + assert.equal(typeof pathPage, 'string', 'arg1 pathPage must be type string') + return path.extname(pathPage) === '' +} From 959816a100692a5c2c48ce4670ed82210836f4a5 Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Sun, 20 May 2018 18:23:22 -0700 Subject: [PATCH 03/16] switch to content state schema --- lib/readFile.js | 42 ++++++++++++++++++++++++- lib/readFileSync.js | 33 ++++++++++++++++++-- lib/readPage.js | 75 ++------------------------------------------- lib/readPageSync.js | 69 ++--------------------------------------- utils/file.js | 5 ++- 5 files changed, 80 insertions(+), 144 deletions(-) diff --git a/lib/readFile.js b/lib/readFile.js index 3588f70..fc99a4f 100644 --- a/lib/readFile.js +++ b/lib/readFile.js @@ -1,7 +1,13 @@ +var { promisify } = require('util') var assert = require('assert') +var smarkt = require('smarkt') +var xtend = require('xtend') +var path = require('path') +var utilFile = require('../utils/file') var utilPage = require('../utils/page') var readPage = require('./readPage') +var defaults = require('./defaults') module.exports = readFile @@ -13,5 +19,39 @@ async function readFile (pathFile, opts) { // pages if (utilPage.isPage(pathFile)) return readPage(pathFile, opts) - return false + var fs = opts.fs + var parse = typeof opts.parse === 'function' ? opts.parse : smarkt.parse + var fileIndex = opts.file || defaults.file + var fileExtname = path.extname(fileIndex).toLowerCase() + var filetypes = opts.filetypes || defaults.filetypes + var pathRoot = opts.pathRoot || '' + var encoding = opts.encoding || defaults.encoding + var fileParsed = utilFile.getFileMeta({ + pathFile: pathFile, + pathRoot: pathRoot, + filetypes: filetypes, + pathSource: opts.source, + pathSiteParent: opts.parent + }) + + // skip text files + if (fileExtname === fileParsed.extension) return false + + // async readFile + var readFile = isAsync(fs.readFile) + ? fs.readFile + : promisify(fs.readFile) + + try { + var pathMeta = pathFile + fileExtname + var text = await readFile(pathMeta, encoding) + return xtend(parse(text), fileParsed) + } catch (err) { + if (fileParsed.filename) return Promise.resolve(fileParsed) + else return Promise.resolve(false) + } +} + +function isAsync (fn) { + return fn.constructor.name === 'AsyncFunction' } diff --git a/lib/readFileSync.js b/lib/readFileSync.js index 10f1986..d38a67b 100644 --- a/lib/readFileSync.js +++ b/lib/readFileSync.js @@ -1,7 +1,12 @@ var assert = require('assert') +var smarkt = require('smarkt') +var xtend = require('xtend') +var path = require('path') var readPageSync = require('./readPageSync') +var utilFile = require('../utils/file') var utilPage = require('../utils/page') +var defaults = require('./defaults') module.exports = readFileSync @@ -10,8 +15,32 @@ function readFileSync (pathFile, opts) { assert.equal(typeof opts, 'object', 'arg2: opts must be type object') assert.equal(typeof opts.fs, 'object', 'arg2: opts.fs must be type object') - // pages + // page if (utilPage.isPage(pathFile)) return readPageSync(pathFile, opts) - return false + var fs = opts.fs + var parse = typeof opts.parse === 'function' ? opts.parse : smarkt.parse + var fileIndex = opts.file || defaults.file + var fileExtname = path.extname(fileIndex).toLowerCase() + var filetypes = opts.filetypes || defaults.filetypes + var pathRoot = opts.pathRoot || '' + var encoding = opts.encoding || defaults.encoding + var fileParsed = utilFile.getFileMeta({ + pathFile: pathFile, + pathRoot: pathRoot, + filetypes: filetypes, + pathSiteParent: opts.parent + }) + + // skip text files + if (fileExtname === fileParsed.extension) return false + + try { + var pathMeta = pathFile + fileExtname + var text = fs.readFileSync(pathMeta, encoding) + return xtend(parse(text), fileParsed) + } catch (err) { + if (fileParsed.filename) return fileParsed + else return false + } } diff --git a/lib/readPage.js b/lib/readPage.js index 3bcee63..ee04bd4 100644 --- a/lib/readPage.js +++ b/lib/readPage.js @@ -18,92 +18,23 @@ async function readPage (pathPage, opts) { var fs = opts.fs.url ? opts.fs : pify(opts.fs) // web api or node var parse = typeof opts.parse === 'function' ? opts.parse : smarkt.parse var fileIndex = opts.file || defaults.file - var fileExtname = path.extname(fileIndex) - var filetypes = opts.filetypes || defaults.filetypes var pathRoot = opts.pathRoot || '' var pathUrl = utilFile.formatUrl(pathPage, pathRoot, opts.parent) var encoding = opts.encoding || defaults.encoding var content = await getContent() - var childrenInput = await getChildren() - var children = childrenInput - .filter(file => utilFile.filterFile(file, fileIndex)) - .reduce(utilFile.sortChildren, { files: [], pages: [] }) - var files = await getFiles(children.files) - var pages = getPages(children.pages) return xtend(content, { name: path.basename(pathPage), path: utilFile.formatUrl(pathPage, pathRoot), - url: pathUrl, - files: files, - pages: pages + url: pathUrl }) - async function getChildren () { - try { - return await fs.readdir(pathPage) - } catch (err) { - return [] - } - } - async function getContent () { try { - var content - content = await fs.readFile(slash(path.join(pathPage, fileIndex)), encoding) - content = parse(content) - return content + var content = await fs.readFile(slash(path.join(pathPage, fileIndex)), encoding) + return parse(content) } catch (err) { return '' } } - - async function getFiles (files) { - var result = { } - await Promise.all(files.map(read)) - return result - - async function read (pathFile) { - var fileParsed = utilFile.getFileMeta({ - pathFile: pathFile, - pathRoot: pathRoot, - filetypes: filetypes, - pathParent: pathPage, - pathSource: opts.source, - pathSiteParent: opts.parent - }) - - try { - var fileMeta = pathFile + fileExtname - var pathMeta = path.join(pathPage, fileMeta) - var text = await fs.readFile(pathMeta, encoding) - // set - result[fileParsed.filename] = xtend(parse(text), fileParsed) - files.splice(files.indexOf(fileMeta), 1) - // cleanup - delete result[fileMeta] - return text - } catch (err) { - if (fileParsed.filename) { - result[fileParsed.filename] = fileParsed - } - return Promise.resolve() - } - } - } - - function getPages (pages) { - return pages.reduce(function (result, pathSubpage) { - var fileParsed = utilFile.getFileMeta({ - pathRoot: pathRoot, - pathFile: pathSubpage, - filetypes: filetypes, - pathParent: pathPage, - pathSiteParent: opts.parent - }) - - if (fileParsed.name) result[fileParsed.name] = fileParsed - return result - }, {}) - } } diff --git a/lib/readPageSync.js b/lib/readPageSync.js index e81901b..d4ae702 100644 --- a/lib/readPageSync.js +++ b/lib/readPageSync.js @@ -17,86 +17,23 @@ function readPageSync (pathPage, opts) { var fs = opts.fs var parse = typeof opts.parse === 'function' ? opts.parse : smarkt.parse var fileIndex = opts.file || defaults.file - var fileExtname = path.extname(fileIndex) - var filetypes = opts.filetypes || defaults.filetypes var pathRoot = opts.pathRoot || '' var pathUrl = utilFile.formatUrl(pathPage, pathRoot, opts.parent) var encoding = opts.encoding || defaults.encoding var content = getContent() - var children = getChildren() - .filter(file => utilFile.filterFile(file, fileIndex)) - .reduce(utilFile.sortChildren, { files: [ ], pages: [ ] }) - var files = getFiles(children.files) - var pages = getPages(children.pages) return xtend(content, { name: path.basename(pathPage), path: utilFile.formatUrl(pathPage, pathRoot), - url: pathUrl, - files: files, - pages: pages + url: pathUrl }) - function getChildren () { - try { - return fs.readdirSync(pathPage) - } catch (err) { - return [ ] - } - } - function getContent () { try { - var content - content = fs.readFileSync(slash(path.join(pathPage, fileIndex)), encoding) - content = parse(content) - return content + var content = fs.readFileSync(slash(path.join(pathPage, fileIndex)), encoding) + return parse(content) } catch (err) { return '' } } - - function getFiles (files) { - return files.reduce(function (result, pathFile) { - var fileParsed = utilFile.getFileMeta({ - pathFile: pathFile, - pathRoot: pathRoot, - filetypes: filetypes, - pathParent: pathPage, - pathSiteParent: opts.parent - }) - - try { - var fileMeta = pathFile + fileExtname - var text = fs.readFileSync(slash(path.join(pathPage, fileMeta)), encoding) - // set - result[fileParsed.filename] = xtend(parse(text), fileParsed) - files.splice(files.indexOf(fileMeta), 1) - // cleanup - delete result[fileMeta] - } catch (err) { - if (fileParsed.filename) { - result[fileParsed.filename] = fileParsed - } - } - - return result - }, { }) - } - - function getPages (pages) { - return pages.reduce(function (result, pathSubpage) { - var fileParsed = utilFile.getFileMeta({ - pathRoot: pathRoot, - pathFile: pathSubpage, - filetypes: filetypes, - pathParent: pathPage, - pathSource: opts.source, - pathSiteParent: opts.parent - }) - - if (fileParsed.name) result[fileParsed.name] = fileParsed - return result - }, { }) - } } diff --git a/utils/file.js b/utils/file.js index 6eb9317..4da6e68 100644 --- a/utils/file.js +++ b/utils/file.js @@ -56,15 +56,14 @@ function isFile (pathFile) { function getFileMeta (opts) { assert.equal(typeof opts, 'object', 'arg1 opts must be type object') assert.equal(typeof opts.pathFile, 'string', 'arg1 opts.pathFile must be type string') - assert.equal(typeof opts.pathParent, 'string', 'arg1 opts.pathParent must be type string') assert.equal(typeof opts.pathRoot, 'string', 'arg1 opts.pathRoot must be type string') assert.equal(typeof opts.filetypes, 'object', 'arg1 opts.filetypes must be type string') var output = { } var ext = path.extname(opts.pathFile) output.name = path.basename(opts.pathFile, ext) - output.path = formatUrl(path.join('/', opts.pathParent, '/', opts.pathFile), opts.pathRoot) - output.url = formatUrl(path.join('/', opts.pathParent, '/', opts.pathFile), opts.pathRoot, opts.pathSiteParent) + output.path = formatUrl(opts.pathFile, opts.pathRoot) + output.url = formatUrl(opts.pathFile, opts.pathRoot, opts.pathSiteParent) output.source = opts.pathSource ? (opts.pathSource + output.path) : output.path if (ext) { From 4bcdd57b38657d60f1b16da9eef0d90e416f57a4 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 21 May 2018 09:02:10 +0000 Subject: [PATCH 04/16] chore: add Greenkeeper config file --- greenkeeper.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 greenkeeper.json diff --git a/greenkeeper.json b/greenkeeper.json new file mode 100644 index 0000000..798c8fa --- /dev/null +++ b/greenkeeper.json @@ -0,0 +1,10 @@ +{ + "groups": { + "default": { + "packages": [ + "example/package.json", + "package.json" + ] + } + } +} From fd2d666da313617d7eb2c7531aef23f724205b74 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 21 May 2018 09:04:06 +0000 Subject: [PATCH 05/16] chore(package): update dependencies --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index e448511..8354389 100644 --- a/package.json +++ b/package.json @@ -20,17 +20,17 @@ "dependencies": { "glob": "^7.1.2", "js-yaml": "^3.10.0", - "normalize-path": "^2.1.1", + "normalize-path": "^3.0.0", "object-keys": "^1.0.11", - "object-values": "^1.0.0", + "object-values": "^2.0.0", "pify": "^3.0.0", "smarkt": "0.0.6", - "static-module": "^1.5.0", + "static-module": "^2.2.5", "through2": "^2.0.3", "xtend": "^4.0.1" }, "devDependencies": { "ava": "^0.25.0", - "standard": "^10.0.3" + "standard": "^11.0.1" } } From e8cb94da105a960cf8eb45212c723f1149203828 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 21 May 2018 09:05:08 +0000 Subject: [PATCH 06/16] chore(package): update dependencies --- example/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/example/package.json b/example/package.json index c23f003..4817fbb 100644 --- a/example/package.json +++ b/example/package.json @@ -10,13 +10,13 @@ "author": "", "license": "ISC", "dependencies": { - "bel": "^5.1.3", + "bel": "^6.0.0", "choo": "^6.5.1", "gr8": "^3.1.3", "markdown-it": "^8.4.0", "object-keys": "^1.0.11", - "object-values": "^1.0.0", - "sheetify": "^6.2.0", + "object-values": "^2.0.0", + "sheetify": "^7.3.2", "xtend": "^4.0.1" }, "devDependencies": { From a97962c784afc5ff8051c7575bd55b2b5befea0a Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 21 May 2018 09:05:57 +0000 Subject: [PATCH 07/16] docs(readme): add Greenkeeper badge --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 06f696e..bbd0697 100644 --- a/readme.md +++ b/readme.md @@ -27,6 +27,8 @@ npm install nanocontent --save ## Usage +[![Greenkeeper badge](https://badges.greenkeeper.io/jondashkyle/nanocontent.svg)](https://greenkeeper.io/) + Format some plain text files using [smarkt](https://github.com/jondashkyle/smarkt) fields. ``` From 94cf028fc38d466743f5ee4a015e739f59451fb1 Mon Sep 17 00:00:00 2001 From: Jon-Kyle Date: Mon, 21 May 2018 10:30:22 -0700 Subject: [PATCH 08/16] Remove greenkeeper badge --- readme.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/readme.md b/readme.md index bbd0697..06f696e 100644 --- a/readme.md +++ b/readme.md @@ -27,8 +27,6 @@ npm install nanocontent --save ## Usage -[![Greenkeeper badge](https://badges.greenkeeper.io/jondashkyle/nanocontent.svg)](https://greenkeeper.io/) - Format some plain text files using [smarkt](https://github.com/jondashkyle/smarkt) fields. ``` From 8389d219a6c34c9050611d6b148ee4e89ef181eb Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Mon, 21 May 2018 10:36:07 -0700 Subject: [PATCH 09/16] v bump and docs img --- package.json | 2 +- readme.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e448511..0571688 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nanocontent", - "version": "0.4.5", + "version": "1.0.0-next1", "description": "read a directory of content into an object", "main": "index.js", "scripts": { diff --git a/readme.md b/readme.md index bb27c43..c9726c7 100644 --- a/readme.md +++ b/readme.md @@ -69,6 +69,7 @@ Each directory becomes a path containing a sub-object of the content in your tex '/about': { }, '/blog': { }, '/blog/30-01-19-technopastoral': { } + '/blog/30-01-19-technopastoral/header.jpg': { } } ``` From fbd01158fc65f403f41c1b8751d1f5f737c8cc82 Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Mon, 21 May 2018 10:40:27 -0700 Subject: [PATCH 10/16] remote testing --- .travis.yml | 14 ++++++++++++++ package.json | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4fe2b0e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +node_js: +- '7' +- '8' +sudo: false +language: node_js +env: + - CXX=g++-4.8 +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.8 +script: npm run test \ No newline at end of file diff --git a/package.json b/package.json index 8354389..b4f70b5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "read a directory of content into an object", "main": "index.js", "scripts": { - "test": "standard --fix; ava" + "test": "standard; ava" }, "keywords": [], "author": "Jon-Kyle (http://jon-kyle.com)", From b6ca221d5ba9220b562963e48eb59c58b27b82eb Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Mon, 21 May 2018 10:55:01 -0700 Subject: [PATCH 11/16] promisify for node < 8 --- lib/readFile.js | 2 +- package.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/readFile.js b/lib/readFile.js index fc99a4f..8180da6 100644 --- a/lib/readFile.js +++ b/lib/readFile.js @@ -1,4 +1,4 @@ -var { promisify } = require('util') +var promisify = require('promisify-node') var assert = require('assert') var smarkt = require('smarkt') var xtend = require('xtend') diff --git a/package.json b/package.json index 445b8d3..f0a56b6 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "object-keys": "^1.0.11", "object-values": "^2.0.0", "pify": "^3.0.0", + "promisify-node": "^0.4.0", "smarkt": "0.0.6", "static-module": "^2.2.5", "through2": "^2.0.3", From 1eb7c1cf8f9688fbf0ea7644f2939f1b1d2b5dea Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Wed, 23 May 2018 10:57:34 -0700 Subject: [PATCH 12/16] files and pages convenience keys --- lib/readPage.js | 54 ++++++++++++++++++++++++++++++++++++++++++++- lib/readPageSync.js | 50 ++++++++++++++++++++++++++++++++++++++++- utils/file.js | 8 +++---- 3 files changed, 106 insertions(+), 6 deletions(-) diff --git a/lib/readPage.js b/lib/readPage.js index ee04bd4..daca18a 100644 --- a/lib/readPage.js +++ b/lib/readPage.js @@ -19,14 +19,23 @@ async function readPage (pathPage, opts) { var parse = typeof opts.parse === 'function' ? opts.parse : smarkt.parse var fileIndex = opts.file || defaults.file var pathRoot = opts.pathRoot || '' + var filetypes = opts.filetypes || defaults.filetypes var pathUrl = utilFile.formatUrl(pathPage, pathRoot, opts.parent) var encoding = opts.encoding || defaults.encoding var content = await getContent() + var childrenInput = await getChildren() + var children = childrenInput + .filter(file => utilFile.filterFile(file, fileIndex)) + .reduce(utilFile.sortChildren, { files: [], pages: [] }) + var files = await getFiles(children.files) + var pages = getPages(children.pages) return xtend(content, { name: path.basename(pathPage), path: utilFile.formatUrl(pathPage, pathRoot), - url: pathUrl + url: pathUrl, + files: files, + pages: pages }) async function getContent () { @@ -37,4 +46,47 @@ async function readPage (pathPage, opts) { return '' } } + + async function getChildren () { + try { + return await fs.readdir(pathPage) + } catch (err) { + return [] + } + } + + async function getFiles (files) { + var result = { } + await Promise.all(files.map(read)) + return result + + async function read (pathFile) { + var fileParsed = utilFile.getFileMeta({ + pathFile: pathFile, + pathRoot: pathRoot, + filetypes: filetypes, + pathParent: pathPage, + pathSource: opts.source, + pathSiteParent: opts.parent + }) + + if (!utilFile.filterFile(pathFile, fileIndex)) return false + else result[fileParsed.filename] = fileParsed.url + } + } + + function getPages (pages) { + return pages.reduce(function (result, pathSubpage) { + var fileParsed = utilFile.getFileMeta({ + pathRoot: pathRoot, + pathFile: pathSubpage, + filetypes: filetypes, + pathParent: pathPage, + pathSiteParent: opts.parent + }) + + if (fileParsed.name) result[fileParsed.name] = fileParsed.url + return result + }, {}) + } } diff --git a/lib/readPageSync.js b/lib/readPageSync.js index d4ae702..8968b9d 100644 --- a/lib/readPageSync.js +++ b/lib/readPageSync.js @@ -17,17 +17,33 @@ function readPageSync (pathPage, opts) { var fs = opts.fs var parse = typeof opts.parse === 'function' ? opts.parse : smarkt.parse var fileIndex = opts.file || defaults.file + var filetypes = opts.filetypes || defaults.filetypes var pathRoot = opts.pathRoot || '' var pathUrl = utilFile.formatUrl(pathPage, pathRoot, opts.parent) var encoding = opts.encoding || defaults.encoding var content = getContent() + var children = getChildren() + .filter(file => utilFile.filterFile(file, fileIndex)) + .reduce(utilFile.sortChildren, { files: [ ], pages: [ ] }) + var files = getFiles(children.files) + var pages = getPages(children.pages) return xtend(content, { name: path.basename(pathPage), path: utilFile.formatUrl(pathPage, pathRoot), - url: pathUrl + url: pathUrl, + files: files, + pages: pages }) + function getChildren () { + try { + return fs.readdirSync(pathPage) + } catch (err) { + return [ ] + } + } + function getContent () { try { var content = fs.readFileSync(slash(path.join(pathPage, fileIndex)), encoding) @@ -36,4 +52,36 @@ function readPageSync (pathPage, opts) { return '' } } + + function getFiles (files) { + return files.reduce(function (result, pathFile) { + var fileParsed = utilFile.getFileMeta({ + pathFile: pathFile, + pathRoot: pathRoot, + filetypes: filetypes, + pathParent: pathPage, + pathSiteParent: opts.parent + }) + + if (!utilFile.filterFile(pathFile, fileIndex)) return false + else result[fileParsed.filename] = fileParsed.url + return result + }, { }) + } + + function getPages (pages) { + return pages.reduce(function (result, pathSubpage) { + var fileParsed = utilFile.getFileMeta({ + pathRoot: pathRoot, + pathFile: pathSubpage, + filetypes: filetypes, + pathParent: pathPage, + pathSource: opts.source, + pathSiteParent: opts.parent + }) + + if (fileParsed.name) result[fileParsed.name] = fileParsed.url + return result + }, { }) + } } diff --git a/utils/file.js b/utils/file.js index 4da6e68..d8cd6b7 100644 --- a/utils/file.js +++ b/utils/file.js @@ -27,8 +27,7 @@ function sortChildren (result, active) { function filterFile (file, index) { if (file === '.DS_Store') return false if (/(^[.#]|(?:__|~)$)/.test(file)) return false - if (file.indexOf(index) >= 0) return false - if (file.indexOf('src') >= 0) return false + if (path.extname(file) === path.extname(index)) return false return true } @@ -61,9 +60,10 @@ function getFileMeta (opts) { var output = { } var ext = path.extname(opts.pathFile) + var pathFile = slash(path.join(opts.pathParent || '', opts.pathFile)) output.name = path.basename(opts.pathFile, ext) - output.path = formatUrl(opts.pathFile, opts.pathRoot) - output.url = formatUrl(opts.pathFile, opts.pathRoot, opts.pathSiteParent) + output.path = formatUrl(pathFile, opts.pathRoot) + output.url = formatUrl(pathFile, opts.pathRoot, opts.pathSiteParent) output.source = opts.pathSource ? (opts.pathSource + output.path) : output.path if (ext) { From b45e30209d18d123fdae07df16b51f7edd44faee Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Fri, 25 May 2018 23:32:53 -0700 Subject: [PATCH 13/16] file reading --- lib/readFiles.js | 8 +++++++- lib/readFilesSync.js | 8 +++++++- utils/file.js | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/readFiles.js b/lib/readFiles.js index 2f692ab..ef76649 100644 --- a/lib/readFiles.js +++ b/lib/readFiles.js @@ -23,7 +23,13 @@ async function readFiles (files, pathSite, opts) { async function read (pathFile) { var content = await readFile(pathFile, opts) if (content && !content.name.match(defaults.ignore)) { - output[content.url] = content + var key = content.url + + // temp cleanup + content.url = content.extension ? content.path : content.url + delete content.path + + output[key] = content } return content } diff --git a/lib/readFilesSync.js b/lib/readFilesSync.js index 9e4fa9c..eb89d98 100644 --- a/lib/readFilesSync.js +++ b/lib/readFilesSync.js @@ -21,7 +21,13 @@ function readFilesSync (files, pathSite, opts) { if (typeof opts.onFile === 'function') opts.onFile(pathFile) var content = readFileSync(pathFile, opts) if (content && !content.name.match(defaults.ignore)) { - output[content.url] = content + var key = content.url + + // temp cleanup + content.url = content.extension ? content.path : content.url + delete content.path + + output[key] = content } }) diff --git a/utils/file.js b/utils/file.js index d8cd6b7..2f1a6c8 100644 --- a/utils/file.js +++ b/utils/file.js @@ -61,10 +61,11 @@ function getFileMeta (opts) { var output = { } var ext = path.extname(opts.pathFile) var pathFile = slash(path.join(opts.pathParent || '', opts.pathFile)) + output.name = path.basename(opts.pathFile, ext) output.path = formatUrl(pathFile, opts.pathRoot) output.url = formatUrl(pathFile, opts.pathRoot, opts.pathSiteParent) - output.source = opts.pathSource ? (opts.pathSource + output.path) : output.path + // output.source = opts.pathSource ? (opts.pathSource + output.path) : output.path if (ext) { output.extension = ext.toLowerCase() From aedeaeaa7a2a460935416903a36b5940367c41ea Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Fri, 25 May 2018 23:33:08 -0700 Subject: [PATCH 14/16] v bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f0a56b6..8c3ad4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nanocontent", - "version": "1.0.0-next1", + "version": "1.0.0-next2", "description": "read a directory of content into an object", "main": "index.js", "scripts": { From f39c98a4f7970fb1974a30a90b5bc3104d2c7b40 Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Sat, 26 May 2018 15:17:25 -0700 Subject: [PATCH 15/16] read file async fallback --- lib/readFile.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/readFile.js b/lib/readFile.js index 8180da6..31d220e 100644 --- a/lib/readFile.js +++ b/lib/readFile.js @@ -37,16 +37,14 @@ async function readFile (pathFile, opts) { // skip text files if (fileExtname === fileParsed.extension) return false - // async readFile - var readFile = isAsync(fs.readFile) - ? fs.readFile - : promisify(fs.readFile) - try { var pathMeta = pathFile + fileExtname - var text = await readFile(pathMeta, encoding) + var text = isAsync(fs.readFile) + ? await fs.readFile(pathMeta, encoding) + : await promisify(fs.readFile)(pathMeta, encoding) return xtend(parse(text), fileParsed) } catch (err) { + // console.log(fileParsed.filename, err.message) if (fileParsed.filename) return Promise.resolve(fileParsed) else return Promise.resolve(false) } From b7a926cc00f8e479e41fa07525fc37d5f61e9340 Mon Sep 17 00:00:00 2001 From: jondashkyle Date: Sat, 26 May 2018 15:17:36 -0700 Subject: [PATCH 16/16] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8c3ad4e..0255394 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nanocontent", - "version": "1.0.0-next2", + "version": "1.0.0-next3", "description": "read a directory of content into an object", "main": "index.js", "scripts": {