@baeta/generator-sdk 2.0.0-next.2 → 2.0.0-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,433 +1,382 @@
1
- // lib/config.ts
2
- import { posixPath, resolve } from "@baeta/util-path";
1
+ import path, { dirname, extname, posixPath, resolve } from "@baeta/util-path";
2
+ import fs, { mkdir, open, writeFile } from "node:fs/promises";
3
+ import { pascalCase } from "change-case-all";
4
+ import { PluginType } from "@baeta/plugin";
5
+ import { subscribe } from "@parcel/watcher";
6
+ import micromatch, { default as micromatch$1 } from "micromatch";
7
+
8
+ //#region lib/config.ts
3
9
  function loadOptions(options) {
4
- const cwd = posixPath(options.cwd ?? process.cwd());
5
- const schemas = options.schemas ?? ["src/**/*.graphql"];
6
- const modulesDir = posixPath(resolve(cwd, options.modulesDir || "src/modules"));
7
- const moduleDefinitionName = options.moduleDefinitionName || "typedef.ts";
8
- const defaultTypesDir = resolve(modulesDir, "../__generated__/");
9
- const typesDir = resolve(cwd, options.typesDir || defaultTypesDir);
10
- return {
11
- cwd,
12
- schemas,
13
- modulesDir,
14
- moduleDefinitionName,
15
- typesDir,
16
- fileOptions: options.fileOptions,
17
- loaders: options.loaders,
18
- importExtension: options.importExtension === false ? "" : options.importExtension ?? ".ts"
19
- };
10
+ const cwd = posixPath(options.cwd ?? process.cwd());
11
+ const schemas = options.schemas ?? ["src/**/*.graphql"];
12
+ const modulesDir = posixPath(resolve(cwd, options.modulesDir || "src/modules"));
13
+ const moduleDefinitionName = options.moduleDefinitionName || "typedef.ts";
14
+ const defaultTypesDir = resolve(modulesDir, "../__generated__/");
15
+ return {
16
+ cwd,
17
+ schemas,
18
+ modulesDir,
19
+ moduleDefinitionName,
20
+ typesDir: resolve(cwd, options.typesDir || defaultTypesDir),
21
+ fileOptions: options.fileOptions,
22
+ loaders: options.loaders,
23
+ importExtension: options.importExtension === false ? "" : options.importExtension ?? ".ts"
24
+ };
20
25
  }
21
26
 
