storybook-addon-sdc 0.7.2 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/preset.js +107 -79
- package/package.json +3 -2
package/dist/preset.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/vite-plugin-storybook-yaml-stories.ts
|
|
2
|
-
import { readdirSync, readFileSync
|
|
2
|
+
import { readdirSync as readdirSync2, readFileSync } from "fs";
|
|
3
3
|
import { parse as parseYaml } from "yaml";
|
|
4
|
-
import { basename, dirname as dirname2, extname
|
|
4
|
+
import { basename, dirname as dirname2, extname } from "path";
|
|
5
5
|
|
|
6
6
|
// src/argsGenerator.ts
|
|
7
7
|
import { JSONSchemaFaker } from "json-schema-faker";
|
|
@@ -51,10 +51,25 @@ var argTypesGenerator_default = (content) => {
|
|
|
51
51
|
return generated;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
+
// src/utils.ts
|
|
55
|
+
import { readdirSync, existsSync } from "fs";
|
|
56
|
+
import { join, resolve } from "path";
|
|
57
|
+
var capitalize = (str) => str[0].toUpperCase() + str.slice(1);
|
|
58
|
+
var convertToKebabCase = (str) => str.replace(/[-:]/g, "");
|
|
59
|
+
var getSubdirectories = (baseDir) => readdirSync(baseDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => join(baseDir, entry.name));
|
|
60
|
+
var resolveComponentPath = (namespace, component) => {
|
|
61
|
+
const baseDir = resolve("./components");
|
|
62
|
+
const directories = [baseDir, ...getSubdirectories(baseDir)];
|
|
63
|
+
const possiblePaths = directories.map(
|
|
64
|
+
(dir) => join(dir, component, `${component}.component.yml`)
|
|
65
|
+
);
|
|
66
|
+
return possiblePaths.find(existsSync);
|
|
67
|
+
};
|
|
68
|
+
|
|
54
69
|
// src/storiesGenerator.ts
|
|
55
70
|
var storiesGenerator_default = (stories) => Object.entries(stories).map(
|
|
56
71
|
([storyKey, { props = {}, slots = {}, variants = {} }]) => `
|
|
57
|
-
export const ${
|
|
72
|
+
export const ${capitalize(storyKey)} = {
|
|
58
73
|
args: {
|
|
59
74
|
...Basic.args,
|
|
60
75
|
${generateArgs2(props)}
|
|
@@ -78,7 +93,7 @@ var formatArgValue = (value, isSlot) => {
|
|
|
78
93
|
return JSON.stringify(value);
|
|
79
94
|
};
|
|
80
95
|
var generateComponent = (item) => {
|
|
81
|
-
const kebabCaseName = item.component
|
|
96
|
+
const kebabCaseName = convertToKebabCase(item.component);
|
|
82
97
|
const componentProps = { ...item.props, ...item.slots };
|
|
83
98
|
const storyArgs = item.story ? `...${kebabCaseName}.${item.story}.args` : "...{}";
|
|
84
99
|
return `${kebabCaseName}.default.component({...${kebabCaseName}.Basic.args, ${storyArgs}, ...${JSON.stringify(componentProps)}})`;
|
|
@@ -165,16 +180,7 @@ var readSDC = (filePath, defs, validate) => {
|
|
|
165
180
|
}
|
|
166
181
|
return sdcSchema;
|
|
167
182
|
};
|
|
168
|
-
var
|
|
169
|
-
var resolveComponentPath = (namespace, component) => {
|
|
170
|
-
const baseDir = resolve("./components");
|
|
171
|
-
const directories = [baseDir, ...getSubdirectories(baseDir)];
|
|
172
|
-
const possiblePaths = directories.map(
|
|
173
|
-
(dir) => join(dir, component, `${component}.component.yml`)
|
|
174
|
-
);
|
|
175
|
-
return possiblePaths.find(existsSync);
|
|
176
|
-
};
|
|
177
|
-
var generateImports = (directory) => readdirSync(directory).filter((file) => [".css", ".js", ".mjs", ".twig"].includes(extname(file))).map((file) => {
|
|
183
|
+
var generateImports = (directory) => readdirSync2(directory).filter((file) => [".css", ".js", ".mjs", ".twig"].includes(extname(file))).map((file) => {
|
|
178
184
|
const filePath = `./${file}`;
|
|
179
185
|
return extname(file) === ".twig" ? `import COMPONENT from '${filePath}';` : `import '${filePath}';`;
|
|
180
186
|
}).join("\n");
|
|
@@ -187,7 +193,7 @@ var dynamicImports = (stories) => {
|
|
|
187
193
|
if (item.type === "component") {
|
|
188
194
|
const [namespace, componentName] = item.component.split(":");
|
|
189
195
|
const resolvedPath = resolveComponentPath(namespace, componentName);
|
|
190
|
-
const kebabCaseName = item.component
|
|
196
|
+
const kebabCaseName = convertToKebabCase(item.component);
|
|
191
197
|
if (resolvedPath) {
|
|
192
198
|
imports.add(
|
|
193
199
|
`import * as ${kebabCaseName} from '${resolvedPath}';`
|
|
@@ -205,6 +211,27 @@ var dynamicImports = (stories) => {
|
|
|
205
211
|
);
|
|
206
212
|
return Array.from(imports).join("\n");
|
|
207
213
|
};
|
|
214
|
+
var createStoryIndex = (fileName, baseTitle, stories) => {
|
|
215
|
+
const storiesIndex = [
|
|
216
|
+
{
|
|
217
|
+
type: "story",
|
|
218
|
+
importPath: fileName,
|
|
219
|
+
exportName: "Basic",
|
|
220
|
+
title: baseTitle
|
|
221
|
+
}
|
|
222
|
+
];
|
|
223
|
+
if (stories) {
|
|
224
|
+
Object.keys(stories).forEach((storyKey) => {
|
|
225
|
+
storiesIndex.push({
|
|
226
|
+
type: "story",
|
|
227
|
+
importPath: fileName,
|
|
228
|
+
exportName: storyKey,
|
|
229
|
+
title: baseTitle
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
return storiesIndex;
|
|
234
|
+
};
|
|
208
235
|
var vite_plugin_storybook_yaml_stories_default = ({
|
|
209
236
|
jsonSchemaFakerOptions = {},
|
|
210
237
|
sdcStorybookOptions = {},
|
|
@@ -213,33 +240,34 @@ var vite_plugin_storybook_yaml_stories_default = ({
|
|
|
213
240
|
name: "vite-plugin-storybook-yaml-stories",
|
|
214
241
|
async load(id) {
|
|
215
242
|
if (!id.endsWith("component.yml")) return;
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
+
try {
|
|
244
|
+
const content = readSDC(id, globalDefs, sdcStorybookOptions.validate);
|
|
245
|
+
const imports = generateImports(dirname2(id));
|
|
246
|
+
const storiesImports = dynamicImports(
|
|
247
|
+
content.thirdPartySettings?.sdcStorybook?.stories || {}
|
|
248
|
+
);
|
|
249
|
+
const metadata = componentMetadata_default(id, content);
|
|
250
|
+
const argTypes = {
|
|
251
|
+
componentMetadata: { table: { disable: true } },
|
|
252
|
+
...content.variants && {
|
|
253
|
+
variant: {
|
|
254
|
+
control: "select",
|
|
255
|
+
options: Object.keys(content.variants)
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
...argTypesGenerator_default(content)
|
|
259
|
+
};
|
|
260
|
+
const args = {
|
|
261
|
+
defaultAttributes: [["data-component-id", `${sdcStorybookOptions?.namespace}:${basename(id, ".component.yml")}`]],
|
|
262
|
+
componentMetadata: metadata,
|
|
263
|
+
...content.variants && {
|
|
264
|
+
variant: Object.keys(content.variants)[0]
|
|
265
|
+
},
|
|
266
|
+
...generateStorybookArgs(content, jsonSchemaFakerOptions)
|
|
267
|
+
};
|
|
268
|
+
const basicArgs = { ...args };
|
|
269
|
+
const stories = content.thirdPartySettings?.sdcStorybook?.stories ? storiesGenerator_default(content.thirdPartySettings.sdcStorybook.stories) : "";
|
|
270
|
+
return `
|
|
243
271
|
${imports}
|
|
244
272
|
${storiesImports}
|
|
245
273
|
|
|
@@ -257,34 +285,25 @@ export const Basic = {
|
|
|
257
285
|
};
|
|
258
286
|
|
|
259
287
|
${stories}
|
|
260
|
-
|
|
288
|
+
`;
|
|
289
|
+
} catch (error) {
|
|
290
|
+
console.error(`Error loading component YAML file: ${id}`, error);
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
261
293
|
}
|
|
262
294
|
});
|
|
263
295
|
var yamlStoriesIndexer = {
|
|
264
296
|
test: /component\.yml$/,
|
|
265
297
|
createIndex: async (fileName, { makeTitle }) => {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
];
|
|
276
|
-
const stories = content.thirdPartySettings?.sdcStorybook?.stories;
|
|
277
|
-
if (stories) {
|
|
278
|
-
Object.keys(stories).forEach((storyKey) => {
|
|
279
|
-
storiesIndex.push({
|
|
280
|
-
type: "story",
|
|
281
|
-
importPath: fileName,
|
|
282
|
-
exportName: storyKey,
|
|
283
|
-
title: baseTitle
|
|
284
|
-
});
|
|
285
|
-
});
|
|
298
|
+
try {
|
|
299
|
+
const content = readSDC(fileName);
|
|
300
|
+
const baseTitle = makeTitle(`SDC/${content.name}`);
|
|
301
|
+
const stories = content.thirdPartySettings?.sdcStorybook?.stories;
|
|
302
|
+
return createStoryIndex(fileName, baseTitle, stories);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
console.error(`Error creating index for YAML file: ${fileName}`, error);
|
|
305
|
+
throw error;
|
|
286
306
|
}
|
|
287
|
-
return storiesIndex;
|
|
288
307
|
}
|
|
289
308
|
};
|
|
290
309
|
|
|
@@ -297,16 +316,21 @@ import { parse } from "yaml";
|
|
|
297
316
|
import { sync } from "glob";
|
|
298
317
|
import fetch2 from "node-fetch";
|
|
299
318
|
async function loadExternalDef(defPath) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
319
|
+
try {
|
|
320
|
+
if (defPath.startsWith("http://") || defPath.startsWith("https://")) {
|
|
321
|
+
const response = await fetch2(defPath);
|
|
322
|
+
if (!response.ok) {
|
|
323
|
+
throw new Error(`Failed to fetch ${defPath}: ${response.statusText}`);
|
|
324
|
+
}
|
|
325
|
+
const content = await response.text();
|
|
326
|
+
return parse(content);
|
|
327
|
+
} else {
|
|
328
|
+
const content = readFileSync2(defPath, "utf8");
|
|
329
|
+
return parse(content);
|
|
304
330
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
const content = readFileSync2(defPath, "utf8");
|
|
309
|
-
return parse(content);
|
|
331
|
+
} catch (error) {
|
|
332
|
+
logger.error(`Error loading external definition from ${defPath}: ${error}`);
|
|
333
|
+
throw error;
|
|
310
334
|
}
|
|
311
335
|
}
|
|
312
336
|
function getComponentDirectories() {
|
|
@@ -330,12 +354,7 @@ var defaultOptions = {
|
|
|
330
354
|
// validate:
|
|
331
355
|
// 'https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json',
|
|
332
356
|
};
|
|
333
|
-
async function
|
|
334
|
-
options.sdcStorybookOptions = {
|
|
335
|
-
...defaultOptions,
|
|
336
|
-
...options.sdcStorybookOptions
|
|
337
|
-
};
|
|
338
|
-
const { namespace, customDefs, externalDefs, validate } = options.sdcStorybookOptions;
|
|
357
|
+
async function loadAndMergeDefinitions(externalDefs, customDefs) {
|
|
339
358
|
const globalDefs = {};
|
|
340
359
|
if (externalDefs) {
|
|
341
360
|
await Promise.all(
|
|
@@ -357,6 +376,15 @@ async function viteFinal(config, options) {
|
|
|
357
376
|
`Registering custom definitions: ${Object.keys(globalDefs).join(", ")}`
|
|
358
377
|
);
|
|
359
378
|
}
|
|
379
|
+
return globalDefs;
|
|
380
|
+
}
|
|
381
|
+
async function viteFinal(config, options) {
|
|
382
|
+
options.sdcStorybookOptions = {
|
|
383
|
+
...defaultOptions,
|
|
384
|
+
...options.sdcStorybookOptions
|
|
385
|
+
};
|
|
386
|
+
const { namespace, customDefs, externalDefs } = options.sdcStorybookOptions;
|
|
387
|
+
const globalDefs = await loadAndMergeDefinitions(externalDefs, customDefs);
|
|
360
388
|
return mergeConfig(config, {
|
|
361
389
|
plugins: [
|
|
362
390
|
twig(options.vitePluginTwigDrupalOptions),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "storybook-addon-sdc",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Drupal Single Directory Components as stories",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"SDC",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"scripts": {
|
|
28
28
|
"build": "tsup",
|
|
29
29
|
"build:watch": "npm run build -- --watch",
|
|
30
|
-
"test": "
|
|
30
|
+
"test": "vitest",
|
|
31
31
|
"start": "run-p build:watch \"storybook --quiet\"",
|
|
32
32
|
"prerelease": "zx scripts/prepublish-checks.js",
|
|
33
33
|
"release": "npm run build && auto shipit",
|
|
@@ -68,6 +68,7 @@
|
|
|
68
68
|
"tsup": "^8.2.4",
|
|
69
69
|
"typescript": "^5.5.4",
|
|
70
70
|
"vite": "^6.0.11",
|
|
71
|
+
"vitest": "^3.0.7",
|
|
71
72
|
"zx": "^8.2.0"
|
|
72
73
|
},
|
|
73
74
|
"publishConfig": {
|