/
storybook.impl.ts
95 lines (86 loc) · 2.55 KB
/
storybook.impl.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { join } from 'path';
import { ExecutorContext, logger } from '@nrwl/devkit';
import { fileExists } from '@nrwl/workspace/src/utilities/fileutils';
import * as chalk from 'chalk';
import { ReactNativeStorybookOptions } from './schema';
import { ChildProcess, fork } from 'child_process';
import {
displayNewlyAddedDepsMessage,
syncDeps,
} from '../sync-deps/sync-deps.impl';
let childProcess: ChildProcess;
export default async function* reactNatievStorybookExecutor(
options: ReactNativeStorybookOptions,
context: ExecutorContext
): AsyncGenerator<{ success: boolean }> {
const projectRoot = context.workspace.projects[context.projectName].root;
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).`
);
// add storybook addons to app's package.json
const packageJsonPath = join(context.root, projectRoot, 'package.json');
if (fileExists(packageJsonPath))
displayNewlyAddedDepsMessage(
context.projectName,
await syncDeps(
context.projectName,
projectRoot,
context.root,
'@storybook/addon-ondevice-actions,@storybook/addon-ondevice-backgrounds,@storybook/addon-ondevice-controls,@storybook/addon-ondevice-notes'
)
);
try {
await runCliStorybook(context.root, projectRoot, options);
yield { success: true };
} finally {
if (childProcess) {
childProcess.kill();
}
}
}
function runCliStorybook(
workspaceRoot: string,
projectRoot: string,
options: ReactNativeStorybookOptions
) {
return new Promise((resolve, reject) => {
childProcess = fork(
join(
workspaceRoot,
'./node_modules/react-native-storybook-loader/out/rnstl-cli.js'
),
createStorybookOptions(options),
{
cwd: workspaceRoot,
}
);
// Ensure the child process is killed when the parent exits
process.on('exit', () => childProcess.kill());
process.on('SIGTERM', () => childProcess.kill());
childProcess.on('error', (err) => {
reject(err);
});
childProcess.on('exit', (code) => {
if (code === 0) {
resolve(code);
} else {
reject(code);
}
});
});
}
function createStorybookOptions(options) {
return Object.keys(options).reduce((acc, k) => {
const v = options[k];
if (typeof v === 'boolean') {
if (v === true) {
acc.push(`--${k}`);
}
} else {
acc.push(`--${k}`, options[k]);
}
return acc;
}, []);
}