22
- // lib/file.ts
23
- import fs from "fs/promises";
24
- import { dirname, extname } from "@baeta/util-path";
27
+ //#endregion
28
+ //#region lib/file.ts
25
29
  var File = class {
26
- persisted = false;
27
- filename;
28
- content;
29
- tag;
30
- options;
31
- constructor(filename, content, tag, options) {
32
- this.filename = filename;
33
- this.content = content;
34
- this.tag = tag;
35
- this.options = options;
36
- }
37
- write = async () => {
38
- if (this.persisted) {
39
- return;
40
- }
41
- this.persisted = true;
42
- if (this.options?.allowOverwrite === false) {
43
- const exists = await fs.stat(this.filename).then((res) => res.isFile()).catch(() => false);
44
- if (exists) return;
45
- }
46
- const dir = dirname(this.filename);
47
- await fs.mkdir(dir, { recursive: true });
48
- const content = await this.buildContent();
49
- return fs.writeFile(this.filename, content, "utf-8");
50
- };
51
- unlink = async () => {
52
- this.persisted = false;
53
- return fs.unlink(this.filename);
54
- };
55
- async buildContent() {
56
- const content = this.buildHeader() + this.content;
57
- if (this.options?.transformContent) {
58
- return this.options.transformContent(this.filename, content, this.tag);
59
- }
60
- return content;
61
- }
62
- buildHeader() {
63
- const headerItems = [];
64
- if (this.options?.disableGenerationNoticeHeader !== true) {
65
- const comment = this.createComment(
66
- "This file was generated by Baeta. Do not edit it directly. All changes will be overwritten by the generator."
67
- );
68
- headerItems.push(comment);
69
- }
70
- if (this.options?.disableEslintHeader !== true) {
71
- const comment = this.createComment("eslint-disable");
72
- headerItems.push(comment);
73
- }
74
- if (this.options?.disableBiomeHeader !== true) {
75
- const comment = this.createComment("@biome-ignore-all: generated file");
76
- headerItems.push(comment);
77
- }
78
- if (this.options?.addHeader) {
79
- const customHeader = this.options.addHeader(this.filename, this.content, this.tag);
80
- headerItems.push(customHeader);
81
- }
82
- if (headerItems.length === 0) {
83
- return "";
84
- }
85
- return `${headerItems.join("\n")}
86
-
87
- `;
88
- }
89
- createComment(comment) {
90
- const extension = extname(this.filename);
91
- if ([".gql", ".graphql"].includes(extension)) {
92
- return `# ${comment}`;
93
- }
94
- return `/* ${comment} */`;
95
- }
30
+ persisted = false;
31
+ filename;
32
+ content;
33
+ tag;
34
+ options;
35
+ constructor(filename, content, tag, options) {
36
+ this.filename = filename;
37
+ this.content = content;
38
+ this.tag = tag;
39
+ this.options = options;
40
+ }
41
+ write = async () => {
42
+ if (this.persisted) return;
43
+ this.persisted = true;
44
+ if (this.options?.allowOverwrite === false) {
45
+ if (await fs.stat(this.filename).then((res) => res.isFile()).catch(() => false)) return;
46
+ }
47
+ const dir = dirname(this.filename);
48
+ await fs.mkdir(dir, { recursive: true });
49
+ const content = await this.buildContent();
50
+ return fs.writeFile(this.filename, content, "utf-8");
51
+ };
52
+ unlink = async () => {
53
+ this.persisted = false;
54
+ return fs.unlink(this.filename);
55
+ };
56
+ async buildContent() {
57
+ const content = this.buildHeader() + this.content;
58
+ if (this.options?.transformContent) return this.options.transformContent(this.filename, content, this.tag);
59
+ return content;
60
+ }
61
+ buildHeader() {
62
+ const headerItems = [];
63
+ if (this.options?.disableGenerationNoticeHeader !== true) {
64
+ const comment = this.createComment("This file was generated by Baeta. Do not edit it directly. All changes will be overwritten by the generator.");
65
+ headerItems.push(comment);
66
+ }
67
+ if (this.options?.disableEslintHeader !== true) {
68
+ const comment = this.createComment("eslint-disable");
69
+ headerItems.push(comment);
70
+ }
71
+ if (this.options?.disableBiomeV1Header !== true) {
72
+ const comment = this.createComment("@biome-ignore-all: generated file");
73
+ headerItems.push(comment);
74
+ }
75
+ if (this.options?.disableBiomeV2Header !== true) {
76
+ const comment = this.createComment("biome-ignore-all lint: generated file");
77
+ headerItems.push(comment);
78
+ }
79
+ if (this.options?.addHeader) {
80
+ const customHeader = this.options.addHeader(this.filename, this.content, this.tag);
81
+ headerItems.push(customHeader);
82
+ }
83
+ if (headerItems.length === 0) return "";
84
+ return `${headerItems.join("\n")}\n\n`;
85
+ }
86
+ createComment(comment) {
87
+ const extension = extname(this.filename);
88
+ if ([".gql", ".graphql"].includes(extension)) return `# ${comment}`;
89
+ return `/* ${comment} */`;
90
+ }
96
91
  };
97
92
 
