histoire 0.2.4 → 0.3.1

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/Breadcrumb.vue +1 -1
  6. package/dist/client/app/components/base/BaseEmpty.vue +17 -1
  7. package/dist/client/app/components/exposed/Story.vue +22 -1
  8. package/dist/client/app/components/exposed/Variant.vue +13 -1
  9. package/dist/client/app/components/sandbox/SandboxVue3.vue +6 -4
  10. package/dist/client/app/components/sandbox/lib.js +2 -1
  11. package/dist/client/app/components/search/SearchItem.vue +1 -0
  12. package/dist/client/app/components/search/SearchLoading.vue +16 -0
  13. package/dist/client/app/components/search/SearchModal.vue +12 -149
  14. package/dist/client/app/components/search/SearchPane.vue +210 -0
  15. package/dist/client/app/components/search/search-data.d.ts +2 -0
  16. package/dist/client/app/components/search/search-data.js +7 -0
  17. package/dist/client/app/components/story/StoryControls.vue +5 -0
  18. package/dist/client/app/components/story/StoryDocs.vue +18 -11
  19. package/dist/client/app/components/story/StoryEvent.vue +53 -0
  20. package/dist/client/app/components/story/StoryEvents.vue +45 -0
  21. package/dist/client/app/components/story/StoryResponsivePreview.vue +41 -8
  22. package/dist/client/app/components/story/StorySidePanel.vue +28 -3
  23. package/dist/client/app/components/story/StorySourceCode.vue +24 -14
  24. package/dist/client/app/components/story/StoryView.vue +16 -2
  25. package/dist/client/app/components/tree/StoryGroup.vue +75 -0
  26. package/dist/client/app/components/{story → tree}/StoryList.vue +8 -2
  27. package/{src/client/app/components/story → dist/client/app/components/tree}/StoryListFolder.vue +10 -8
  28. package/{src/client/app/components/story → dist/client/app/components/tree}/StoryListItem.vue +7 -4
  29. package/dist/client/app/router.d.ts +1 -0
  30. package/dist/client/app/router.js +3 -1
  31. package/dist/client/app/sandbox.js +11 -1
  32. package/dist/client/app/stores/events.d.ts +29 -0
  33. package/dist/client/app/stores/events.js +25 -0
  34. package/dist/client/app/stores/folder.d.ts +10 -0
  35. package/dist/client/app/stores/folder.js +47 -0
  36. package/dist/client/app/stores/story.d.ts +20 -8
  37. package/dist/client/app/stores/story.js +20 -28
  38. package/dist/client/app/style/floating-vue.pcss +3 -1
  39. package/dist/client/app/style/main.pcss +5 -0
  40. package/dist/client/app/style/sandbox.css +2 -0
  41. package/dist/client/app/types.d.ts +9 -1
  42. package/dist/client/app/util/const.d.ts +1 -0
  43. package/dist/client/app/util/const.js +1 -0
  44. package/dist/client/app/util/events.d.ts +1 -0
  45. package/dist/client/app/util/events.js +31 -0
  46. package/dist/client/app/util/mapping.js +2 -2
  47. package/dist/client/index.d.ts +4 -3
  48. package/dist/client/index.js +2 -1
  49. package/dist/client/server/index.d.ts +7 -2
  50. package/dist/client/server/index.js +8 -26
  51. package/{src/client/server/Story.server.vue → dist/client/server/vue3/Story.vue} +21 -4
  52. package/dist/client/server/{Variant.server.vue → vue3/Variant.vue} +0 -0
  53. package/dist/client/server/vue3/run.d.ts +2 -0
  54. package/dist/client/server/vue3/run.js +32 -0
  55. package/dist/node/alias.d.ts +2 -1
  56. package/dist/node/alias.js +3 -2
  57. package/dist/node/build.js +74 -21
  58. package/dist/node/builtin-plugins/tailwind-tokens.d.ts +5 -0
  59. package/dist/node/builtin-plugins/tailwind-tokens.js +510 -0
  60. package/dist/node/collect/index.js +5 -3
  61. package/dist/node/collect/worker.js +6 -2
  62. package/dist/node/commands/preview.js +4 -25
  63. package/dist/node/config.d.ts +111 -5
  64. package/dist/node/config.js +40 -23
  65. package/dist/node/context.d.ts +3 -3
  66. package/dist/node/context.js +1 -1
  67. package/dist/node/index.d.ts +1 -0
  68. package/dist/node/index.js +1 -0
  69. package/dist/node/load.d.ts +12 -0
  70. package/dist/node/load.js +42 -0
  71. package/dist/node/plugin.d.ts +67 -11
  72. package/dist/node/plugin.js +42 -245
  73. package/dist/node/preview.d.ts +8 -0
  74. package/dist/node/preview.js +41 -0
  75. package/dist/node/search.d.ts +2 -0
  76. package/dist/node/search.js +68 -0
  77. package/dist/node/server.js +43 -21
  78. package/dist/node/stories.d.ts +2 -0
  79. package/dist/node/stories.js +9 -4
  80. package/dist/node/tree.d.ts +16 -12
  81. package/dist/node/tree.js +45 -5
  82. package/dist/node/types.d.ts +11 -0
  83. package/dist/node/util/find-up.d.ts +1 -0
  84. package/dist/node/util/find-up.js +20 -0
  85. package/dist/node/vite.d.ts +13 -0
  86. package/dist/node/vite.js +278 -0
  87. package/dist/style.css +174 -36
  88. package/package.json +7 -6
  89. package/scripts/copy.mjs +2 -1
  90. package/src/client/app/App.vue +9 -4
  91. package/src/client/app/codegen/const.ts +3 -0
  92. package/src/client/app/codegen/vue3.ts +80 -22
  93. package/src/client/app/components/app/Breadcrumb.vue +1 -1
  94. package/src/client/app/components/base/BaseEmpty.vue +17 -1
  95. package/src/client/app/components/exposed/Story.vue +22 -1
  96. package/src/client/app/components/exposed/Variant.vue +13 -1
  97. package/src/client/app/components/sandbox/SandboxVue3.vue +6 -4
  98. package/src/client/app/components/sandbox/lib.ts +3 -1
  99. package/src/client/app/components/search/SearchItem.vue +1 -0
  100. package/src/client/app/components/search/SearchLoading.vue +16 -0
  101. package/src/client/app/components/search/SearchModal.vue +12 -149
  102. package/src/client/app/components/search/SearchPane.vue +210 -0
  103. package/src/client/app/components/search/search-data.ts +9 -0
  104. package/src/client/app/components/story/StoryControls.vue +5 -0
  105. package/src/client/app/components/story/StoryDocs.vue +18 -11
  106. package/src/client/app/components/story/StoryEvent.vue +53 -0
  107. package/src/client/app/components/story/StoryEvents.vue +45 -0
  108. package/src/client/app/components/story/StoryResponsivePreview.vue +41 -8
  109. package/src/client/app/components/story/StorySidePanel.vue +28 -3
  110. package/src/client/app/components/story/StorySourceCode.vue +24 -14
  111. package/src/client/app/components/story/StoryView.vue +16 -2
  112. package/src/client/app/components/tree/StoryGroup.vue +75 -0
  113. package/src/client/app/components/{story → tree}/StoryList.vue +8 -2
  114. package/{dist/client/app/components/story → src/client/app/components/tree}/StoryListFolder.vue +10 -8
  115. package/{dist/client/app/components/story → src/client/app/components/tree}/StoryListItem.vue +7 -4
  116. package/src/client/app/router.ts +4 -1
  117. package/src/client/app/sandbox.ts +12 -2
  118. package/src/client/app/stores/events.ts +36 -0
  119. package/src/client/app/stores/folder.ts +59 -0
  120. package/src/client/app/stores/story.ts +20 -35
  121. package/src/client/app/style/floating-vue.pcss +3 -1
  122. package/src/client/app/style/main.pcss +5 -0
  123. package/src/client/app/style/sandbox.css +2 -0
  124. package/src/client/app/types.ts +10 -1
  125. package/src/client/app/util/const.ts +1 -0
  126. package/src/client/app/util/events.ts +30 -0
  127. package/src/client/app/util/mapping.ts +2 -2
  128. package/src/client/index.ts +4 -3
  129. package/src/client/server/index.ts +14 -27
  130. package/{dist/client/server/Story.server.vue → src/client/server/vue3/Story.vue} +21 -4
  131. package/src/client/server/{Variant.server.vue → vue3/Variant.vue} +0 -0
  132. package/{dist/client/server/index.ts → src/client/server/vue3/run.ts} +10 -4
  133. package/src/node/alias.ts +4 -2
  134. package/src/node/build.ts +86 -21
  135. package/src/node/builtin-plugins/tailwind-tokens.ts +521 -0
  136. package/src/node/collect/index.ts +6 -4
  137. package/src/node/collect/worker.ts +7 -2
  138. package/src/node/commands/preview.ts +4 -33
  139. package/src/node/config.ts +62 -29
  140. package/src/node/context.ts +3 -3
  141. package/src/node/index.ts +1 -0
  142. package/src/node/load.ts +63 -0
  143. package/src/node/plugin.ts +75 -252
  144. package/src/node/preview.ts +56 -0
  145. package/src/node/search.ts +75 -0
  146. package/src/node/server.ts +49 -23
  147. package/src/node/stories.ts +9 -4
  148. package/src/node/tree.spec.ts +35 -17
  149. package/src/node/tree.ts +64 -10
  150. package/src/node/types.ts +12 -0
  151. package/src/node/util/find-up.ts +24 -0
  152. package/src/node/vite.ts +296 -0
  153. package/tailwind.config.cjs +1 -0
  154. package/dist/client/app/codegen/serialize-js.ts +0 -119
  155. package/dist/client/app/codegen/util.ts +0 -79
  156. package/dist/client/app/codegen/vue3.ts +0 -276
  157. package/dist/client/app/components/sandbox/lib.ts +0 -8
  158. package/dist/client/app/global-components.ts +0 -13
  159. package/dist/client/app/index.ts +0 -28
  160. package/dist/client/app/router.ts +0 -17
  161. package/dist/client/app/sandbox.ts +0 -94
  162. package/dist/client/app/stores/preview-settings.ts +0 -17
  163. package/dist/client/app/stores/story.ts +0 -62
  164. package/dist/client/app/types.ts +0 -70
  165. package/dist/client/app/util/config.ts +0 -6
  166. package/dist/client/app/util/const.ts +0 -3
  167. package/dist/client/app/util/dark.ts +0 -4
  168. package/dist/client/app/util/env.ts +0 -1
  169. package/dist/client/app/util/keyboard.ts +0 -99
  170. package/dist/client/app/util/mapping.ts +0 -48
  171. package/dist/client/app/util/preview-settings.ts +0 -8
  172. package/dist/client/app/util/reactivity.ts +0 -22
  173. package/dist/client/app/util/responsive.ts +0 -3
  174. package/dist/client/app/util/scroll.ts +0 -31
  175. package/dist/client/app/util/tooltip.ts +0 -13
  176. package/dist/client/app/util/variant.ts +0 -20
  177. package/dist/client/index.ts +0 -12
  178. package/dist/client/shim.d.ts +0 -5
