-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
stories.ts
69 lines (59 loc) · 2.01 KB
/
stories.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
convertNxGenerator,
getProjects,
Tree,
visitNotIgnoredFiles,
} from '@nrwl/devkit';
import { join } from 'path';
import componentStoryGenerator from '../component-story/component-story';
import { StorybookStoriesSchema } from './schema';
import {
containsComponentDeclaration,
projectRootPath,
} from '@nrwl/react/src/generators/stories/stories';
import { isTheFileAStory } from '@nrwl/storybook/src/utils/utilities';
export async function createAllStories(tree: Tree, projectName: string) {
const projects = getProjects(tree);
const project = projects.get(projectName);
const { sourceRoot, projectType } = project;
const projectPath = projectRootPath(tree, sourceRoot, projectType);
let componentPaths: string[] = [];
visitNotIgnoredFiles(tree, projectPath, (path) => {
if (
(path.endsWith('.tsx') && !path.endsWith('.spec.tsx')) ||
(path.endsWith('.js') && !path.endsWith('.spec.js')) ||
(path.endsWith('.jsx') && !path.endsWith('.spec.jsx'))
) {
// Check if file is NOT a story (either ts/tsx or js/jsx)
if (!isTheFileAStory(tree, path)) {
// Since the file is not a story
// Let's see if the .stories.* file exists
const ext = path.slice(path.lastIndexOf('.'));
const storyPath = `${path.split(ext)[0]}.stories${ext}`;
if (!tree.exists(storyPath)) {
componentPaths.push(path);
}
}
}
});
await Promise.all(
componentPaths.map(async (componentPath) => {
const relativeCmpDir = componentPath.replace(join(sourceRoot, '/'), '');
if (!containsComponentDeclaration(tree, componentPath)) {
return;
}
await componentStoryGenerator(tree, {
componentPath: relativeCmpDir,
project: projectName,
});
})
);
}
export async function storiesGenerator(
host: Tree,
schema: StorybookStoriesSchema
) {
await createAllStories(host, schema.project);
}
export default storiesGenerator;
export const storiesSchematic = convertNxGenerator(storiesGenerator);