98
- // lib/file-block.ts
99
- import { mkdir, open, writeFile } from "fs/promises";
100
- import { dirname as dirname2 } from "@baeta/util-path";
93
+ //#endregion
94
+ //#region lib/file-block.ts
101
95
  var FileBlock = class extends File {
102
- filename;
103
- content;
104
- start;
105
- end;
106
- tag;
107
- constructor(filename, content, start, end, tag, options) {
108
- super(filename, content, tag, {
109
- disableBiomeHeader: options?.disableBiomeHeader ?? true,
110
- disableEslintHeader: options?.disableEslintHeader ?? true,
111
- disableGenerationNoticeHeader: options?.disableGenerationNoticeHeader ?? true
112
- });
113
- this.filename = filename;
114
- this.content = content;
115
- this.start = start;
116
- this.end = end;
117
- this.tag = tag;
118
- }
119
- write = async () => {
120
- if (this.persisted) {
121
- return;
122
- }
123
- this.persisted = true;
124
- const dir = dirname2(this.filename);
125
- await mkdir(dir, { recursive: true });
126
- const [existingContent, fd] = await this.getExistingContent();
127
- this.content = this.addBlockToContent(existingContent);
128
- const content = await this.buildContent();
129
- if (fd) {
130
- await fd.truncate(0);
131
- await fd.write(content, 0, "utf-8");
132
- await fd.close();
133
- } else {
134
- await writeFile(this.filename, content, "utf-8");
135
- }
136
- };
137
- unlink = async () => {
138
- this.persisted = false;
139
- const [existingContent, fd] = await this.getExistingContent();
140
- if (fd) {
141
- const [start, end] = this.getSlices(existingContent);
142
- await fd.truncate(0);
143
- await fd.write(start + end, 0, "utf-8");
144
- await fd.close();
145
- }
146
- };
147
- async getExistingContent() {
148
- try {
149
- const fd = await open(this.filename, "r+");
150
- const existingContent = await fd.readFile("utf-8");
151
- return [existingContent, fd];
152
- } catch {
153
- return ["", null];
154
- }
155
- }
156
- getSlices(existingContent) {
157
- const startMarkerIndex = existingContent.indexOf(this.start);
158
- const endMarkerIndex = existingContent.lastIndexOf(this.end);
159
- if (startMarkerIndex === -1 || endMarkerIndex === -1) {
160
- return [existingContent, "", false];
161
- }
162
- return [
163
- existingContent.slice(0, startMarkerIndex),
164
- existingContent.slice(endMarkerIndex + this.end.length),
165
- true
166
- ];
167
- }
168
- addBlockToContent(existingContent) {
169
- const block = `${this.start}
170
- ${this.content}
171
- ${this.end}`;
172
- const [startSlice, endSlice, hasMarkers] = this.getSlices(existingContent);
173
- const padding = hasMarkers ? "" : this.buildPadding(existingContent);
174
- return startSlice + padding + block + endSlice;
175
- }
176
- buildPadding(existingContent) {
177
- if (existingContent === "") {
178
- return "";
179
- }
180
- if (existingContent.endsWith("\n\n")) {
181
- return "";
182
- }
183
- if (existingContent.endsWith("\n")) {
184
- return "\n";
185
- }
186
- return "\n\n";
187
- }
96
+ filename;
97
+ content;
98
+ start;
99
+ end;
100
+ tag;
101
+ constructor(filename, content, start, end, tag, options) {
102
+ super(filename, content, tag, {
103
+ disableBiomeV1Header: options?.disableBiomeV1Header ?? true,
104
+ disableBiomeV2Header: options?.disableBiomeV2Header ?? true,
105
+ disableEslintHeader: options?.disableEslintHeader ?? true,
106
+ disableGenerationNoticeHeader: options?.disableGenerationNoticeHeader ?? true
107
+ });
108
+ this.filename = filename;
109
+ this.content = content;
110
+ this.start = start;
111
+ this.end = end;
112
+ this.tag = tag;
113
+ }
114
+ write = async () => {
115
+ if (this.persisted) return;
116
+ this.persisted = true;
117
+ await mkdir(dirname(this.filename), { recursive: true });
118
+ const [existingContent, fd] = await this.getExistingContent();
119
+ this.content = this.addBlockToContent(existingContent);
120
+ const content = await this.buildContent();
121
+ if (fd) {
122
+ await fd.truncate(0);
123
+ await fd.write(content, 0, "utf-8");
124
+ await fd.close();
125
+ } else await writeFile(this.filename, content, "utf-8");
126
+ };
127
+ unlink = async () => {
128
+ this.persisted = false;
129
+ const [existingContent, fd] = await this.getExistingContent();
130
+ if (fd) {
131
+ const [start, end] = this.getSlices(existingContent);
132
+ await fd.truncate(0);
133
+ await fd.write(start + end, 0, "utf-8");
134
+ await fd.close();
135
+ }
136
+ };
137
+ async getExistingContent() {
138
+ try {
139
+ const fd = await open(this.filename, "r+");
140
+ return [await fd.readFile("utf-8"), fd];
141
+ } catch {
142
+ return ["", null];
143
+ }
144
+ }
145
+ getSlices(existingContent) {
146
+ const startMarkerIndex = existingContent.indexOf(this.start);
147
+ const endMarkerIndex = existingContent.lastIndexOf(this.end);
148
+ if (startMarkerIndex === -1 || endMarkerIndex === -1) return [
149
+ existingContent,
150
+ "",
151
+ false
152
+ ];
153
+ return [
154
+ existingContent.slice(0, startMarkerIndex),
155
+ existingContent.slice(endMarkerIndex + this.end.length),
156
+ true
157
+ ];
158
+ }
159
+ addBlockToContent(existingContent) {
160
+ const block = `${this.start}\n${this.content}\n${this.end}`;
161
+ const [startSlice, endSlice, hasMarkers] = this.getSlices(existingContent);
162
+ return startSlice + (hasMarkers ? "" : this.buildPadding(existingContent)) + block + endSlice;
163
+ }
164
+ buildPadding(existingContent) {
165
+ if (existingContent === "") return "";
166
+ if (existingContent.endsWith("\n\n")) return "";
167
+ if (existingContent.endsWith("\n")) return "\n";
168
+ return "\n\n";
169
+ }
188
170
  };
