@kubb/core 0.28.0 → 0.29.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.
- package/dist/index.d.ts +67 -38
- package/dist/index.js +143 -118
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +143 -119
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import EventEmitter from 'events';
|
|
2
|
+
import { DirectoryTreeOptions } from 'directory-tree';
|
|
2
3
|
|
|
3
|
-
type GenerateTreeNodeOptions = {
|
|
4
|
-
extensions?: RegExp;
|
|
5
|
-
exclude?: RegExp[];
|
|
6
|
-
};
|
|
7
4
|
declare class TreeNode<T = unknown> {
|
|
8
5
|
data: T;
|
|
9
6
|
parent?: TreeNode<T>;
|
|
@@ -14,7 +11,7 @@ declare class TreeNode<T = unknown> {
|
|
|
14
11
|
leaves(): this[];
|
|
15
12
|
root(): any;
|
|
16
13
|
forEach(callback: (treeNode: TreeNode<T>) => void): this;
|
|
17
|
-
static generate(path: string, options?:
|
|
14
|
+
static generate(path: string, options?: DirectoryTreeOptions): TreeNode<{
|
|
18
15
|
name: string;
|
|
19
16
|
path: string;
|
|
20
17
|
type: "file" | "directory";
|
|
@@ -65,8 +62,6 @@ type Status = 'new' | 'success' | 'removed';
|
|
|
65
62
|
|
|
66
63
|
declare class FileManager {
|
|
67
64
|
private cache;
|
|
68
|
-
private readonly root;
|
|
69
|
-
private readonly output;
|
|
70
65
|
emitter: EventEmitter;
|
|
71
66
|
events: {
|
|
72
67
|
emitFile: (id: string, file: File) => void;
|
|
@@ -74,29 +69,23 @@ declare class FileManager {
|
|
|
74
69
|
emitStatusChangeById: (id: string, status: Status) => void;
|
|
75
70
|
emitSuccess: () => void;
|
|
76
71
|
emitRemove: (id: string, file: File) => void;
|
|
77
|
-
onAdd: (callback: (id: string, file: File) => void) =>
|
|
78
|
-
onStatusChange: (callback: (file: File) => void) =>
|
|
79
|
-
onStatusChangeById: (id: string, callback: (status: Status) => void) =>
|
|
80
|
-
onSuccess: (callback: () => void) =>
|
|
81
|
-
onRemove: (id: string, callback: (file: File) => void) =>
|
|
72
|
+
onAdd: (callback: (id: string, file: File) => void) => () => void;
|
|
73
|
+
onStatusChange: (callback: (file: File) => void) => () => void;
|
|
74
|
+
onStatusChangeById: (id: string, callback: (status: Status) => void) => () => void;
|
|
75
|
+
onSuccess: (callback: () => void) => () => void;
|
|
76
|
+
onRemove: (id: string, callback: (file: File) => void) => () => void;
|
|
82
77
|
};
|
|
83
|
-
constructor(
|
|
84
|
-
root?: string;
|
|
85
|
-
output?: string;
|
|
86
|
-
});
|
|
78
|
+
constructor();
|
|
87
79
|
private getCache;
|
|
88
|
-
private
|
|
80
|
+
private getCacheByPath;
|
|
81
|
+
private getCountByStatus;
|
|
89
82
|
get files(): File[];
|
|
90
|
-
get tree(): TreeNode<{
|
|
91
|
-
name: string;
|
|
92
|
-
path: string;
|
|
93
|
-
type: "file" | "directory";
|
|
94
|
-
}>;
|
|
95
|
-
generateRootFiles(options: GenerateTreeNodeOptions): Promise<void>[];
|
|
96
83
|
add(file: File): Promise<File>;
|
|
84
|
+
addOrAppend(file: File): Promise<File>;
|
|
97
85
|
setStatus(id: UUID, status: Status): void;
|
|
98
86
|
get(id: UUID): File | undefined;
|
|
99
87
|
remove(id: UUID): void;
|
|
88
|
+
static writeIndexes(root: string, output: string, options: Parameters<typeof TreeNode.generate>[1]): Promise<void>[];
|
|
100
89
|
}
|
|
101
90
|
|
|
102
91
|
interface Cache<TCache = any> {
|
|
@@ -126,30 +115,36 @@ type KubbConfig = {
|
|
|
126
115
|
* @default process.cwd()
|
|
127
116
|
*/
|
|
128
117
|
root: string;
|
|
129
|
-
mode?: 'single';
|
|
130
118
|
input: {
|
|
131
119
|
/**
|
|
132
|
-
* Path
|
|
120
|
+
* Path to be used as the input. Can be an absolute path, or a path relative from
|
|
121
|
+
* the defined root option.
|
|
133
122
|
*/
|
|
134
123
|
path: string;
|
|
135
124
|
};
|
|
136
125
|
output: {
|
|
137
126
|
/**
|
|
138
|
-
* Path to export
|
|
127
|
+
* Path to be used to export all generated files. Can be an absolute path, or a path relative from
|
|
128
|
+
* the defined root option.
|
|
139
129
|
*/
|
|
140
130
|
path: string;
|
|
131
|
+
/**
|
|
132
|
+
* Remove previous generated files and folders.
|
|
133
|
+
*/
|
|
141
134
|
clean?: boolean;
|
|
142
135
|
};
|
|
143
136
|
/**
|
|
144
137
|
* Array of Kubb plugins to use.
|
|
138
|
+
* The plugin/package can forsee some options that you need to pass through.
|
|
139
|
+
* Sometimes a plugin is depended on another plugin, if that's the case you will get an error back from the plugin you installed.
|
|
145
140
|
*/
|
|
146
141
|
plugins?: KubbPlugin[];
|
|
147
142
|
/**
|
|
148
|
-
* Hooks that
|
|
143
|
+
* Hooks that will be called when a specific action is triggered in Kubb.
|
|
149
144
|
*/
|
|
150
145
|
hooks?: {
|
|
151
146
|
/**
|
|
152
|
-
* Hook that will be
|
|
147
|
+
* Hook that will be triggerend at the end of all executions.
|
|
153
148
|
*/
|
|
154
149
|
done?: string | string[];
|
|
155
150
|
};
|
|
@@ -173,23 +168,55 @@ type PluginFactoryOptions<Options = unknown, Nested extends boolean = false, Api
|
|
|
173
168
|
api: Api;
|
|
174
169
|
};
|
|
175
170
|
type PluginLifecycle = {
|
|
171
|
+
/**
|
|
172
|
+
* Valdiate all plugins to see if their depended plugins are installed and configured.
|
|
173
|
+
* @type hookParallel
|
|
174
|
+
*/
|
|
176
175
|
validate: (this: PluginContext, plugins: KubbPlugin[]) => MaybePromise<ValidationResult>;
|
|
176
|
+
/**
|
|
177
|
+
* Start of the lifecycle of a plugin.
|
|
178
|
+
* @type hookParallel
|
|
179
|
+
*/
|
|
177
180
|
buildStart: (this: PluginContext, kubbConfig: KubbConfig) => MaybePromise<void>;
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Resolve to an id based on importee(example: `./Pet.ts`) and directory(example: `./models`).
|
|
183
|
+
* @type hookFirst
|
|
184
|
+
* @example ('./Pet.ts', './src/gen/')
|
|
185
|
+
*/
|
|
186
|
+
resolveId: (this: PluginContext, fileName: string, directory?: string, options?: Record<string, any>) => OptionalPath;
|
|
187
|
+
/**
|
|
188
|
+
* Makes it possible to run async logic to override the path defined previously by `resolveId`.
|
|
189
|
+
* @type hookFirst
|
|
190
|
+
*/
|
|
191
|
+
load: (this: PluginContext, path: Path) => MaybePromise<TransformResult | null>;
|
|
192
|
+
/**
|
|
193
|
+
* Transform the source-code.
|
|
194
|
+
* @type hookReduceArg0
|
|
195
|
+
*/
|
|
196
|
+
transform: (this: PluginContext, source: string, path: Path) => MaybePromise<TransformResult>;
|
|
197
|
+
/**
|
|
198
|
+
* Write the result to the file-system based on the id(defined by `resolveId` or changed by `load`).
|
|
199
|
+
* @type hookParallel
|
|
200
|
+
*/
|
|
201
|
+
writeFile: (this: PluginContext, source: string | undefined, path: Path) => MaybePromise<void>;
|
|
202
|
+
/**
|
|
203
|
+
* End of the plugin lifecycle.
|
|
204
|
+
* @type hookParallel
|
|
205
|
+
*/
|
|
182
206
|
buildEnd: (this: PluginContext) => MaybePromise<void>;
|
|
183
207
|
};
|
|
184
208
|
type PluginLifecycleHooks = keyof PluginLifecycle;
|
|
185
209
|
type ResolveIdParams = {
|
|
186
|
-
|
|
187
|
-
|
|
210
|
+
fileName: string;
|
|
211
|
+
directory?: string | undefined;
|
|
188
212
|
/**
|
|
189
213
|
* When set, resolveId will only call resolveId of the name of the plugin set here.
|
|
190
214
|
* If not defined it will fall back on the resolveId of the core plugin.
|
|
191
215
|
*/
|
|
192
216
|
pluginName?: string;
|
|
217
|
+
/**
|
|
218
|
+
* Options to be passed to 'resolveId' 3th parameter
|
|
219
|
+
*/
|
|
193
220
|
options?: Record<string, any>;
|
|
194
221
|
};
|
|
195
222
|
type PluginContext = {
|
|
@@ -199,7 +226,7 @@ type PluginContext = {
|
|
|
199
226
|
addFile: (emitedFile: EmittedFile | File, options?: {
|
|
200
227
|
root?: true;
|
|
201
228
|
}) => Promise<File>;
|
|
202
|
-
resolveId: (params: ResolveIdParams) => MaybePromise<
|
|
229
|
+
resolveId: (params: ResolveIdParams) => MaybePromise<OptionalPath>;
|
|
203
230
|
load: (id: string) => MaybePromise<TransformResult | void>;
|
|
204
231
|
};
|
|
205
232
|
type ValidationResult = true | {
|
|
@@ -209,7 +236,8 @@ type TransformResult = string | null;
|
|
|
209
236
|
/**
|
|
210
237
|
* @description Computing the name of a file or directory together with its position in relation to other directories traced back in a line to the root
|
|
211
238
|
*/
|
|
212
|
-
type Path = string
|
|
239
|
+
type Path = string;
|
|
240
|
+
type OptionalPath = Path | null | undefined;
|
|
213
241
|
type FileName = string | null | undefined;
|
|
214
242
|
type LogType = 'error' | 'warn' | 'info';
|
|
215
243
|
type LogLevel = LogType | 'silent';
|
|
@@ -263,6 +291,7 @@ declare const write: (data: string, path: string, options?: WriteOptions) => Pro
|
|
|
263
291
|
declare const format: (text: string) => string;
|
|
264
292
|
|
|
265
293
|
declare const getRelativePath: (root?: string | null, file?: string | null) => string;
|
|
294
|
+
declare const getPathMode: (path: string | undefined | null) => "file" | "directory" | undefined;
|
|
266
295
|
|
|
267
296
|
/**
|
|
268
297
|
* Get the type of the first argument in a function.
|
|
@@ -281,7 +310,7 @@ declare class PluginManager {
|
|
|
281
310
|
constructor(config: KubbConfig, options: {
|
|
282
311
|
logger?: Logger;
|
|
283
312
|
});
|
|
284
|
-
resolveId: (params: ResolveIdParams) => Promise<
|
|
313
|
+
resolveId: (params: ResolveIdParams) => Promise<OptionalPath>;
|
|
285
314
|
load: (id: string) => Promise<TransformResult>;
|
|
286
315
|
hookForPlugin<H extends PluginLifecycleHooks>(pluginName: string, hookName: H, parameters: Parameters<PluginLifecycle[H]>, skipped?: ReadonlySet<KubbPlugin> | null): Promise<ReturnType<PluginLifecycle[H]> | null>;
|
|
287
316
|
hookFirst<H extends PluginLifecycleHooks>(hookName: H, parameters: Parameters<PluginLifecycle[H]>, skipped?: ReadonlySet<KubbPlugin> | null): Promise<ReturnType<PluginLifecycle[H]> | null>;
|
|
@@ -306,4 +335,4 @@ declare class PluginManager {
|
|
|
306
335
|
private runSync;
|
|
307
336
|
}
|
|
308
337
|
|
|
309
|
-
export { Argument0, CLIOptions, Cache, CacheStore, CorePluginOptions, EmittedFile, File, FileManager, FileName,
|
|
338
|
+
export { Argument0, CLIOptions, Cache, CacheStore, CorePluginOptions, EmittedFile, File, FileManager, FileName, KubbConfig, KubbPlugin, KubbUserConfig, LogLevel, LogType, Logger, MaybePromise, OptionalPath, Path, PluginContext, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, ResolveIdParams, Status, Strategy, TransformResult, TreeNode, UUID, ValidationResult, build, createPlugin, createPluginCache, build as default, defineConfig, format, getPathMode, getRelativePath, hooks, isPromise, write };
|
package/dist/index.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var path3 = require('path');
|
|
6
5
|
var fse = require('fs-extra');
|
|
6
|
+
var pathParser = require('path');
|
|
7
7
|
var prettier = require('prettier');
|
|
8
8
|
var EventEmitter = require('events');
|
|
9
9
|
var crypto = require('crypto');
|
|
@@ -29,18 +29,18 @@ var format = (text) => {
|
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
// src/utils/write.ts
|
|
32
|
-
var write = async (data,
|
|
32
|
+
var write = async (data, path, options = { format: false }) => {
|
|
33
33
|
const formattedData = options.format ? format(data) : data;
|
|
34
34
|
try {
|
|
35
|
-
await fse.stat(
|
|
36
|
-
const oldContent = await fse.readFile(
|
|
35
|
+
await fse.stat(path);
|
|
36
|
+
const oldContent = await fse.readFile(path, { encoding: "utf-8" });
|
|
37
37
|
if (oldContent?.toString() === formattedData) {
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
40
|
} catch (_err) {
|
|
41
|
-
return fse.outputFile(
|
|
41
|
+
return fse.outputFile(path, formattedData);
|
|
42
42
|
}
|
|
43
|
-
return fse.outputFile(
|
|
43
|
+
return fse.outputFile(path, formattedData);
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
// src/utils/cache.ts
|
|
@@ -72,67 +72,15 @@ var getRelativePath = (root, file) => {
|
|
|
72
72
|
if (!root || !file) {
|
|
73
73
|
throw new Error("Root and file should be filled in when retrieving the relativePath");
|
|
74
74
|
}
|
|
75
|
-
const newPath =
|
|
75
|
+
const newPath = pathParser.relative(root, file).replace("../", "").replace(".ts", "").trimEnd();
|
|
76
76
|
return `./${newPath}`;
|
|
77
77
|
};
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (Array.isArray(plugin)) {
|
|
84
|
-
throw new Error("Not implemented");
|
|
85
|
-
}
|
|
86
|
-
if (!plugin.transform) {
|
|
87
|
-
plugin.transform = function transform(code) {
|
|
88
|
-
return code;
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
return plugin;
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
var name = "core";
|
|
95
|
-
var isEmittedFile = (result) => {
|
|
96
|
-
return !!result.id;
|
|
78
|
+
var getPathMode = (path) => {
|
|
79
|
+
if (!path) {
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
return pathParser.extname(path) ? "file" : "directory";
|
|
97
83
|
};
|
|
98
|
-
var definePlugin = createPlugin((options) => {
|
|
99
|
-
const { fileManager, resolveId, load } = options;
|
|
100
|
-
const api = {
|
|
101
|
-
get config() {
|
|
102
|
-
return options.config;
|
|
103
|
-
},
|
|
104
|
-
fileManager,
|
|
105
|
-
async addFile(file, options2) {
|
|
106
|
-
if (isEmittedFile(file)) {
|
|
107
|
-
const resolvedId = await resolveId({ importee: file.id, importer: file.importer, options: file.options });
|
|
108
|
-
const filePath = resolvedId || file.importer || file.id;
|
|
109
|
-
return fileManager.add({
|
|
110
|
-
path: filePath,
|
|
111
|
-
fileName: file.name || file.id,
|
|
112
|
-
source: file.source || ""
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
if (options2?.root) ;
|
|
116
|
-
return fileManager.add(file);
|
|
117
|
-
},
|
|
118
|
-
resolveId,
|
|
119
|
-
load,
|
|
120
|
-
cache: createPluginCache(/* @__PURE__ */ Object.create(null))
|
|
121
|
-
};
|
|
122
|
-
return {
|
|
123
|
-
name,
|
|
124
|
-
api,
|
|
125
|
-
async buildEnd() {
|
|
126
|
-
await this.fileManager.generateRootFiles({ exclude: [/schemas/, /json/] });
|
|
127
|
-
},
|
|
128
|
-
resolveId(importee, importer) {
|
|
129
|
-
if (!importer) {
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
return path3.resolve(importer, importee);
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
});
|
|
136
84
|
|
|
137
85
|
// src/managers/fileManager/events.ts
|
|
138
86
|
var keys = {
|
|
@@ -242,8 +190,8 @@ var TreeNode = class {
|
|
|
242
190
|
}
|
|
243
191
|
return this;
|
|
244
192
|
}
|
|
245
|
-
static generate(
|
|
246
|
-
const filteredTree = dirTree(
|
|
193
|
+
static generate(path, options = {}) {
|
|
194
|
+
const filteredTree = dirTree(path, { extensions: options?.extensions, exclude: options.exclude });
|
|
247
195
|
const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type });
|
|
248
196
|
const recurse = (node, item) => {
|
|
249
197
|
const subNode = node.addChild({ name: item.name, path: item.path, type: item.type });
|
|
@@ -261,15 +209,11 @@ var TreeNode = class {
|
|
|
261
209
|
// src/managers/fileManager/FileManager.ts
|
|
262
210
|
var FileManager = class {
|
|
263
211
|
cache = /* @__PURE__ */ new Map();
|
|
264
|
-
root;
|
|
265
|
-
output;
|
|
266
212
|
emitter = new EventEmitter();
|
|
267
213
|
events = getFileManagerEvents(this.emitter);
|
|
268
|
-
constructor(
|
|
269
|
-
this.root = path3.resolve(options.root || ".");
|
|
270
|
-
this.output = options?.output || process.cwd();
|
|
214
|
+
constructor() {
|
|
271
215
|
this.events.onStatusChange(() => {
|
|
272
|
-
if (this.
|
|
216
|
+
if (this.getCountByStatus("removed") === this.cache.size) {
|
|
273
217
|
this.events.emitSuccess();
|
|
274
218
|
}
|
|
275
219
|
});
|
|
@@ -277,7 +221,16 @@ var FileManager = class {
|
|
|
277
221
|
getCache(id) {
|
|
278
222
|
return this.cache.get(id);
|
|
279
223
|
}
|
|
280
|
-
|
|
224
|
+
getCacheByPath(path) {
|
|
225
|
+
let cache;
|
|
226
|
+
this.cache.forEach((item) => {
|
|
227
|
+
if (item.file.path === path) {
|
|
228
|
+
cache = item;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
return cache;
|
|
232
|
+
}
|
|
233
|
+
getCountByStatus(status) {
|
|
281
234
|
let count = 0;
|
|
282
235
|
this.cache.forEach((item) => {
|
|
283
236
|
if (item.status === status) {
|
|
@@ -293,34 +246,81 @@ var FileManager = class {
|
|
|
293
246
|
});
|
|
294
247
|
return files;
|
|
295
248
|
}
|
|
296
|
-
|
|
297
|
-
|
|
249
|
+
add(file) {
|
|
250
|
+
const cacheItem = { id: crypto.randomUUID(), file, status: "new" };
|
|
251
|
+
this.cache.set(cacheItem.id, cacheItem);
|
|
252
|
+
this.events.emitFile(cacheItem.id, file);
|
|
253
|
+
return new Promise((resolve) => {
|
|
254
|
+
const unsubscribe = this.events.onRemove(cacheItem.id, (file2) => {
|
|
255
|
+
resolve(file2);
|
|
256
|
+
unsubscribe();
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
addOrAppend(file) {
|
|
261
|
+
const previousCache = this.getCacheByPath(file.path);
|
|
262
|
+
if (previousCache) {
|
|
263
|
+
this.remove(previousCache.id);
|
|
264
|
+
return this.add({
|
|
265
|
+
...file,
|
|
266
|
+
source: `${previousCache.file.source}
|
|
267
|
+
${file.source}`
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
return this.add(file);
|
|
271
|
+
}
|
|
272
|
+
setStatus(id, status) {
|
|
273
|
+
const cacheItem = this.getCache(id);
|
|
274
|
+
if (!cacheItem) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
cacheItem.status = status;
|
|
278
|
+
this.cache.set(id, cacheItem);
|
|
279
|
+
this.events.emitStatusChange(cacheItem.file);
|
|
280
|
+
this.events.emitStatusChangeById(id, status);
|
|
281
|
+
}
|
|
282
|
+
get(id) {
|
|
283
|
+
const cacheItem = this.getCache(id);
|
|
284
|
+
return cacheItem?.file;
|
|
285
|
+
}
|
|
286
|
+
remove(id) {
|
|
287
|
+
const cacheItem = this.getCache(id);
|
|
288
|
+
if (!cacheItem) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
this.setStatus(id, "removed");
|
|
292
|
+
this.events.emitRemove(id, cacheItem.file);
|
|
298
293
|
}
|
|
299
|
-
|
|
300
|
-
const tree = TreeNode.generate(
|
|
294
|
+
static writeIndexes(root, output, options) {
|
|
295
|
+
const tree = TreeNode.generate(output, { extensions: /\.ts/, ...options });
|
|
301
296
|
const fileReducer = (files2, item) => {
|
|
302
297
|
if (!item.children) {
|
|
303
298
|
return [];
|
|
304
299
|
}
|
|
305
300
|
if (item.children?.length > 1) {
|
|
301
|
+
const path = pathParser.resolve(root, item.data.path, "index.ts");
|
|
306
302
|
const source = item.children.reduce((source2, file) => {
|
|
307
303
|
if (!file) {
|
|
308
304
|
return source2;
|
|
309
305
|
}
|
|
310
306
|
const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`;
|
|
307
|
+
if (importPath.includes("index") && path.includes("index")) {
|
|
308
|
+
return source2;
|
|
309
|
+
}
|
|
311
310
|
return `${source2}export * from "${importPath}";
|
|
312
311
|
`;
|
|
313
312
|
}, "");
|
|
314
313
|
files2.push({
|
|
315
|
-
path
|
|
314
|
+
path,
|
|
316
315
|
fileName: "index.ts",
|
|
317
316
|
source
|
|
318
317
|
});
|
|
319
318
|
} else {
|
|
320
319
|
item.children?.forEach((child) => {
|
|
320
|
+
const path = pathParser.resolve(root, item.data.path, "index.ts");
|
|
321
321
|
const importPath = child.data.type === "directory" ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, "")}`;
|
|
322
322
|
files2.push({
|
|
323
|
-
path
|
|
323
|
+
path,
|
|
324
324
|
fileName: "index.ts",
|
|
325
325
|
source: `export * from "${importPath}";
|
|
326
326
|
`
|
|
@@ -335,40 +335,65 @@ var FileManager = class {
|
|
|
335
335
|
const files = fileReducer([], tree);
|
|
336
336
|
return files.map((file) => write(file.source, file.path));
|
|
337
337
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
});
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
setStatus(id, status) {
|
|
350
|
-
const cacheItem = this.getCache(id);
|
|
351
|
-
if (!cacheItem) {
|
|
352
|
-
return;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
// src/plugin.ts
|
|
341
|
+
function createPlugin(factory) {
|
|
342
|
+
return (options) => {
|
|
343
|
+
const plugin = factory(options);
|
|
344
|
+
if (Array.isArray(plugin)) {
|
|
345
|
+
throw new Error("Not implemented");
|
|
353
346
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
359
|
-
get(id) {
|
|
360
|
-
const cacheItem = this.getCache(id);
|
|
361
|
-
return cacheItem?.file;
|
|
362
|
-
}
|
|
363
|
-
remove(id) {
|
|
364
|
-
const cacheItem = this.getCache(id);
|
|
365
|
-
if (!cacheItem) {
|
|
366
|
-
return;
|
|
347
|
+
if (!plugin.transform) {
|
|
348
|
+
plugin.transform = function transform(code) {
|
|
349
|
+
return code;
|
|
350
|
+
};
|
|
367
351
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
352
|
+
return plugin;
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
var name = "core";
|
|
356
|
+
var isEmittedFile = (result) => {
|
|
357
|
+
return !!result.id;
|
|
371
358
|
};
|
|
359
|
+
var definePlugin = createPlugin((options) => {
|
|
360
|
+
const { fileManager, resolveId, load } = options;
|
|
361
|
+
const api = {
|
|
362
|
+
get config() {
|
|
363
|
+
return options.config;
|
|
364
|
+
},
|
|
365
|
+
fileManager,
|
|
366
|
+
async addFile(file, options2) {
|
|
367
|
+
if (isEmittedFile(file)) {
|
|
368
|
+
const resolvedId = await resolveId({ fileName: file.id, directory: file.importer, options: file.options });
|
|
369
|
+
const path = resolvedId || file.importer || file.id;
|
|
370
|
+
return fileManager.add({
|
|
371
|
+
path,
|
|
372
|
+
fileName: file.name || file.id,
|
|
373
|
+
source: file.source || ""
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
if (options2?.root) ;
|
|
377
|
+
return fileManager.addOrAppend(file);
|
|
378
|
+
},
|
|
379
|
+
resolveId,
|
|
380
|
+
load,
|
|
381
|
+
cache: createPluginCache(/* @__PURE__ */ Object.create(null))
|
|
382
|
+
};
|
|
383
|
+
return {
|
|
384
|
+
name,
|
|
385
|
+
api,
|
|
386
|
+
async buildEnd() {
|
|
387
|
+
await FileManager.writeIndexes(this.config.root, this.config.output.path, { exclude: [/schemas/, /json/] });
|
|
388
|
+
},
|
|
389
|
+
resolveId(fileName, directory) {
|
|
390
|
+
if (!directory) {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
return pathParser.resolve(directory, fileName);
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
});
|
|
372
397
|
|
|
373
398
|
// src/managers/pluginManager/PluginManager.ts
|
|
374
399
|
var hookNames = {
|
|
@@ -390,7 +415,7 @@ var PluginManager = class {
|
|
|
390
415
|
constructor(config, options) {
|
|
391
416
|
this.logger = options.logger;
|
|
392
417
|
this.config = config;
|
|
393
|
-
this.fileManager = new FileManager(
|
|
418
|
+
this.fileManager = new FileManager();
|
|
394
419
|
this.core = definePlugin({
|
|
395
420
|
config,
|
|
396
421
|
fileManager: this.fileManager,
|
|
@@ -401,9 +426,9 @@ var PluginManager = class {
|
|
|
401
426
|
}
|
|
402
427
|
resolveId = (params) => {
|
|
403
428
|
if (params.pluginName) {
|
|
404
|
-
return this.hookForPlugin(params.pluginName, "resolveId", [params.
|
|
429
|
+
return this.hookForPlugin(params.pluginName, "resolveId", [params.fileName, params.directory, params.options]);
|
|
405
430
|
}
|
|
406
|
-
return this.hookFirst("resolveId", [params.
|
|
431
|
+
return this.hookFirst("resolveId", [params.fileName, params.directory, params.options]);
|
|
407
432
|
};
|
|
408
433
|
load = async (id) => {
|
|
409
434
|
return this.hookFirst("load", [id]);
|
|
@@ -533,7 +558,6 @@ async function buildImplementation(options, done) {
|
|
|
533
558
|
}
|
|
534
559
|
const pluginManager = new PluginManager(config, { logger });
|
|
535
560
|
const { plugins, fileManager } = pluginManager;
|
|
536
|
-
fse.readFileSync(path3.resolve(config.root, config.input.path), "utf-8");
|
|
537
561
|
const validations = await pluginManager.hookParallel("validate", [plugins]);
|
|
538
562
|
const validationsWithMessage = validations.filter(Boolean);
|
|
539
563
|
if (validationsWithMessage.some((validation) => typeof validation !== "boolean")) {
|
|
@@ -551,15 +575,15 @@ async function buildImplementation(options, done) {
|
|
|
551
575
|
}, 1e3);
|
|
552
576
|
});
|
|
553
577
|
fileManager.events.onAdd(async (id, file) => {
|
|
554
|
-
const { path
|
|
578
|
+
const { path } = file;
|
|
555
579
|
let { source: code } = file;
|
|
556
|
-
const loadedResult = await pluginManager.hookFirst("load", [
|
|
580
|
+
const loadedResult = await pluginManager.hookFirst("load", [path]);
|
|
557
581
|
if (loadedResult) {
|
|
558
582
|
code = loadedResult;
|
|
559
583
|
}
|
|
560
584
|
if (code) {
|
|
561
|
-
const transformedCode = await pluginManager.hookReduceArg0("transform", [code,
|
|
562
|
-
await pluginManager.hookParallel("writeFile", [transformedCode,
|
|
585
|
+
const transformedCode = await pluginManager.hookReduceArg0("transform", [code, path], transformReducer);
|
|
586
|
+
await pluginManager.hookParallel("writeFile", [transformedCode, path]);
|
|
563
587
|
fileManager.setStatus(id, "success");
|
|
564
588
|
fileManager.remove(id);
|
|
565
589
|
}
|
|
@@ -591,6 +615,7 @@ exports.createPluginCache = createPluginCache;
|
|
|
591
615
|
exports.default = src_default;
|
|
592
616
|
exports.defineConfig = defineConfig;
|
|
593
617
|
exports.format = format;
|
|
618
|
+
exports.getPathMode = getPathMode;
|
|
594
619
|
exports.getRelativePath = getRelativePath;
|
|
595
620
|
exports.hooks = hooks;
|
|
596
621
|
exports.isPromise = isPromise;
|