Skip to content

Commit

Permalink
Add ability to specify asynd functions in json data that resolve usin…
Browse files Browse the repository at this point in the history
…g the key, previous value and object. Remove file.data overriding file.type. They now compose together.
  • Loading branch information
treshugart committed Jun 22, 2019
1 parent 2a7e12d commit 69bf6d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/handler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
const fs = require("fs-extra");
const isFunction = require("lodash/isFunction");
const merge = require("lodash/merge");
const mergeWith = require("lodash/mergeWith");
const path = require("path");
const prettier = require("prettier");
const stripIndent = require("strip-indent");
const uniq = require("lodash/uniq");

// Recursively maps over values and allows async functions to be used to
// return new values. If a previousObj is provided, it uses it to retrieve
// a value that corresponds to the same place in oldObj so you can use it
// to return the new value.
async function mapValues(oldObj, previousObj) {
previousObj = previousObj || {};
const newObj = Array.isArray(oldObj) ? [] : {};
for (const key in oldObj) {
newObj[key] = isFunction(oldObj[key])
? await oldObj[key](key, previousObj[key], previousObj)
: oldObj[key];
if (typeof newObj[key] === "object") {
newObj[key] = await mapValues(newObj[key], previousObj[key]);
}
}
return newObj;
}

// Allows for custom merging.
function merge(...src) {
return mergeWith({}, ...src, (objval, srcval, key, obj) => {
// Remove item if explicitly set to undefined.
if (typeof srcval === "undefined") {
delete obj[key];
}
});
}

async function getPrettierConfig(file) {
return await prettier.resolveConfig(file);
}
Expand Down Expand Up @@ -57,6 +85,8 @@ async function handleJson(file) {
data = file.data;
}

data = await mapValues(data, currJson);

return JSON.stringify(data, null, 2);
}

Expand Down Expand Up @@ -88,13 +118,13 @@ const mapType = {
object: handleJson
};

async function getData(file) {
return isFunction(file.data) ? await file.data(file) : file.data;
}

function getType(file) {
let type;

if (isFunction(file.data)) {
return file.data;
}

if (isFunction(file.type)) {
return file.type;
}
Expand All @@ -118,7 +148,9 @@ function getType(file) {
}

async function handler(file) {
return await getType(file)(file);
file.data = await getData(file);
file.type = await getType(file);
return file.type(file);
}

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ async function sync(cfg, opt) {
// and in the same convention, files at the top level override everything
// else before it.
for (let file of cfg.files) {
// We copy file so that we can mutate it without affecting the original
// copy.
file = {
...cfg.fileDefaults,
...file,
Expand Down

0 comments on commit 69bf6d8

Please sign in to comment.