forked from SAP/fundamental-styles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stories-transformer.ts
34 lines (31 loc) · 1.4 KB
/
stories-transformer.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
import glob from 'glob';
import { moveSync } from 'fs-extra';
import { parse } from '@babel/core';
import { dirname, join, relative } from 'path';
import { readFileSync, writeFileSync } from 'fs';
const storyFiles = glob.sync('packages/styles/stories/**/*.stories.js');
(async () => {
for (const storyFile of storyFiles) {
const storyDirname = dirname(storyFile);
const { default: defaultExport } = await import(storyFile);
if (defaultExport.parameters?.components) {
const imports = defaultExport.parameters?.components
.map((componentName) => {
if (componentName.startsWith('sap-')) {
const pathToComponentStyles = relative(
storyDirname,
`packages/common-css/src/${componentName}.scss`
).replace(/\\/g, '/');
return `import '${pathToComponentStyles}';`;
}
const pathToComponentStyles = relative(
storyDirname,
`packages/styles/src/${componentName}.scss`
).replace(/\\/g, '/');
return `import '${pathToComponentStyles}';`;
})
.join('\n');
writeFileSync(storyFile, `${imports}\n${readFileSync(storyFile, { encoding: 'utf-8' })}`);
}
}
})();