-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added inital version of the documentation generation for config files
- Loading branch information
Showing
7 changed files
with
580 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { isPlainObject, isString, isFunction } from 'lodash'; | ||
|
||
import { toCliFlag } from './helpers'; | ||
|
||
export default function buildDocumentationObject( | ||
initalObject, initalGroups, initalDescriptions, initalValidations, initalFilter = [] | ||
) { | ||
const allObjects = (object, callback) => { | ||
return Object.keys(object).map(callback).filter((value) => value !== undefined); | ||
}; | ||
|
||
const getDefaultValue = (object) => { | ||
if (Array.isArray(object) && !object.length || | ||
isString(object) && !object || | ||
isPlainObject(object) && Object.keys(object).length === 0) { | ||
return null; | ||
} | ||
|
||
return JSON.stringify(object); | ||
}; | ||
|
||
const manageGroup = (object, name, group = {}, description, validation, parents, level) => { | ||
const groupDescription = isPlainObject(group) ? undefined : group; | ||
return { | ||
name, | ||
level, | ||
description: groupDescription, | ||
objects: recursiveHelper(object, group, description, validation, [], parents, level + 1, true), | ||
children: recursiveHelper(object, group, description, validation, [], parents, level + 1) | ||
}; | ||
}; | ||
|
||
const manageLeaf = (object, name, description, validation = () => ({type: 'Unknown'}), parents) => { | ||
const { type, required } = isFunction(validation) ? | ||
validation(null, true) : | ||
({type: validation.toString(), req: false }); | ||
|
||
return { | ||
name, | ||
description, | ||
type, | ||
required, | ||
path: parents.join('.'), | ||
cli: toCliFlag(parents), | ||
defaultValue: getDefaultValue(object) | ||
}; | ||
}; | ||
|
||
function recursiveHelper(object, groups = {}, descriptions = {}, validations = {}, filter = [], | ||
initalParents = [], level = 0, leaves = false) { | ||
return allObjects(object, (key) => { | ||
// Make sure that we either have no filter or that there is a match | ||
if (filter.length === 0 || filter.indexOf(key) !== -1) { | ||
const parents = [].concat(initalParents, key); | ||
const value = object[key]; | ||
if (isPlainObject(value) && Object.keys(value).length > 0 && !leaves) { | ||
return manageGroup(value, key, groups[key], descriptions[key], validations[key], parents, level); | ||
} else if ((!isPlainObject(value) || Object.keys(value).length === 0) && leaves) { | ||
return manageLeaf(value, key, descriptions[key], validations[key], parents); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return recursiveHelper(initalObject, initalGroups, initalDescriptions, initalValidations, initalFilter); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import deepExtend from 'deep-extend'; | ||
import { isUndefined, isString } from 'lodash'; | ||
import stripAnsi from 'strip-ansi'; | ||
|
||
import { pad, addPadding } from './helpers'; | ||
|
||
const defaultSettings = { | ||
groupTitleWrapper: (name) => name, | ||
cellDivider: '|', | ||
rowWrapper: (input) => `|${input}|`, | ||
cellWrapper: (input) => ` ${input} `, | ||
header: true, | ||
compact: false | ||
}; | ||
|
||
export default function generateTable(initalDocumentationObject, header, settings) { | ||
settings = deepExtend({}, defaultSettings, settings); | ||
const headerLengths = createLengthObject(header, header, {}, true); | ||
const lengths = createLengthObject(initalDocumentationObject, header, headerLengths); | ||
|
||
const printTableCell = (key, element, renderer = (input) => input, fill = false) => { | ||
if (fill) { | ||
return settings.cellWrapper(pad(lengths[key], '-')); | ||
} | ||
const padding = isUndefined(header[key].padding) ? true : header[key].padding; | ||
const text = padding ? | ||
addPadding(renderer(element), lengths[key]) : | ||
renderer(element); | ||
|
||
return settings.cellWrapper(text); | ||
}; | ||
|
||
const printTableRow = (object, isHeader = false) => { | ||
return settings.rowWrapper(Object.keys(header).map((key) => { | ||
const { value, renderer } = getValueAndRenderer(isHeader, header[key], object[key]); | ||
|
||
return printTableCell(key, value, renderer); | ||
}).join(settings.cellDivider)); | ||
}; | ||
|
||
const printTableSplitter = () => { | ||
return settings.rowWrapper(Object.keys(header).map((key) => | ||
printTableCell(key, undefined, undefined, true) | ||
).join(settings.cellDivider)); | ||
}; | ||
|
||
const printTableHead = (name, description, level) => { | ||
const rows = []; | ||
rows.push(settings.groupTitleWrapper(name, level)); | ||
|
||
if (description) { | ||
rows.push(description); | ||
} | ||
|
||
// Add a new empty row | ||
rows.push(''); | ||
|
||
if (settings.header) { | ||
rows.push(printTableRow(header, true)); | ||
rows.push(printTableSplitter()); | ||
} | ||
|
||
return rows; | ||
}; | ||
|
||
const recursiveHelper = (documentationObject) => { | ||
const spacing = settings.compact ? [] : ['']; | ||
|
||
return documentationObject.map((group) => { | ||
let rows = []; | ||
if (group.level === 0 || !settings.compact) { | ||
rows = rows.concat(printTableHead(group.name, group.description, group.level)); | ||
} | ||
|
||
rows = rows.concat(group.objects.map((element) => | ||
printTableRow(element))); | ||
|
||
rows = rows.concat(recursiveHelper(group.children)); | ||
|
||
if (group.level === 0 && settings.compact) { | ||
rows.push(''); | ||
} | ||
|
||
return rows; | ||
}).reduce((a, b) => a.concat(b), spacing); | ||
}; | ||
|
||
return recursiveHelper(initalDocumentationObject).join('\n'); | ||
} | ||
|
||
function createLengthObject(initalElements, header, initalLengths, isHeader = false) { | ||
const getObjectLength = (object, renderer = (input) => input) => { | ||
return stripAnsi(renderer(object)).length; | ||
}; | ||
|
||
const getLength = (newLength, currentLength = 0) => { | ||
return newLength > currentLength ? newLength : currentLength; | ||
}; | ||
|
||
const recursiveHelper = (elements, lengths = {}) => { | ||
let newLengths = { ...lengths }; | ||
elements.forEach((element) => { | ||
element.objects.forEach((object) => { | ||
Object.keys(header).forEach((key) => { | ||
const { value, renderer } = getValueAndRenderer(isHeader, header[key], object[key]); | ||
if (isString(value)) { | ||
newLengths[key] = getLength(getObjectLength(value, renderer), newLengths[key]); | ||
} | ||
}); | ||
}); | ||
|
||
newLengths = recursiveHelper(element.children, newLengths); | ||
}); | ||
|
||
return newLengths; | ||
}; | ||
|
||
// Make sure the data matches the expected format | ||
if (!Array.isArray(initalElements)) { | ||
initalElements = [{objects: [initalElements], children: []}]; | ||
} | ||
|
||
return recursiveHelper(initalElements, initalLengths); | ||
} | ||
|
||
function getValueAndRenderer(isHeader, headerObject, object) { | ||
const value = isHeader ? object.name : object; | ||
const renderer = isHeader ? (input) => input : headerObject.renderer; | ||
|
||
return { | ||
value, | ||
renderer | ||
}; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import stripAnsi from 'strip-ansi'; | ||
|
||
export function pad(length, character = ' ') { | ||
return Array(length + 1).join(character); | ||
} | ||
|
||
export function addPadding(string, length) { | ||
string = string || ''; | ||
return string + pad(length - stripAnsi(string).length); | ||
} | ||
|
||
export function toCliFlag(configPaths) { | ||
// Runtime should be added directly | ||
if (configPaths[0] === 'runtime') { | ||
configPaths.shift(); | ||
} | ||
return '--' + configPaths.join('-'); | ||
} |
This file contains 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
Oops, something went wrong.