@eserstack/collector 0.0.0-stage → 5.0.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/LICENSE +13 -0
- package/README.md +177 -2
- package/dist/collector.d.ts +20 -0
- package/dist/collector.js +101 -0
- package/dist/ffi-client.d.ts +8 -0
- package/dist/ffi-client.js +9 -0
- package/dist/manifest.d.ts +26 -0
- package/dist/manifest.js +86 -0
- package/dist/mod.d.ts +2 -0
- package/dist/mod.js +3 -0
- package/dist/walk-fs.d.ts +13 -0
- package/dist/walk-fs.js +112 -0
- package/package.json +29 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023-present Eser Ozvataf and other contributors.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,178 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ⚙️ [@eserstack/collector](./)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Note:** JSR is frozen at 4.5.x. From 5.0.0, `@eserstack/collector` is
|
|
4
|
+
> published on npm under the same name (`pnpm add @eserstack/collector`); the
|
|
5
|
+
> JSR versions stay available but get no updates.
|
|
6
|
+
|
|
7
|
+
> **eserstack Library** — [eser/stack on GitHub](https://github.com/eser/stack)
|
|
8
|
+
> **Install:** `pnpm add @eserstack/collector`
|
|
9
|
+
|
|
10
|
+
Module export collector and manifest generator. Walks a directory tree,
|
|
11
|
+
dynamically imports modules, collects their exports, and generates a typed
|
|
12
|
+
manifest file — useful for auto-discovery of routes, handlers, plugins, or any
|
|
13
|
+
convention-based module system.
|
|
14
|
+
|
|
15
|
+
## 🚀 Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import * as collector from "@eserstack/collector";
|
|
19
|
+
|
|
20
|
+
// Collect all exports from a directory
|
|
21
|
+
const exports = await collector.collectExports({
|
|
22
|
+
baseDir: "./src/routes",
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
for (const [file, moduleExports] of exports) {
|
|
26
|
+
console.log(`${file}: ${moduleExports.map(([name]) => name).join(", ")}`);
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 🛠 Features
|
|
31
|
+
|
|
32
|
+
- **File Walking** — Recursively walk directories for JS/TS modules
|
|
33
|
+
- **Export Collection** — Dynamically import and collect module exports
|
|
34
|
+
- **Manifest Generation** — Generate typed manifest files with import statements
|
|
35
|
+
- **Glob Filtering** — Filter files with glob patterns
|
|
36
|
+
- **Export Filtering** — Custom filter functions for selective export collection
|
|
37
|
+
- **Test File Exclusion** — Automatically skips test files by default
|
|
38
|
+
|
|
39
|
+
## 🔌 API Reference
|
|
40
|
+
|
|
41
|
+
### `collectExports(options)`
|
|
42
|
+
|
|
43
|
+
Walk a directory tree, import each module, and return its exports.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import * as collector from "@eserstack/collector";
|
|
47
|
+
|
|
48
|
+
const exports = await collector.collectExports({
|
|
49
|
+
baseDir: "./src/handlers",
|
|
50
|
+
globFilter: "**/mod.ts",
|
|
51
|
+
exportFilter: async (entries) =>
|
|
52
|
+
entries.filter(([name]) => name.startsWith("handle")),
|
|
53
|
+
});
|
|
54
|
+
// Returns: Array<[filename, Array<[exportName, exportValue]>]>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
| Option | Type | Description |
|
|
58
|
+
| ------------------- | ---------------------------------------------------------------- | ----------------------------------------------- |
|
|
59
|
+
| `baseDir` | `string` | Root directory to walk |
|
|
60
|
+
| `globFilter` | `string` | Glob pattern to filter files |
|
|
61
|
+
| `exportFilter` | `(entries: [string, unknown][]) => Promise<[string, unknown][]>` | Filter collected exports |
|
|
62
|
+
| `ignoreFilePattern` | `RegExp` | Pattern for files to skip (default: test files) |
|
|
63
|
+
|
|
64
|
+
### `walkFiles(baseDir, globFilter, ignoreFilePattern)`
|
|
65
|
+
|
|
66
|
+
Async generator that yields relative file paths matching the criteria. It walks
|
|
67
|
+
`.ts`, `.tsx`, `.js`, `.jsx`, `.mts`, `.cts`, `.mjs` and `.cjs` files, skips
|
|
68
|
+
`node_modules`, `.git`, `dist`, `coverage`, `.output` and `.cache` directories
|
|
69
|
+
and unreadable directories below `baseDir`, and yields files in depth-first,
|
|
70
|
+
name-sorted order. A `baseDir` that cannot be read is an error.
|
|
71
|
+
|
|
72
|
+
The walk runs in Go when the native ajan library is loaded. Without it (no
|
|
73
|
+
library, `ESER_AJAN=disabled`, or only the WASM build, which has no filesystem)
|
|
74
|
+
the same walk runs in TypeScript on the runtime's file system, so no native
|
|
75
|
+
library is required.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import * as collector from "@eserstack/collector";
|
|
79
|
+
|
|
80
|
+
for await (const file of collector.walkFiles("./src", "**/*.ts", /test/)) {
|
|
81
|
+
console.log(file);
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### `buildManifestFile(filepath, options)`
|
|
86
|
+
|
|
87
|
+
Collect exports and write a manifest file to disk.
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import * as collector from "@eserstack/collector";
|
|
91
|
+
|
|
92
|
+
await collector.buildManifestFile("./src/manifest.gen.ts", {
|
|
93
|
+
baseDir: "./src/routes",
|
|
94
|
+
});
|
|
95
|
+
// Generates a file with import statements and a typed manifest object
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### `buildManifest(target, options)`
|
|
99
|
+
|
|
100
|
+
Collect exports and write a manifest to a `WritableStream`.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import * as fs from "node:fs";
|
|
104
|
+
import * as stream from "node:stream";
|
|
105
|
+
import * as collector from "@eserstack/collector";
|
|
106
|
+
|
|
107
|
+
const file = stream.Writable.toWeb(fs.createWriteStream("manifest.gen.ts"));
|
|
108
|
+
await collector.buildManifest(file, {
|
|
109
|
+
baseDir: "./src/routes",
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### `writeManifestToString(collection)`
|
|
114
|
+
|
|
115
|
+
Convert a collected export array into a manifest string: one
|
|
116
|
+
`import * as $$identifier from "./path"` line per file and a `manifest` object
|
|
117
|
+
whose `exports` array refers to each export by the name of its value (a
|
|
118
|
+
function's `name`). Imports and references are sorted in natural order, with
|
|
119
|
+
numbers compared by value and lower case before upper case. The source is not
|
|
120
|
+
formatted; run it through a formatter such as oxfmt for canonical style.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import * as collector from "@eserstack/collector";
|
|
124
|
+
|
|
125
|
+
const source = await collector.writeManifestToString(
|
|
126
|
+
await collector.collectExports({ baseDir: "./src/routes" }),
|
|
127
|
+
);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### `specifierToIdentifier(specifier, used)`
|
|
131
|
+
|
|
132
|
+
Convert a file path into a valid JavaScript identifier for use in generated
|
|
133
|
+
imports, and add it to `used`. A name already in `used` gets a `_1`, `_2`, ...
|
|
134
|
+
suffix.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import * as collector from "@eserstack/collector";
|
|
138
|
+
|
|
139
|
+
const used = new Set<string>();
|
|
140
|
+
collector.specifierToIdentifier("routes/[id].ts", used); // "routes_id_"
|
|
141
|
+
collector.specifierToIdentifier("routes/[id].js", used); // "routes_id__1"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Where the work runs
|
|
145
|
+
|
|
146
|
+
Go writes the manifest and the identifiers (`pkg/ajan/collectorfx`), reached
|
|
147
|
+
through `@eserstack/ajan`: the native library, or its WASM build where the
|
|
148
|
+
native library cannot load. `writeManifestToString`, `buildManifest`,
|
|
149
|
+
`buildManifestFile` and `specifierToIdentifier` throw the bridge's load error
|
|
150
|
+
when neither loads, for example with `ESER_AJAN=disabled`.
|
|
151
|
+
`specifierToIdentifier` is synchronous and loads the bridge synchronously on
|
|
152
|
+
first use. `walkFiles` and `collectExports` work without the bridge, as
|
|
153
|
+
described above.
|
|
154
|
+
|
|
155
|
+
## 📋 Types
|
|
156
|
+
|
|
157
|
+
### `ExportItem`
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
type ExportItem = [string, Array<[string, unknown]>];
|
|
161
|
+
// [filename, Array<[exportName, exportValue]>]
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### `CollectExportsOptions`
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
type CollectExportsOptions = {
|
|
168
|
+
baseDir: string;
|
|
169
|
+
globFilter?: string;
|
|
170
|
+
exportFilter?: (entries: [string, unknown][]) => Promise<[string, unknown][]>;
|
|
171
|
+
ignoreFilePattern?: RegExp;
|
|
172
|
+
};
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
🔗 For further details, visit the
|
|
178
|
+
[eserstack repository](https://github.com/eser/stack).
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Yields the paths, relative to `baseDir`, of the collectable source files
|
|
3
|
+
* under it.
|
|
4
|
+
*
|
|
5
|
+
* With the native bridge loaded, the walk runs in Go (collectorfx). Without it
|
|
6
|
+
* (the bridge is missing, `ESER_AJAN=disabled`, or only the WASM build loads,
|
|
7
|
+
* which has no filesystem), or when the Go walk reports an error, the walk
|
|
8
|
+
* runs in TypeScript on the runtime's file system with the same rules
|
|
9
|
+
* ({@link walkFs.walkCollectableFiles}). A base directory that cannot be read
|
|
10
|
+
* is an error either way.
|
|
11
|
+
*/
|
|
12
|
+
export declare function walkFiles(baseDir: string, globFilter: string | undefined, ignoreFilePattern: RegExp): AsyncGenerator<string>;
|
|
13
|
+
export type CollectExportsOptions = {
|
|
14
|
+
baseDir: string;
|
|
15
|
+
globFilter?: string;
|
|
16
|
+
exportFilter?: (entries: [string, unknown][]) => Promise<[string, unknown][]>;
|
|
17
|
+
ignoreFilePattern?: RegExp;
|
|
18
|
+
};
|
|
19
|
+
export type ExportItem = [string, Array<[string, unknown]>];
|
|
20
|
+
export declare const collectExports: (options: CollectExportsOptions) => Promise<Array<ExportItem>>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
3
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
4
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
5
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
return path;
|
|
9
|
+
};
|
|
10
|
+
// This file contains code from deno fresh (https://github.com/denoland/fresh),
|
|
11
|
+
// which is a web framework, licensed under the MIT license.
|
|
12
|
+
// Copyright (c) 2023 Eser Ozvataf and other contributors
|
|
13
|
+
// Copyright (c) 2021-2023 Luca Casonato
|
|
14
|
+
import * as logger from "@eserstack/logging/logger";
|
|
15
|
+
import * as patterns from "@eserstack/standards/patterns";
|
|
16
|
+
import * as ffiClient from "./ffi-client.js";
|
|
17
|
+
import * as walkFs from "./walk-fs.js";
|
|
18
|
+
const collectorLogger = logger.getLogger(["collector"]);
|
|
19
|
+
/**
|
|
20
|
+
* Convert a simple glob pattern to a RegExp.
|
|
21
|
+
* Supports `*` (any non-separator chars) and `**` (any chars including separators).
|
|
22
|
+
*/
|
|
23
|
+
const simpleGlobToRegExp = (glob) => {
|
|
24
|
+
const escaped = glob
|
|
25
|
+
.replace(/[.+?^${}()|[\]\\]/g, "\\$&")
|
|
26
|
+
.replace(/\*\*/g, ".*")
|
|
27
|
+
.replace(/\*/g, "[^/]*");
|
|
28
|
+
return new RegExp(`^${escaped}$`);
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Yields the paths, relative to `baseDir`, of the collectable source files
|
|
32
|
+
* under it.
|
|
33
|
+
*
|
|
34
|
+
* With the native bridge loaded, the walk runs in Go (collectorfx). Without it
|
|
35
|
+
* (the bridge is missing, `ESER_AJAN=disabled`, or only the WASM build loads,
|
|
36
|
+
* which has no filesystem), or when the Go walk reports an error, the walk
|
|
37
|
+
* runs in TypeScript on the runtime's file system with the same rules
|
|
38
|
+
* ({@link walkFs.walkCollectableFiles}). A base directory that cannot be read
|
|
39
|
+
* is an error either way.
|
|
40
|
+
*/
|
|
41
|
+
export async function* walkFiles(baseDir, globFilter, ignoreFilePattern) {
|
|
42
|
+
const glob = globFilter === undefined ? undefined : simpleGlobToRegExp(globFilter);
|
|
43
|
+
for (const relPath of await listFiles(baseDir, ignoreFilePattern)) {
|
|
44
|
+
if (glob !== undefined && !glob.test(relPath)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
yield relPath;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** The Go walk's result, or null when it cannot serve this walk. */
|
|
51
|
+
const listFilesInGo = async (baseDir, ignoreFilePattern) => {
|
|
52
|
+
await ffiClient.ensureLib();
|
|
53
|
+
const lib = ffiClient.getLib();
|
|
54
|
+
if (lib === null) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
const result = lib.call("collectorWalkFiles", {
|
|
58
|
+
dir: baseDir,
|
|
59
|
+
ignoreFilePattern: ignoreFilePattern.source,
|
|
60
|
+
});
|
|
61
|
+
if (result.error !== undefined && result.error !== "") {
|
|
62
|
+
// The WASM build without a preopen cannot read the directory, and Go's
|
|
63
|
+
// regexp rejects some JavaScript patterns, such as lookaheads. The
|
|
64
|
+
// TypeScript walk handles both, and reports a root that is really
|
|
65
|
+
// unreadable itself.
|
|
66
|
+
collectorLogger.debug(`collector walk in Go failed (${result.error}); walking in TypeScript`);
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
return (result.files ?? []).map((file) => file.relPath);
|
|
70
|
+
};
|
|
71
|
+
const listFiles = async (baseDir, ignoreFilePattern) => (await listFilesInGo(baseDir, ignoreFilePattern)) ??
|
|
72
|
+
(await walkFs.walkCollectableFiles(baseDir, ignoreFilePattern));
|
|
73
|
+
export const collectExports = async (options) => {
|
|
74
|
+
const ignoreFilePattern = options.ignoreFilePattern ?? patterns.JS_TEST_FILE_PATTERN;
|
|
75
|
+
const exports = [];
|
|
76
|
+
for await (const entry of walkFiles(options.baseDir, options.globFilter, ignoreFilePattern)) {
|
|
77
|
+
const entryUri = `${options.baseDir}/${entry}`;
|
|
78
|
+
try {
|
|
79
|
+
const entryModule = await import(__rewriteRelativeImportExtension(entryUri));
|
|
80
|
+
const moduleExports = Object.entries(entryModule);
|
|
81
|
+
if (options.exportFilter === undefined) {
|
|
82
|
+
exports.push([entry, moduleExports]);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const selectedExports = await options.exportFilter(moduleExports);
|
|
86
|
+
if (selectedExports.length === 0) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
exports.push([entry, selectedExports]);
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
// A module that fails to import is skipped, not fatal: the remaining
|
|
93
|
+
// modules are still collected, and the warning names the one left out.
|
|
94
|
+
// The reason goes into the message text, since an Error passed as a log
|
|
95
|
+
// argument serializes to "{}" and would lose it.
|
|
96
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
97
|
+
await logger.current.warn(`collectExports skipped ${entryUri}: importing it failed (${reason}). Fix the module or exclude it with ignoreFilePattern.`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return exports;
|
|
101
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This package's view of the Go bridge.
|
|
3
|
+
*
|
|
4
|
+
* The loader itself lives in `@eserstack/ajan/ffi/client` and is shared with
|
|
5
|
+
* every other package, so the whole process opens the library once. This module
|
|
6
|
+
* stays as the import path the package's own code already uses.
|
|
7
|
+
*/
|
|
8
|
+
export { ensureLib, getLib, getLoadError, requireLib, requireLibSync, } from "@eserstack/ajan/ffi/client";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* This package's view of the Go bridge.
|
|
4
|
+
*
|
|
5
|
+
* The loader itself lives in `@eserstack/ajan/ffi/client` and is shared with
|
|
6
|
+
* every other package, so the whole process opens the library once. This module
|
|
7
|
+
* stays as the import path the package's own code already uses.
|
|
8
|
+
*/
|
|
9
|
+
export { ensureLib, getLib, getLoadError, requireLib, requireLibSync, } from "@eserstack/ajan/ffi/client";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as collector from "./collector.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a JavaScript identifier for a project-relative specifier that is not
|
|
4
|
+
* in `used`, and adds it to `used`.
|
|
5
|
+
*
|
|
6
|
+
* Go answers it (`pkg/ajan/collectorfx`) through the `@eserstack/ajan`
|
|
7
|
+
* bridge, which loads synchronously on first use.
|
|
8
|
+
*
|
|
9
|
+
* @throws {Error} When the bridge does not load (its load error is the
|
|
10
|
+
* `cause`) or Go refuses the request
|
|
11
|
+
*/
|
|
12
|
+
export declare const specifierToIdentifier: (specifier: string, used: Set<string>) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Writes the source of a manifest module for a collection: one namespace
|
|
15
|
+
* import per file and a `manifest` object that refers to each export by the
|
|
16
|
+
* name of its value.
|
|
17
|
+
*
|
|
18
|
+
* Go writes it (`pkg/ajan/collectorfx`) through the `@eserstack/ajan` bridge.
|
|
19
|
+
* The source is not formatted.
|
|
20
|
+
*
|
|
21
|
+
* @throws {Error} When the bridge does not load (its load error is the
|
|
22
|
+
* `cause`) or Go refuses the request
|
|
23
|
+
*/
|
|
24
|
+
export declare const writeManifestToString: (collection: Array<[string, Array<[string, unknown]>]>) => Promise<string>;
|
|
25
|
+
export declare const buildManifest: (target: WritableStream, options: collector.CollectExportsOptions) => Promise<void>;
|
|
26
|
+
export declare const buildManifestFile: (filepath: string, options: collector.CollectExportsOptions) => Promise<void>;
|
package/dist/manifest.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
// This file contains code from deno fresh (https://github.com/denoland/fresh),
|
|
3
|
+
// which is a web framework, licensed under the MIT license.
|
|
4
|
+
// Copyright (c) 2023 Eser Ozvataf and other contributors
|
|
5
|
+
// Copyright (c) 2021-2023 Luca Casonato
|
|
6
|
+
import * as crossRuntime from "@eserstack/standards/cross-runtime";
|
|
7
|
+
import * as logger from "@eserstack/logging/logger";
|
|
8
|
+
import * as collector from "./collector.js";
|
|
9
|
+
import * as ffiClient from "./ffi-client.js";
|
|
10
|
+
/**
|
|
11
|
+
* Creates a JavaScript identifier for a project-relative specifier that is not
|
|
12
|
+
* in `used`, and adds it to `used`.
|
|
13
|
+
*
|
|
14
|
+
* Go answers it (`pkg/ajan/collectorfx`) through the `@eserstack/ajan`
|
|
15
|
+
* bridge, which loads synchronously on first use.
|
|
16
|
+
*
|
|
17
|
+
* @throws {Error} When the bridge does not load (its load error is the
|
|
18
|
+
* `cause`) or Go refuses the request
|
|
19
|
+
*/
|
|
20
|
+
export const specifierToIdentifier = (specifier, used) => {
|
|
21
|
+
const answer = ffiClient
|
|
22
|
+
.requireLibSync()
|
|
23
|
+
.call("collectorSpecifierToIdentifier", { specifier, used: [...used] });
|
|
24
|
+
if (answer.error !== undefined) {
|
|
25
|
+
throw new Error(answer.error);
|
|
26
|
+
}
|
|
27
|
+
const identifier = answer.identifier ?? "";
|
|
28
|
+
used.add(identifier);
|
|
29
|
+
return identifier;
|
|
30
|
+
};
|
|
31
|
+
/** The name a manifest refers to for one export: the name of its value. */
|
|
32
|
+
const referenceNameOf = (value) => `${value.name}`;
|
|
33
|
+
/**
|
|
34
|
+
* Writes the source of a manifest module for a collection: one namespace
|
|
35
|
+
* import per file and a `manifest` object that refers to each export by the
|
|
36
|
+
* name of its value.
|
|
37
|
+
*
|
|
38
|
+
* Go writes it (`pkg/ajan/collectorfx`) through the `@eserstack/ajan` bridge.
|
|
39
|
+
* The source is not formatted.
|
|
40
|
+
*
|
|
41
|
+
* @throws {Error} When the bridge does not load (its load error is the
|
|
42
|
+
* `cause`) or Go refuses the request
|
|
43
|
+
*/
|
|
44
|
+
export const writeManifestToString = async (collection) => {
|
|
45
|
+
const lib = await ffiClient.requireLib();
|
|
46
|
+
const answer = lib.call("collectorGenerateManifest", {
|
|
47
|
+
entries: collection.map(([relPath, exports]) => ({
|
|
48
|
+
relPath,
|
|
49
|
+
exports: exports.map(([, value]) => referenceNameOf(value)),
|
|
50
|
+
})),
|
|
51
|
+
});
|
|
52
|
+
if (answer.error !== undefined) {
|
|
53
|
+
throw new Error(answer.error);
|
|
54
|
+
}
|
|
55
|
+
return answer.source ?? "";
|
|
56
|
+
};
|
|
57
|
+
export const buildManifest = async (target, options) => {
|
|
58
|
+
const collection = await collector.collectExports(options);
|
|
59
|
+
const manifestStr = await writeManifestToString(collection);
|
|
60
|
+
const outputWriter = target.getWriter();
|
|
61
|
+
try {
|
|
62
|
+
await outputWriter.ready;
|
|
63
|
+
const encoded = new TextEncoder().encode(manifestStr);
|
|
64
|
+
await outputWriter.write(encoded);
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
outputWriter.releaseLock();
|
|
68
|
+
}
|
|
69
|
+
const exportModules = Object.values(collection);
|
|
70
|
+
const exportCount = exportModules.reduce((acc, [, moduleFns]) => {
|
|
71
|
+
acc += moduleFns.length;
|
|
72
|
+
return acc;
|
|
73
|
+
}, 0);
|
|
74
|
+
logger.current.info(`The manifest file has been generated for ${exportCount} exports in ${exportModules.length} modules.`);
|
|
75
|
+
};
|
|
76
|
+
export const buildManifestFile = async (filepath, options) => {
|
|
77
|
+
const collection = await collector.collectExports(options);
|
|
78
|
+
const manifestStr = await writeManifestToString(collection);
|
|
79
|
+
await crossRuntime.runtime.fs.writeTextFile(filepath, manifestStr);
|
|
80
|
+
const exportModules = Object.values(collection);
|
|
81
|
+
const exportCount = exportModules.reduce((acc, [, moduleFns]) => {
|
|
82
|
+
acc += moduleFns.length;
|
|
83
|
+
return acc;
|
|
84
|
+
}, 0);
|
|
85
|
+
logger.current.info(`The manifest file has been generated for ${exportCount} exports in ${exportModules.length} modules.`);
|
|
86
|
+
};
|
package/dist/mod.d.ts
ADDED
package/dist/mod.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
export { collectExports, walkFiles, } from "./collector.js";
|
|
3
|
+
export { buildManifest, buildManifestFile, specifierToIdentifier, writeManifestToString, } from "./manifest.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** The base directory of a walk does not exist or cannot be read. */
|
|
2
|
+
export declare class UnreadableWalkRootError extends Error {
|
|
3
|
+
readonly root: string;
|
|
4
|
+
constructor(root: string, options?: ErrorOptions);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Returns the paths, relative to `baseDir` and with forward slashes, of the
|
|
8
|
+
* collectable source files under it, leaving out those `ignoreFilePattern`
|
|
9
|
+
* matches.
|
|
10
|
+
*
|
|
11
|
+
* @throws {UnreadableWalkRootError} when `baseDir` cannot be read
|
|
12
|
+
*/
|
|
13
|
+
export declare const walkCollectableFiles: (baseDir: string, ignoreFilePattern: RegExp) => Promise<string[]>;
|
package/dist/walk-fs.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* The TypeScript twin of collectorfx.WalkCollectableFiles
|
|
4
|
+
* (pkg/ajan/collectorfx/walker.go), used when the native bridge is not
|
|
5
|
+
* available. It keeps the Go walker's rules: the same source extensions, the
|
|
6
|
+
* same skipped directory names, entries below the root that cannot be read are
|
|
7
|
+
* skipped, a root that cannot be read is an error, and files come in the order
|
|
8
|
+
* of a depth-first walk over directory entries sorted by name.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
13
|
+
/** File extensions treated as collectable source files. */
|
|
14
|
+
const JS_EXTENSIONS = new Set([
|
|
15
|
+
".ts",
|
|
16
|
+
".tsx",
|
|
17
|
+
".js",
|
|
18
|
+
".jsx",
|
|
19
|
+
".mts",
|
|
20
|
+
".cts",
|
|
21
|
+
".mjs",
|
|
22
|
+
".cjs",
|
|
23
|
+
]);
|
|
24
|
+
/** Directory names that are never walked into. */
|
|
25
|
+
const EXCLUDED_DIRS = new Set([
|
|
26
|
+
"node_modules",
|
|
27
|
+
".git",
|
|
28
|
+
"dist",
|
|
29
|
+
"coverage",
|
|
30
|
+
".output",
|
|
31
|
+
".cache",
|
|
32
|
+
]);
|
|
33
|
+
/** The base directory of a walk does not exist or cannot be read. */
|
|
34
|
+
export class UnreadableWalkRootError extends Error {
|
|
35
|
+
root;
|
|
36
|
+
constructor(root, options) {
|
|
37
|
+
super(`walk root cannot be read: ${root}`, options);
|
|
38
|
+
this.root = root;
|
|
39
|
+
this.name = "UnreadableWalkRootError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const extensionOf = (name) => {
|
|
43
|
+
const dot = name.lastIndexOf(".");
|
|
44
|
+
return dot < 0 ? "" : name.slice(dot).toLowerCase();
|
|
45
|
+
};
|
|
46
|
+
/** Byte order on names, as Go's os.ReadDir sorts them. */
|
|
47
|
+
const byName = (a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
|
|
48
|
+
const readSortedDir = async (dir) => {
|
|
49
|
+
const entries = [];
|
|
50
|
+
for await (const entry of runtime.fs.readDir(dir)) {
|
|
51
|
+
// A symbolic link is reported as itself and never followed, as
|
|
52
|
+
// filepath.WalkDir does.
|
|
53
|
+
entries.push({
|
|
54
|
+
name: entry.name,
|
|
55
|
+
isDirectory: entry.isDirectory && !entry.isSymlink,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return entries.sort(byName);
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Returns the paths, relative to `baseDir` and with forward slashes, of the
|
|
62
|
+
* collectable source files under it, leaving out those `ignoreFilePattern`
|
|
63
|
+
* matches.
|
|
64
|
+
*
|
|
65
|
+
* @throws {UnreadableWalkRootError} when `baseDir` cannot be read
|
|
66
|
+
*/
|
|
67
|
+
export const walkCollectableFiles = async (baseDir, ignoreFilePattern) => {
|
|
68
|
+
const root = runtime.path.resolve(baseDir);
|
|
69
|
+
const files = [];
|
|
70
|
+
let rootEntries;
|
|
71
|
+
try {
|
|
72
|
+
rootEntries = await readSortedDir(root);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
throw new UnreadableWalkRootError(root, { cause: error });
|
|
76
|
+
}
|
|
77
|
+
const visit = async (entries, relDir) => {
|
|
78
|
+
for (const entry of entries) {
|
|
79
|
+
const rel = relDir === "" ? entry.name : `${relDir}/${entry.name}`;
|
|
80
|
+
if (entry.isDirectory) {
|
|
81
|
+
if (EXCLUDED_DIRS.has(entry.name)) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
let children;
|
|
85
|
+
try {
|
|
86
|
+
// Depth-first and in name order, so the output order is the Go
|
|
87
|
+
// walker's.
|
|
88
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
89
|
+
children = await readSortedDir(runtime.path.join(root, rel));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Unreadable entries below the root are skipped, as in Go.
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
96
|
+
await visit(children, rel);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (!JS_EXTENSIONS.has(extensionOf(entry.name))) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
// A pattern with the g or y flag keeps state between test() calls.
|
|
103
|
+
ignoreFilePattern.lastIndex = 0;
|
|
104
|
+
if (ignoreFilePattern.test(rel)) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
files.push(rel);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
await visit(rootEntries, "");
|
|
111
|
+
return files;
|
|
112
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eserstack/collector",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/eser/stack",
|
|
8
|
+
"directory": "pkg/@eserstack/collector"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/**/*.js",
|
|
12
|
+
"dist/**/*.d.ts",
|
|
13
|
+
"dist/**/*.json"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/mod.d.ts",
|
|
18
|
+
"import": "./dist/mod.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@eserstack/ajan": "5.0.0",
|
|
23
|
+
"@eserstack/logging": "5.0.0",
|
|
24
|
+
"@eserstack/standards": "5.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@eserstack/test-kit": "5.0.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "node ../../../etc/scripts/build-library.ts"
|
|
31
|
+
}
|
|
6
32
|
}
|