@mxpicture/build-api 0.2.49 → 0.2.50
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/barrel/Barrel.d.ts +8 -1
- package/dist/barrel/Barrel.d.ts.map +1 -1
- package/dist/barrel/Barrel.js +54 -11
- package/dist/barrel/Barrel.js.map +1 -1
- package/dist/code/code.common.d.ts +7 -0
- package/dist/code/code.common.d.ts.map +1 -1
- package/dist/code/code.common.js +22 -0
- package/dist/code/code.common.js.map +1 -1
- package/dist/code/code.exports.d.ts +2 -2
- package/dist/code/code.exports.d.ts.map +1 -1
- package/dist/code/code.exports.js +4 -2
- package/dist/code/code.exports.js.map +1 -1
- package/package.json +1 -1
package/dist/barrel/Barrel.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BarrelGroup, BarrelParams, BarrelResult } from "../types/types.barrel.js";
|
|
2
2
|
import { Pkg } from "../pkg/Pkg.js";
|
|
3
|
+
import { SourceFile } from "../types/types.code.js";
|
|
3
4
|
export declare const DEFAULT_EXCLUDED_PATTERNS: RegExp[];
|
|
4
5
|
export declare const DEFAULT_HEADER = "// This file is auto-generated. Do not edit manually.\n";
|
|
5
6
|
export declare const runBarrel: (params: BarrelParams) => Promise<void>;
|
|
@@ -9,7 +10,13 @@ export declare class Barrel {
|
|
|
9
10
|
constructor(excludes: RegExp[], fileHeader: string);
|
|
10
11
|
run(): Promise<void>;
|
|
11
12
|
protected isExcluded(fileName: string): boolean;
|
|
12
|
-
protected persistBarrel(group: BarrelGroup): Promise<BarrelGroup>;
|
|
13
|
+
protected persistBarrel(group: BarrelGroup, sourceFileMap: Map<string, SourceFile>): Promise<BarrelGroup>;
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if any source file in the group is newer than the barrel
|
|
16
|
+
* file, or if the barrel file does not yet exist. File deletions are handled
|
|
17
|
+
* implicitly because the group is rebuilt from readdir on every run.
|
|
18
|
+
*/
|
|
19
|
+
protected hasGroupChanged(group: BarrelGroup, barrelPath: string): Promise<boolean>;
|
|
13
20
|
protected updatePackageExports(pkg: Pkg, exports: Record<string, string>): Promise<void>;
|
|
14
21
|
protected hasExportsChanged(a: Record<string, string> | undefined, b: Record<string, string> | undefined): boolean;
|
|
15
22
|
protected extractExports(result: BarrelResult): Record<string, string>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Barrel.d.ts","sourceRoot":"","sources":["../../src/barrel/Barrel.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,YAAY,EACZ,YAAY,EACb,MAAM,0BAA0B,CAAC;AAIlC,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"Barrel.d.ts","sourceRoot":"","sources":["../../src/barrel/Barrel.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,YAAY,EACZ,YAAY,EACb,MAAM,0BAA0B,CAAC;AAIlC,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAIpC,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAGpD,eAAO,MAAM,yBAAyB,EAAE,MAAM,EAQ7C,CAAC;AAGF,eAAO,MAAM,cAAc,4DACgC,CAAC;AAE5D,eAAO,MAAM,SAAS,GAAU,QAAQ,YAAY,kBAMnD,CAAC;AAEF,qBAAa,MAAM;IAEf,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE;IACrC,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM;gBADlB,QAAQ,EAAE,MAAM,EAAE,EAClB,UAAU,EAAE,MAAM;IAG1B,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAoGjC,SAAS,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;cAI/B,aAAa,CAC3B,KAAK,EAAE,WAAW,EAClB,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,GACrC,OAAO,CAAC,WAAW,CAAC;IAwCvB;;;;OAIG;cACa,eAAe,CAC7B,KAAK,EAAE,WAAW,EAClB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,OAAO,CAAC;cAwEH,oBAAoB,CAClC,GAAG,EAAE,GAAG,EACR,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAoBjC,SAAS,CAAC,iBAAiB,CACzB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EACrC,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,GACpC,OAAO;IAcV,SAAS,CAAC,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;CASvE"}
|
package/dist/barrel/Barrel.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { basename, dirname, join, relative } from "node:path";
|
|
2
|
-
import { readdir, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { readdir, rm, stat, writeFile } from "node:fs/promises";
|
|
3
3
|
import { logInfo, logSuccess } from "../logger/Logger.js";
|
|
4
4
|
import { initWorkspace, workspace } from "../workspace/Workspace.js";
|
|
5
5
|
import { formatCode } from "../code/code.format.js";
|
|
6
6
|
import { fileExports } from "../code/code.exports.js";
|
|
7
|
+
import { createSourceFileMap } from "../code/code.common.js";
|
|
7
8
|
// /** File patterns to exclude from barrel exports */
|
|
8
9
|
export const DEFAULT_EXCLUDED_PATTERNS = [
|
|
9
10
|
/\.test\.ts$/,
|
|
@@ -30,23 +31,26 @@ export class Barrel {
|
|
|
30
31
|
async run() {
|
|
31
32
|
const ws = workspace();
|
|
32
33
|
await ws.read();
|
|
33
|
-
|
|
34
|
+
// Phase 1: collect all barrel groups, keyed by package name
|
|
35
|
+
const groupsByPkg = new Map();
|
|
34
36
|
for (const pkg of ws.packages) {
|
|
35
37
|
try {
|
|
36
38
|
const items = (await readdir(pkg.srcPath, { recursive: true })).sort();
|
|
37
39
|
let currentGroup;
|
|
40
|
+
const pushGroup = (g) => {
|
|
41
|
+
const key = g.pkg.content.name;
|
|
42
|
+
if (!groupsByPkg.has(key))
|
|
43
|
+
groupsByPkg.set(key, []);
|
|
44
|
+
groupsByPkg.get(key).push({ ...g });
|
|
45
|
+
};
|
|
38
46
|
for (const item of items) {
|
|
39
47
|
const filename = basename(item);
|
|
40
48
|
const dirPath = join(pkg.srcPath, dirname(item));
|
|
41
49
|
const filePath = join(dirPath, filename);
|
|
42
50
|
if (dirPath !== currentGroup?.dirPath) {
|
|
43
51
|
if (currentGroup)
|
|
44
|
-
|
|
45
|
-
currentGroup = {
|
|
46
|
-
dirPath,
|
|
47
|
-
files: [],
|
|
48
|
-
pkg,
|
|
49
|
-
};
|
|
52
|
+
pushGroup(currentGroup);
|
|
53
|
+
currentGroup = { dirPath, files: [], pkg };
|
|
50
54
|
}
|
|
51
55
|
if (this.isExcluded(filePath) ||
|
|
52
56
|
(!filename.endsWith(".ts") && !filename.endsWith(".mts")))
|
|
@@ -54,12 +58,23 @@ export class Barrel {
|
|
|
54
58
|
currentGroup.files.push(filename);
|
|
55
59
|
}
|
|
56
60
|
if (currentGroup)
|
|
57
|
-
|
|
61
|
+
pushGroup(currentGroup);
|
|
58
62
|
}
|
|
59
63
|
catch {
|
|
60
64
|
continue;
|
|
61
65
|
}
|
|
62
66
|
}
|
|
67
|
+
// Phase 2: build one ts.Program per package, then persist all groups in parallel
|
|
68
|
+
const promises = [];
|
|
69
|
+
for (const groups of groupsByPkg.values()) {
|
|
70
|
+
const allFilePaths = groups.flatMap((g) => g.files.map((f) => join(g.dirPath, f)));
|
|
71
|
+
const sourceFileMap = allFilePaths.length > 0
|
|
72
|
+
? createSourceFileMap(allFilePaths)
|
|
73
|
+
: new Map();
|
|
74
|
+
for (const group of groups) {
|
|
75
|
+
promises.push(this.persistBarrel({ ...group }, sourceFileMap));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
63
78
|
const barrelGroups = (await Promise.all(promises)).filter((prom) => prom.files.length > 0);
|
|
64
79
|
const results = [];
|
|
65
80
|
for (const barrelGroup of barrelGroups) {
|
|
@@ -87,17 +102,20 @@ export class Barrel {
|
|
|
87
102
|
isExcluded(fileName) {
|
|
88
103
|
return this.excludes.some((pattern) => pattern.test(fileName));
|
|
89
104
|
}
|
|
90
|
-
async persistBarrel(group) {
|
|
105
|
+
async persistBarrel(group, sourceFileMap) {
|
|
91
106
|
const barrelPath = join(group.dirPath, "index.ts");
|
|
92
107
|
// no files --> skip/delete index.ts
|
|
93
108
|
if (group.files.length === 0) {
|
|
94
109
|
await rm(barrelPath, { recursive: true, force: true });
|
|
95
110
|
return group;
|
|
96
111
|
}
|
|
112
|
+
// Skip if all source files are older than the existing barrel
|
|
113
|
+
if (!(await this.hasGroupChanged(group, barrelPath)))
|
|
114
|
+
return group;
|
|
97
115
|
const exportLines = [];
|
|
98
116
|
for (const file of group.files.sort()) {
|
|
99
117
|
const filePath = join(group.dirPath, file);
|
|
100
|
-
const expResult = fileExports(filePath);
|
|
118
|
+
const expResult = fileExports(filePath, undefined, sourceFileMap.get(filePath));
|
|
101
119
|
if (expResult.items.length === 0)
|
|
102
120
|
continue;
|
|
103
121
|
const exports = expResult.items.map((item) => item.name);
|
|
@@ -114,6 +132,31 @@ export class Barrel {
|
|
|
114
132
|
await writeFile(barrelPath, content);
|
|
115
133
|
return group;
|
|
116
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns true if any source file in the group is newer than the barrel
|
|
137
|
+
* file, or if the barrel file does not yet exist. File deletions are handled
|
|
138
|
+
* implicitly because the group is rebuilt from readdir on every run.
|
|
139
|
+
*/
|
|
140
|
+
async hasGroupChanged(group, barrelPath) {
|
|
141
|
+
let barrelMtime;
|
|
142
|
+
try {
|
|
143
|
+
barrelMtime = (await stat(barrelPath)).mtimeMs;
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return true; // barrel doesn't exist yet
|
|
147
|
+
}
|
|
148
|
+
for (const file of group.files) {
|
|
149
|
+
try {
|
|
150
|
+
const fileMtime = (await stat(join(group.dirPath, file))).mtimeMs;
|
|
151
|
+
if (fileMtime > barrelMtime)
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return true; // file accessibility changed
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
117
160
|
// protected async extractExportsFromFile(
|
|
118
161
|
// filePath: string,
|
|
119
162
|
// ): Promise<{ exports: string[]; hasDefault: boolean }> {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Barrel.js","sourceRoot":"","sources":["../../src/barrel/Barrel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAM9D,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAErE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,sDAAsD;AACtD,MAAM,CAAC,MAAM,yBAAyB,GAAa;IACjD,aAAa;IACb,aAAa;IACb,uBAAuB;IACvB,UAAU;IACV,YAAY;IACZ,yCAAyC;IACzC,kBAAkB;CACnB,CAAC;AAEF,6DAA6D;AAC7D,MAAM,CAAC,MAAM,cAAc,GACzB,yDAAyD,CAAC;AAE5D,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAAE,MAAoB,EAAE,EAAE;IACtD,aAAa,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC/B,OAAO,IAAI,MAAM,CACf,MAAM,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f,IAAI,CAAC,CAAC;YAAE,CAAC,GAAG,EAAE,CAAC;QAEf,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC;QACjE,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC;QAC3B,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC;YAAE,OAAO,IAAI,CAAC;QAE1B,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;YACvC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC;gBAAE,OAAO,IAAI,CAAC;QAC9C,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAES,cAAc,CAAC,MAAoB;QAC3C,MAAM,MAAM,GAA2B,EAAE,CAAC;QAC1C,KAAK,MAAM,SAAS,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YAC1C,MAAM,EAAE,GAAG,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;YACnD,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,UAAU,EAAE,WAAW,CAAC;QAC9C,CAAC;QACD,MAAM,CAAC,gBAAgB,CAAC,GAAG,gBAAgB,CAAC;QAC5C,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import { basename, dirname, join, relative } from \"node:path\";\nimport {\n BarrelGroup,\n BarrelParams,\n BarrelResult,\n} from \"../types/types.barrel.js\";\nimport { readdir, rm, writeFile } from \"node:fs/promises\";\nimport { logInfo, logSuccess } from \"../logger/Logger.js\";\nimport { initWorkspace, workspace } from \"../workspace/Workspace.js\";\nimport { Pkg } from \"../pkg/Pkg.js\";\nimport { formatCode } from \"../code/code.format.js\";\nimport { fileExports } from \"../code/code.exports.js\";\n\n// /** File patterns to exclude from barrel exports */\nexport const DEFAULT_EXCLUDED_PATTERNS: RegExp[] = [\n /\\.test\\.ts$/,\n /\\.spec\\.ts$/,\n /(^|\\/)__tests__(\\/|$)/,\n /\\.d\\.ts$/,\n /index\\.ts$/,\n /(.+\\/|^)src\\/(.+\\/|)scripts?\\/.+\\.m?ts$/,\n /\\/4testing(\\/|$)/,\n];\n\n// /** Header comment added to every generated barrel file */\nexport const DEFAULT_HEADER =\n \"// This file is auto-generated. Do not edit manually.\\n\";\n\nexport const runBarrel = async (params: BarrelParams) => {\n initWorkspace(params.repoRoot);\n return new Barrel(\n params.excludes ?? DEFAULT_EXCLUDED_PATTERNS,\n params.fileHeader ?? DEFAULT_HEADER,\n ).run();\n};\n\nexport class Barrel {\n public constructor(\n protected readonly excludes: RegExp[],\n protected readonly fileHeader: string,\n ) {}\n\n public async run(): Promise<void> {\n const ws = workspace();\n await ws.read();\n\n const promises: Promise<BarrelGroup>[] = [];\n\n for (const pkg of ws.packages) {\n try {\n const items = (await readdir(pkg.srcPath, { recursive: true })).sort();\n let currentGroup: BarrelGroup | undefined;\n\n for (const item of items) {\n const filename = basename(item);\n const dirPath = join(pkg.srcPath, dirname(item));\n const filePath = join(dirPath, filename);\n\n if (dirPath !== currentGroup?.dirPath) {\n if (currentGroup)\n promises.push(this.persistBarrel({ ...currentGroup }));\n currentGroup = {\n dirPath,\n files: [],\n pkg,\n };\n }\n\n if (\n this.isExcluded(filePath) ||\n (!filename.endsWith(\".ts\") && !filename.endsWith(\".mts\"))\n )\n continue;\n\n currentGroup.files.push(filename);\n }\n\n if (currentGroup)\n promises.push(this.persistBarrel({ ...currentGroup }));\n } catch {\n continue;\n }\n }\n\n const barrelGroups = (await Promise.all(promises)).filter(\n (prom) => prom.files.length > 0,\n );\n\n const results: BarrelResult[] = [];\n\n for (const barrelGroup of barrelGroups) {\n let found = results.find(\n (res) => barrelGroup.dirPath === res.pkg.dirPath,\n );\n if (!found) {\n found = {\n barrelDirs: [],\n pkg: barrelGroup.pkg,\n };\n results.push(found);\n }\n\n found.barrelDirs.push(barrelGroup.dirPath);\n }\n\n const pkgExps: Record<string, { pkg: Pkg; exps: Record<string, string> }> =\n {};\n for (const res of results) {\n if (!(res.pkg.content.name in pkgExps))\n pkgExps[res.pkg.content.name] = { pkg: res.pkg, exps: {} };\n\n pkgExps[res.pkg.content.name].exps = {\n ...pkgExps[res.pkg.content.name].exps,\n ...this.extractExports(res),\n };\n }\n\n await Promise.all(\n Object.values(pkgExps).map(async (pkgExp) =>\n this.updatePackageExports(pkgExp.pkg, pkgExp.exps),\n ),\n );\n }\n\n protected isExcluded(fileName: string): boolean {\n return this.excludes.some((pattern) => pattern.test(fileName));\n }\n\n protected async persistBarrel(group: BarrelGroup): Promise<BarrelGroup> {\n const barrelPath = join(group.dirPath, \"index.ts\");\n // no files --> skip/delete index.ts\n if (group.files.length === 0) {\n await rm(barrelPath, { recursive: true, force: true });\n return group;\n }\n\n const exportLines: string[] = [];\n for (const file of group.files.sort()) {\n const filePath = join(group.dirPath, file);\n\n const expResult = fileExports(filePath);\n if (expResult.items.length === 0) continue;\n\n const exports = expResult.items.map((item) => item.name);\n const moduleRef = `./${basename(file, \".ts\")}.js`;\n\n if (expResult.hasDefault) {\n exportLines.push(`export * from \"${moduleRef}\";`);\n } else if (exports.length > 0) {\n exportLines.push(\n `export { ${exports.join(\", \")} } from \"${moduleRef}\";`,\n );\n }\n }\n\n let content = `${this.fileHeader}${exportLines.length > 0 ? exportLines.join(\"\\n\") + \"\\n\" : \"\"}`;\n content = await formatCode(content);\n await writeFile(barrelPath, content);\n return group;\n }\n\n // protected async extractExportsFromFile(\n // filePath: string,\n // ): Promise<{ exports: string[]; hasDefault: boolean }> {\n // try {\n // const content = await readFile(filePath, \"utf-8\");\n // const exports: Set<string> = new Set();\n // let hasDefault = false;\n\n // // Detect any default export\n // if (/export\\s+default\\b/.test(content)) hasDefault = true;\n\n // // Match named exports: export const|function|class Name\n // const namedExportPattern =\n // /export\\s+(?:const|let|var|function|class|interface|type|enum)\\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/g;\n // let match;\n // while ((match = namedExportPattern.exec(content)) !== null) {\n // const name = match[1];\n // if (\n // !name.startsWith(\"_\") &&\n // !name.startsWith(\"//\") &&\n // !name.startsWith(\"/*\")\n // )\n // exports.add(name);\n // }\n\n // // Match export { a, b, c } and export { a, b, c } from '...'\n // const exportBracePattern = /export\\s*\\{\\s*([^}]+)\\s*\\}/g;\n // while ((match = exportBracePattern.exec(content)) !== null) {\n // const items = match[1].split(\",\");\n // for (const item of items) {\n // const trimmed = item.trim();\n // if (!trimmed) continue;\n // // Handle \"export { name as alias }\"\n // const parts = trimmed.split(/\\s+as\\s+/);\n // const name = parts[0].trim();\n // const alias = parts[1]?.trim();\n // // Skip default exports (e.g. \"export { Foo as default }\")\n // if (alias === \"default\") {\n // hasDefault = true;\n // continue;\n // }\n // if (name && name !== \"default\" && !name.startsWith(\"_\")) {\n // exports.add(name);\n // }\n // }\n // }\n\n // return { exports: Array.from(exports).sort(), hasDefault };\n // } catch {\n // return { exports: [], hasDefault: false };\n // }\n // }\n\n protected async updatePackageExports(\n pkg: Pkg,\n exports: Record<string, string>,\n ) {\n try {\n // const pkgName = relative(this.paths.workspacesDir, packageDir);\n // // const pkgs = await workspace().loadPackages();\n\n logInfo(`📦 updating ${pkg.repoDirPath} package.json ...`);\n // const pkg = await readPackageJsonThrow(packageJsonPath);\n if (!this.hasExportsChanged(exports, pkg.content.exports)) {\n logInfo(`🧹 No changes in ${pkg.repoDirPath}, nothing to do`);\n return;\n }\n pkg.content.exports = { ...exports };\n await pkg.write();\n logSuccess(`✅ ${pkg.repoDirPath} update successful`);\n } catch (error) {\n logInfo(error);\n }\n }\n\n protected hasExportsChanged(\n a: Record<string, string> | undefined,\n b: Record<string, string> | undefined,\n ): boolean {\n if (!a) a = {};\n if (!b) b = {};\n\n if (Object.keys(a).length !== Object.keys(b).length) return true;\n if (!a && !b) return false;\n if (!a || !b) return true;\n\n for (const entryA of Object.entries(a)) {\n if (b[entryA[0]] !== entryA[1]) return true;\n }\n return false;\n }\n\n protected extractExports(result: BarrelResult): Record<string, string> {\n const record: Record<string, string> = {};\n for (const barrelDir of result.barrelDirs) {\n const ex = relative(result.pkg.srcPath, barrelDir);\n record[`./${ex}`] = `./dist/${ex}/index.js`;\n }\n record[\"./package.json\"] = \"./package.json\";\n return record;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"Barrel.js","sourceRoot":"","sources":["../../src/barrel/Barrel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAM9D,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAErE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAG7D,sDAAsD;AACtD,MAAM,CAAC,MAAM,yBAAyB,GAAa;IACjD,aAAa;IACb,aAAa;IACb,uBAAuB;IACvB,UAAU;IACV,YAAY;IACZ,yCAAyC;IACzC,kBAAkB;CACnB,CAAC;AAEF,6DAA6D;AAC7D,MAAM,CAAC,MAAM,cAAc,GACzB,yDAAyD,CAAC;AAE5D,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAAE,MAAoB,EAAE,EAAE;IACtD,aAAa,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC/B,OAAO,IAAI,MAAM,CACf,MAAM,CAAC,QAAQ,IAAI,yBAAyB,EAC5C,M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C,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;YACnD,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,UAAU,EAAE,WAAW,CAAC;QAC9C,CAAC;QACD,MAAM,CAAC,gBAAgB,CAAC,GAAG,gBAAgB,CAAC;QAC5C,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import { basename, dirname, join, relative } from \"node:path\";\nimport {\n BarrelGroup,\n BarrelParams,\n BarrelResult,\n} from \"../types/types.barrel.js\";\nimport { readdir, rm, stat, writeFile } from \"node:fs/promises\";\nimport { logInfo, logSuccess } from \"../logger/Logger.js\";\nimport { initWorkspace, workspace } from \"../workspace/Workspace.js\";\nimport { Pkg } from \"../pkg/Pkg.js\";\nimport { formatCode } from \"../code/code.format.js\";\nimport { fileExports } from \"../code/code.exports.js\";\nimport { createSourceFileMap } from \"../code/code.common.js\";\nimport { SourceFile } from \"../types/types.code.js\";\n\n// /** File patterns to exclude from barrel exports */\nexport const DEFAULT_EXCLUDED_PATTERNS: RegExp[] = [\n /\\.test\\.ts$/,\n /\\.spec\\.ts$/,\n /(^|\\/)__tests__(\\/|$)/,\n /\\.d\\.ts$/,\n /index\\.ts$/,\n /(.+\\/|^)src\\/(.+\\/|)scripts?\\/.+\\.m?ts$/,\n /\\/4testing(\\/|$)/,\n];\n\n// /** Header comment added to every generated barrel file */\nexport const DEFAULT_HEADER =\n \"// This file is auto-generated. Do not edit manually.\\n\";\n\nexport const runBarrel = async (params: BarrelParams) => {\n initWorkspace(params.repoRoot);\n return new Barrel(\n params.excludes ?? DEFAULT_EXCLUDED_PATTERNS,\n params.fileHeader ?? DEFAULT_HEADER,\n ).run();\n};\n\nexport class Barrel {\n public constructor(\n protected readonly excludes: RegExp[],\n protected readonly fileHeader: string,\n ) {}\n\n public async run(): Promise<void> {\n const ws = workspace();\n await ws.read();\n\n // Phase 1: collect all barrel groups, keyed by package name\n const groupsByPkg = new Map<string, BarrelGroup[]>();\n\n for (const pkg of ws.packages) {\n try {\n const items = (await readdir(pkg.srcPath, { recursive: true })).sort();\n let currentGroup: BarrelGroup | undefined;\n\n const pushGroup = (g: BarrelGroup) => {\n const key = g.pkg.content.name;\n if (!groupsByPkg.has(key)) groupsByPkg.set(key, []);\n groupsByPkg.get(key)!.push({ ...g });\n };\n\n for (const item of items) {\n const filename = basename(item);\n const dirPath = join(pkg.srcPath, dirname(item));\n const filePath = join(dirPath, filename);\n\n if (dirPath !== currentGroup?.dirPath) {\n if (currentGroup) pushGroup(currentGroup);\n currentGroup = { dirPath, files: [], pkg };\n }\n\n if (\n this.isExcluded(filePath) ||\n (!filename.endsWith(\".ts\") && !filename.endsWith(\".mts\"))\n )\n continue;\n\n currentGroup.files.push(filename);\n }\n\n if (currentGroup) pushGroup(currentGroup);\n } catch {\n continue;\n }\n }\n\n // Phase 2: build one ts.Program per package, then persist all groups in parallel\n const promises: Promise<BarrelGroup>[] = [];\n\n for (const groups of groupsByPkg.values()) {\n const allFilePaths = groups.flatMap((g) =>\n g.files.map((f) => join(g.dirPath, f)),\n );\n const sourceFileMap: Map<string, SourceFile> =\n allFilePaths.length > 0\n ? createSourceFileMap(allFilePaths)\n : new Map<string, SourceFile>();\n\n for (const group of groups) {\n promises.push(this.persistBarrel({ ...group }, sourceFileMap));\n }\n }\n\n const barrelGroups = (await Promise.all(promises)).filter(\n (prom) => prom.files.length > 0,\n );\n\n const results: BarrelResult[] = [];\n\n for (const barrelGroup of barrelGroups) {\n let found = results.find(\n (res) => barrelGroup.dirPath === res.pkg.dirPath,\n );\n if (!found) {\n found = {\n barrelDirs: [],\n pkg: barrelGroup.pkg,\n };\n results.push(found);\n }\n\n found.barrelDirs.push(barrelGroup.dirPath);\n }\n\n const pkgExps: Record<string, { pkg: Pkg; exps: Record<string, string> }> =\n {};\n for (const res of results) {\n if (!(res.pkg.content.name in pkgExps))\n pkgExps[res.pkg.content.name] = { pkg: res.pkg, exps: {} };\n\n pkgExps[res.pkg.content.name].exps = {\n ...pkgExps[res.pkg.content.name].exps,\n ...this.extractExports(res),\n };\n }\n\n await Promise.all(\n Object.values(pkgExps).map(async (pkgExp) =>\n this.updatePackageExports(pkgExp.pkg, pkgExp.exps),\n ),\n );\n }\n\n protected isExcluded(fileName: string): boolean {\n return this.excludes.some((pattern) => pattern.test(fileName));\n }\n\n protected async persistBarrel(\n group: BarrelGroup,\n sourceFileMap: Map<string, SourceFile>,\n ): Promise<BarrelGroup> {\n const barrelPath = join(group.dirPath, \"index.ts\");\n // no files --> skip/delete index.ts\n if (group.files.length === 0) {\n await rm(barrelPath, { recursive: true, force: true });\n return group;\n }\n\n // Skip if all source files are older than the existing barrel\n if (!(await this.hasGroupChanged(group, barrelPath))) return group;\n\n const exportLines: string[] = [];\n for (const file of group.files.sort()) {\n const filePath = join(group.dirPath, file);\n\n const expResult = fileExports(\n filePath,\n undefined,\n sourceFileMap.get(filePath),\n );\n if (expResult.items.length === 0) continue;\n\n const exports = expResult.items.map((item) => item.name);\n const moduleRef = `./${basename(file, \".ts\")}.js`;\n\n if (expResult.hasDefault) {\n exportLines.push(`export * from \"${moduleRef}\";`);\n } else if (exports.length > 0) {\n exportLines.push(\n `export { ${exports.join(\", \")} } from \"${moduleRef}\";`,\n );\n }\n }\n\n let content = `${this.fileHeader}${exportLines.length > 0 ? exportLines.join(\"\\n\") + \"\\n\" : \"\"}`;\n content = await formatCode(content);\n await writeFile(barrelPath, content);\n return group;\n }\n\n /**\n * Returns true if any source file in the group is newer than the barrel\n * file, or if the barrel file does not yet exist. File deletions are handled\n * implicitly because the group is rebuilt from readdir on every run.\n */\n protected async hasGroupChanged(\n group: BarrelGroup,\n barrelPath: string,\n ): Promise<boolean> {\n let barrelMtime: number;\n try {\n barrelMtime = (await stat(barrelPath)).mtimeMs;\n } catch {\n return true; // barrel doesn't exist yet\n }\n\n for (const file of group.files) {\n try {\n const fileMtime = (await stat(join(group.dirPath, file))).mtimeMs;\n if (fileMtime > barrelMtime) return true;\n } catch {\n return true; // file accessibility changed\n }\n }\n return false;\n }\n\n // protected async extractExportsFromFile(\n // filePath: string,\n // ): Promise<{ exports: string[]; hasDefault: boolean }> {\n // try {\n // const content = await readFile(filePath, \"utf-8\");\n // const exports: Set<string> = new Set();\n // let hasDefault = false;\n\n // // Detect any default export\n // if (/export\\s+default\\b/.test(content)) hasDefault = true;\n\n // // Match named exports: export const|function|class Name\n // const namedExportPattern =\n // /export\\s+(?:const|let|var|function|class|interface|type|enum)\\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/g;\n // let match;\n // while ((match = namedExportPattern.exec(content)) !== null) {\n // const name = match[1];\n // if (\n // !name.startsWith(\"_\") &&\n // !name.startsWith(\"//\") &&\n // !name.startsWith(\"/*\")\n // )\n // exports.add(name);\n // }\n\n // // Match export { a, b, c } and export { a, b, c } from '...'\n // const exportBracePattern = /export\\s*\\{\\s*([^}]+)\\s*\\}/g;\n // while ((match = exportBracePattern.exec(content)) !== null) {\n // const items = match[1].split(\",\");\n // for (const item of items) {\n // const trimmed = item.trim();\n // if (!trimmed) continue;\n // // Handle \"export { name as alias }\"\n // const parts = trimmed.split(/\\s+as\\s+/);\n // const name = parts[0].trim();\n // const alias = parts[1]?.trim();\n // // Skip default exports (e.g. \"export { Foo as default }\")\n // if (alias === \"default\") {\n // hasDefault = true;\n // continue;\n // }\n // if (name && name !== \"default\" && !name.startsWith(\"_\")) {\n // exports.add(name);\n // }\n // }\n // }\n\n // return { exports: Array.from(exports).sort(), hasDefault };\n // } catch {\n // return { exports: [], hasDefault: false };\n // }\n // }\n\n protected async updatePackageExports(\n pkg: Pkg,\n exports: Record<string, string>,\n ) {\n try {\n // const pkgName = relative(this.paths.workspacesDir, packageDir);\n // // const pkgs = await workspace().loadPackages();\n\n logInfo(`📦 updating ${pkg.repoDirPath} package.json ...`);\n // const pkg = await readPackageJsonThrow(packageJsonPath);\n if (!this.hasExportsChanged(exports, pkg.content.exports)) {\n logInfo(`🧹 No changes in ${pkg.repoDirPath}, nothing to do`);\n return;\n }\n pkg.content.exports = { ...exports };\n await pkg.write();\n logSuccess(`✅ ${pkg.repoDirPath} update successful`);\n } catch (error) {\n logInfo(error);\n }\n }\n\n protected hasExportsChanged(\n a: Record<string, string> | undefined,\n b: Record<string, string> | undefined,\n ): boolean {\n if (!a) a = {};\n if (!b) b = {};\n\n if (Object.keys(a).length !== Object.keys(b).length) return true;\n if (!a && !b) return false;\n if (!a || !b) return true;\n\n for (const entryA of Object.entries(a)) {\n if (b[entryA[0]] !== entryA[1]) return true;\n }\n return false;\n }\n\n protected extractExports(result: BarrelResult): Record<string, string> {\n const record: Record<string, string> = {};\n for (const barrelDir of result.barrelDirs) {\n const ex = relative(result.pkg.srcPath, barrelDir);\n record[`./${ex}`] = `./dist/${ex}/index.js`;\n }\n record[\"./package.json\"] = \"./package.json\";\n return record;\n }\n}\n"]}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { SourceFile } from "../types/types.code.js";
|
|
3
3
|
export declare const createSourceFiles: (filePaths: string[]) => SourceFile[];
|
|
4
|
+
/**
|
|
5
|
+
* Creates a single ts.Program for all provided file paths and returns a map
|
|
6
|
+
* from filePath → SourceFile. This is much more efficient than calling
|
|
7
|
+
* createSourceFiles() per file because the TypeScript compiler is only
|
|
8
|
+
* instantiated once.
|
|
9
|
+
*/
|
|
10
|
+
export declare const createSourceFileMap: (filePaths: string[]) => Map<string, SourceFile>;
|
|
4
11
|
export declare const declarationKindName: (d: ts.Declaration) => string;
|
|
5
12
|
export declare const guessKindFromFlags: (sym: ts.Symbol) => string;
|
|
6
13
|
//# sourceMappingURL=code.common.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code.common.d.ts","sourceRoot":"","sources":["../../src/code/code.common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,eAAO,MAAM,iBAAiB,GAAI,WAAW,MAAM,EAAE,KAAG,UAAU,EAgBjE,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,GAAG,EAAE,CAAC,WAAW,WACjB,CAAC;AAErC,eAAO,MAAM,kBAAkB,GAAI,KAAK,EAAE,CAAC,MAAM,KAAG,MAYnD,CAAC"}
|
|
1
|
+
{"version":3,"file":"code.common.d.ts","sourceRoot":"","sources":["../../src/code/code.common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,eAAO,MAAM,iBAAiB,GAAI,WAAW,MAAM,EAAE,KAAG,UAAU,EAgBjE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,GAC9B,WAAW,MAAM,EAAE,KAClB,GAAG,CAAC,MAAM,EAAE,UAAU,CAcxB,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,GAAG,EAAE,CAAC,WAAW,WACjB,CAAC;AAErC,eAAO,MAAM,kBAAkB,GAAI,KAAK,EAAE,CAAC,MAAM,KAAG,MAYnD,CAAC"}
|
package/dist/code/code.common.js
CHANGED
|
@@ -15,6 +15,28 @@ export const createSourceFiles = (filePaths) => {
|
|
|
15
15
|
}
|
|
16
16
|
return results;
|
|
17
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* Creates a single ts.Program for all provided file paths and returns a map
|
|
20
|
+
* from filePath → SourceFile. This is much more efficient than calling
|
|
21
|
+
* createSourceFiles() per file because the TypeScript compiler is only
|
|
22
|
+
* instantiated once.
|
|
23
|
+
*/
|
|
24
|
+
export const createSourceFileMap = (filePaths) => {
|
|
25
|
+
const program = ts.createProgram(filePaths, {
|
|
26
|
+
target: ts.ScriptTarget.ES2023,
|
|
27
|
+
module: ts.ModuleKind.Node20,
|
|
28
|
+
strict: true,
|
|
29
|
+
});
|
|
30
|
+
const checker = program.getTypeChecker();
|
|
31
|
+
const map = new Map();
|
|
32
|
+
for (const filePath of filePaths) {
|
|
33
|
+
const sourceFile = program.getSourceFile(filePath);
|
|
34
|
+
if (!sourceFile)
|
|
35
|
+
continue;
|
|
36
|
+
map.set(filePath, { sourceFile, filePath, checker });
|
|
37
|
+
}
|
|
38
|
+
return map;
|
|
39
|
+
};
|
|
18
40
|
export const declarationKindName = (d) => ts.SyntaxKind[d.kind] ?? "Unknown";
|
|
19
41
|
export const guessKindFromFlags = (sym) => {
|
|
20
42
|
// Not perfect, but good enough for reporting.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code.common.js","sourceRoot":"","sources":["../../src/code/code.common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAG5B,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,SAAmB,EAAgB,EAAE;IACrE,MAAM,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,SAAS,EAAE;QAC1C,MAAM,EAAE,EAAE,CAAC,YAAY,CAAC,MAAM;QAC9B,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM;QAC5B,MAAM,EAAE,IAAI;KACb,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IACzC,MAAM,OAAO,GAAiB,EAAE,CAAC;IAEjC,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,MAAM,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACnD,IAAI,CAAC,UAAU;YAAE,SAAS;QAC1B,OAAO,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAClD,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAiB,EAAE,EAAE,CACvD,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,SAAS,CAAC;AAErC,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,GAAc,EAAU,EAAE;IAC3D,8CAA8C;IAC9C,MAAM,CAAC,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAC;IACzB,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,QAAQ;QAAE,OAAO,UAAU,CAAC;IACnD,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,KAAK;QAAE,OAAO,OAAO,CAAC;IAC7C,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI;QAAE,OAAO,MAAM,CAAC;IAC3C,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,WAAW;QAAE,OAAO,WAAW,CAAC;IACvD,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,SAAS;QAAE,OAAO,MAAM,CAAC;IAChD,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,SAAS;QAAE,OAAO,WAAW,CAAC;IACrD,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,QAAQ;QAAE,OAAO,UAAU,CAAC;IACnD,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,KAAK;QAAE,OAAO,OAAO,CAAC;IAC7C,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import ts from \"typescript\";\nimport { SourceFile } from \"../types/types.code.js\";\n\nexport const createSourceFiles = (filePaths: string[]): SourceFile[] => {\n const program = ts.createProgram(filePaths, {\n target: ts.ScriptTarget.ES2023,\n module: ts.ModuleKind.Node20,\n strict: true,\n });\n const checker = program.getTypeChecker();\n const results: SourceFile[] = [];\n\n for (const filePath of filePaths) {\n const sourceFile = program.getSourceFile(filePath);\n if (!sourceFile) continue;\n results.push({ sourceFile, filePath, checker });\n }\n\n return results;\n};\n\nexport const declarationKindName = (d: ts.Declaration) =>\n ts.SyntaxKind[d.kind] ?? \"Unknown\";\n\nexport const guessKindFromFlags = (sym: ts.Symbol): string => {\n // Not perfect, but good enough for reporting.\n const f = sym.getFlags();\n if (f & ts.SymbolFlags.Function) return \"function\";\n if (f & ts.SymbolFlags.Class) return \"class\";\n if (f & ts.SymbolFlags.Enum) return \"enum\";\n if (f & ts.SymbolFlags.ValueModule) return \"namespace\";\n if (f & ts.SymbolFlags.TypeAlias) return \"type\";\n if (f & ts.SymbolFlags.Interface) return \"interface\";\n if (f & ts.SymbolFlags.Variable) return \"variable\";\n if (f & ts.SymbolFlags.Alias) return \"alias\";\n return \"unknown\";\n};\n"]}
|
|
1
|
+
{"version":3,"file":"code.common.js","sourceRoot":"","sources":["../../src/code/code.common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAG5B,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,SAAmB,EAAgB,EAAE;IACrE,MAAM,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,SAAS,EAAE;QAC1C,MAAM,EAAE,EAAE,CAAC,YAAY,CAAC,MAAM;QAC9B,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM;QAC5B,MAAM,EAAE,IAAI;KACb,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IACzC,MAAM,OAAO,GAAiB,EAAE,CAAC;IAEjC,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,MAAM,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACnD,IAAI,CAAC,UAAU;YAAE,SAAS;QAC1B,OAAO,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAClD,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,SAAmB,EACM,EAAE;IAC3B,MAAM,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,SAAS,EAAE;QAC1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,EAAE,CAAC,WAAW,CAAC,SAAS;QAAE,OAAO,WAAW,CAAC;IACrD,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,QAAQ;QAAE,OAAO,UAAU,CAAC;IACnD,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,KAAK;QAAE,OAAO,OAAO,CAAC;IAC7C,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import ts from \"typescript\";\nimport { SourceFile } from \"../types/types.code.js\";\n\nexport const createSourceFiles = (filePaths: string[]): SourceFile[] => {\n const program = ts.createProgram(filePaths, {\n target: ts.ScriptTarget.ES2023,\n module: ts.ModuleKind.Node20,\n strict: true,\n });\n const checker = program.getTypeChecker();\n const results: SourceFile[] = [];\n\n for (const filePath of filePaths) {\n const sourceFile = program.getSourceFile(filePath);\n if (!sourceFile) continue;\n results.push({ sourceFile, filePath, checker });\n }\n\n return results;\n};\n\n/**\n * Creates a single ts.Program for all provided file paths and returns a map\n * from filePath → SourceFile. This is much more efficient than calling\n * createSourceFiles() per file because the TypeScript compiler is only\n * instantiated once.\n */\nexport const createSourceFileMap = (\n filePaths: string[],\n): Map<string, SourceFile> => {\n const program = ts.createProgram(filePaths, {\n target: ts.ScriptTarget.ES2023,\n module: ts.ModuleKind.Node20,\n strict: true,\n });\n const checker = program.getTypeChecker();\n const map = new Map<string, SourceFile>();\n for (const filePath of filePaths) {\n const sourceFile = program.getSourceFile(filePath);\n if (!sourceFile) continue;\n map.set(filePath, { sourceFile, filePath, checker });\n }\n return map;\n};\n\nexport const declarationKindName = (d: ts.Declaration) =>\n ts.SyntaxKind[d.kind] ?? \"Unknown\";\n\nexport const guessKindFromFlags = (sym: ts.Symbol): string => {\n // Not perfect, but good enough for reporting.\n const f = sym.getFlags();\n if (f & ts.SymbolFlags.Function) return \"function\";\n if (f & ts.SymbolFlags.Class) return \"class\";\n if (f & ts.SymbolFlags.Enum) return \"enum\";\n if (f & ts.SymbolFlags.ValueModule) return \"namespace\";\n if (f & ts.SymbolFlags.TypeAlias) return \"type\";\n if (f & ts.SymbolFlags.Interface) return \"interface\";\n if (f & ts.SymbolFlags.Variable) return \"variable\";\n if (f & ts.SymbolFlags.Alias) return \"alias\";\n return \"unknown\";\n};\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { SourceExport } from "../types/types.code.js";
|
|
1
|
+
import { SourceExport, SourceFile } from "../types/types.code.js";
|
|
2
2
|
export declare const fileExports: (filePath: string, opts?: {
|
|
3
3
|
includeTypeOnly?: boolean;
|
|
4
4
|
includeReExports?: boolean;
|
|
5
5
|
includeDefault?: boolean;
|
|
6
6
|
includePrivate?: boolean;
|
|
7
|
-
}) => SourceExport;
|
|
7
|
+
}, preBuiltSource?: SourceFile) => SourceExport;
|
|
8
8
|
//# sourceMappingURL=code.exports.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code.exports.d.ts","sourceRoot":"","sources":["../../src/code/code.exports.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"code.exports.d.ts","sourceRoot":"","sources":["../../src/code/code.exports.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAQlE,eAAO,MAAM,WAAW,GACtB,UAAU,MAAM,EAChB,OAAO;IACL,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,EACD,iBAAiB,UAAU,KAC1B,YA2EF,CAAC"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { createSourceFiles, declarationKindName, guessKindFromFlags, } from "./code.common.js";
|
|
4
|
-
export const fileExports = (filePath, opts) => {
|
|
4
|
+
export const fileExports = (filePath, opts, preBuiltSource) => {
|
|
5
5
|
const includeTypeOnly = opts?.includeTypeOnly ?? true;
|
|
6
6
|
const includeReExports = opts?.includeReExports ?? true;
|
|
7
7
|
const abs = resolve(filePath);
|
|
8
|
-
const files =
|
|
8
|
+
const files = preBuiltSource
|
|
9
|
+
? [preBuiltSource]
|
|
10
|
+
: createSourceFiles([filePath]);
|
|
9
11
|
if (files.length === 0)
|
|
10
12
|
throw new Error(`Could not load source file: ${abs}`);
|
|
11
13
|
const { checker, sourceFile } = files[0];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code.exports.js","sourceRoot":"","sources":["../../src/code/code.exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAE1B,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,QAAgB,EAChB,IAKC,
|
|
1
|
+
{"version":3,"file":"code.exports.js","sourceRoot":"","sources":["../../src/code/code.exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAE1B,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,QAAgB,EAChB,IAKC,EACD,cAA2B,EACb,EAAE;IAChB,MAAM,eAAe,GAAG,IAAI,EAAE,eAAe,IAAI,IAAI,CAAC;IACtD,MAAM,gBAAgB,GAAG,IAAI,EAAE,gBAAgB,IAAI,IAAI,CAAC;IACxD,MAAM,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAE9B,MAAM,KAAK,GAAG,cAAc;Q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ClB,EAAE,CAAC,WAAW,CAAC,SAAS;oBACxB,EAAE,CAAC,WAAW,CAAC,SAAS,CAAC,CAC5B,CAAC;QAER,IAAI,CAAC,eAAe,IAAI,UAAU;YAAE,SAAS;QAE7C,oEAAoE;QACpE,IAAI,UAAU,GAAG,KAAK,CAAC;QACvB,IAAI,QAAQ,GAAG,GAAG,CAAC;QACnB,IAAI,GAAG,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;YAC1C,MAAM,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;YAC9C,IAAI,OAAO,IAAI,OAAO,KAAK,GAAG,EAAE,CAAC;gBAC/B,UAAU,GAAG,IAAI,CAAC;gBAClB,QAAQ,GAAG,OAAO,CAAC;YACrB,CAAC;QACH,CAAC;QAED,IAAI,CAAC,gBAAgB,IAAI,UAAU;YAAE,SAAS;QAE9C,MAAM,KAAK,GAAG,CAAC,QAAQ,CAAC,eAAe,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC;QAE1E,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;YAChB,IAAI;YACJ,IAAI,EAAE,kBAAkB,CAAC,QAAQ,CAAC;YAClC,UAAU;YACV,UAAU;YACV,YAAY,EAAE,KAAK;SACpB,CAAC,CAAC;IACL,CAAC;IAED,oBAAoB;IACpB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1D,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["import ts from \"typescript\";\nimport { SourceExport, SourceFile } from \"../types/types.code.js\";\nimport { resolve } from \"node:path\";\nimport {\n createSourceFiles,\n declarationKindName,\n guessKindFromFlags,\n} from \"./code.common.js\";\n\nexport const fileExports = (\n filePath: string,\n opts?: {\n includeTypeOnly?: boolean;\n includeReExports?: boolean;\n includeDefault?: boolean;\n includePrivate?: boolean;\n },\n preBuiltSource?: SourceFile,\n): SourceExport => {\n const includeTypeOnly = opts?.includeTypeOnly ?? true;\n const includeReExports = opts?.includeReExports ?? true;\n const abs = resolve(filePath);\n\n const files = preBuiltSource\n ? [preBuiltSource]\n : createSourceFiles([filePath]);\n if (files.length === 0) throw new Error(`Could not load source file: ${abs}`);\n\n const { checker, sourceFile } = files[0];\n\n const result: SourceExport = {\n items: [],\n hasDefault: false,\n };\n\n const moduleSymbol = checker.getSymbolAtLocation(sourceFile);\n if (!moduleSymbol) return result;\n\n // This gives the \"export surface\" of the module (including re-exports).\n const exports = checker.getExportsOfModule(moduleSymbol);\n\n for (const sym of exports) {\n const name = sym.getName();\n if (name === \"default\") {\n result.hasDefault = true;\n if (!opts?.includeDefault) continue;\n } else if (name.startsWith(\"_\")) {\n if (!opts?.includePrivate) continue;\n }\n\n // Determine if it's type-only (TypeScript 5.x provides isTypeOnlySymbol).\n // Fallback: approximate using flags.\n const isTypeOnly =\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n typeof (checker as any).isTypeOnlySymbol === \"function\"\n ? // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ((checker as any).isTypeOnlySymbol(sym) as boolean)\n : !!(\n sym.getFlags() &\n (ts.SymbolFlags.Type |\n ts.SymbolFlags.Interface |\n ts.SymbolFlags.TypeAlias)\n );\n\n if (!includeTypeOnly && isTypeOnly) continue;\n\n // Re-export detection: if symbol is an alias and resolves elsewhere\n let isReExport = false;\n let resolved = sym;\n if (sym.getFlags() & ts.SymbolFlags.Alias) {\n const aliased = checker.getAliasedSymbol(sym);\n if (aliased && aliased !== sym) {\n isReExport = true;\n resolved = aliased;\n }\n }\n\n if (!includeReExports && isReExport) continue;\n\n const decls = (resolved.getDeclarations() ?? []).map(declarationKindName);\n\n result.items.push({\n name,\n kind: guessKindFromFlags(resolved),\n isTypeOnly,\n isReExport,\n declarations: decls,\n });\n }\n\n // stable-ish output\n result.items.sort((a, b) => a.name.localeCompare(b.name));\n return result;\n};\n"]}
|