@emdaer/core
Advanced tools
Comparing version 1.0.0-alpha.5 to 1.0.0-alpha.7
jest.mock('fs-extra'); | ||
jest.mock('emdaer-plugin-heading'); | ||
jest.mock('emdaer-transform-twice'); | ||
jest.mock('emdaer-plugin-linebreak'); | ||
jest.mock('@emdaer/plugin-heading'); | ||
jest.mock('@emdaer/plugin-stars'); | ||
jest.mock('@emdaer/transform-twice'); | ||
@@ -12,3 +12,2 @@ const fs = require('fs-extra'); | ||
NO_GENERATED, | ||
NO_UTILITY, | ||
NO_PLUGIN, | ||
@@ -19,3 +18,3 @@ NO_TRANSFORM, | ||
describe('emdaer-core', () => { | ||
describe('@emdaer/core', () => { | ||
describe('#constructor', () => { | ||
@@ -26,3 +25,3 @@ test('throws when destination is not provided', () => { | ||
destination: '', | ||
content: ['linebreak'], | ||
content: ['stars'], | ||
}); | ||
@@ -40,9 +39,2 @@ }).toThrow(NO_DESTINATION); | ||
}); | ||
describe('#resolveUtility', () => { | ||
test('throws when specified utility is not available', () => { | ||
expect(() => { | ||
Emdaer.resolveUtility('not-a-thing'); | ||
}).toThrow(NO_UTILITY); | ||
}); | ||
}); | ||
describe('#resolvePlugin', () => { | ||
@@ -65,14 +57,6 @@ test('throws when specified plugin is not available', () => { | ||
expect( | ||
await Emdaer.applyTransforms( | ||
[{ transform: 'twice' }], | ||
'alrightalrightalright' | ||
) | ||
await Emdaer.applyTransforms([['twice']], 'alrightalrightalright') | ||
).toBe('alrightalrightalrightalrightalrightalright'); | ||
}); | ||
}); | ||
describe('#executeUtility', () => { | ||
test('executes utilities', async () => { | ||
expect(await Emdaer.executeUtility('stars', {})).toBe(0.7734); | ||
}); | ||
}); | ||
describe('#handleNested', () => { | ||
@@ -82,6 +66,6 @@ test('handles nested plugins', async () => { | ||
await Emdaer.handleNested({ | ||
from: { utility: 'heading', options: { content: 'Hello, World!' } }, | ||
from: ['heading', { content: 'Hello, World!' }], | ||
}) | ||
).toEqual({ | ||
from: { utility: 'heading', options: { content: 'Hello, World!' } }, | ||
from: ['heading', { content: 'Hello, World!' }], | ||
content: '<h1>Hello, World!</h1>', | ||
@@ -91,6 +75,4 @@ }); | ||
test('handles nested utilities', async () => { | ||
expect( | ||
await Emdaer.handleNested({ from: { utility: 'stars' } }) | ||
).toEqual({ | ||
from: { utility: 'stars' }, | ||
expect(await Emdaer.handleNested({ from: ['stars'] })).toEqual({ | ||
from: ['stars'], | ||
content: 0.7734, | ||
@@ -109,8 +91,3 @@ }); | ||
destination: './README.md', | ||
content: [ | ||
{ | ||
plugin: 'heading', | ||
options: { content: 'Hello, World!' }, | ||
}, | ||
], | ||
content: [['heading', { content: 'Hello, World!' }]], | ||
}); | ||
@@ -125,8 +102,3 @@ await readme.generate(); | ||
destination: './README.md', | ||
content: [ | ||
{ | ||
plugin: 'heading', | ||
options: { content: 'Hello, World!' }, | ||
}, | ||
], | ||
content: [['heading', { content: 'Hello, World!' }]], | ||
}); | ||
@@ -138,8 +110,3 @@ expect(await readme.generate()).toBe('<h1>Hello, World!</h1>\n'); | ||
destination: './README.md', | ||
content: [ | ||
{ | ||
plugin: 'heading', | ||
options: { content: 'Hello, World!' }, | ||
}, | ||
], | ||
content: [['heading', { content: 'Hello, World!' }]], | ||
}); | ||
@@ -158,9 +125,4 @@ await readme.generate(); | ||
destination: filePath, | ||
content: [ | ||
{ | ||
plugin: 'heading', | ||
options: { content: 'Hello, World!' }, | ||
}, | ||
], | ||
transforms: [{ transform: 'twice' }], | ||
content: [['heading', { content: 'Hello, World!' }]], | ||
transforms: [['twice']], | ||
}); | ||
@@ -179,9 +141,4 @@ | ||
destination: './.tmp/README.md', | ||
content: [ | ||
{ | ||
plugin: 'heading', | ||
options: { content: 'Hello, World!' }, | ||
}, | ||
], | ||
transforms: [{ transform: 'twice' }], | ||
content: [['heading', { content: 'Hello, World!' }]], | ||
transforms: [['twice']], | ||
}); | ||
@@ -188,0 +145,0 @@ await expect(readme.write()).rejects.toEqual(Error(NO_GENERATED)); |
/* */ | ||
// #resolveUtility | ||
module.exports.NO_UTILITY = 'utility not found'; | ||
// #resolvePlugin | ||
@@ -7,0 +4,0 @@ module.exports.NO_PLUGIN = 'plugin not found'; |
/* */ | ||
/* eslint-disable no-use-before-define */ | ||
/** | ||
* A utility for emdaer | ||
* | ||
* @param {Options} options Whatever input a utility should need | ||
* @returns {Promise<mixed>} Fetched content | ||
*/ | ||
/** | ||
* A plugin for emdaer | ||
@@ -26,9 +20,2 @@ * | ||
/** | ||
* A description of a utility to call and which options to call it with | ||
* | ||
* @property {string} plugin The utility to invoke. Looks for installed modules named emdaer-utility-<utility> | ||
* @property {Options} options The options to call the utility with | ||
*/ | ||
/** | ||
* A description of a plugin to call and which options to call it with | ||
@@ -35,0 +22,0 @@ * |
@@ -5,3 +5,2 @@ /* */ | ||
const { | ||
NO_UTILITY, | ||
NO_PLUGIN, | ||
@@ -42,19 +41,2 @@ NO_TRANSFORM, | ||
/** | ||
* Loads a provided utility | ||
* @private | ||
*/ | ||
static resolveUtility(utilityName) { | ||
try { | ||
let utility; | ||
try { | ||
utility = require(`emdaer-utility-${utilityName}`); | ||
} catch (error) { | ||
utility = require(`emdaer-plugin-${utilityName}`); | ||
} | ||
return utility; | ||
} catch (error) { | ||
throw new Error(`${NO_UTILITY}: ${utilityName} \n ${error}`); | ||
} | ||
} | ||
/** | ||
* Loads a provided plugin | ||
@@ -65,3 +47,3 @@ * @private | ||
try { | ||
return require(`emdaer-plugin-${pluginName}`); | ||
return require(`@emdaer/plugin-${pluginName}`); | ||
} catch (error) { | ||
@@ -77,3 +59,3 @@ throw new Error(`${NO_PLUGIN}: ${pluginName}`); | ||
try { | ||
return require(`emdaer-transform-${transformName}`); | ||
return require(`@emdaer/transform-${transformName}`); | ||
} catch (error) { | ||
@@ -97,3 +79,3 @@ throw new Error(`${NO_TRANSFORM}: ${transformName}`); | ||
acc, | ||
{ transform, options } | ||
[transform, options] | ||
) => | ||
@@ -105,9 +87,2 @@ Emdaer.resolveTransform(transform)(await acc, options), | ||
/** | ||
* Executes a utility to fetch content | ||
* @private | ||
*/ | ||
static async executeUtility(utility, options) { | ||
return Emdaer.resolveUtility(utility)(await Emdaer.handleNested(options)); | ||
} | ||
/** | ||
* Handles nested utilities and plugins | ||
@@ -119,12 +94,9 @@ * @private | ||
options && | ||
options.from && | ||
typeof options.from.utility === 'string' && | ||
(typeof options.from.options === 'object' || | ||
typeof options.from.options === 'undefined') | ||
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.executeUtility( | ||
options.from.utility, | ||
options.from.options | ||
), | ||
content: await Emdaer.executePlugin([utilityPlugin, utilityOptions]), | ||
}); | ||
@@ -138,3 +110,3 @@ } | ||
*/ | ||
static async executePlugin(plugin, options) { | ||
static async executePlugin([plugin, options]) { | ||
return Emdaer.resolvePlugin(plugin)(await Emdaer.handleNested(options)); | ||
@@ -150,4 +122,4 @@ } | ||
return Promise.all( | ||
content.map(({ plugin, options }) => | ||
Emdaer.executePlugin(plugin, options) | ||
content.map((pluginCall) => | ||
Emdaer.executePlugin(pluginCall) | ||
) | ||
@@ -154,0 +126,0 @@ ); |
{ | ||
"name": "@emdaer/core", | ||
"description": "emdaer core", | ||
"version": "1.0.0-alpha.5", | ||
"version": "1.0.0-alpha.7", | ||
"repository": "emdaer/emdaer", | ||
@@ -13,3 +13,3 @@ "homepage": "https://emdaer.github.io/", | ||
"scripts": { | ||
"prepare": "flow-remove-types -p src -d lib" | ||
"prepublish": "flow-remove-types -p src -d lib" | ||
}, | ||
@@ -16,0 +16,0 @@ "devDependencies": { |
/* @flow */ | ||
// #resolveUtility | ||
module.exports.NO_UTILITY = 'utility not found'; | ||
// #resolvePlugin | ||
@@ -7,0 +4,0 @@ module.exports.NO_PLUGIN = 'plugin not found'; |
/* @flow */ | ||
/* eslint-disable no-use-before-define */ | ||
type Options = ?{}; | ||
export type Options = ?{}; | ||
/** | ||
* A utility for emdaer | ||
* | ||
* @param {Options} options Whatever input a utility should need | ||
* @returns {Promise<mixed>} Fetched content | ||
*/ | ||
export type Utility = (options: Options) => Promise<string>; | ||
/** | ||
* A plugin for emdaer | ||
@@ -30,13 +23,2 @@ * | ||
/** | ||
* A description of a utility to call and which options to call it with | ||
* | ||
* @property {string} plugin The utility to invoke. Looks for installed modules named emdaer-utility-<utility> | ||
* @property {Options} options The options to call the utility with | ||
*/ | ||
export type UtilityCall = { | ||
utility: string, | ||
options: Options, | ||
}; | ||
/** | ||
* A description of a plugin to call and which options to call it with | ||
@@ -47,6 +29,3 @@ * | ||
*/ | ||
export type PluginCall = { | ||
plugin: string, | ||
options: Options, | ||
}; | ||
export type PluginCall = [string, Options]; | ||
@@ -59,5 +38,2 @@ /** | ||
*/ | ||
export type TransformCall = { | ||
transform: string, | ||
options: Options, | ||
}; | ||
export type TransformCall = [string, Options]; |
/* @flow */ | ||
import type { | ||
Utility, | ||
Plugin, | ||
PluginCall, | ||
Transform, | ||
TransformCall, | ||
} from './_types'; | ||
import type { Plugin, PluginCall, Transform, TransformCall } from './_types'; | ||
const { | ||
NO_UTILITY, | ||
NO_PLUGIN, | ||
@@ -56,19 +49,2 @@ NO_TRANSFORM, | ||
/** | ||
* Loads a provided utility | ||
* @private | ||
*/ | ||
static resolveUtility(utilityName: string): Utility { | ||
try { | ||
let utility; | ||
try { | ||
utility = require(`emdaer-utility-${utilityName}`); | ||
} catch (error) { | ||
utility = require(`emdaer-plugin-${utilityName}`); | ||
} | ||
return utility; | ||
} catch (error) { | ||
throw new Error(`${NO_UTILITY}: ${utilityName} \n ${error}`); | ||
} | ||
} | ||
/** | ||
* Loads a provided plugin | ||
@@ -79,3 +55,3 @@ * @private | ||
try { | ||
return require(`emdaer-plugin-${pluginName}`); | ||
return require(`@emdaer/plugin-${pluginName}`); | ||
} catch (error) { | ||
@@ -91,3 +67,3 @@ throw new Error(`${NO_PLUGIN}: ${pluginName}`); | ||
try { | ||
return require(`emdaer-transform-${transformName}`); | ||
return require(`@emdaer/transform-${transformName}`); | ||
} catch (error) { | ||
@@ -111,3 +87,3 @@ throw new Error(`${NO_TRANSFORM}: ${transformName}`); | ||
acc: Promise<string>, | ||
{ transform, options }: TransformCall | ||
[transform, options]: TransformCall | ||
): Promise<string> => | ||
@@ -119,25 +95,15 @@ Emdaer.resolveTransform(transform)(await acc, options), | ||
/** | ||
* Executes a utility to fetch content | ||
* @private | ||
*/ | ||
static async executeUtility(utility, options): Promise<mixed> { | ||
return Emdaer.resolveUtility(utility)(await Emdaer.handleNested(options)); | ||
} | ||
/** | ||
* Handles nested utilities and plugins | ||
* @private | ||
*/ | ||
static async handleNested(options): Promise<?{}> { | ||
static async handleNested(options: ?{}): Promise<?{}> { | ||
if ( | ||
options && | ||
options.from && | ||
typeof options.from.utility === 'string' && | ||
(typeof options.from.options === 'object' || | ||
typeof options.from.options === 'undefined') | ||
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.executeUtility( | ||
options.from.utility, | ||
options.from.options | ||
), | ||
content: await Emdaer.executePlugin([utilityPlugin, utilityOptions]), | ||
}); | ||
@@ -151,3 +117,3 @@ } | ||
*/ | ||
static async executePlugin(plugin, options): Promise<string> { | ||
static async executePlugin([plugin, options]: PluginCall): Promise<string> { | ||
return Emdaer.resolvePlugin(plugin)(await Emdaer.handleNested(options)); | ||
@@ -163,4 +129,4 @@ } | ||
return Promise.all( | ||
content.map(({ plugin, options }: PluginCall): Promise<string> => | ||
Emdaer.executePlugin(plugin, options) | ||
content.map((pluginCall: PluginCall): Promise<string> => | ||
Emdaer.executePlugin(pluginCall) | ||
) | ||
@@ -167,0 +133,0 @@ ); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
8
15419
506