forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-storybooks.js
executable file
·178 lines (149 loc) · 4.17 KB
/
build-storybooks.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
const { spawn } = require('child_process');
const { promisify } = require('util');
const {
readdir: readdirRaw,
readFile: readFileRaw,
writeFile: writeFileRaw,
statSync,
} = require('fs');
const { join } = require('path');
const readdir = promisify(readdirRaw);
const readFile = promisify(readFileRaw);
const writeFile = promisify(writeFileRaw);
const p = l => join(__dirname, '..', ...l);
const logger = console;
const exec = async (command, args = [], options = {}) =>
new Promise((resolve, reject) => {
const child = spawn(command, args, { ...options, stdio: 'inherit', shell: true });
child
.on('close', code => {
if (code) {
reject();
} else {
resolve();
}
})
.on('error', e => {
logger.error(e);
reject();
});
});
const hasBuildScript = async l => {
const text = await readFile(l, 'utf8');
const json = JSON.parse(text);
return !!json.scripts['build-storybook'];
};
const createContent = deployables => {
return `
<style>
body {
background: black;
color: white;
}
#frame {
position: absolute;
left: 0;
right: 0;
width: 100vw;
height: calc(100vh - 30px);
bottom: 0;
top: 30px;
border: 0 none;
margin: 0;
padding: 0;
}
#select {
position: absolute;
top: 0;
right: 100px;
left: 10px;
height: 30px;
width: calc(100vw - 120px);
background: black;
color: white;
border: 0 none;
border-radius: 0;
padding: 10px;
box-sizing: border-box;
}
#open {
position: absolute;
top: 0;
right: 0;
width: 100px;
height: 30px;
background: black;
color: white;
border: 0 none;
border-radius: 0;
padding: 10px;
box-sizing: border-box;
}
</style>
<script>
function handleClick() {
var value = document.getElementById("select").value;
window.location = document.location.origin + value;
}
function handleSelect() {
var value = document.getElementById("select").value;
var frame = document.getElementById("frame");
sessionStorage.clear();
localStorage.clear();
frame.setAttribute('src', value);
}
</script>
<button id="open" onclick="handleClick()">open</button>
<select id="select" onchange="handleSelect()">
${deployables.map(i => `<option value="/${i}/">${i}</option>`).join('\n')}
</select>
<iframe id="frame" src="/${deployables[0]}/" />
`;
};
const handleExamples = async files => {
const deployables = files.filter(f => {
const packageJsonLocation = p(['examples', f, 'package.json']);
const stats = statSync(packageJsonLocation);
return stats.isFile() && hasBuildScript(packageJsonLocation);
});
await deployables.reduce(async (acc, d) => {
await acc;
logger.log('');
logger.log(
`-----------------${Array(d.length)
.fill('-')
.join('')}`
);
logger.log(`▶️ building: ${d}`);
logger.log(
`-----------------${Array(d.length)
.fill('-')
.join('')}`
);
const out = p(['built-storybooks', d]);
const cwd = p(['examples', d]);
await exec(`yarn`, [`build-storybook`, `--output-dir=${out}`, '--quiet'], { cwd });
logger.log('-------');
logger.log('✅ done');
logger.log('-------');
}, Promise.resolve());
logger.log('');
logger.log(`📑 creating index`);
const indexLocation = p(['built-storybooks', 'index.html']);
const indexContent = createContent(deployables);
await writeFile(indexLocation, indexContent);
logger.log('-------');
logger.log('✅ done');
logger.log('-------');
};
const run = async () => {
const examples = await readdir(p(['examples']));
const { length } = examples;
const [a, b] = [process.env.CIRCLE_NODE_INDEX || 0, process.env.CIRCLE_NODE_TOTAL || 1];
const step = Math.ceil(length / b);
const offset = step * a;
const list = examples.slice().splice(offset, step);
await handleExamples(list);
};
run();