@nx/react-native
Version:
58 lines (57 loc) • 3.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = reactNativeStorybookExecutor;
exports.runCliStorybook = runCliStorybook;
const node_fs_1 = require("node:fs");
const path_1 = require("path");
const devkit_1 = require("@nx/devkit");
const fileutils_1 = require("@nx/workspace/src/utilities/fileutils");
const pc = require("picocolors");
const glob_1 = require("glob");
const sync_deps_impl_1 = require("../sync-deps/sync-deps.impl");
/**
* TODO (@xiongemi): remove this function in v20.
* @deprecated Going to use the default react storybook target. Use @nx/react:storybook executor instead.
*/
async function* reactNativeStorybookExecutor(options, context) {
const { syncDeps: isSyncDepsEnabled = true } = options;
const projectRoot = context.projectsConfigurations.projects[context.projectName].root;
devkit_1.logger.info(`${pc.bold(pc.cyan('info'))} To see your Storybook stories on the device, you should start your mobile app for the <platform> of your choice (typically ios or android).`);
// add storybook addons to app's package.json
const packageJsonPath = (0, path_1.join)(context.root, projectRoot, 'package.json');
const workspacePackageJsonPath = (0, path_1.join)(context.root, 'package.json');
const workspacePackageJson = (0, devkit_1.readJsonFile)(workspacePackageJsonPath);
const projectPackageJson = (0, devkit_1.readJsonFile)(packageJsonPath);
if (isSyncDepsEnabled && (0, fileutils_1.fileExists)(packageJsonPath))
(0, sync_deps_impl_1.displayNewlyAddedDepsMessage)(context.projectName, await (0, sync_deps_impl_1.syncDeps)(context.projectName, projectPackageJson, packageJsonPath, workspacePackageJson, context.projectGraph, [
`@storybook/react-native`,
'@storybook/addon-ondevice-actions',
'@storybook/addon-ondevice-backgrounds',
'@storybook/addon-ondevice-controls',
'@storybook/addon-ondevice-notes',
'@react-native-async-storage/async-storage',
'react-native-safe-area-context',
]));
runCliStorybook(context.root, options);
yield { success: true };
}
function runCliStorybook(workspaceRoot, options) {
const storiesFiles = options.searchDir.flatMap((dir) => {
const storyFilePaths = (0, glob_1.sync)((0, path_1.join)(dir, options.pattern));
return storyFilePaths.map((storyFilePath) => {
const loaderPath = (0, path_1.resolve)((0, path_1.dirname)(options.outputFile));
return (0, path_1.relative)(loaderPath, storyFilePath);
});
});
if (storiesFiles.length === 0) {
devkit_1.logger.warn(`${pc.bold(pc.yellow('warn'))} No stories found.`);
}
const newContents = `// Auto-generated file created by nx
// DO NOT EDIT.
export function loadStories() {
return [
${storiesFiles.map((story) => `require('${story}')`).join(',\n')}
];
}`;
(0, node_fs_1.writeFileSync)((0, path_1.join)(workspaceRoot, options.outputFile), newContents);
}
;