wix-storybook-utils
Version:
Utilities for automated component documentation within Storybook
67 lines • 3.13 kB
JavaScript
;
var tslib_1 = require("tslib");
var loaderUtils = require('loader-utils');
var path = require('path');
var pathFinder = require('react-autodocs-utils/src/path-finder');
var gatherAll = require('react-autodocs-utils/src/gather-all');
var metadataMerger = require('react-autodocs-utils/src/metadata-merger');
var prepareStory = require('./prepareStory/prepareStory');
var applyPlugins = function (_a) {
var _b = _a.plugins, plugins = _b === void 0 ? [] : _b, source = _a.source, basePath = _a.basePath;
return function (metadata) {
if (metadata === void 0) { metadata = {}; }
return plugins.reduce(function (promise, pluginPath) {
var plugin;
try {
plugin = require(pluginPath);
}
catch (e) {
console.warn("ERROR: could not require wix-storybook-utils plugin module \"".concat(pluginPath, "\""));
return promise;
}
if (typeof plugin !== 'function') {
console.warn("ERROR: wix-storybook-utils plugin \"".concat(pluginPath, "\" is not a function"));
return promise;
}
return promise
.then(function (accumulatedMetadata) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, plugin({ source: source, metadata: accumulatedMetadata, basePath: basePath })];
case 1: return [2 /*return*/, (_a.sent())
.metadata];
}
});
}); })
.catch(function (e) {
console.warn("ERROR: failure with custom wix-storybook-utils plugin \"".concat(pluginPath, "\""), e);
return promise;
});
}, Promise.resolve(metadata));
};
};
module.exports = function (source) {
var _this = this;
var callback = this.async();
var _a = loaderUtils.getOptions(this), storyConfig = _a.storyConfig, plugins = _a.plugins;
// 1. find component path
pathFinder(source)
.then(function (componentPath) {
// 2. get component metadata
var metadata = componentPath
? gatherAll(path.join(_this.context, componentPath), storyConfig.parserOptions)
: Promise.resolve({});
return metadata
.then(applyPlugins({ source: source, plugins: plugins, basePath: _this.context })) // 3. apply plugged in analyzers
.then(metadataMerger(source)) // 4. merge component metadata with storybook config
.then(prepareStory(storyConfig, _this.context)); // 5. import and wrap with `story` function
})
// 5. succeed with augmented source
.then(function (finalSource) { return callback(null, finalSource); })
// otherwise callback with error
.catch(function (e) {
console.log('ERROR: Failure within story loader', e);
callback(e);
});
};
//# sourceMappingURL=index.js.map