189
171
 
190
- // lib/file-manager.ts
172
+ //#endregion
173
+ //#region lib/file-manager.ts
191
174
  var FileManager = class {
192
- files = [];
193
- fileOptions;
194
- constructor(fileOptions) {
195
- this.fileOptions = fileOptions;
196
- }
197
- createAndAdd(filename, content, tag, options) {
198
- const file = new File(filename, content, tag, { ...this.fileOptions, ...options });
199
- this.add(file);
200
- return file;
201
- }
202
- add(...file) {
203
- this.files.push(...file);
204
- }
205
- get(filename) {
206
- return this.files.find((file) => file.filename === filename);
207
- }
208
- getAll() {
209
- return this.files;
210
- }
211
- getByTag(tag) {
212
- return this.files.filter((file) => file.tag === tag);
213
- }
214
- remove(filename) {
215
- const index = this.files.findIndex((file) => file.filename === filename);
216
- this.files.splice(index, 1);
217
- }
218
- removeAll() {
219
- this.files = [];
220
- }
221
- removeByTag(tag) {
222
- this.files = this.files.filter((file) => file.tag !== tag);
223
- }
224
- writeAll() {
225
- const toWrite = this.files.filter((file) => !file.persisted);
226
- return Promise.all(toWrite.map((file) => file.write()));
227
- }
228
- writeByTag(tag) {
229
- const files = this.getByTag(tag);
230
- const toWrite = files.filter((file) => !file.persisted);
231
- return Promise.all(toWrite.map((file) => file.write()));
232
- }
233
- unlinkAll() {
234
- return Promise.all(this.files.map((file) => file.unlink())).then(() => {
235
- });
236
- }
237
- getPersistedFiles() {
238
- return this.files.filter((file) => file.persisted);
239
- }
175
+ files = [];
176
+ fileOptions;
177
+ constructor(fileOptions) {
178
+ this.fileOptions = fileOptions;
179
+ }
180
+ createAndAdd(filename, content, tag, options) {
181
+ const file = new File(filename, content, tag, {
182
+ ...this.fileOptions,
183
+ ...options
184
+ });
185
+ this.add(file);
186
+ return file;
187
+ }
188
+ add(...file) {
189
+ this.files.push(...file);
190
+ }
191
+ get(filename) {
192
+ return this.files.find((file) => file.filename === filename);
193
+ }
194
+ getAll() {
195
+ return this.files;
196
+ }
197
+ getByTag(tag) {
198
+ return this.files.filter((file) => file.tag === tag);
199
+ }
200
+ remove(filename) {
201
+ const index = this.files.findIndex((file) => file.filename === filename);
202
+ this.files.splice(index, 1);
203
+ }
204
+ removeAll() {
205
+ this.files = [];
206
+ }
207
+ removeByTag(tag) {
208
+ this.files = this.files.filter((file) => file.tag !== tag);
209
+ }
210
+ writeAll() {
211
+ const toWrite = this.files.filter((file) => !file.persisted);
212
+ return Promise.all(toWrite.map((file) => file.write()));
213
+ }
214
+ writeByTag(tag) {
215
+ const toWrite = this.getByTag(tag).filter((file) => !file.persisted);
216
+ return Promise.all(toWrite.map((file) => file.write()));
217
+ }
218
+ unlinkAll() {
219
+ return Promise.all(this.files.map((file) => file.unlink())).then(() => {});
220
+ }
221
+ getPersistedFiles() {
222
+ return this.files.filter((file) => file.persisted);
223
+ }
240
224
  };
