histoire 0.2.3 → 0.3.0

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.
Files changed (178) hide show
  1. package/dist/client/app/App.vue +9 -4
  2. package/dist/client/app/codegen/const.d.ts +1 -0
  3. package/dist/client/app/codegen/const.js +3 -0
  4. package/dist/client/app/codegen/vue3.js +82 -21
  5. package/dist/client/app/components/app/AppHeader.vue +2 -2
  6. package/dist/client/app/components/app/Breadcrumb.vue +1 -1
  7. package/dist/client/app/components/base/BaseEmpty.vue +17 -1
  8. package/dist/client/app/components/base/BaseListItemLink.vue +1 -1
  9. package/dist/client/app/components/base/BaseTab.vue +1 -1
  10. package/dist/client/app/components/exposed/Story.vue +22 -1
  11. package/dist/client/app/components/exposed/Variant.vue +13 -1
  12. package/dist/client/app/components/sandbox/SandboxVue3.vue +6 -4
  13. package/dist/client/app/components/sandbox/lib.js +2 -1
  14. package/dist/client/app/components/search/SearchItem.vue +1 -0
  15. package/dist/client/app/components/search/SearchLoading.vue +16 -0
  16. package/dist/client/app/components/search/SearchModal.vue +12 -148
  17. package/dist/client/app/components/search/SearchPane.vue +210 -0
  18. package/dist/client/app/components/search/search-data.d.ts +2 -0
  19. package/dist/client/app/components/search/search-data.js +7 -0
  20. package/dist/client/app/components/story/StoryControls.vue +5 -0
  21. package/dist/client/app/components/story/StoryDocs.vue +18 -11
  22. package/dist/client/app/components/story/StoryEvent.vue +53 -0
  23. package/dist/client/app/components/story/StoryEvents.vue +45 -0
  24. package/dist/client/app/components/story/StoryResponsivePreview.vue +41 -8
  25. package/dist/client/app/components/story/StorySidePanel.vue +28 -3
  26. package/dist/client/app/components/story/StorySourceCode.vue +24 -14
  27. package/dist/client/app/components/story/StoryView.vue +16 -2
  28. package/dist/client/app/components/story/toolbar/StoryVariantNewTab.vue +1 -1
  29. package/dist/client/app/components/tree/StoryGroup.vue +75 -0
  30. package/dist/client/app/components/{story → tree}/StoryList.vue +8 -2
  31. package/{src/client/app/components/story → dist/client/app/components/tree}/StoryListFolder.vue +10 -8
  32. package/{src/client/app/components/story → dist/client/app/components/tree}/StoryListItem.vue +7 -4
  33. package/dist/client/app/router.d.ts +1 -0
  34. package/dist/client/app/router.js +3 -1
  35. package/dist/client/app/sandbox.js +11 -1
  36. package/dist/client/app/stores/events.d.ts +29 -0
  37. package/dist/client/app/stores/events.js +25 -0
  38. package/dist/client/app/stores/folder.d.ts +10 -0
  39. package/dist/client/app/stores/folder.js +47 -0
  40. package/dist/client/app/stores/story.d.ts +20 -8
  41. package/dist/client/app/stores/story.js +20 -28
  42. package/dist/client/app/style/floating-vue.pcss +3 -1
  43. package/dist/client/app/style/main.pcss +5 -0
  44. package/dist/client/app/style/sandbox.css +2 -0
  45. package/dist/client/app/types.d.ts +9 -1
  46. package/dist/client/app/util/const.d.ts +1 -0
  47. package/dist/client/app/util/const.js +1 -0
  48. package/dist/client/app/util/events.d.ts +1 -0
  49. package/dist/client/app/util/events.js +31 -0
  50. package/dist/client/app/util/mapping.js +2 -2
  51. package/dist/client/index.d.ts +2 -1
  52. package/dist/client/index.js +2 -1
  53. package/dist/client/server/Story.server.vue +21 -4
  54. package/dist/node/alias.d.ts +2 -1
  55. package/dist/node/alias.js +3 -2
  56. package/dist/node/build.js +74 -21
  57. package/dist/node/builtin-plugins/tailwind-tokens.d.ts +5 -0
  58. package/dist/node/builtin-plugins/tailwind-tokens.js +510 -0
  59. package/dist/node/collect/index.js +5 -3
  60. package/dist/node/commands/preview.js +4 -25
  61. package/dist/node/config.d.ts +111 -5
  62. package/dist/node/config.js +40 -23
  63. package/dist/node/context.d.ts +3 -3
  64. package/dist/node/context.js +1 -1
  65. package/dist/node/index.d.ts +1 -0
  66. package/dist/node/index.js +1 -0
  67. package/dist/node/load.d.ts +12 -0
  68. package/dist/node/load.js +42 -0
  69. package/dist/node/plugin.d.ts +67 -11
  70. package/dist/node/plugin.js +42 -245
  71. package/dist/node/preview.d.ts +8 -0
  72. package/dist/node/preview.js +41 -0
  73. package/dist/node/search.d.ts +2 -0
  74. package/dist/node/search.js +68 -0
  75. package/dist/node/server.js +43 -21
  76. package/dist/node/stories.d.ts +2 -0
  77. package/dist/node/stories.js +9 -4
  78. package/dist/node/tree.d.ts +16 -12
  79. package/dist/node/tree.js +45 -5
  80. package/dist/node/types.d.ts +11 -0
  81. package/dist/node/util/find-up.d.ts +1 -0
  82. package/dist/node/util/find-up.js +20 -0
  83. package/dist/node/vite.d.ts +13 -0
  84. package/dist/node/vite.js +278 -0
  85. package/dist/style.css +179 -36
  86. package/package.json +5 -3
  87. package/scripts/copy.mjs +2 -1
  88. package/src/client/app/App.vue +9 -4
  89. package/src/client/app/codegen/const.ts +3 -0
  90. package/src/client/app/codegen/vue3.ts +80 -22
  91. package/src/client/app/components/app/AppHeader.vue +2 -2
  92. package/src/client/app/components/app/Breadcrumb.vue +1 -1
  93. package/src/client/app/components/base/BaseEmpty.vue +17 -1
  94. package/src/client/app/components/base/BaseListItemLink.vue +1 -1
  95. package/src/client/app/components/base/BaseTab.vue +1 -1
  96. package/src/client/app/components/exposed/Story.vue +22 -1
  97. package/src/client/app/components/exposed/Variant.vue +13 -1
  98. package/src/client/app/components/sandbox/SandboxVue3.vue +6 -4
  99. package/src/client/app/components/sandbox/lib.ts +3 -1
  100. package/src/client/app/components/search/SearchItem.vue +1 -0
  101. package/src/client/app/components/search/SearchLoading.vue +16 -0
  102. package/src/client/app/components/search/SearchModal.vue +12 -148
  103. package/src/client/app/components/search/SearchPane.vue +210 -0
  104. package/src/client/app/components/search/search-data.ts +9 -0
  105. package/src/client/app/components/story/StoryControls.vue +5 -0
  106. package/src/client/app/components/story/StoryDocs.vue +18 -11
  107. package/src/client/app/components/story/StoryEvent.vue +53 -0
  108. package/src/client/app/components/story/StoryEvents.vue +45 -0
  109. package/src/client/app/components/story/StoryResponsivePreview.vue +41 -8
  110. package/src/client/app/components/story/StorySidePanel.vue +28 -3
  111. package/src/client/app/components/story/StorySourceCode.vue +24 -14
  112. package/src/client/app/components/story/StoryView.vue +16 -2
  113. package/src/client/app/components/story/toolbar/StoryVariantNewTab.vue +1 -1
  114. package/src/client/app/components/tree/StoryGroup.vue +75 -0
  115. package/src/client/app/components/{story → tree}/StoryList.vue +8 -2
  116. package/{dist/client/app/components/story → src/client/app/components/tree}/StoryListFolder.vue +10 -8
  117. package/{dist/client/app/components/story → src/client/app/components/tree}/StoryListItem.vue +7 -4
  118. package/src/client/app/router.ts +4 -1
  119. package/src/client/app/sandbox.ts +12 -2
  120. package/src/client/app/stores/events.ts +36 -0
  121. package/src/client/app/stores/folder.ts +59 -0
  122. package/src/client/app/stores/story.ts +20 -35
  123. package/src/client/app/style/floating-vue.pcss +3 -1
  124. package/src/client/app/style/main.pcss +5 -0
  125. package/src/client/app/style/sandbox.css +2 -0
  126. package/src/client/app/types.ts +10 -1
  127. package/src/client/app/util/const.ts +1 -0
  128. package/src/client/app/util/events.ts +30 -0
  129. package/src/client/app/util/mapping.ts +2 -2
  130. package/src/client/index.ts +2 -1
  131. package/src/client/server/Story.server.vue +21 -4
  132. package/src/node/alias.ts +4 -2
  133. package/src/node/build.ts +86 -21
  134. package/src/node/builtin-plugins/tailwind-tokens.ts +521 -0
  135. package/src/node/collect/index.ts +6 -4
  136. package/src/node/commands/preview.ts +4 -33
  137. package/src/node/config.ts +62 -29
  138. package/src/node/context.ts +3 -3
  139. package/src/node/index.ts +1 -0
  140. package/src/node/load.ts +63 -0
  141. package/src/node/plugin.ts +75 -252
  142. package/src/node/preview.ts +56 -0
  143. package/src/node/search.ts +75 -0
  144. package/src/node/server.ts +49 -23
  145. package/src/node/stories.ts +9 -4
  146. package/src/node/tree.spec.ts +35 -17
  147. package/src/node/tree.ts +64 -10
  148. package/src/node/types.ts +12 -0
  149. package/src/node/util/find-up.ts +24 -0
  150. package/src/node/vite.ts +296 -0
  151. package/tailwind.config.cjs +1 -0
  152. package/tsconfig.json +2 -0
  153. package/dist/client/app/codegen/serialize-js.ts +0 -119
  154. package/dist/client/app/codegen/util.ts +0 -79
  155. package/dist/client/app/codegen/vue3.ts +0 -276
  156. package/dist/client/app/components/sandbox/lib.ts +0 -8
  157. package/dist/client/app/global-components.ts +0 -13
  158. package/dist/client/app/index.ts +0 -28
  159. package/dist/client/app/router.ts +0 -17
  160. package/dist/client/app/sandbox.ts +0 -94
  161. package/dist/client/app/stores/preview-settings.ts +0 -17
  162. package/dist/client/app/stores/story.ts +0 -62
  163. package/dist/client/app/types.ts +0 -70
  164. package/dist/client/app/util/config.ts +0 -6
  165. package/dist/client/app/util/const.ts +0 -3
  166. package/dist/client/app/util/dark.ts +0 -4
  167. package/dist/client/app/util/env.ts +0 -1
  168. package/dist/client/app/util/keyboard.ts +0 -99
  169. package/dist/client/app/util/mapping.ts +0 -48
  170. package/dist/client/app/util/preview-settings.ts +0 -8
  171. package/dist/client/app/util/reactivity.ts +0 -22
  172. package/dist/client/app/util/responsive.ts +0 -3
  173. package/dist/client/app/util/scroll.ts +0 -31
  174. package/dist/client/app/util/tooltip.ts +0 -13
  175. package/dist/client/app/util/variant.ts +0 -20
  176. package/dist/client/index.ts +0 -12
  177. package/dist/client/server/index.ts +0 -32
  178. package/dist/client/shim.d.ts +0 -5
