@kubb/fabric-core 0.1.1 → 0.1.2
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/App-DZuROf6f.d.ts +292 -0
- package/dist/App-zyf9KG3p.d.cts +292 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/defineApp-D3B0bU-z.d.cts +14 -0
- package/dist/defineApp-DJVMk9lc.d.ts +14 -0
- package/dist/index.cjs +165 -88
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -8
- package/dist/index.d.ts +4 -8
- package/dist/index.js +154 -77
- package/dist/index.js.map +1 -1
- package/dist/parsers/typescript.cjs +5 -6
- package/dist/parsers/typescript.d.cts +3 -51
- package/dist/parsers/typescript.d.ts +3 -51
- package/dist/parsers/typescript.js +2 -3
- package/dist/parsers.cjs +7 -0
- package/dist/parsers.d.cts +14 -0
- package/dist/parsers.d.ts +14 -0
- package/dist/parsers.js +4 -0
- package/dist/plugins.cjs +76 -0
- package/dist/plugins.cjs.map +1 -0
- package/dist/plugins.d.cts +28 -0
- package/dist/plugins.d.ts +28 -0
- package/dist/plugins.js +71 -0
- package/dist/plugins.js.map +1 -0
- package/dist/tsxParser-C741ZKCN.js +26 -0
- package/dist/tsxParser-C741ZKCN.js.map +1 -0
- package/dist/tsxParser-HDf_3TMc.cjs +37 -0
- package/dist/tsxParser-HDf_3TMc.cjs.map +1 -0
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/{typescript-C60gWBu8.js → typescriptParser-BBGeFKlP.js} +52 -69
- package/dist/typescriptParser-BBGeFKlP.js.map +1 -0
- package/dist/{typescript-Z90jN87k.cjs → typescriptParser-BBbbmG5W.cjs} +61 -108
- package/dist/typescriptParser-BBbbmG5W.cjs.map +1 -0
- package/dist/typescriptParser-C-sBy1iR.d.cts +50 -0
- package/dist/typescriptParser-CtMmz0UV.d.ts +50 -0
- package/package.json +13 -13
- package/src/App.ts +91 -0
- package/src/FileManager.ts +12 -12
- package/src/FileProcessor.ts +49 -46
- package/src/createFile.ts +1 -1
- package/src/defineApp.ts +47 -72
- package/src/index.ts +1 -1
- package/src/parsers/createParser.ts +8 -0
- package/src/parsers/defaultParser.ts +10 -0
- package/src/parsers/index.ts +5 -0
- package/src/parsers/tsxParser.ts +11 -0
- package/src/parsers/types.ts +12 -2
- package/src/parsers/{typescript.ts → typescriptParser.ts} +8 -4
- package/src/plugins/createPlugin.ts +10 -0
- package/src/plugins/fsPlugin.ts +112 -0
- package/src/plugins/index.ts +3 -0
- package/src/plugins/types.ts +15 -0
- package/src/types.ts +4 -1
- package/src/utils/AsyncEventEmitter.ts +37 -0
- package/src/utils/getRelativePath.ts +32 -0
- package/src/utils/trimExtName.ts +3 -0
- package/dist/KubbFile-BrN7Wwp6.d.cts +0 -119
- package/dist/KubbFile-BzVkcu9M.d.ts +0 -119
- package/dist/createFileParser-BD8yn0LT.cjs +0 -14
- package/dist/createFileParser-BD8yn0LT.cjs.map +0 -1
- package/dist/createFileParser-Cix3AMLd.js +0 -8
- package/dist/createFileParser-Cix3AMLd.js.map +0 -1
- package/dist/default-DCpuPmrL.js +0 -10
- package/dist/default-DCpuPmrL.js.map +0 -1
- package/dist/default-DNBu_jsL.cjs +0 -15
- package/dist/default-DNBu_jsL.cjs.map +0 -1
- package/dist/defineApp-CZYKsxTp.d.ts +0 -95
- package/dist/defineApp-c9lWJ96_.d.cts +0 -95
- package/dist/parsers/default.cjs +0 -4
- package/dist/parsers/default.d.cts +0 -8
- package/dist/parsers/default.d.ts +0 -8
- package/dist/parsers/default.js +0 -4
- package/dist/parsers/tsx.cjs +0 -5
- package/dist/parsers/tsx.d.cts +0 -8
- package/dist/parsers/tsx.d.ts +0 -8
- package/dist/parsers/tsx.js +0 -5
- package/dist/tsx-BSUaIML3.cjs +0 -16
- package/dist/tsx-BSUaIML3.cjs.map +0 -1
- package/dist/tsx-DBAk9dqS.js +0 -11
- package/dist/tsx-DBAk9dqS.js.map +0 -1
- package/dist/types-CkbelZaS.d.ts +0 -15
- package/dist/types-GueHciQ3.d.cts +0 -15
- package/dist/typescript-C60gWBu8.js.map +0 -1
- package/dist/typescript-Z90jN87k.cjs.map +0 -1
- package/src/fs.ts +0 -167
- package/src/parsers/createFileParser.ts +0 -5
- package/src/parsers/default.ts +0 -7
- package/src/parsers/tsx.ts +0 -8
package/src/FileProcessor.ts
CHANGED
|
@@ -1,85 +1,88 @@
|
|
|
1
1
|
import type * as KubbFile from './KubbFile.ts'
|
|
2
|
-
import { EventEmitter } from './utils/EventEmitter.ts'
|
|
3
|
-
import { write } from './fs.ts'
|
|
4
2
|
import pLimit from 'p-limit'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
|
|
5
5
|
import type { Parser } from './parsers/types.ts'
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
start: [{ files: KubbFile.ResolvedFile[] }]
|
|
12
|
-
finish: [{ files: KubbFile.ResolvedFile[] }]
|
|
13
|
-
'file:start': [{ file: KubbFile.ResolvedFile }]
|
|
14
|
-
'file:finish': [{ file: KubbFile.ResolvedFile }]
|
|
15
|
-
}
|
|
6
|
+
import { defaultParser } from './parsers/defaultParser.ts'
|
|
7
|
+
import { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'
|
|
8
|
+
import type { AppEvents } from './App.ts'
|
|
9
|
+
import { typescriptParser } from './parsers/typescriptParser.ts'
|
|
10
|
+
import { tsxParser } from './parsers/tsxParser.ts'
|
|
16
11
|
|
|
17
12
|
export type ProcessFilesProps = {
|
|
13
|
+
parsers?: Set<Parser>
|
|
18
14
|
extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>
|
|
19
15
|
dryRun?: boolean
|
|
20
16
|
}
|
|
21
17
|
|
|
22
|
-
type
|
|
18
|
+
type GetParseOptions = {
|
|
19
|
+
parsers?: Set<Parser>
|
|
23
20
|
extname?: KubbFile.Extname
|
|
24
21
|
}
|
|
25
22
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
23
|
+
type Options = {
|
|
24
|
+
events?: AsyncEventEmitter<AppEvents>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class FileProcessor {
|
|
28
|
+
#limit = pLimit(100)
|
|
29
|
+
events: AsyncEventEmitter<AppEvents>
|
|
30
|
+
|
|
31
|
+
constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {
|
|
32
|
+
this.events = events
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
return defaultParser
|
|
34
|
+
return this
|
|
37
35
|
}
|
|
38
36
|
|
|
39
|
-
|
|
37
|
+
get #defaultParser(): Set<Parser> {
|
|
38
|
+
console.warn(`[parser] using default parsers, please consider using the "use" method to add custom parsers.`)
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
console.warn(`[parser] No parser found for ${extname}, default parser will be used`)
|
|
40
|
+
return new Set<Parser>([typescriptParser, tsxParser, defaultParser])
|
|
43
41
|
}
|
|
44
42
|
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
async parse(file: KubbFile.ResolvedFile, { parsers = this.#defaultParser, extname }: GetParseOptions = {}): Promise<string> {
|
|
44
|
+
if (!extname) {
|
|
45
|
+
console.warn('[parser] No extname found, default parser will be used')
|
|
46
|
+
return defaultParser.parse(file, { extname })
|
|
47
|
+
}
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
const parser = await getParser(file.extname)
|
|
49
|
+
const parser = [...parsers].find((item) => item.extNames?.includes(extname))
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
}
|
|
51
|
+
if (!parser) {
|
|
52
|
+
console.warn(`[parser] No parser found for ${extname}, default parser will be used`)
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
return defaultParser.parse(file, { extname })
|
|
55
|
+
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
super(maxListener)
|
|
59
|
-
return this
|
|
57
|
+
return parser.parse(file, { extname })
|
|
60
58
|
}
|
|
61
59
|
|
|
62
|
-
async run(files: Array<KubbFile.ResolvedFile>, { dryRun, extension }: ProcessFilesProps): Promise<KubbFile.ResolvedFile[]> {
|
|
63
|
-
this.emit('start', { files })
|
|
60
|
+
async run(files: Array<KubbFile.ResolvedFile>, { parsers, dryRun, extension }: ProcessFilesProps = {}): Promise<KubbFile.ResolvedFile[]> {
|
|
61
|
+
await this.events.emit('process:start', { files })
|
|
62
|
+
|
|
63
|
+
let processed = 0
|
|
64
|
+
const total = files.length
|
|
64
65
|
|
|
65
|
-
const promises = files.map((resolvedFile) =>
|
|
66
|
+
const promises = files.map((resolvedFile, index) =>
|
|
66
67
|
this.#limit(async () => {
|
|
67
|
-
const extname = extension?.[resolvedFile.extname] ||
|
|
68
|
+
const extname = extension?.[resolvedFile.extname] || (path.extname(resolvedFile.path) as KubbFile.Extname)
|
|
68
69
|
|
|
69
|
-
this.emit('file:start', { file: resolvedFile })
|
|
70
|
+
await this.events.emit('file:start', { file: resolvedFile, index, total })
|
|
70
71
|
|
|
71
72
|
if (!dryRun) {
|
|
72
|
-
const source = await
|
|
73
|
-
await
|
|
73
|
+
const source = await this.parse(resolvedFile, { extname, parsers })
|
|
74
|
+
await this.events.emit('process:progress', { file: resolvedFile, source, processed, percentage: (processed / total) * 100, total })
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
this.emit('file:
|
|
77
|
+
await this.events.emit('file:end', { file: resolvedFile, index, total })
|
|
78
|
+
|
|
79
|
+
processed++
|
|
77
80
|
}),
|
|
78
81
|
)
|
|
79
82
|
|
|
80
83
|
await Promise.all(promises)
|
|
81
84
|
|
|
82
|
-
this.emit('
|
|
85
|
+
await this.events.emit('process:end', { files })
|
|
83
86
|
|
|
84
87
|
return files
|
|
85
88
|
}
|
package/src/createFile.ts
CHANGED
package/src/defineApp.ts
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import type * as KubbFile from './KubbFile.ts'
|
|
2
1
|
import { FileManager } from './FileManager.ts'
|
|
3
|
-
import { isPromise } from 'remeda'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type Component
|
|
8
|
-
|
|
9
|
-
type PluginInstallFunction<Options = any[]> = Options extends unknown[] ? (app: App, ...options: Options) => any : (app: App, options: Options) => any
|
|
10
|
-
|
|
11
|
-
export type ObjectPlugin<Options = any[]> = {
|
|
12
|
-
install: PluginInstallFunction<Options>
|
|
13
|
-
}
|
|
14
|
-
export type FunctionPlugin<Options = any[]> = PluginInstallFunction<Options> & Partial<ObjectPlugin<Options>>
|
|
2
|
+
import { isFunction, isPromise } from 'remeda'
|
|
3
|
+
import type { Plugin } from './plugins/types.ts'
|
|
4
|
+
import type { Parser } from './parsers/types.ts'
|
|
5
|
+
import { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'
|
|
6
|
+
import type { App, AppContext, Component, AppEvents } from './App.ts'
|
|
15
7
|
|
|
16
8
|
type AppRenderer = {
|
|
17
9
|
render(): Promise<void> | void
|
|
@@ -19,57 +11,27 @@ type AppRenderer = {
|
|
|
19
11
|
waitUntilExit(): Promise<void>
|
|
20
12
|
}
|
|
21
13
|
|
|
22
|
-
export type AppContext<TOptions = unknown> = {
|
|
23
|
-
options?: TOptions
|
|
24
|
-
fileManager: FileManager
|
|
25
|
-
addFile(...files: Array<KubbFile.File>): Promise<void>
|
|
26
|
-
files: Array<KubbFile.ResolvedFile>
|
|
27
|
-
clear: () => void
|
|
28
|
-
}
|
|
29
|
-
|
|
30
14
|
type RootRenderFunction<THostElement, TContext extends AppContext> = (this: TContext, container: THostElement, context: TContext) => AppRenderer
|
|
31
15
|
|
|
32
|
-
type Plugin<Options = any[], P extends unknown[] = Options extends unknown[] ? Options : [Options]> = FunctionPlugin<P> | ObjectPlugin<P>
|
|
33
|
-
|
|
34
|
-
type WriteOptions = {
|
|
35
|
-
extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>
|
|
36
|
-
dryRun?: boolean
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface App {
|
|
40
|
-
_component: Component
|
|
41
|
-
render(): Promise<void>
|
|
42
|
-
renderToString(): Promise<string>
|
|
43
|
-
files: Array<KubbFile.ResolvedFile>
|
|
44
|
-
use<Options>(plugin: Plugin<Options>, options: NoInfer<Options>): this
|
|
45
|
-
write(options?: WriteOptions): Promise<void>
|
|
46
|
-
addFile(...files: Array<KubbFile.File>): Promise<void>
|
|
47
|
-
waitUntilExit(): Promise<void>
|
|
48
|
-
}
|
|
49
|
-
|
|
50
16
|
export type DefineApp<TContext extends AppContext> = (rootComponent?: Component, options?: TContext['options']) => App
|
|
51
17
|
|
|
52
18
|
export function defineApp<THostElement, TContext extends AppContext>(instance: RootRenderFunction<THostElement, TContext>): DefineApp<TContext> {
|
|
53
19
|
function createApp(rootComponent: Component, options?: TContext['options']): App {
|
|
54
|
-
const
|
|
55
|
-
const
|
|
20
|
+
const events = new AsyncEventEmitter<AppEvents>()
|
|
21
|
+
const installedPlugins = new Set<Plugin>()
|
|
22
|
+
const installedParsers = new Set<Parser>()
|
|
23
|
+
const fileManager = new FileManager({ events })
|
|
56
24
|
const context = {
|
|
25
|
+
events,
|
|
57
26
|
options,
|
|
58
27
|
fileManager,
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
},
|
|
62
|
-
clear() {
|
|
63
|
-
context.fileManager.clear()
|
|
64
|
-
},
|
|
65
|
-
get files() {
|
|
66
|
-
return fileManager.files
|
|
67
|
-
},
|
|
28
|
+
installedPlugins,
|
|
29
|
+
installedParsers,
|
|
68
30
|
} as TContext
|
|
69
31
|
|
|
70
32
|
const { render, renderToString, waitUntilExit } = instance.call(context, rootComponent, context)
|
|
71
33
|
|
|
72
|
-
const app
|
|
34
|
+
const app = {
|
|
73
35
|
_component: rootComponent,
|
|
74
36
|
async render() {
|
|
75
37
|
if (isPromise(render)) {
|
|
@@ -85,32 +47,45 @@ export function defineApp<THostElement, TContext extends AppContext>(instance: R
|
|
|
85
47
|
return fileManager.files
|
|
86
48
|
},
|
|
87
49
|
waitUntilExit,
|
|
88
|
-
addFile
|
|
89
|
-
|
|
90
|
-
options = {
|
|
91
|
-
extension: { '.ts': '.ts' },
|
|
92
|
-
dryRun: false,
|
|
93
|
-
},
|
|
94
|
-
) {
|
|
95
|
-
await fileManager.processor.run({
|
|
96
|
-
extension: options.extension,
|
|
97
|
-
dryRun: options.dryRun,
|
|
98
|
-
})
|
|
50
|
+
async addFile(...newFiles) {
|
|
51
|
+
await fileManager.add(...newFiles)
|
|
99
52
|
},
|
|
100
|
-
use(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
installedPlugins.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
53
|
+
use(pluginOrParser, ...options) {
|
|
54
|
+
const args = Array.isArray(options) ? options : [options[0]]
|
|
55
|
+
|
|
56
|
+
if (pluginOrParser.type === 'plugin') {
|
|
57
|
+
if (installedPlugins.has(pluginOrParser)) {
|
|
58
|
+
console.warn('Plugin has already been applied to target app.')
|
|
59
|
+
} else {
|
|
60
|
+
installedPlugins.add(pluginOrParser)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (pluginOrParser.override && isFunction(pluginOrParser.override)) {
|
|
64
|
+
const overrider = pluginOrParser.override
|
|
65
|
+
|
|
66
|
+
const extraApp = (overrider as any)(app, context, ...args)
|
|
67
|
+
Object.assign(app, extraApp)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (pluginOrParser.type === 'parser') {
|
|
71
|
+
if (installedParsers.has(pluginOrParser)) {
|
|
72
|
+
console.warn('Parser has already been applied to target app.')
|
|
73
|
+
} else {
|
|
74
|
+
installedParsers.add(pluginOrParser)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (pluginOrParser && isFunction(pluginOrParser.install)) {
|
|
79
|
+
const installer = pluginOrParser.install
|
|
80
|
+
|
|
81
|
+
;(installer as any)(app, context, ...args)
|
|
109
82
|
}
|
|
110
83
|
|
|
111
84
|
return app
|
|
112
85
|
},
|
|
113
|
-
}
|
|
86
|
+
} as App
|
|
87
|
+
|
|
88
|
+
events.emit('start', { app })
|
|
114
89
|
|
|
115
90
|
return app
|
|
116
91
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,4 +3,4 @@ export { defineApp } from './defineApp.ts'
|
|
|
3
3
|
export { FileManager } from './FileManager.ts'
|
|
4
4
|
export { createFile } from './createFile.ts'
|
|
5
5
|
export { FileProcessor } from './FileProcessor.ts'
|
|
6
|
-
export {
|
|
6
|
+
export type { App } from './App.ts'
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { typescriptParser } from './typescriptParser.ts'
|
|
2
|
+
import { createParser } from './createParser.ts'
|
|
3
|
+
|
|
4
|
+
export const tsxParser = createParser({
|
|
5
|
+
name: 'tsx',
|
|
6
|
+
extNames: ['.tsx', '.jsx'],
|
|
7
|
+
install() {},
|
|
8
|
+
async parse(file, options = { extname: '.tsx' }) {
|
|
9
|
+
return typescriptParser.parse(file, options)
|
|
10
|
+
},
|
|
11
|
+
})
|
package/src/parsers/types.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import type * as KubbFile from '../KubbFile.ts'
|
|
2
|
+
import type { Install } from '../App.ts'
|
|
2
3
|
|
|
3
4
|
type PrintOptions = {
|
|
4
5
|
extname?: KubbFile.Extname
|
|
5
6
|
}
|
|
6
7
|
|
|
7
|
-
export type Parser<TMeta extends object =
|
|
8
|
+
export type Parser<TOptions = any[], TMeta extends object = any> = {
|
|
9
|
+
name: string
|
|
10
|
+
type: 'parser'
|
|
11
|
+
/**
|
|
12
|
+
* Undefined is being used for the defaultParser
|
|
13
|
+
*/
|
|
14
|
+
extNames: Array<KubbFile.Extname> | undefined
|
|
15
|
+
install: Install<TOptions>
|
|
8
16
|
/**
|
|
9
17
|
* Convert a file to string
|
|
10
18
|
*/
|
|
11
|
-
|
|
19
|
+
parse(file: KubbFile.ResolvedFile<TMeta>, options: PrintOptions): Promise<string>
|
|
12
20
|
}
|
|
21
|
+
|
|
22
|
+
export type UserParser<TOptions = any[], TMeta extends object = any> = Omit<Parser<TOptions, TMeta>, 'type'>
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import ts from 'typescript'
|
|
2
|
-
import { getRelativePath
|
|
2
|
+
import { getRelativePath } from '../utils/getRelativePath.ts'
|
|
3
|
+
import { trimExtName } from '../utils/trimExtName.ts'
|
|
3
4
|
import path from 'node:path'
|
|
4
|
-
import {
|
|
5
|
+
import { createParser } from './createParser.ts'
|
|
5
6
|
|
|
6
7
|
const { factory } = ts
|
|
7
8
|
|
|
@@ -147,8 +148,11 @@ export function createExport({
|
|
|
147
148
|
)
|
|
148
149
|
}
|
|
149
150
|
|
|
150
|
-
export const
|
|
151
|
-
|
|
151
|
+
export const typescriptParser = createParser({
|
|
152
|
+
name: 'typescript',
|
|
153
|
+
extNames: ['.ts', '.js'],
|
|
154
|
+
install() {},
|
|
155
|
+
async parse(file, options = { extname: '.ts' }) {
|
|
152
156
|
const source = file.sources.map((item) => item.value).join('\n\n')
|
|
153
157
|
|
|
154
158
|
const importNodes = file.imports
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Plugin, UserPlugin } from './types.ts'
|
|
2
|
+
|
|
3
|
+
export function createPlugin<Options = any[], TAppExtension extends Record<string, any> = {}>(
|
|
4
|
+
plugin: UserPlugin<Options, TAppExtension>,
|
|
5
|
+
): Plugin<Options, TAppExtension> {
|
|
6
|
+
return {
|
|
7
|
+
type: 'plugin',
|
|
8
|
+
...plugin,
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { createPlugin } from './createPlugin.ts'
|
|
2
|
+
import { switcher } from 'js-runtime'
|
|
3
|
+
import fs from 'fs-extra'
|
|
4
|
+
import { resolve } from 'node:path'
|
|
5
|
+
import type * as KubbFile from '../KubbFile.ts'
|
|
6
|
+
|
|
7
|
+
type Options = {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback that is invoked whenever a file is written by the plugin.
|
|
10
|
+
* Useful for tests to observe write operations without spying on internal functions.
|
|
11
|
+
*/
|
|
12
|
+
onWrite?: (path: string, data: string) => void | Promise<void>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function write(path: string, data: string, options: { sanity?: boolean } = {}): Promise<string | undefined> {
|
|
16
|
+
if (data.trim() === '') {
|
|
17
|
+
return undefined
|
|
18
|
+
}
|
|
19
|
+
return switcher(
|
|
20
|
+
{
|
|
21
|
+
node: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {
|
|
22
|
+
try {
|
|
23
|
+
const oldContent = await fs.readFile(resolve(path), {
|
|
24
|
+
encoding: 'utf-8',
|
|
25
|
+
})
|
|
26
|
+
if (oldContent?.toString() === data?.toString()) {
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
} catch (_err) {
|
|
30
|
+
/* empty */
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await fs.outputFile(resolve(path), data, { encoding: 'utf-8' })
|
|
34
|
+
|
|
35
|
+
if (sanity) {
|
|
36
|
+
const savedData = await fs.readFile(resolve(path), {
|
|
37
|
+
encoding: 'utf-8',
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
if (savedData?.toString() !== data?.toString()) {
|
|
41
|
+
throw new Error(`Sanity check failed for ${path}\n\nData[${data.length}]:\n${data}\n\nSaved[${savedData.length}]:\n${savedData}\n`)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return savedData
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return data
|
|
48
|
+
},
|
|
49
|
+
bun: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {
|
|
50
|
+
try {
|
|
51
|
+
await Bun.write(resolve(path), data)
|
|
52
|
+
|
|
53
|
+
if (sanity) {
|
|
54
|
+
const file = Bun.file(resolve(path))
|
|
55
|
+
const savedData = await file.text()
|
|
56
|
+
|
|
57
|
+
if (savedData?.toString() !== data?.toString()) {
|
|
58
|
+
throw new Error(`Sanity check failed for ${path}\n\nData[${path.length}]:\n${path}\n\nSaved[${savedData.length}]:\n${savedData}\n`)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return savedData
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return data
|
|
65
|
+
} catch (e) {
|
|
66
|
+
console.error(e)
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
'node',
|
|
71
|
+
)(path, data.trim(), options)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
type WriteOptions = {
|
|
75
|
+
extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>
|
|
76
|
+
dryRun?: boolean
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
declare module '../index.ts' {
|
|
80
|
+
interface App {
|
|
81
|
+
write(options?: WriteOptions): Promise<void>
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const fsPlugin = createPlugin<Options, { write(options?: WriteOptions): Promise<void> }>({
|
|
86
|
+
name: 'fs',
|
|
87
|
+
scope: 'write',
|
|
88
|
+
async install(_app, context, options) {
|
|
89
|
+
context.events.on('process:progress', async ({ file, source }) => {
|
|
90
|
+
if (options?.onWrite) {
|
|
91
|
+
await options.onWrite(file.path, source)
|
|
92
|
+
}
|
|
93
|
+
await write(file.path, source, { sanity: false })
|
|
94
|
+
})
|
|
95
|
+
},
|
|
96
|
+
override(_app, context) {
|
|
97
|
+
return {
|
|
98
|
+
async write(
|
|
99
|
+
options = {
|
|
100
|
+
extension: { '.ts': '.ts' },
|
|
101
|
+
dryRun: false,
|
|
102
|
+
},
|
|
103
|
+
) {
|
|
104
|
+
await context.fileManager.write({
|
|
105
|
+
extension: options.extension,
|
|
106
|
+
dryRun: options.dryRun,
|
|
107
|
+
parsers: context.installedParsers,
|
|
108
|
+
})
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Install, Override } from '../App.ts'
|
|
2
|
+
|
|
3
|
+
export type Plugin<TOptions = any[], TAppExtension extends Record<string, any> = {}> = {
|
|
4
|
+
name: string
|
|
5
|
+
type: 'plugin'
|
|
6
|
+
scope?: 'write' | 'read' | (string & {})
|
|
7
|
+
install: Install<TOptions> | Promise<Install<TOptions>>
|
|
8
|
+
/**
|
|
9
|
+
* Runtime app overrides or extensions.
|
|
10
|
+
* Merged into the app instance after install.
|
|
11
|
+
*/
|
|
12
|
+
override?: Override<TOptions, TAppExtension>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type UserPlugin<TOptions = any[], TAppExtension extends Record<string, any> = {}> = Omit<Plugin<TOptions, TAppExtension>, 'type'>
|
package/src/types.ts
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { EventEmitter as NodeEventEmitter } from 'node:events'
|
|
2
|
+
|
|
3
|
+
export class AsyncEventEmitter<TEvents extends Record<string, any>> {
|
|
4
|
+
constructor(maxListener = 100) {
|
|
5
|
+
this.#emitter.setMaxListeners(maxListener)
|
|
6
|
+
}
|
|
7
|
+
#emitter = new NodeEventEmitter()
|
|
8
|
+
|
|
9
|
+
async emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void> {
|
|
10
|
+
const listeners = this.#emitter.listeners(eventName) as Array<(...args: TEvents[TEventName]) => any>
|
|
11
|
+
|
|
12
|
+
if (listeners.length === 0) {
|
|
13
|
+
return undefined
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
await Promise.all(
|
|
17
|
+
listeners.map(async (listener) => {
|
|
18
|
+
try {
|
|
19
|
+
return await listener(...eventArgs)
|
|
20
|
+
} catch (err) {
|
|
21
|
+
console.error(`Error in async listener for "${eventName}":`, err)
|
|
22
|
+
}
|
|
23
|
+
}),
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
on<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {
|
|
28
|
+
this.#emitter.on(eventName, handler as any)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {
|
|
32
|
+
this.#emitter.off(eventName, handler as any)
|
|
33
|
+
}
|
|
34
|
+
removeAll(): void {
|
|
35
|
+
this.#emitter.removeAllListeners()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { normalize, relative } from 'node:path'
|
|
2
|
+
|
|
3
|
+
function slash(path: string, platform: 'windows' | 'mac' | 'linux' = 'linux') {
|
|
4
|
+
const isWindowsPath = /^\\\\\?\\/.test(path)
|
|
5
|
+
const normalizedPath = normalize(path)
|
|
6
|
+
|
|
7
|
+
if (['linux', 'mac'].includes(platform) && !isWindowsPath) {
|
|
8
|
+
// linux and mac
|
|
9
|
+
return normalizedPath.replaceAll(/\\/g, '/').replace('../', '')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// windows
|
|
13
|
+
return normalizedPath.replaceAll(/\\/g, '/').replace('../', '')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getRelativePath(rootDir?: string | null, filePath?: string | null, platform: 'windows' | 'mac' | 'linux' = 'linux'): string {
|
|
17
|
+
if (!rootDir || !filePath) {
|
|
18
|
+
throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const relativePath = relative(rootDir, filePath)
|
|
22
|
+
|
|
23
|
+
// On Windows, paths are separated with a "\"
|
|
24
|
+
// However, web browsers use "/" no matter the platform
|
|
25
|
+
const slashedPath = slash(relativePath, platform)
|
|
26
|
+
|
|
27
|
+
if (slashedPath.startsWith('../')) {
|
|
28
|
+
return slashedPath
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return `./${slashedPath}`
|
|
32
|
+
}
|