@storybook/react-native
Version:
A better way to develop React Native Components for your app
198 lines (197 loc) • 6.76 kB
JavaScript
const require_chunk = require("./chunk-Ble4zEEl.js");
let storybook_internal_preview_api = require("storybook/internal/preview-api");
let storybook_internal_csf = require("storybook/internal/csf");
let path = require("path");
path = require_chunk.__toESM(path);
let storybook_internal_common = require("storybook/internal/common");
let node_fs = require("node:fs");
let glob = require("glob");
let storybook_internal_csf_tools = require("storybook/internal/csf-tools");
//#region scripts/common.js
var require_common = /* @__PURE__ */ require_chunk.__commonJSMin(((exports, module) => {
const { globToRegexp } = require("storybook/internal/common");
const path$2 = require("path");
const fs = require("fs");
const cwd = process.cwd();
const toRequireContext = (specifier) => {
const { directory, files } = specifier;
const match = globToRegexp(`./${files}`);
return {
path: directory,
recursive: files.includes("**") || files.split("/").length > 1,
match
};
};
const supportedExtensions = [
"js",
"jsx",
"ts",
"tsx",
"cjs",
"mjs"
];
function getFilePathExtension({ configPath }, fileName) {
for (const ext of supportedExtensions) {
const filePath = path$2.resolve(cwd, configPath, `${fileName}.${ext}`);
if (fs.existsSync(filePath)) return ext;
}
return null;
}
function getFilePathWithExtension({ configPath }, fileName) {
for (const ext of supportedExtensions) {
const filePath = path$2.resolve(cwd, configPath, `${fileName}.${ext}`);
if (fs.existsSync(filePath)) return filePath;
}
return null;
}
function ensureRelativePathHasDot(relativePath) {
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
}
function getPreviewExists({ configPath }) {
return !!getFilePathExtension({ configPath }, "preview");
}
function resolveAddonFile(addon, file, extensions = [
"js",
"mjs",
"ts"
], configPath) {
if (!addon || typeof addon !== "string") return null;
const resolvePaths = { paths: [cwd] };
try {
const basePath = `${addon}/${file}`;
require.resolve(basePath, resolvePaths);
return basePath;
} catch (_error) {}
for (const ext of extensions) try {
const filePath = `${addon}/${file}.${ext}`;
require.resolve(filePath, resolvePaths);
return filePath;
} catch (_error) {}
if (addon.startsWith("./") || addon.startsWith("../")) try {
if (getFilePathExtension({ configPath }, `${addon}/${file}`)) return `${addon}/${file}`;
} catch (_error) {}
return null;
}
function getAddonName(addon) {
if (typeof addon === "string") return addon;
if (typeof addon === "object" && addon.name && typeof addon.name === "string") return addon.name;
console.error("Invalid addon configuration", addon);
return null;
}
module.exports = {
toRequireContext,
getFilePathExtension,
ensureRelativePathHasDot,
getPreviewExists,
resolveAddonFile,
getAddonName,
getFilePathWithExtension
};
}));
//#endregion
//#region src/metro/buildIndex.ts
var buildIndex_exports = /* @__PURE__ */ require_chunk.__exportAll({ buildIndex: () => buildIndex });
var import_common = require_common();
const cwd = process.cwd();
const makeTitle = (fileName, specifier, userTitle) => {
const title = (0, storybook_internal_preview_api.userOrAutoTitleFromSpecifier)(fileName, specifier, userTitle);
if (title) return title.replace("./", "");
else if (userTitle) return userTitle.replace("./", "");
else {
console.error("Could not generate title!!");
process.exit(1);
}
};
function ensureRelativePathHasDot(relativePath) {
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
}
async function buildIndex({ configPath }) {
const main = await (0, storybook_internal_common.loadMainConfig)({
configDir: configPath,
cwd
});
if (!main.stories || !Array.isArray(main.stories)) throw new Error("No stories found");
const storiesSpecifiers = (0, storybook_internal_common.normalizeStories)(main.stories, {
configDir: configPath,
workingDir: cwd
});
const csfStories = storiesSpecifiers.map((specifier) => {
return (0, glob.sync)(specifier.files, {
cwd: path.default.resolve(process.cwd(), specifier.directory),
absolute: true,
ignore: ["**/node_modules"]
}).map((storyPath) => {
const normalizePathForWindows = (str) => path.default.sep === "\\" ? str.replace(/\\/g, "/") : str;
return normalizePathForWindows(storyPath);
});
}).reduce((acc, specifierStoryPathList, specifierIndex) => {
const paths = specifierStoryPathList.map((storyPath) => {
const code = (0, node_fs.readFileSync)(storyPath, { encoding: "utf-8" }).toString();
const relativePath = ensureRelativePathHasDot(path.default.posix.relative(cwd, storyPath));
return {
result: (0, storybook_internal_csf_tools.loadCsf)(code, {
fileName: storyPath,
makeTitle: (userTitle) => makeTitle(relativePath, storiesSpecifiers[specifierIndex], userTitle)
}).parse(),
specifier: storiesSpecifiers[specifierIndex],
fileName: relativePath
};
});
return [...acc, ...paths];
}, new Array());
const index = {
v: 5,
entries: {}
};
for (const { result, specifier, fileName } of csfStories) {
const { meta, stories } = result;
if (stories && stories.length > 0) for (const story of stories) {
const id = story.id ?? (0, storybook_internal_csf.toId)(meta.title, story.name);
if (!id) throw new Error(`Failed to generate id for story ${story.name} in file ${fileName}`);
index.entries[id] = {
type: "story",
subtype: "story",
id,
name: story.name,
title: meta.title,
importPath: `${specifier.directory}/${path.default.posix.relative(specifier.directory, fileName)}`,
tags: ["story"]
};
}
else console.log(`No stories found for ${fileName}`);
}
try {
const storySort = (0, storybook_internal_csf_tools.getStorySortParameter)((0, node_fs.readFileSync)((0, import_common.getFilePathWithExtension)({ configPath }, "preview"), { encoding: "utf-8" }).toString());
const sortableStories = Object.values(index.entries);
(0, storybook_internal_preview_api.sortStoriesV7)(sortableStories, storySort, sortableStories.map((entry) => entry.importPath));
return {
v: 5,
entries: sortableStories.reduce((acc, item) => {
acc[item.id] = item;
return acc;
}, {})
};
} catch {
console.warn("Failed to sort stories, using unordered index");
return index;
}
}
//#endregion
Object.defineProperty(exports, "buildIndex", {
enumerable: true,
get: function() {
return buildIndex;
}
});
Object.defineProperty(exports, "buildIndex_exports", {
enumerable: true,
get: function() {
return buildIndex_exports;
}
});
Object.defineProperty(exports, "require_common", {
enumerable: true,
get: function() {
return require_common;
}
});