@@ -0,0 +1,75 @@
1
+ import { createRequire } from 'module'
2
+ import flexsearch from 'flexsearch'
3
+ import path from 'pathe'
4
+ import { Context } from './context.js'
5
+ import { loadModule } from './load.js'
6
+
7
+ const require = createRequire(import.meta.url)
8
+
9
+ export async function generateSearchData (ctx: Context) {
10
+ const flexsearchRoot = path.dirname(require.resolve('flexsearch/package.json'))
11
+ const searchIndex = new flexsearch.Document({
12
+ preset: 'match',
13
+ document: {
14
+ id: 'id',
15
+ index: [
16
+ 'title',
17
+ 'docs',
18
+ ],
19
+ store: [
20
+ 'kind',
21
+ ],
22
+ },
23
+ charset: await loadModule(`${flexsearchRoot}/dist/module/lang/latin/advanced.js`),
24
+ language: await loadModule(`${flexsearchRoot}/dist/module/lang/en.js`),
25
+ tokenize: 'full',
26
+ })
27
+
28
+ for (const storyFile of ctx.storyFiles) {
29
+ if (storyFile.story) {
30
+ searchIndex.add({
31
+ id: storyFile.story.id,
32
+ kind: 'story',
33
+ title: storyFile.story.title,
34
+ docs: '', // @TODO
35
+ })
36
+ for (const variant of storyFile.story.variants) {
37
+ searchIndex.add({
38
+ id: `${storyFile.story.id}:${variant.id}`,
39
+ kind: 'variant',
40
+ title: `${storyFile.story.title} ${variant.title}`,
41
+ docs: '', // @TODO
42
+ })
43
+ }
44
+ }
45
+ }
46
+
47
+ return exportSearchIndex(searchIndex)
48
+ }
49
+
50
+ const exportKeys = new Set([
51
+ 'reg',
52
+ 'title.cfg',
53
+ 'title.map',
54
+ 'title.ctx',
55
+ 'docs.cfg',
56
+ 'docs.map',
57
+ 'docs.ctx',
58
+ 'tag',
59
+ 'store',
60
+ ])
61
+
62
+ async function exportSearchIndex (index) {
63
+ // eslint-disable-next-line no-async-promise-executor
64
+ return new Promise(async (resolve, reject) => {
65
+ const exportedData: Record<string, any> = {}
66
+ const exportedKeys = new Set<string>()
67
+ await index.export((key, data) => {
68
+ exportedData[key] = data
69
+ exportedKeys.add(key)
70
+ if (exportedKeys.size === exportKeys.size) {
71
+ resolve(exportedData)
72
+ }
73
+ })
74
+ })
75
+ }
@@ -1,13 +1,13 @@
1
1
  import { createServer as createViteServer } from 'vite'