@@ -0,0 +1,296 @@
1
+ import { createRequire } from 'module'
2
+ import { relative, dirname } from 'pathe'
3
+ import { resolveConfig as resolveViteConfig, Plugin as VitePlugin } from 'vite'
4
+ import { lookup as lookupMime } from 'mrmime'
5
+ import { APP_PATH, DIST_PATH, TEMP_PATH } from './alias.js'
6
+ import { Context } from './context.js'
7
+ import { notifyStoryChange } from './stories.js'
8
+ import { makeTree } from './tree.js'
9
+ import { parseColor } from './colors.js'
10
+ import { createMarkdownRenderer } from './markdown.js'
11
+ import { generateSearchData } from './search.js'
12
+
13
+ const require = createRequire(import.meta.url)
14
+
15
+ export const STORIES_ID = '$histoire-stories'
16
+ export const RESOLVED_STORIES_ID = `/${STORIES_ID}-resolved`
17
+ export const SETUP_ID = '$histoire-setup'
18
+ export const NOOP_ID = '/$histoire-noop'
19
+ export const CONFIG_ID = '$histoire-config'
20
+ export const RESOLVED_CONFIG_ID = `/${CONFIG_ID}-resolved`
21
+ export const THEME_ID = '$histoire-theme'
22
+ export const RESOLVED_THEME_ID = `/${THEME_ID}-resolved.css`
23
+ export const SEARCH_DATA_ID = '$histoire-search-data'
24
+ export const RESOLVED_SEARCH_DATA_ID = `/${SEARCH_DATA_ID}-resolved`
25
+
26
+ export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
27
+ const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build')
28
+ const hasVuePlugin = viteConfig.plugins.find(p => p.name === 'vite:vue')
29
+
30
+ const plugins: VitePlugin[] = []
31
+
32
+ if (!hasVuePlugin) {
33
+ plugins.push((await import('@vitejs/plugin-vue')).default())
34
+ }
35
+
36
+ if (ctx.config.vite) {
37
+ plugins.push({
38
+ name: 'histoire-vite-config-override',
39
+ config (config, env) {
40
+ return typeof ctx.config.vite === 'function' ? ctx.config.vite(config, env) : ctx.config.vite
41
+ },
42
+ })
43
+ }
44
+
45
+ plugins.push({
46
+ name: 'histoire-vite-plugin',
47
+
48
+ config () {
49
+ return {
50
+ optimizeDeps: {
51
+ // force include vue to avoid duplicated copies when linked + optimized
52
+ include: [
53
+ 'vue',
54
+ dirname(require.resolve('@vue/runtime-core/package.json')),
55
+ dirname(require.resolve('shiki/package.json')),
56
+ dirname(require.resolve('vscode-textmate/package.json')),
57
+ dirname(require.resolve('vscode-oniguruma/package.json')),
58
+ dirname(require.resolve('case/package.json')),
59
+ ],
60
+ },
61
+ server: {
62
+ fs: {
63
+ allow: [DIST_PATH, TEMP_PATH, viteConfig.root, process.cwd()],
64
+ },
65
+ },
66
+ define: {
67
+ // We need to force this to be able to use `devtoolsRawSetupState`
68
+ __VUE_PROD_DEVTOOLS__: 'true',
69
+ },
70
+ }
71
+ },
72
+
73
+ async resolveId (id, importer) {
74
+ if (id.startsWith(STORIES_ID)) {
75
+ return RESOLVED_STORIES_ID
76
+ }
77
+ if (id.startsWith(SETUP_ID)) {
78
+ if (ctx.config.setupFile) {
79
+ return this.resolve(ctx.config.setupFile, importer, {
80
+ skipSelf: true,
81
+ })
82
+ } else {
83
+ return NOOP_ID
84
+ }
85
+ }
86
+ if (id.startsWith(CONFIG_ID)) {
87
+ return RESOLVED_CONFIG_ID
88
+ }
89
+ if (id.startsWith(THEME_ID)) {
90
+ return RESOLVED_THEME_ID
91
+ }
92
+ if (id.startsWith(SEARCH_DATA_ID)) {
93
+ return RESOLVED_SEARCH_DATA_ID
94
+ }
95
+ },
96
+
97
+ async load (id) {
98
+ if (id === RESOLVED_STORIES_ID) {
99
+ const resolvedStories = ctx.storyFiles.filter(s => !!s.story)
100
+ const files = resolvedStories.map((file, index) => {
101
+ return {
102
+ id: file.id,
103
+ path: file.treePath,
104
+ story: file.story,
105
+ framework: 'vue3',
106
+ index,
107
+ }
108
+ })
109
+ return `import { defineAsyncComponent } from 'vue'
110
+ ${resolvedStories.map((file, index) => `const Comp${index} = defineAsyncComponent(() => import('${file.path}'))`).join('\n')}
111
+ export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}}`).join(',\n')}]
112
+ export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
113
+ const handlers = []
114
+ export function onUpdate (cb) {
115
+ handlers.push(cb)
116
+ }
117
+ if (import.meta.hot) {
118
+ import.meta.hot.accept(newModule => {
119
+ files = newModule.files
120
+ tree = newModule.tree
121
+ handlers.forEach(h => {
122
+ h(newModule.files, newModule.tree)
123
+ newModule.onUpdate(h)
124
+ })
125
+ })
126
+ }`
127
+ }
128
+ if (id === NOOP_ID) {
129
+ return `export default () => {}`
130
+ }
131
+ if (id === RESOLVED_CONFIG_ID) {
132
+ let js = `export const config = ${JSON.stringify(ctx.config)}\n`
133
+ if (ctx.config.theme?.logo) {
134
+ for (const key in ctx.config.theme.logo) {
135
+ js += `import Logo_${key} from '${ctx.config.theme.logo[key]}'\n`
136
+ }
137
+ }
138
+ js += `export const logos = {${Object.keys(ctx.config.theme?.logo ?? {}).map(key => `${key}: Logo_${key}`).join(', ')}}\n`
139
+ return js
140
+ }
141
+ if (id === RESOLVED_THEME_ID) {
142
+ let css = '*, ::before, ::after {'
143
+ // Colors
144
+ for (const color in ctx.config.theme?.colors ?? {}) {
145
+ for (const key in ctx.config.theme.colors[color]) {
146
+ css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`
147
+ }
148
+ }
149
+ css += '}'
150
+ return css
151
+ }
152
+ if (id === RESOLVED_SEARCH_DATA_ID) {
153
+ return `export let searchData = ${JSON.stringify(await generateSearchData(ctx))}
154
+ const handlers = []
155
+ export function onUpdate (cb) {
156
+ handlers.push(cb)
157
+ }
158
+ if (import.meta.hot) {
159
+ import.meta.hot.accept(newModule => {
160
+ searchData = newModule.searchData
161
+ handlers.forEach(h => {
162
+ h(newModule.searchData)
163
+ newModule.onUpdate(h)
164
+ })
165
+ })
166
+ }`
167
+ }
168
+ },
169
+
170
+ handleHotUpdate (updateContext) {
171
+ const story = ctx.storyFiles.find(file => file.path === updateContext.file)
172
+ if (story) {
173
+ notifyStoryChange(story)
174
+ }
175
+ },
176
+
177
+ configureServer (server) {
178
+ server.middlewares.use(async (req, res, next) => {
179
+ if (req.url!.startsWith(`${server.config.base}__sandbox`)) {
180
+ res.statusCode = 200
181
+ let html = `
182
+ <!DOCTYPE html>
183
+ <html>
184
+ <head>
185
+ <title></title>
186
+ <meta charset="utf-8">
187
+ <meta name="viewport" content="width=device-width,initial-scale=1">
188
+ <meta name="description" content="">
189
+ </head>
190
+ <body>
191
+ <div id="app"></div>
192
+ <script>
193
+ // Hide spammy vite messages
194
+ const origConsoleLog = console.log
195
+ console.log = (...args) => {
196
+ if (typeof args[0] !== 'string' || !args[0].startsWith('[vite] connect')) {
197
+ origConsoleLog(...args)
198
+ }
199
+ }
200
+ </script>
201
+ <script type="module" src="/@fs/${APP_PATH}/sandbox.js"></script>
202
+ </body>
203
+ </html>`
204
+ // Apply Vite HTML transforms. This injects the Vite HMR client, and
205
+ // also applies HTML transforms from Vite plugins
206
+ html = await server.transformIndexHtml(req.url, html)
207
+ res.end(html)
208
+ return
209
+ }
210
+ next()
211
+ })
212
+
213
+ // serve our index.html after vite history fallback
214
+ return () => {
215
+ server.middlewares.use(async (req, res, next) => {
216
+ if (req.url!.endsWith('.html')) {
217
+ res.statusCode = 200
218
+
219
+ let html = `
220
+ <!DOCTYPE html>
221
+ <html>
222
+ <head>
223
+ <title></title>
224
+ <link rel="icon" href=""/>
225
+ <meta charset="utf-8">
226
+ <meta name="viewport" content="width=device-width,initial-scale=1">
227
+ <meta name="description" content="">
228
+ ${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${server.config.base}${ctx.config.theme.favicon}"/>` : ''}
229
+ </head>
230
+ <body>
231
+ <div id="app"></div>
232
+ <script type="module" src="/@fs/${APP_PATH}/index.js"></script>
233
+ </body>
234
+ </html>`
235
+ // Apply Vite HTML transforms. This injects the Vite HMR client, and
236
+ // also applies HTML transforms from Vite plugins
237
+ html = await server.transformIndexHtml(req.url, html)
238
+ res.end(html)
239
+ return
240
+ }
241
+ next()
242
+ })
243
+ }
244
+ },
245
+ })
246
+
247
+ // Custom blocks
248
+ plugins.push(...await createCustomBlocksPlugins(ctx))
249
+
250
+ if (ctx.mode === 'build') {
251
+ // Add file name in build mode to have components names instead of <Anonymous>
252
+ const include = [/\.vue$/]
253
+ const exclude = [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/]
254
+ plugins.push({
255
+ name: 'histoire-file-name-plugin',
256
+ enforce: 'post',
257
+
258
+ transform (code, id) {
259
+ if (exclude.some(r => r.test(id))) return
260
+ if (include.some(r => r.test(id))) {
261
+ const file = relative(viteConfig.root, id)
262
+ const index = code.indexOf('export default')
263
+ const result = `${code.substring(0, index)}_sfc_main.__file = '${file}'\n${code.substring(index)}`
264
+ return result
265
+ }
266
+ },
267
+ })
268
+ }
269
+
270
+ return plugins
271
+ }
272
+
273
+ async function createCustomBlocksPlugins (ctx) {
274
+ const plugins: VitePlugin[] = []
275
+
276
+ let md = await createMarkdownRenderer()
277
+ if (ctx.config.markdown) {
278
+ const result = await ctx.config.markdown(md)
279
+ if (result) {
280
+ md = result
281
+ }
282
+ }
283
+ plugins.push({
284
+ name: 'histoire-vue-docs-block',
285
+ transform (code, id) {
286
+ if (!id.includes('?vue&type=docs')) return
287
+ if (!id.includes('lang.md')) return
288
+ const html = md.render(code)
289
+ return `export default Comp => {
290
+ Comp.doc = ${JSON.stringify(html)}
291
+ }`
292
+ },
293
+ })
294
+
295
+ return plugins
296
+ }
@@ -93,6 +93,7 @@ module.exports.theme.extend.typography = (theme) => ({
93
93
  },
94
94
 
95
95
  '--tw-prose-invert-quote-borders': theme('colors.gray-800'),
96
+ '--tw-prose-invert-hr': theme('colors.gray-800'),
96
97
 
97
98
  blockquote: {
98
99
  marginLeft: 0,
@@ -1,119 +0,0 @@
1
- const KEY_ESCAPE_REG = /[\s-.:|#@$£*%]/
2
- const MAX_SINGLE_LINE_ARRAY_LENGTH = 3
3
-
4
- interface Line {
5
- spaces: number
6
- line: string
7
- }
8
-
9
- export function serializeJs (value: any): string {
10
- if (value === undefined) {
11
- return 'undefined'
12
- }
13
- if (value === null) {
14
- return 'null'
15
- }
16
- if (typeof value === 'string') {
17
- return `'${value}'`
18
- }
19
- if (typeof value === 'boolean') {
20
- return value ? 'true' : 'false'
21
- }
22
- if (Array.isArray(value)) {
23
- return printLines(arrayToSourceLines(value))
24
- }
25
- if (typeof value === 'object') {
26
- return printLines(objectToSourceLines(value))
27
- }
28
- if (value?.__autoBuildingObject) {
29
- return value
30
- }
31
- if (typeof value === 'function' && value.name) {
32
- return value.name
33
- }
34
- return value.toString()
35
- }
36
-
37
- function printLines (lines: Line[]) {
38
- return lines.map(line => ' '.repeat(line.spaces) + line.line).join('\n')
39
- }
40
-
41
- function objectToSourceLines (object, indentCount = 0) {
42
- return createLines(indentCount, lines => {
43
- lines.push('{')
44
- lines.push(...createLines(1, lines => {
45
- for (const key in object) {
46
- const value = object[key]
47
-
48
- let printedKey = key
49
- if (KEY_ESCAPE_REG.test(key)) {
50
- printedKey = `'${printedKey}'`
51
- }
52
-
53
- addLinesFromValue(lines, value, `${printedKey}: `, ',')
54
- }
55
- }))
56
- lines.push('}')
57
- })
58
- }
59
-
60
- function arrayToSourceLines (array: any[], indentCount = 0) {
61
- return createLines(indentCount, lines => {
62
- const contentLines = createLines(1, lines => {
63
- for (const value of array) {
64
- addLinesFromValue(lines, value, '', ',')
65
- }
66
- })
67
- if (contentLines.length === 0) {
68
- lines.push('[]')
69
- } else if (contentLines.length <= MAX_SINGLE_LINE_ARRAY_LENGTH) {
70
- const [first] = contentLines
71
- first.line = contentLines.map(({ line }) => line.substring(0, line.length - 1)).join(', ')
72
- first.line = `[${first.line}]`
73
- first.spaces--
74
- lines.push(first)
75
- } else {
76
- lines.push('[', ...contentLines, ']')
77
- }
78
- })
79
- }
80
-
81
- function createLines (indentCount: number, handler: (lines: any[]) => unknown) {
82
- const lines: any[] = []
83
- handler(lines)
84
- return lines.map(line => {
85
- if (line.spaces != null) {
86
- line.spaces += indentCount
87
- return line
88
- }
89
- return { spaces: indentCount, line }
90
- })
91
- }
92
-
93
- function addLinesFromValue (lines: Line[], value, before, after) {
94
- let result
95
- if (Array.isArray(value)) {
96
- lines.push(...wrap(arrayToSourceLines(value), before, after))
97
- return
98
- } else if (value && typeof value === 'object') {
99
- lines.push(...wrap(objectToSourceLines(value), before, after))
100
- return
101
- } else if (typeof value === 'string') {
102
- result = value.includes('\'') ? `\`${value}\`` : `'${value}'`
103
- } else if (typeof value === 'undefined') {
104
- result = 'undefined'
105
- } else if (value === null) {
106
- result = 'null'
107
- } else if (typeof value === 'boolean') {
108
- result = value ? 'true' : 'false'
109
- } else {
110
- result = value
111
- }
112
- lines.push(before + result + after)
113
- }
114
-
115
- function wrap (lines: Line[], before: string, after: string) {
116
- lines[0].line = before + lines[0].line
117
- lines[lines.length - 1].line += after
118
- return lines
119
- }
@@ -1,79 +0,0 @@
1
- /* eslint-disable @typescript-eslint/ban-types */
2
-
3
- export function indent (lines: string[], count = 1) {
4
- return lines.map(line => `${' '.repeat(count)}${line}`)
5
- }
6
-
7
- export function unindent (code: string) {
8
- const lines = code.split('\n')
9
- let indentLevel = -1
10
- let indentText: string
11
- const linesToAnalyze = lines.filter(line => line.trim().length > 0)
12
- for (const line of linesToAnalyze) {
13
- const match = /^\s*/.exec(line)
14
- if (match && (indentLevel === -1 || indentLevel > match[0].length)) {
15
- indentLevel = match[0].length
16
- indentText = match[0]
17
- }
18
- }
19
- const result: string[] = []
20
- for (const line of lines) {
21
- result.push(line.replace(indentText, ''))
22
- }
23
- return result.join('\n').trim()
24
- }
25
-
26
- interface AutoBuildingOject {
27
- key: string
28
- cache: Record<string | symbol, AutoBuildingOject>
29
- target: any
30
- proxy: any
31
- }
32
-
33
- export function createAutoBuildingObject (format?: (key: string) => string, specialKeysHandler?: (target: any, p: string | symbol) => (() => unknown) | null, key = '', depth = 0): AutoBuildingOject {
34
- const cache: Record<string | symbol, AutoBuildingOject> = {}
35
- if (depth > 32) return { key, cache, target: {}, proxy: () => key }
36
- const target: any = () => {
37
- const k = key + '()'
38
- return format ? format(k) : k
39
- }
40
- const proxy = new Proxy(target, {
41
- get (_, p) {
42
- if (p === '__autoBuildingObject') {
43
- return true
44
- }
45
- if (p === '__autoBuildingObjectGetKey') {
46
- return key
47
- }
48
- if (specialKeysHandler) {
49
- const fn = specialKeysHandler(target, p)
50
- if (fn) {
51
- return fn()
52
- }
53
- }
54
- if (p === 'toString') {
55
- const k = key + '.toString()'
56
- return () => format ? format(k) : k
57
- }
58
- if (p === Symbol.toPrimitive) {
59
- return () => format ? format(key) : key
60
- }
61
- if (!cache[p]) {
62
- const childKey = key ? `${key}.${p.toString()}` : p.toString()
63
- const child = createAutoBuildingObject(format, specialKeysHandler, childKey, depth + 1)
64
- cache[p] = { key: childKey, ...child }
65
- }
66
- return cache[p].proxy
67
- },
68
- apply (_, thisArg, args) {
69
- const k = `${key}(${args.join(', ')})`
70
- return format ? format(k) : k
71
- },
72
- })
73
- return {
74
- key,
75
- cache,
76
- target,
77
- proxy,
78
- }
79
- }