/
create-storybook-files.ts
77 lines (67 loc) · 1.88 KB
/
create-storybook-files.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
70
71
72
73
74
75
76
77
import {
generateFiles,
logger,
offsetFromRoot,
readProjectConfiguration,
toJS,
Tree,
} from '@nrwl/devkit';
import { join } from 'path';
import * as chalk from 'chalk';
import { StorybookConfigureSchema } from '../schema';
/**
* This function generate ./storybook under project root.
*/
export async function createStorybookFiles(
host: Tree,
schema: StorybookConfigureSchema
) {
const { root, projectType, targets, sourceRoot } = readProjectConfiguration(
host,
schema.name
);
// do not proceed if not a react native project
if (targets?.start?.executor !== '@nrwl/react-native:start') {
return;
}
const storybookUIFileName = schema.js ? 'storybook.js' : 'storybook.ts';
const storybookUIFilePath = join(root, `./${storybookUIFileName}`);
if (host.exists(storybookUIFilePath)) {
logger.warn(
`${storybookUIFileName} file already exists for React Native ${projectType} ${schema.name}! Skipping generating this file.`
);
return;
}
if (projectType !== 'application') {
logger.info(
`${chalk.bold.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).`
);
}
const projectDirectory = projectType === 'application' ? 'app' : 'lib';
logger.debug(`Adding storybook file to React Native app ${projectDirectory}`);
// copy files to app's .storybook folder
generateFiles(
host,
join(__dirname, '../files/app'),
join(root, 'src', 'storybook'),
{
tmpl: '',
offsetFromRoot: offsetFromRoot(sourceRoot),
projectType: projectDirectory,
}
);
// copy files to workspace root's .storybook folder
generateFiles(
host,
join(__dirname, '../files/root'),
join(root, offsetFromRoot(root), '.storybook'),
{
tmpl: '',
}
);
if (schema.js) {
toJS(host);
}
}