2
2
  import { Context } from './context.js'
3
- import { createVitePlugins, RESOLVED_STORIES_ID } from './plugin.js'
3
+ import { createVitePlugins, RESOLVED_SEARCH_DATA_ID, RESOLVED_STORIES_ID } from './vite.js'
4
4
  import { notifyStoryChange, onStoryChange, watchStories } from './stories.js'
5
5
  import { useCollectStories } from './collect/index.js'
6
6
  import type { StoryFile } from './types.js'
7
+ import { DevPluginApi } from './plugin.js'
8
+ import { useModuleLoader } from './load.js'
7
9
 
8
10
  export async function createServer (ctx: Context, port: number) {
9
- await watchStories(ctx)
10
-
11
11
  const server = await createViteServer({
12
12
  plugins: await createVitePlugins(ctx),
13
13
  server: {
@@ -17,6 +17,24 @@ export async function createServer (ctx: Context, port: number) {
17
17
  },
18
18
  })
19
19
  await server.pluginContainer.buildStart({})
20
+
21
+ const moduleLoader = useModuleLoader({
22
+ server,
23
+ })
24
+
25
+ const pluginOnCleanups: (() => void | Promise<void>)[] = []
26
+ for (const plugin of ctx.config.plugins) {
27
+ if (plugin.onDev) {
28
+ const api = new DevPluginApi(ctx, plugin, moduleLoader)
29
+ const onCleanup = (cb: () => void | Promise<void>) => {
30
+ pluginOnCleanups.push(cb)
31
+ }
32
+ await plugin.onDev(api, onCleanup)
33
+ }
34
+ }
35
+
36
+ await watchStories(ctx)
37
+
20
38
  // Wait for pre-bundling (in `listen()`)
21
39
  await server.listen(port)
22
40
 
@@ -69,30 +87,38 @@ export async function createServer (ctx: Context, port: number) {
69
87
  queuedFiles = []
70
88
  queueResolve()
71
89
 
72
- // Invalidate stories data module
73
- const mod = server.moduleGraph.getModuleById(RESOLVED_STORIES_ID)
74
- if (!mod) {
75
- return
90
+ // Invalidate modules
91
+ function invalidateModule (id: string) {
92
+ const mod = server.moduleGraph.getModuleById(id)
93
+ if (!mod) {
94
+ return
95
+ }
96
+ server.moduleGraph.invalidateModule(mod)
97
+
98
+ // Send HMR update
99
+ const timestamp = Date.now()
100
+ mod.lastHMRTimestamp = timestamp
101
+ server.ws.send({
102
+ type: 'update',
103
+ updates: [
104
+ {
105
+ type: 'js-update',
106
+ acceptedPath: mod.url,
107
+ path: mod.url,
108
+ timestamp: timestamp,
109
+ },
110
+ ],
111
+ })
76
112
  }
77
- server.moduleGraph.invalidateModule(mod)
78
-
79
- // Send stories data HMR update
80
- const timestamp = Date.now()
81
- mod.lastHMRTimestamp = timestamp
82
- server.ws.send({
83
- type: 'update',
84
- updates: [
85
- {
86
- type: 'js-update',
87
- acceptedPath: mod.url,
88
- path: mod.url,
89
- timestamp: timestamp,
90
- },
91
- ],
92
- })
113
+
114
+ invalidateModule(RESOLVED_STORIES_ID)
115
+ invalidateModule(RESOLVED_SEARCH_DATA_ID)
93
116
  })
94
117
 
95
118
  async function close () {
119
+ for (const cb of pluginOnCleanups) {
120
+ await cb()
121
+ }
96
122
  await server.close()
97
123
  await destroyCollectStories()
98
124
  }
@@ -1,7 +1,7 @@
1
1
  import chokidar from 'chokidar'
2
2
  import { globby } from 'globby'
3
3
  import Case from 'case'
4
- import { join } from 'pathe'
4
+ import { resolve, basename } from 'pathe'
5
5
  import { Context } from './context.js'
6
6
  import type { StoryFile } from './types.js'
7
7
 
@@ -41,23 +41,28 @@ export function notifyStoryChange (file?: StoryFile) {
41
41
  }
42
42
 
43
43
  function getAbsoluteFilePath (relativeFilePath: string) {
44
- return join(context.root, relativeFilePath)
44
+ return resolve(context.root, relativeFilePath)
45
45
  }
46
46
 
47
- function addStory (relativeFilePath: string) {
47
+ export function addStory (relativeFilePath: string) {
48
48
  const absoluteFilePath = getAbsoluteFilePath(relativeFilePath)
49
49
  const fileId = Case.kebab(relativeFilePath)
50
+ let fileName = basename(relativeFilePath)
51
+ if (fileName.includes('.')) {
52
+ fileName = fileName.substring(0, fileName.indexOf('.'))
53
+ }
50
54
 
51
55
  const file: StoryFile = {
52
56
  id: fileId,
53
57
  path: absoluteFilePath,
58
+ fileName,
54
59
  moduleId: `/${relativeFilePath}`,
55
60
  }
56
61
  context.storyFiles.push(file)
57
62
  return file
58
63
  }
59
64
 
60
- function removeStory (relativeFilePath: string) {
65
+ export function removeStory (relativeFilePath: string) {
61
66
  const fileId = Case.kebab(relativeFilePath)
62
67
  const index = context.storyFiles.findIndex((file) => file.id === fileId)
63
68
  if (index !== -1) context.storyFiles.splice(index, 1)
@@ -1,17 +1,35 @@
1
+ import { describe, test, expect } from '@peeky/test'
1
2
  import { makeTree } from './tree'
2
3
  import { getDefaultConfig } from './config'
4
+ import type { StoryFile } from './types'
5
+
6
+ let id = 0
7
+
8
+ interface StoryFileFactoryOptions {
9
+ treePath: string[]
10
+ }
11
+
12
+ function storyFileFactory (options: StoryFileFactoryOptions): StoryFile {
13
+ return {
14
+ id: `id_${id++}`,
15
+ path: options.treePath.join('/'),
16
+ treePath: options.treePath,
17
+ fileName: 'fileName',
18
+ moduleId: 'moduleId',
19
+ }
20
+ }
3
21
 
4
22
  describe('makeTree', () => {
5
23
  test('should create an ascending ordered tree', () => {
6
24
  const config = getDefaultConfig()
7
25
  const files = [
8
- { index: 0, path: ['hi'] },
9
- { index: 1, path: ['hey'] },
10
- { index: 2, path: ['hello', 'world'] },
11
- { index: 3, path: ['hello', 'mom'] },
26
+ { treePath: ['hi'] },
27
+ { treePath: ['hey'] },
28
+ { treePath: ['hello', 'world'] },
29
+ { treePath: ['hello', 'mom'] },
12
30
  ]
13
31
 
14
- const tree = makeTree(config, files)
32
+ const tree = makeTree(config, files.map(storyFileFactory))
15
33
 
16
34
  expect(tree).toEqual([
17
35
  {
@@ -29,12 +47,12 @@ describe('makeTree', () => {
29
47
  test('should handle title conflict', () => {
30
48
  const config = getDefaultConfig()
31
49
  const files = [
32
- { index: 0, path: ['hi'] },
33
- { index: 1, path: ['hi'] },
34
- { index: 2, path: ['hi'] },
50
+ { treePath: ['hi'] },
51
+ { treePath: ['hi'] },
52
+ { treePath: ['hi'] },
35
53
  ]
36
54
 
37
- const tree = makeTree(config, files)
55
+ const tree = makeTree(config, files.map(storyFileFactory))
38
56
 
39
57
  expect(tree).toEqual([
40
58
  { title: 'hi', index: 0 },
@@ -46,12 +64,12 @@ describe('makeTree', () => {
46
64
  test('should handle file-folder conflict when folder in first', () => {
47
65
  const config = getDefaultConfig()
48
66
  const files = [
49
- { index: 0, path: ['hi', 'dad'] },
50
- { index: 1, path: ['hi'] },
51
- { index: 2, path: ['hi', 'mom'] },
67
+ { treePath: ['hi', 'dad'] },
68
+ { treePath: ['hi'] },
69
+ { treePath: ['hi', 'mom'] },
52
70
  ]
53
71
 
54
- const tree = makeTree(config, files)
72
+ const tree = makeTree(config, files.map(storyFileFactory))
55
73
 
56
74
  expect(tree).toEqual([
57
75
  {
@@ -68,12 +86,12 @@ describe('makeTree', () => {
68
86
  test('should handle file-folder conflict when file in first', () => {
69
87
  const config = getDefaultConfig()
70
88
  const files = [
71
- { index: 0, path: ['hi'] },
72
- { index: 1, path: ['hi', 'dad'] },
73
- { index: 2, path: ['hi', 'mom'] },
89
+ { treePath: ['hi'] },
90
+ { treePath: ['hi', 'dad'] },
91
+ { treePath: ['hi', 'mom'] },
74
92
  ]
75
93
 
76
- const tree = makeTree(config, files)
94
+ const tree = makeTree(config, files.map(storyFileFactory))
77
95
 
78
96
  expect(tree).toEqual([
79
97
  {
package/src/node/tree.ts CHANGED
@@ -1,21 +1,30 @@
1
- import { HistoireConfig } from './config.js'
1
+ import pc from 'picocolors'
2
+ import type { HistoireConfig, TreeGroupConfig } from './config.js'
3
+ import type { StoryFile } from './types.js'
2
4
 
3
- export type TreeFile = {
5
+ export interface TreeFile {
4
6
  title: string
5
7
  path: string
6
8
  }
7
9
 
8
- export type TreeLeaf = {
10
+ export interface TreeLeaf {
9
11
  title: string
10
12
  index: number
11
13
  }
12
14
 
13
- export type TreeFolder = {
15
+ export interface TreeFolder {
14
16
  title: string
15
17
  children: (TreeFolder | TreeLeaf)[]
16
18
  }
17
19
 
18
- export type Tree = (TreeFolder | TreeLeaf)[]
20
+ export interface TreeGroup {
21
+ group: true
22
+ id: string
23
+ title: string
24
+ children: (TreeFolder | TreeLeaf)[]
25
+ }
26
+
27
+ export type Tree = (TreeGroup | TreeFolder | TreeLeaf)[]
19
28
 
20
29
  export function createPath (config: HistoireConfig, file: TreeFile) {
21
30
  if (config.tree.file === 'title') {
@@ -32,16 +41,29 @@ export function createPath (config: HistoireConfig, file: TreeFile) {
32
41
  return config.tree.file(file)
33
42
  }
34
43
 
35
- export function makeTree (config: HistoireConfig, files: { path: string[], index: number }[]) {
44
+ export function makeTree (config: HistoireConfig, files: StoryFile[]) {
36
45
  interface ITreeObject {
37
46
  [index: string]: number | ITreeObject
38
47
  }
39
48
 
40
- const treeObject: ITreeObject = {}
49
+ interface ITreeGroup {
50
+ groupConfig?: TreeGroupConfig
51
+ treeObject: ITreeObject
52
+ }
41
53
 
42
- for (const file of files) {
43
- setPath(file.path, file.index, treeObject)
54
+ const groups: ITreeGroup[] = config.tree?.groups?.map(g => ({
55
+ groupConfig: g,
56
+ treeObject: {},
57
+ })) || []
58
+ const defaultGroup = {
59
+ treeObject: {},
44
60
  }
61
+ groups.push(defaultGroup)
62
+
63
+ files.forEach((file, index) => {
64
+ const group = getGroup(file)
65
+ setPath(file.treePath, index, group.treeObject)
66
+ })
45
67
 
46
68
  let sortingFunction = (a: string, b: string) => a.localeCompare(b)
47
69
 
@@ -49,7 +71,39 @@ export function makeTree (config: HistoireConfig, files: { path: string[], index
49
71
  sortingFunction = config.tree.order
50
72
  }
51
73
 
52
- return buildTree(treeObject)
74
+ const result: Tree = []
75
+
76
+ for (const group of groups) {
77
+ if (group === defaultGroup) {
78
+ result.push(...buildTree(group.treeObject))
79
+ } else {
80
+ result.push({
81
+ group: true,
82
+ id: group.groupConfig.id,
83
+ title: group.groupConfig.title,
84
+ children: buildTree(group.treeObject),
85
+ })
86
+ }
87
+ }
88
+
89
+ return result
90
+
91
+ function getGroup (file: StoryFile): ITreeGroup {
92
+ if (file.story?.group) {
93
+ const group = groups.find(g => g.groupConfig?.id === file.story.group)
94
+ if (group) {
95
+ return group
96
+ } else {
97
+ console.error(pc.red(`Group ${file.story.group} not found for story ${file.path}`))
98
+ }
99
+ }
100
+ for (const group of groups) {
101
+ if (group.groupConfig?.include && group.groupConfig.include(file.treeFile)) {
102
+ return group
103
+ }
104
+ }
105
+ return defaultGroup
106
+ }
53
107
 
54
108
  function setPath (path: string[], value: unknown, tree: unknown) {
55
109
  path.reduce((subtree, key, i) => {
package/src/node/types.ts CHANGED
@@ -1,9 +1,15 @@
1
+ import type { TreeFile } from './tree.js'
2
+
1
3
  export interface StoryFile {
2
4
  id: string
3
5
  /**
4
6
  * Absolute path
5
7
  */
6
8
  path: string
9
+ /**
10
+ * File name without extension
11
+ */
12
+ fileName: string
7
13
  /**
8
14
  * Generated path for tree UI
9
15
  */
@@ -16,11 +22,16 @@ export interface StoryFile {
16
22
  * Resolved story data from story file execution
17
23
  */
18
24
  story?: Story
25
+ /**
26
+ * Data sent to user tree config functions
27
+ */
28
+ treeFile?: TreeFile
19
29
  }
20
30
 
21
31
  export interface Story {
22
32
  id: string
23
33
  title: string
34
+ group?: string
24
35
  variants: Variant[]
25
36
  layout?: {
26
37
  type: 'single'
@@ -30,6 +41,7 @@ export interface Story {
30
41
  }
31
42
  icon?: string
32
43
  iconColor?: string
44
+ docsOnly?: boolean
33
45
  }
34
46
 
35
47
  export interface Variant {
@@ -0,0 +1,24 @@
1
+ import path from 'pathe'
2
+ import fs from 'fs'
3
+
4
+ export function findUp (cwd: string = process.cwd(), fileNames: string[]): string {
5
+ let { root } = path.parse(cwd)
6
+ let dir = cwd
7
+
8
+ // Fix for windows, waiting for pathe to fix this: https://github.com/unjs/pathe/issues/5
9
+ if (root === '' && dir[1] === ':') {
10
+ root = dir.substring(0, 2)
11
+ }
12
+
13
+ while (dir !== root) {
14
+ for (const fileName of fileNames) {
15
+ const searchPath = path.join(dir, fileName)
16
+ if (fs.existsSync(searchPath)) {
17
+ return searchPath
18
+ }
19
+ }
20
+ dir = path.dirname(dir)
21
+ }
22
+
23
+ return null
24
+ }