@timeax/scaffold 0.0.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.
@@ -0,0 +1,439 @@
1
+ /**
2
+ * Common options that can be applied to both files and directories
3
+ * in the scaffold structure.
4
+ *
5
+ * These options are *declarative* — they do not enforce behavior by
6
+ * themselves; they are consumed by the core engine.
7
+ */
8
+ interface BaseEntryOptions {
9
+ /**
10
+ * Glob patterns relative to the group root or project root
11
+ * (depending on how the engine is called).
12
+ *
13
+ * If provided, at least one pattern must match the entry path
14
+ * for the entry to be considered.
15
+ */
16
+ include?: string[];
17
+ /**
18
+ * Glob patterns relative to the group root or project root.
19
+ *
20
+ * If any pattern matches the entry path, the entry will be ignored.
21
+ */
22
+ exclude?: string[];
23
+ /**
24
+ * Name of the stub to use when creating this file or directory’s
25
+ * content. For directories, this can act as an “inherited” stub
26
+ * for child files if the engine chooses to support that behavior.
27
+ */
28
+ stub?: string;
29
+ }
30
+ /**
31
+ * A single file entry in the structure tree.
32
+ *
33
+ * Paths are always stored as POSIX-style forward-slash paths
34
+ * relative to the group root / project root.
35
+ */
36
+ interface FileEntry extends BaseEntryOptions {
37
+ type: 'file';
38
+ /**
39
+ * File path (e.g. "src/index.ts", "Models/User.php").
40
+ * Paths should never end with a trailing slash.
41
+ */
42
+ path: string;
43
+ }
44
+ /**
45
+ * A directory entry in the structure tree.
46
+ *
47
+ * Paths should *logically* represent directories and may end
48
+ * with a trailing slash for readability (the engine can normalize).
49
+ */
50
+ interface DirEntry extends BaseEntryOptions {
51
+ type: 'dir';
52
+ /**
53
+ * Directory path (e.g. "src/", "src/schema/", "Models/").
54
+ * It is recommended (but not strictly required) that directory
55
+ * paths end with a trailing slash.
56
+ */
57
+ path: string;
58
+ /**
59
+ * Nested structure entries for files and subdirectories.
60
+ */
61
+ children?: StructureEntry[];
62
+ }
63
+ /**
64
+ * A single node in the structure tree:
65
+ * either a file or a directory.
66
+ */
67
+ type StructureEntry = FileEntry | DirEntry;
68
+
69
+ /**
70
+ * Lifecycle stages for non-stub (regular) hooks.
71
+ *
72
+ * These hooks are called around file operations (create/delete).
73
+ */
74
+ type RegularHookKind = 'preCreateFile' | 'postCreateFile' | 'preDeleteFile' | 'postDeleteFile';
75
+ /**
76
+ * Lifecycle stages for stub-related hooks.
77
+ *
78
+ * These hooks are called around stub resolution for file content.
79
+ */
80
+ type StubHookKind = 'preStub' | 'postStub';
81
+ /**
82
+ * Context object passed to all hooks (both regular and stub).
83
+ */
84
+ interface HookContext {
85
+ /**
86
+ * Absolute path to the group root / project root that this
87
+ * scaffold run is targeting.
88
+ */
89
+ projectRoot: string;
90
+ /**
91
+ * Path of the file or directory relative to the project root
92
+ * used for this run (or group root, if grouped).
93
+ *
94
+ * Example: "src/index.ts", "Http/Controllers/Controller.php".
95
+ */
96
+ targetPath: string;
97
+ /**
98
+ * Absolute path to the file or directory on disk.
99
+ */
100
+ absolutePath: string;
101
+ /**
102
+ * Whether the target is a directory.
103
+ * (For now, most hooks will be for files, but this is future-proofing.)
104
+ */
105
+ isDirectory: boolean;
106
+ /**
107
+ * The stub name associated with the file (if any).
108
+ *
109
+ * For regular hooks, this can be used to detect which stub
110
+ * produced a given file.
111
+ */
112
+ stubName?: string;
113
+ }
114
+ /**
115
+ * Common filter options used by both regular and stub hooks.
116
+ *
117
+ * Filters are evaluated against the `targetPath`.
118
+ */
119
+ interface HookFilter {
120
+ /**
121
+ * Glob patterns which must match for the hook to run.
122
+ * If provided, at least one pattern must match.
123
+ */
124
+ include?: string[];
125
+ /**
126
+ * Glob patterns which, if any match, will prevent the hook
127
+ * from running.
128
+ */
129
+ exclude?: string[];
130
+ /**
131
+ * Additional patterns or explicit file paths, treated similarly
132
+ * to `include` — mainly a convenience alias.
133
+ */
134
+ files?: string[];
135
+ }
136
+ /**
137
+ * Function signature for regular hooks.
138
+ */
139
+ type RegularHookFn = (ctx: HookContext) => void | Promise<void>;
140
+ /**
141
+ * Function signature for stub hooks.
142
+ */
143
+ type StubHookFn = (ctx: HookContext) => void | Promise<void>;
144
+ /**
145
+ * Configuration for a regular hook instance.
146
+ *
147
+ * Each hook category (e.g. `preCreateFile`) can have an array
148
+ * of these, each with its own filter.
149
+ */
150
+ interface RegularHookConfig extends HookFilter {
151
+ fn: RegularHookFn;
152
+ }
153
+ /**
154
+ * Configuration for a stub hook instance.
155
+ *
156
+ * Each stub can have its own `preStub` / `postStub` hook arrays,
157
+ * each with independent filters.
158
+ */
159
+ interface StubHookConfig extends HookFilter {
160
+ fn: StubHookFn;
161
+ }
162
+ /**
163
+ * Stub configuration, defining how file content is generated
164
+ * and which stub-specific hooks apply.
165
+ */
166
+ interface StubConfig {
167
+ /**
168
+ * Unique name of this stub within the config.
169
+ * This is referenced from structure entries via `stub: name`.
170
+ */
171
+ name: string;
172
+ /**
173
+ * Content generator for files that use this stub.
174
+ *
175
+ * If omitted, the scaffold engine may default to an empty file.
176
+ */
177
+ getContent?: (ctx: HookContext) => string | Promise<string>;
178
+ /**
179
+ * Stub-specific hooks called for files that reference this stub.
180
+ */
181
+ hooks?: {
182
+ preStub?: StubHookConfig[];
183
+ postStub?: StubHookConfig[];
184
+ };
185
+ }
186
+
187
+ /**
188
+ * Configuration for a single structure group.
189
+ *
190
+ * Groups allow you to clearly separate different roots in a project,
191
+ * such as "app", "routes", "resources/js", etc., each with its own
192
+ * structure definition.
193
+ */
194
+ interface StructureGroupConfig {
195
+ /**
196
+ * Human-readable identifier for the group (e.g. "app", "routes", "frontend").
197
+ * Used mainly for logging and, optionally, cache metadata.
198
+ */
199
+ name: string;
200
+ /**
201
+ * Root directory for this group, relative to the overall project root.
202
+ *
203
+ * Example: "app", "routes", "resources/js".
204
+ *
205
+ * All paths produced from this group's structure are resolved
206
+ * relative to this directory.
207
+ */
208
+ root: string;
209
+ /**
210
+ * Optional inline structure entries for this group.
211
+ * If present and non-empty, these take precedence over `structureFile`.
212
+ */
213
+ structure?: StructureEntry[];
214
+ /**
215
+ * Name of the structure file inside the scaffold directory for this group.
216
+ *
217
+ * Example: "app.txt", "routes.txt".
218
+ *
219
+ * If omitted, the default is `<name>.txt` within the scaffold directory.
220
+ */
221
+ structureFile?: string;
222
+ }
223
+ /**
224
+ * Root configuration object for @timeax/scaffold.
225
+ *
226
+ * This is what you export from `scaffold/config.ts` in a consuming
227
+ * project, or from any programmatic usage of the library.
228
+ */
229
+ interface ScaffoldConfig {
230
+ /**
231
+ * Absolute or relative project root (where files are created).
232
+ *
233
+ * If omitted, the engine will treat `process.cwd()` as the root.
234
+ */
235
+ root?: string;
236
+ /**
237
+ * Base directory where structures are applied and files/folders
238
+ * are actually created.
239
+ *
240
+ * This is resolved relative to `root` (not CWD).
241
+ *
242
+ * Default: same as `root`.
243
+ *
244
+ * Examples:
245
+ * - base: '.' with root: '.' → apply to <cwd>
246
+ * - base: 'src' with root: '.' → apply to <cwd>/src
247
+ * - base: '..' with root: 'tools' → apply to <cwd>/tools/..
248
+ */
249
+ base?: string;
250
+ /**
251
+ * Path to the scaffold cache file, relative to `root`.
252
+ *
253
+ * Default: ".scaffold-cache.json"
254
+ */
255
+ cacheFile?: string;
256
+ /**
257
+ * File size threshold (in bytes) above which deletions become
258
+ * interactive (e.g. ask "are you sure?").
259
+ *
260
+ * Default is determined by the core engine (e.g. 128 KB).
261
+ */
262
+ sizePromptThreshold?: number;
263
+ /**
264
+ * Optional single-root structure (legacy or simple mode).
265
+ *
266
+ * If `groups` is defined and non-empty, this is ignored.
267
+ * Paths are relative to `root` in this mode.
268
+ */
269
+ structure?: StructureEntry[];
270
+ /**
271
+ * Name of the single structure file in the scaffold directory
272
+ * for legacy mode.
273
+ *
274
+ * If `groups` is empty and `structure` is not provided, this
275
+ * file name is used (default: "structure.txt").
276
+ */
277
+ structureFile?: string;
278
+ /**
279
+ * Multiple structure groups (recommended).
280
+ *
281
+ * When provided and non-empty, the engine will iterate over each
282
+ * group and apply its structure relative to each group's `root`.
283
+ */
284
+ groups?: StructureGroupConfig[];
285
+ /**
286
+ * Hook configuration for file lifecycle events.
287
+ *
288
+ * Each category (e.g. "preCreateFile") is an array of hook configs,
289
+ * each with its own `include` / `exclude` / `files` filters.
290
+ */
291
+ hooks?: {
292
+ [K in RegularHookKind]?: RegularHookConfig[];
293
+ };
294
+ /**
295
+ * Stub definitions keyed by stub name.
296
+ *
297
+ * These are referenced from structure entries by `stub: name`.
298
+ */
299
+ stubs?: Record<string, StubConfig>;
300
+ /**
301
+ * When true, the CLI or consuming code may choose to start scaffold
302
+ * in watch mode by default (implementation-specific).
303
+ *
304
+ * This flag itself does not start watch mode; it is a hint to the
305
+ * runner / CLI.
306
+ */
307
+ watch?: boolean;
308
+ }
309
+ /**
310
+ * Options when scanning an existing directory into a structure.txt tree.
311
+ */
312
+ interface ScanStructureOptions {
313
+ /**
314
+ * Glob patterns (relative to the scanned root) to ignore.
315
+ */
316
+ ignore?: string[];
317
+ /**
318
+ * Maximum depth to traverse (0 = only that dir).
319
+ * Default: Infinity (no limit).
320
+ */
321
+ maxDepth?: number;
322
+ }
323
+ /**
324
+ * Options when scanning based on the scaffold config/groups.
325
+ */
326
+ interface ScanFromConfigOptions extends ScanStructureOptions {
327
+ /**
328
+ * If provided, only scan these group names (by `StructureGroupConfig.name`).
329
+ * If omitted, all groups are scanned (or single-root mode).
330
+ */
331
+ groups?: string[];
332
+ /**
333
+ * Optional override for scaffold directory; normally you can let
334
+ * loadScaffoldConfig resolve this from "<cwd>/scaffold".
335
+ */
336
+ scaffoldDir?: string;
337
+ }
338
+
339
+ type LogLevel = 'silent' | 'error' | 'warn' | 'info' | 'debug';
340
+ interface LoggerOptions {
341
+ level?: LogLevel;
342
+ /**
343
+ * Optional prefix string (e.g. "[scaffold]" or "[group:app]").
344
+ */
345
+ prefix?: string;
346
+ }
347
+ /**
348
+ * Minimal logger for @timeax/scaffold with colored output.
349
+ */
350
+ declare class Logger {
351
+ private level;
352
+ private prefix;
353
+ constructor(options?: LoggerOptions);
354
+ setLevel(level: LogLevel): void;
355
+ getLevel(): LogLevel;
356
+ /**
357
+ * Create a child logger with an additional prefix.
358
+ */
359
+ child(prefix: string): Logger;
360
+ private formatMessage;
361
+ private shouldLog;
362
+ error(msg: unknown, ...rest: unknown[]): void;
363
+ warn(msg: unknown, ...rest: unknown[]): void;
364
+ info(msg: unknown, ...rest: unknown[]): void;
365
+ debug(msg: unknown, ...rest: unknown[]): void;
366
+ }
367
+
368
+ interface InteractiveDeleteParams {
369
+ absolutePath: string;
370
+ relativePath: string;
371
+ size: number;
372
+ createdByStub?: string;
373
+ groupName?: string;
374
+ }
375
+
376
+ interface RunOptions {
377
+ /**
378
+ * Optional interactive delete callback; if omitted, deletions
379
+ * above the size threshold will be skipped (kept + removed from cache).
380
+ */
381
+ interactiveDelete?: (params: InteractiveDeleteParams) => Promise<'delete' | 'keep'>;
382
+ /**
383
+ * Optional logger override.
384
+ */
385
+ logger?: Logger;
386
+ /**
387
+ * Optional overrides (e.g. allow CLI to point at a different scaffold dir).
388
+ */
389
+ scaffoldDir?: string;
390
+ configPath?: string;
391
+ }
392
+ /**
393
+ * Run scaffold once for the current working directory.
394
+ */
395
+ declare function runOnce(cwd: string, options?: RunOptions): Promise<void>;
396
+
397
+ /**
398
+ * Generate a structure.txt-style tree from an existing directory.
399
+ *
400
+ * Indenting:
401
+ * - 2 spaces per level.
402
+ * - Directories suffixed with "/".
403
+ * - No stub/include/exclude annotations are guessed (plain tree).
404
+ */
405
+ declare function scanDirectoryToStructureText(rootDir: string, options?: ScanStructureOptions): string;
406
+ /**
407
+ * Result of scanning based on the scaffold config.
408
+ *
409
+ * You can use `structureFilePath` + `text` to write out group structure files.
410
+ */
411
+ interface ScanFromConfigResult {
412
+ groupName: string;
413
+ groupRoot: string;
414
+ structureFileName: string;
415
+ structureFilePath: string;
416
+ text: string;
417
+ }
418
+ /**
419
+ * Scan the project using the scaffold config and its groups.
420
+ *
421
+ * - If `config.groups` exists and is non-empty:
422
+ * - scans each group's `root` (relative to projectRoot)
423
+ * - produces text suitable for that group's structure file
424
+ * - Otherwise:
425
+ * - scans the single `projectRoot` and produces text for a single structure file.
426
+ *
427
+ * NOTE: This function does NOT write files; it just returns what should be written.
428
+ * The CLI (or caller) decides whether/where to save.
429
+ */
430
+ declare function scanProjectFromConfig(cwd: string, options?: ScanFromConfigOptions): Promise<ScanFromConfigResult[]>;
431
+ /**
432
+ * Convenience helper: write scan results to their structure files.
433
+ *
434
+ * This will ensure the scaffold directory exists and overwrite existing
435
+ * structure files.
436
+ */
437
+ declare function writeScannedStructuresFromConfig(cwd: string, options?: ScanFromConfigOptions): Promise<void>;
438
+
439
+ export { type BaseEntryOptions, type DirEntry, type FileEntry, type HookContext, type HookFilter, type RegularHookConfig, type RegularHookFn, type RegularHookKind, type RunOptions, type ScaffoldConfig, type ScanFromConfigOptions, type ScanFromConfigResult, type ScanStructureOptions, type StructureEntry, type StructureGroupConfig, type StubConfig, type StubHookConfig, type StubHookFn, type StubHookKind, runOnce, scanDirectoryToStructureText, scanProjectFromConfig, writeScannedStructuresFromConfig };