241
225
 
242
- // lib/module.ts
243
- import { pascalCase } from "change-case-all";
226
+ //#endregion
227
+ //#region lib/module.ts
244
228
  function getModuleExportName(name) {
245
- return `${pascalCase(name)}Module`;
229
+ return `${pascalCase(name)}Module`;
246
230
  }
247
231
 
248
- // lib/plugin.ts
249
- import { PluginType } from "@baeta/plugin";
250
- var GeneratorPluginVersion = {
251
- V1: "v1"
252
- };
253
- var defaultPluginFn = async (_ctx, next) => {
254
- return next();
232
+ //#endregion
233
+ //#region lib/plugin.ts
234
+ const GeneratorPluginVersion = { V1: "v1" };
235
+ const defaultPluginFn = async (_ctx, next) => {
236
+ return next();
255
237
  };
256
- var defaultWatchFn = () => ({ include: [], ignore: [] });
238
+ const defaultWatchFn = () => ({
239
+ include: [],
240
+ ignore: []
241
+ });
257
242
  function createPluginV1(options) {
258
- return {
259
- name: options.name,
260
- actionName: options.actionName,
261
- version: GeneratorPluginVersion.V1,
262
- type: PluginType.Generator,
263
- end: options.end ?? defaultPluginFn,
264
- generate: options.generate ?? defaultPluginFn,
265
- setup: options.setup ?? defaultPluginFn,
266
- watch: options.watch ?? defaultWatchFn
267
- };
243
+ return {
244
+ name: options.name,
245
+ actionName: options.actionName,
246
+ version: GeneratorPluginVersion.V1,
247
+ type: PluginType.Generator,
248
+ end: options.end ?? defaultPluginFn,
249
+ generate: options.generate ?? defaultPluginFn,
250
+ setup: options.setup ?? defaultPluginFn,
251
+ watch: options.watch ?? defaultWatchFn
252
+ };
268
253
  }
269
254
  function isGeneratorPlugin(plugin) {
270
- return plugin.type === PluginType.Generator;
255
+ return plugin.type === PluginType.Generator;
271
256
  }
272
257
  function getGeneratorPlugins(plugins) {
273
- if (!plugins) {
274
- return [];
275
- }
276
- return plugins.filter(isGeneratorPlugin);
258
+ if (!plugins) return [];
259
+ return plugins.filter(isGeneratorPlugin);
277
260
  }
278
261
 
279
- // lib/watcher.ts
280
- import path2, { posixPath as posixPath2 } from "@baeta/util-path";
281
- import {
282
- subscribe
283
- } from "@parcel/watcher";
284
- import micromatch2 from "micromatch";
285
-
286
- // lib/watcher-ignore.ts
287
- import path from "@baeta/util-path";
288
- import micromatch from "micromatch";
262
+ //#endregion
263
+ //#region lib/watcher-ignore.ts
289
264
  var WatcherIgnore = class {
290
- cwd;
291
- files = [];
292
- regexps = [];
293
- functions = [];
294
- globs = [];
295
- globsMap = /* @__PURE__ */ new Map();
296
- constructor(cwd) {
297
- this.cwd = cwd;
298
- }
299
- ignore(pattern) {
300
- if (pattern instanceof RegExp) {
301
- this.regexps.push(pattern);
302
- return;
303
- }
304
- if (typeof pattern === "function") {
305
- this.functions.push(pattern);
306
- return;
307
- }
308
- if (!this.isMicromatch(pattern)) {
309
- this.files.push(this.resolveFile(pattern));
310
- return;
311
- }
312
- this.globsMap.set(pattern, micromatch.matcher(pattern));
313
- this.globs = Array.from(this.globsMap.values());
314
- }
315
- isMicromatch(pattern) {
316
- const result = micromatch.scan(pattern);
317
- return result.isBrace || result.isGlobstar || result.isExtglob || result.isGlob;
318
- }
319
- resolveFile(file) {
320
- return path.isAbsolute(file) ? file : path.join(this.cwd, file);
321
- }
322
- unignore(pattern) {
323
- if (pattern instanceof RegExp) {
324
- this.regexps = this.regexps.filter((p) => p !== pattern);
325
- return;
326
- }
327
- if (typeof pattern === "function") {
328
- this.functions = this.functions.filter((p) => p !== pattern);
329
- return;
330
- }
331
- if (!this.isMicromatch(pattern)) {
332
- const file = this.resolveFile(pattern);
333
- this.files = this.files.filter((p) => p !== file);
334
- return;
335
- }
336
- this.globsMap.delete(pattern);
337
- this.globs = Array.from(this.globsMap.values());
338
- }
339
- isIgnored(path3) {
340
- if (this.files.includes(path3)) {
341
- return true;
342
- }
343
- if (this.globs.some((f) => f(path3))) {
344
- return true;
345
- }
346
- if (this.regexps.some((r) => r.test(path3))) {
347
- return true;
348
- }
349
- if (this.functions.some((f) => f(path3))) {
350
- return true;
351
- }
352
- return false;
353
- }
265
+ cwd;
266
+ files = [];
267
+ regexps = [];
268
+ functions = [];
269
+ globs = [];
270
+ globsMap = /* @__PURE__ */ new Map();
271
+ constructor(cwd) {
272
+ this.cwd = cwd;
273
+ }
274
+ ignore(pattern) {
275
+ if (pattern instanceof RegExp) {
276
+ this.regexps.push(pattern);
277
+ return;
278
+ }
279
+ if (typeof pattern === "function") {
280
+ this.functions.push(pattern);
281
+ return;
282
+ }
283
+ if (!this.isMicromatch(pattern)) {
284
+ this.files.push(this.resolveFile(pattern));
285
+ return;
286
+ }
287
+ this.globsMap.set(pattern, micromatch$1.matcher(pattern));
288
+ this.globs = Array.from(this.globsMap.values());
289
+ }
290
+ isMicromatch(pattern) {
291
+ const result = micromatch$1.scan(pattern);
292
+ return result.isBrace || result.isGlobstar || result.isExtglob || result.isGlob;
293
+ }
294
+ resolveFile(file) {
295
+ return path.isAbsolute(file) ? file : path.join(this.cwd, file);
296
+ }
297
+ unignore(pattern) {
298
+ if (pattern instanceof RegExp) {
299
+ this.regexps = this.regexps.filter((p) => p !== pattern);
300
+ return;
301
+ }
302
+ if (typeof pattern === "function") {
303
+ this.functions = this.functions.filter((p) => p !== pattern);
304
+ return;
305
+ }
306
+ if (!this.isMicromatch(pattern)) {
307
+ const file = this.resolveFile(pattern);
308
+ this.files = this.files.filter((p) => p !== file);
309
+ return;
310
+ }
311
+ this.globsMap.delete(pattern);
312
+ this.globs = Array.from(this.globsMap.values());
313
+ }
314
+ isIgnored(path$1) {
315
+ if (this.files.includes(path$1)) return true;
316
+ if (this.globs.some((f) => f(path$1))) return true;
317
+ if (this.regexps.some((r) => r.test(path$1))) return true;
318
+ if (this.functions.some((f) => f(path$1))) return true;
319
+ return false;
320
+ }
354
321
  };
355
322
 
356
- // lib/watcher.ts
357
- var isMatch = micromatch2.isMatch;
323
+ //#endregion
324
+ //#region lib/watcher.ts
325
+ const isMatch = micromatch.isMatch;
358
326
  var Watcher = class {
359
- cwd;
360
- options;
361
- subscription;
362
- listeners = {
363
- create: [],
364
- update: [],
365
- delete: []
366
- };
367
- watcherIgnore;
368
- constructor(cwd, options) {
369
- this.cwd = cwd;
370
- this.options = options;
371
- this.watcherIgnore = new WatcherIgnore(cwd);
372
- this.subscription = this.createSubscription();
373
- }
374
- onEvents = (err, events) => {
375
- if (err) {
376
- console.error(err);
377
- return;
378
- }
379
- const filteredEvents = events.filter((event) => {
380
- return !this.watcherIgnore.isIgnored(posixPath2(event.path));
381
- });
382
- for (const event of filteredEvents) {
383
- for (const listener of this.listeners[event.type]) {
384
- listener({
385
- type: event.type,
386
- path: posixPath2(event.path),
387
- relativePath: posixPath2(path2.relative(this.cwd, event.path))
388
- });
389
- }
390
- }
391
- };
392
- on(event, listener) {
393
- this.listeners[event].push(listener);
394
- }
395
- off(event, listener) {
396
- this.listeners[event] = this.listeners[event].filter((l) => l !== listener);
397
- }
398
- ignore(pattern) {
399
- this.watcherIgnore.ignore(pattern);
400
- }
401
- unignore(pattern) {
402
- this.watcherIgnore.unignore(pattern);
403
- }
404
- createSubscription() {
405
- const promise = subscribe(this.cwd, this.onEvents, this.options);
406
- const unsubscribe = async () => {
407
- const subscription = await promise;
408
- await subscription.unsubscribe();
409
- };
410
- return {
411
- unsubscribe
412
- };
413
- }
414
- close() {
415
- return this.subscription.unsubscribe();
416
- }
417
- };
418
- export {
419
- File,
420
- FileBlock,
421
- FileManager,
422
- GeneratorPluginVersion,
423
- Watcher,
424
- WatcherIgnore,
425
- createPluginV1,
426
- getGeneratorPlugins,
427
- getModuleExportName,
428
- isGeneratorPlugin,
429
- isMatch,
430
- loadOptions,
431
- micromatch2 as micromatch
327
+ cwd;
328
+ options;
329
+ subscription;
330
+ listeners = {
331
+ create: [],
332
+ update: [],
333
+ delete: []
334
+ };
335
+ watcherIgnore;
336
+ constructor(cwd, options) {
337
+ this.cwd = cwd;
338
+ this.options = options;
339
+ this.watcherIgnore = new WatcherIgnore(cwd);
340
+ this.subscription = this.createSubscription();
341
+ }
342
+ onEvents = (err, events) => {
343
+ if (err) {
344
+ console.error(err);
345
+ return;
346
+ }
347
+ const filteredEvents = events.filter((event) => {
348
+ return !this.watcherIgnore.isIgnored(posixPath(event.path));
349
+ });
350
+ for (const event of filteredEvents) for (const listener of this.listeners[event.type]) listener({
351
+ type: event.type,
352
+ path: posixPath(event.path),
353
+ relativePath: posixPath(path.relative(this.cwd, event.path))
354
+ });
355
+ };
356
+ on(event, listener) {
357
+ this.listeners[event].push(listener);
358
+ }
359
+ off(event, listener) {
360
+ this.listeners[event] = this.listeners[event].filter((l) => l !== listener);
361
+ }
362
+ ignore(pattern) {
363
+ this.watcherIgnore.ignore(pattern);
364
+ }
365
+ unignore(pattern) {
366
+ this.watcherIgnore.unignore(pattern);
367
+ }
368
+ createSubscription() {
369
+ const promise = subscribe(this.cwd, this.onEvents, this.options);
370
+ const unsubscribe = async () => {
371
+ await (await promise).unsubscribe();
372
+ };
373
+ return { unsubscribe };
374
+ }
375
+ close() {
376
+ return this.subscription.unsubscribe();
377
+ }
432
378
  };
379
+
380
+ //#endregion
381
+ export { File, FileBlock, FileManager, GeneratorPluginVersion, Watcher, WatcherIgnore, createPluginV1, getGeneratorPlugins, getModuleExportName, isGeneratorPlugin, isMatch, loadOptions, micromatch };
433
382
  //# sourceMappingURL=index.js.map