@emdaer/core
Advanced tools
Comparing version 1.0.0-alpha.7 to 1.0.0-alpha.8
@@ -8,9 +8,3 @@ jest.mock('fs-extra'); | ||
const { | ||
NO_CONTENT, | ||
NO_DESTINATION, | ||
NO_GENERATED, | ||
NO_PLUGIN, | ||
NO_TRANSFORM, | ||
} = require('../src/_errors'); | ||
const { NO_CONTENT, NO_DESTINATION, NO_GENERATED } = require('../src/_errors'); | ||
const Emdaer = require('../src'); | ||
@@ -37,56 +31,2 @@ | ||
}); | ||
describe('#resolvePlugin', () => { | ||
test('throws when specified plugin is not available', () => { | ||
expect(() => { | ||
Emdaer.resolvePlugin('not-a-thing'); | ||
}).toThrow(NO_PLUGIN); | ||
}); | ||
}); | ||
describe('#resolveTransform', () => { | ||
test('throws when specified transform is not available', () => { | ||
expect(() => { | ||
Emdaer.resolveTransform('not-a-thing'); | ||
}).toThrow(NO_TRANSFORM); | ||
}); | ||
}); | ||
describe('#applyTransforms', () => { | ||
test('applies transforms', async () => { | ||
expect( | ||
await Emdaer.applyTransforms([['twice']], 'alrightalrightalright') | ||
).toBe('alrightalrightalrightalrightalrightalright'); | ||
}); | ||
}); | ||
describe('#handleNested', () => { | ||
test('handles nested plugins', async () => { | ||
expect( | ||
await Emdaer.handleNested({ | ||
from: ['heading', { content: 'Hello, World!' }], | ||
}) | ||
).toEqual({ | ||
from: ['heading', { content: 'Hello, World!' }], | ||
content: '<h1>Hello, World!</h1>', | ||
}); | ||
}); | ||
test('handles nested utilities', async () => { | ||
expect(await Emdaer.handleNested({ from: ['stars'] })).toEqual({ | ||
from: ['stars'], | ||
content: 0.7734, | ||
}); | ||
}); | ||
test('handles nothing being nested', async () => { | ||
expect(await Emdaer.handleNested({ content: 'weee' })).toEqual({ | ||
content: 'weee', | ||
}); | ||
}); | ||
}); | ||
describe('#executePlugins', () => { | ||
test('executes plugins', async () => { | ||
const readme = new Emdaer({ | ||
destination: './README.md', | ||
content: [['heading', { content: 'Hello, World!' }]], | ||
}); | ||
await readme.generate(); | ||
expect(readme.generated).toBe('<h1>Hello, World!</h1>\n'); | ||
}); | ||
}); | ||
describe('#generate', () => { | ||
@@ -93,0 +33,0 @@ test('returns generated document', async () => { |
/* */ | ||
/* eslint-disable no-use-before-define */ | ||
/** | ||
* A plugin for emdaer | ||
* | ||
* @param {Options} options Whatever input a plugin should need | ||
* @param {*} options Whatever input a plugin should need | ||
* @returns {Promise<string>} Generated content | ||
@@ -15,3 +14,3 @@ */ | ||
* | ||
* @param {Options} options Whatever input a transform should need | ||
* @param {*} options Whatever input a transform should need | ||
* @returns {Promise<string>} Transformed content | ||
@@ -23,4 +22,4 @@ */ | ||
* | ||
* @property {string} plugin The plugin to invoke. Looks for installed modules named emdaer-plugin-<plugin> | ||
* @property {Options} options The options to call the plugin with | ||
* @property {string} plugin The plugin to invoke. Looks for installed modules named emdaer-plugin-<plugin> | ||
* @property {*} options The options to call the plugin with | ||
*/ | ||
@@ -31,4 +30,4 @@ | ||
* | ||
* @property {string} plugin The transform to invoke. Looks for installed modules named emdaer-transform-<transform> | ||
* @property {Options} options The options to call the transform with | ||
* @property {string} plugin The transform to invoke. Looks for installed modules named emdaer-transform-<transform> | ||
* @property {*} options The options to call the transform with | ||
*/ |
103
lib/index.js
/* */ | ||
const { | ||
NO_PLUGIN, | ||
NO_TRANSFORM, | ||
NO_CONTENT, | ||
NO_DESTINATION, | ||
NO_GENERATED, | ||
} = require('./_errors'); | ||
const { NO_CONTENT, NO_DESTINATION, NO_GENERATED } = require('./_errors'); | ||
const combineContent = require('./util/combineContent'); | ||
const executePlugins = require('./util/executePlugins'); | ||
const applyTransforms = require('./util/applyTransforms'); | ||
const fs = require('fs-extra'); | ||
@@ -40,89 +38,2 @@ | ||
/** | ||
* Loads a provided plugin | ||
* @private | ||
*/ | ||
static resolvePlugin(pluginName) { | ||
try { | ||
return require(`@emdaer/plugin-${pluginName}`); | ||
} catch (error) { | ||
throw new Error(`${NO_PLUGIN}: ${pluginName}`); | ||
} | ||
} | ||
/** | ||
* Loads a provided transform | ||
* @private | ||
*/ | ||
static resolveTransform(transformName) { | ||
try { | ||
return require(`@emdaer/transform-${transformName}`); | ||
} catch (error) { | ||
throw new Error(`${NO_TRANSFORM}: ${transformName}`); | ||
} | ||
} | ||
/** | ||
* Applies provided transforms to generated content | ||
* @private | ||
*/ | ||
static async applyTransforms( | ||
transforms, | ||
content | ||
) { | ||
if (!transforms) { | ||
return Promise.resolve(content); | ||
} | ||
return transforms.reduce( | ||
async ( | ||
acc, | ||
[transform, options] | ||
) => | ||
Emdaer.resolveTransform(transform)(await acc, options), | ||
Promise.resolve(content) | ||
); | ||
} | ||
/** | ||
* Handles nested utilities and plugins | ||
* @private | ||
*/ | ||
static async handleNested(options) { | ||
if ( | ||
options && | ||
Array.isArray(options.from) && | ||
typeof options.from[0] === 'string' && | ||
(typeof options.from[1] === 'object' || options.from[1] === undefined) | ||
) { | ||
const [utilityPlugin, utilityOptions] = options.from; | ||
return Object.assign({}, options, { | ||
content: await Emdaer.executePlugin([utilityPlugin, utilityOptions]), | ||
}); | ||
} | ||
return options; | ||
} | ||
/** | ||
* Executes a plugin to generate content | ||
* @private | ||
*/ | ||
static async executePlugin([plugin, options]) { | ||
return Emdaer.resolvePlugin(plugin)(await Emdaer.handleNested(options)); | ||
} | ||
/** | ||
* Executes provided plugins to generate content | ||
* @private | ||
*/ | ||
static async executePlugins( | ||
content | ||
) { | ||
return Promise.all( | ||
content.map((pluginCall) => | ||
Emdaer.executePlugin(pluginCall) | ||
) | ||
); | ||
} | ||
/** | ||
* Combines the output of provided plugins into a single string | ||
* @private | ||
*/ | ||
static combineContent(content) { | ||
return content.reduce((acc, val) => `${acc}${val}\n`, ''); | ||
} | ||
/** | ||
* Generates the document | ||
@@ -133,5 +44,5 @@ * | ||
async generate() { | ||
this.generated = await Emdaer.applyTransforms( | ||
this.generated = await applyTransforms( | ||
this.transforms, | ||
Emdaer.combineContent(await Emdaer.executePlugins(this.content)) | ||
combineContent(await executePlugins(this.content)) | ||
); | ||
@@ -138,0 +49,0 @@ return this.generated; |
{ | ||
"name": "@emdaer/core", | ||
"description": "emdaer core", | ||
"version": "1.0.0-alpha.7", | ||
"version": "1.0.0-alpha.8", | ||
"repository": "emdaer/emdaer", | ||
@@ -6,0 +6,0 @@ "homepage": "https://emdaer.github.io/", |
/* @flow */ | ||
/* eslint-disable no-use-before-define */ | ||
export type Options = ?{}; | ||
/** | ||
* A plugin for emdaer | ||
* | ||
* @param {Options} options Whatever input a plugin should need | ||
* @param {*} options Whatever input a plugin should need | ||
* @returns {Promise<string>} Generated content | ||
*/ | ||
export type Plugin = (options: Options) => Promise<string>; | ||
export type Plugin = (options: *) => Promise<string>; | ||
@@ -17,6 +15,6 @@ /** | ||
* | ||
* @param {Options} options Whatever input a transform should need | ||
* @param {*} options Whatever input a transform should need | ||
* @returns {Promise<string>} Transformed content | ||
*/ | ||
export type Transform = (content: string, options: Options) => Promise<string>; | ||
export type Transform = (content: string, options: *) => Promise<string>; | ||
@@ -26,6 +24,6 @@ /** | ||
* | ||
* @property {string} plugin The plugin to invoke. Looks for installed modules named emdaer-plugin-<plugin> | ||
* @property {Options} options The options to call the plugin with | ||
* @property {string} plugin The plugin to invoke. Looks for installed modules named emdaer-plugin-<plugin> | ||
* @property {*} options The options to call the plugin with | ||
*/ | ||
export type PluginCall = [string, Options]; | ||
export type PluginCall = [string, *]; | ||
@@ -35,5 +33,5 @@ /** | ||
* | ||
* @property {string} plugin The transform to invoke. Looks for installed modules named emdaer-transform-<transform> | ||
* @property {Options} options The options to call the transform with | ||
* @property {string} plugin The transform to invoke. Looks for installed modules named emdaer-transform-<transform> | ||
* @property {*} options The options to call the transform with | ||
*/ | ||
export type TransformCall = [string, Options]; | ||
export type TransformCall = [string, *]; |
105
src/index.js
/* @flow */ | ||
import type { Plugin, PluginCall, Transform, TransformCall } from './_types'; | ||
import type { PluginCall, TransformCall } from './_types'; | ||
const { | ||
NO_PLUGIN, | ||
NO_TRANSFORM, | ||
NO_CONTENT, | ||
NO_DESTINATION, | ||
NO_GENERATED, | ||
} = require('./_errors'); | ||
const { NO_CONTENT, NO_DESTINATION, NO_GENERATED } = require('./_errors'); | ||
const combineContent = require('./util/combineContent'); | ||
const executePlugins = require('./util/executePlugins'); | ||
const applyTransforms = require('./util/applyTransforms'); | ||
const fs = require('fs-extra'); | ||
@@ -49,89 +47,2 @@ | ||
/** | ||
* Loads a provided plugin | ||
* @private | ||
*/ | ||
static resolvePlugin(pluginName: string): Plugin { | ||
try { | ||
return require(`@emdaer/plugin-${pluginName}`); | ||
} catch (error) { | ||
throw new Error(`${NO_PLUGIN}: ${pluginName}`); | ||
} | ||
} | ||
/** | ||
* Loads a provided transform | ||
* @private | ||
*/ | ||
static resolveTransform(transformName: string): Transform { | ||
try { | ||
return require(`@emdaer/transform-${transformName}`); | ||
} catch (error) { | ||
throw new Error(`${NO_TRANSFORM}: ${transformName}`); | ||
} | ||
} | ||
/** | ||
* Applies provided transforms to generated content | ||
* @private | ||
*/ | ||
static async applyTransforms( | ||
transforms: ?Array<TransformCall>, | ||
content: string | ||
): Promise<string> { | ||
if (!transforms) { | ||
return Promise.resolve(content); | ||
} | ||
return transforms.reduce( | ||
async ( | ||
acc: Promise<string>, | ||
[transform, options]: TransformCall | ||
): Promise<string> => | ||
Emdaer.resolveTransform(transform)(await acc, options), | ||
Promise.resolve(content) | ||
); | ||
} | ||
/** | ||
* Handles nested utilities and plugins | ||
* @private | ||
*/ | ||
static async handleNested(options: ?{}): Promise<?{}> { | ||
if ( | ||
options && | ||
Array.isArray(options.from) && | ||
typeof options.from[0] === 'string' && | ||
(typeof options.from[1] === 'object' || options.from[1] === undefined) | ||
) { | ||
const [utilityPlugin, utilityOptions] = options.from; | ||
return Object.assign({}, options, { | ||
content: await Emdaer.executePlugin([utilityPlugin, utilityOptions]), | ||
}); | ||
} | ||
return options; | ||
} | ||
/** | ||
* Executes a plugin to generate content | ||
* @private | ||
*/ | ||
static async executePlugin([plugin, options]: PluginCall): Promise<string> { | ||
return Emdaer.resolvePlugin(plugin)(await Emdaer.handleNested(options)); | ||
} | ||
/** | ||
* Executes provided plugins to generate content | ||
* @private | ||
*/ | ||
static async executePlugins( | ||
content: Array<PluginCall> | ||
): Promise<Array<string>> { | ||
return Promise.all( | ||
content.map((pluginCall: PluginCall): Promise<string> => | ||
Emdaer.executePlugin(pluginCall) | ||
) | ||
); | ||
} | ||
/** | ||
* Combines the output of provided plugins into a single string | ||
* @private | ||
*/ | ||
static combineContent(content: Array<string>): string { | ||
return content.reduce((acc: string, val: string) => `${acc}${val}\n`, ''); | ||
} | ||
/** | ||
* Generates the document | ||
@@ -142,5 +53,5 @@ * | ||
async generate(): Promise<string> { | ||
this.generated = await Emdaer.applyTransforms( | ||
this.generated = await applyTransforms( | ||
this.transforms, | ||
Emdaer.combineContent(await Emdaer.executePlugins(this.content)) | ||
combineContent(await executePlugins(this.content)) | ||
); | ||
@@ -147,0 +58,0 @@ return this.generated; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25
15263
470
1