Last active
April 28, 2016 19:00
-
-
Save mikeumus/8e1783030bd494f3b810 to your computer and use it in GitHub Desktop.
chainy write docpad documents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var fs = require('fs'); | |
var mpath = require('mpath'); // For saving the query from the API json: https://www.npmjs.com/package/mkdirp | |
var mkdirp = require('mkdirp'); // For creating folders that didn't already exist: https://www.npmjs.com/package/mkdirp | |
var menuItem = require('./menuItem.js'); | |
var chainyJsonDoc, name; | |
/** | |
* A module using Bevry's Chainy.js to create DocPad | |
* files via json pulled from an API that follow the following format: | |
* https://www.exmample.com/api/v2/collections?api_key=API_KEY | |
* Where `API_KEY` is an environment variable. | |
* | |
* @module chainyJsonDoc | |
* | |
* @param {url} | |
* The base url of your json API. | |
* | |
* @param {name} | |
* Sets the api call in the url string. | |
* For example passing `chainyJsonDoc('**collections**')` will go to this API url: | |
* https://www.continentalclothing.com/api/v2/`**collections**`?api_key=API_KEY | |
* | |
* @param {path} | |
* The path to write the new file. For example: | |
* `./src/documents/` would create a DocPad document. | |
* | |
*/ | |
module.exports = chainyJsonDoc = (url, name, api_key, query, path) => { | |
require('chainy').create().require('set feed each') | |
.set(url+name+'?api_key='+api_key) // +process.env.CC_APIKEY yhttps://www.continentalclothing.com/api/v2/collections?api_key=NIU3JN7TZLJFS19D98QD4NSODM34S69K | |
.feed() | |
.each((itemValue, itemIndex, complete) => { | |
if (mpath.get("visible", itemValue) === true && mpath.get("primary", itemValue) === true){ | |
var qThis = mpath.get(query, itemValue); | |
var file = path+'/'+name+'/'+ qThis + ".html"; | |
mkdirp.sync(path+'/'+name+'/'); | |
debugger; | |
fs.writeFile(file, menuItem(name, qThis, itemValue), complete); | |
} // end if | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... ^ | |
let Chainy = require('chainy').subclass().require('set feed each'); | |
let chainyInstance = Chainy.create(); | |
if (Object.prototype.toString.call(data) === '[object Object]'){ | |
chainyInstance.set(data); | |
} else if (Object.prototype.toString(data) === '[object String]'){ | |
chainyInstance.set(data).feed(); | |
} | |
chainyInstance | |
.each((itemValue, itemIndex, complete) => { | |
let name = mpath.get(item_name, itemValue); | |
debugger; | |
fs.appendFile(path, template(itemValue, name), (err) => { | |
if (err) throw err; | |
console.log(`The ${name} was appended to file!`); | |
}); | |
complete(); | |
}).done(function(err, result){ | |
if (err) throw err; | |
return false; // For testing. | |
// console.log('result:', result); // result: SOME DATA! | |
}); | |
// ... v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tabbing error in this Gist *
Won't save as 2 tabs :/