@boperators/cli 0.1.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/README.md +68 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +177 -0
- package/license.txt +8 -0
- package/package.json +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @boperators/cli
|
|
2
|
+
|
|
3
|
+
CLI tool for [boperators](https://www.npmjs.com/package/boperators) - transforms TypeScript files with operator overloads.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install -D boperators @boperators/cli
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
The CLI provides two subcommands via `boperate` (or the `bop` alias).
|
|
14
|
+
|
|
15
|
+
### `boperate compile`
|
|
16
|
+
|
|
17
|
+
Injects operator overloads into your TypeScript files and emits JavaScript using your `tsconfig.json` settings.
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
# Compile to JavaScript (output goes to tsconfig's outDir)
|
|
21
|
+
boperate compile
|
|
22
|
+
|
|
23
|
+
# Use a specific tsconfig
|
|
24
|
+
boperate compile --project ./tsconfig.custom.json
|
|
25
|
+
|
|
26
|
+
# Also save the transformed TypeScript (e.g. to feed into another build tool)
|
|
27
|
+
boperate compile --ts-out ./transformed
|
|
28
|
+
|
|
29
|
+
# Only produce transformed TypeScript, skip JavaScript output
|
|
30
|
+
boperate compile --ts-out ./transformed --no-emit
|
|
31
|
+
|
|
32
|
+
# Also write source map files
|
|
33
|
+
boperate compile --ts-out ./transformed --maps-out ./maps
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The CLI reads your `tsconfig.json` to determine which files to process and where to emit output. It parses overload definitions from all files (including dependencies), but only transforms and emits files within your project directory.
|
|
37
|
+
|
|
38
|
+
| Option | Alias | Default | Description |
|
|
39
|
+
|--------|-------|---------|-------------|
|
|
40
|
+
| `--ts-out <dir>` | `-t` | | Save the transformed TypeScript files to this directory (useful for further build tooling) |
|
|
41
|
+
| `--maps-out <dir>` | `-m` | | Output directory for source map files (`.map.ts`) |
|
|
42
|
+
| `--project <path>` | `-p` | `tsconfig.json` | Path to tsconfig.json to use |
|
|
43
|
+
| `--no-emit` | | | Skip JavaScript output |
|
|
44
|
+
| `--error-on-warning` | | `false` | Treat conflicting overload warnings as errors |
|
|
45
|
+
|
|
46
|
+
### `boperate validate`
|
|
47
|
+
|
|
48
|
+
Validates that all classes with operator overloads are properly exported. Intended for library authors to run before publishing.
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
# Level 1 — check every overload class is exported from its source file
|
|
52
|
+
boperate validate
|
|
53
|
+
|
|
54
|
+
# Level 2 — also verify each class is reachable via your package entry point
|
|
55
|
+
boperate validate --entry src/index.ts
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Exit code is 1 on violations (suitable for CI / prepublish scripts). Use `--warn` to emit warnings instead of errors.
|
|
59
|
+
|
|
60
|
+
| Option | Default | Description |
|
|
61
|
+
|--------|---------|-------------|
|
|
62
|
+
| `--entry <path>` | | Source entry point for deep reachability check (e.g. `src/index.ts`) |
|
|
63
|
+
| `--project <path>` | `tsconfig.json` | Path to tsconfig.json to use |
|
|
64
|
+
| `--warn` | `false` | Emit warnings instead of exiting with an error on violations |
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { Command } from "@commander-js/extra-typings";
|
|
6
|
+
import { ErrorManager, loadConfig, OverloadInjector, OverloadStore, Project as TsMorphProject, validateExports, } from "boperators";
|
|
7
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
const packageJsonPath = path.join(__dirname, "..", "package.json");
|
|
9
|
+
const packageJson = fs.readFileSync(packageJsonPath, "utf-8");
|
|
10
|
+
const { version } = JSON.parse(packageJson);
|
|
11
|
+
/** Resolve tsconfig path relative to cwd if not absolute. */
|
|
12
|
+
function resolveTsConfig(project) {
|
|
13
|
+
const p = path.isAbsolute(project)
|
|
14
|
+
? project
|
|
15
|
+
: path.join(process.cwd(), project);
|
|
16
|
+
if (!fs.existsSync(p)) {
|
|
17
|
+
throw new Error(`Unable to find tsconfig file at "${p}".`);
|
|
18
|
+
}
|
|
19
|
+
return p;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Compute the common ancestor directory of a set of file paths — this matches
|
|
23
|
+
* what TypeScript does when `rootDir` is not specified in tsconfig.
|
|
24
|
+
*/
|
|
25
|
+
function computeCommonSourceDirectory(filePaths, fallback) {
|
|
26
|
+
if (filePaths.length === 0)
|
|
27
|
+
return fallback;
|
|
28
|
+
const dirs = filePaths.map((p) => path.dirname(p.replaceAll("\\", "/")));
|
|
29
|
+
const segments = dirs.map((d) => d.split("/"));
|
|
30
|
+
const first = segments[0];
|
|
31
|
+
let commonLength = first.length;
|
|
32
|
+
for (const seg of segments.slice(1)) {
|
|
33
|
+
let i = 0;
|
|
34
|
+
while (i < commonLength && i < seg.length && first[i] === seg[i]) {
|
|
35
|
+
i++;
|
|
36
|
+
}
|
|
37
|
+
commonLength = i;
|
|
38
|
+
}
|
|
39
|
+
return first.slice(0, commonLength).join("/") || "/";
|
|
40
|
+
}
|
|
41
|
+
const program = new Command()
|
|
42
|
+
.name("boperate")
|
|
43
|
+
.description("Parse operator overloads and inject them into TypeScript files.")
|
|
44
|
+
.version(version);
|
|
45
|
+
// ---------- compile ----------
|
|
46
|
+
program
|
|
47
|
+
.command("compile")
|
|
48
|
+
.description("Inject operator overloads into TypeScript files and emit JavaScript.")
|
|
49
|
+
.option("-t, --ts-out <dir>", "Output directory for the transformed TypeScript files (useful if you want to run another build tool over them afterwards).")
|
|
50
|
+
.option("-p, --project <path>", "Path to tsconfig.json to use.", "tsconfig.json")
|
|
51
|
+
.option("-m, --maps-out <dir>", "Output directory for source map files (.map.ts).")
|
|
52
|
+
.option("--no-emit", "Skip JavaScript output. Useful when combined with --ts-out to only produce transformed TypeScript.")
|
|
53
|
+
.option("--error-on-warning", "Instead of showing a warning, error on conflicting overloads.", false)
|
|
54
|
+
.action((options) => {
|
|
55
|
+
const tsConfigFilePath = resolveTsConfig(options.project);
|
|
56
|
+
const config = loadConfig({
|
|
57
|
+
searchDir: path.dirname(tsConfigFilePath),
|
|
58
|
+
overrides: { errorOnWarning: options.errorOnWarning },
|
|
59
|
+
});
|
|
60
|
+
const project = new TsMorphProject({ tsConfigFilePath });
|
|
61
|
+
const errorManager = new ErrorManager(config);
|
|
62
|
+
const overloadStore = new OverloadStore(project, errorManager, config.logger);
|
|
63
|
+
const overloadInjector = new OverloadInjector(project, overloadStore, config.logger);
|
|
64
|
+
const allFiles = project.getSourceFiles();
|
|
65
|
+
// Only transform and write files within the user's project directory
|
|
66
|
+
// ts-morph normalizes paths to forward slashes, so we must match that
|
|
67
|
+
const projectDir = `${path.dirname(tsConfigFilePath).replaceAll("\\", "/")}/`;
|
|
68
|
+
const projectFiles = allFiles.filter((file) => file.getFilePath().startsWith(projectDir));
|
|
69
|
+
// Parse ALL files for overload definitions (including library dependencies)
|
|
70
|
+
allFiles.forEach((file) => {
|
|
71
|
+
overloadStore.addOverloadsFromFile(file);
|
|
72
|
+
});
|
|
73
|
+
errorManager.throwIfErrorsElseLogWarnings();
|
|
74
|
+
// Only transform files belonging to the user's project
|
|
75
|
+
const transformResults = projectFiles.map((file) => overloadInjector.overloadFile(file));
|
|
76
|
+
errorManager.throwIfErrorsElseLogWarnings();
|
|
77
|
+
// Emit JavaScript to the tsconfig outDir by default
|
|
78
|
+
if (options.emit) {
|
|
79
|
+
project.emit();
|
|
80
|
+
}
|
|
81
|
+
if (options.tsOut) {
|
|
82
|
+
const tsOutDir = path.isAbsolute(options.tsOut)
|
|
83
|
+
? options.tsOut
|
|
84
|
+
: path.join(process.cwd(), options.tsOut);
|
|
85
|
+
if (!fs.existsSync(tsOutDir)) {
|
|
86
|
+
fs.mkdirSync(tsOutDir, { recursive: true });
|
|
87
|
+
}
|
|
88
|
+
const compilerOptions = project.getCompilerOptions();
|
|
89
|
+
const rootDir = compilerOptions.rootDir
|
|
90
|
+
? path.resolve(path.dirname(tsConfigFilePath), compilerOptions.rootDir)
|
|
91
|
+
: computeCommonSourceDirectory(projectFiles.map((f) => f.getFilePath()), path.dirname(tsConfigFilePath));
|
|
92
|
+
transformResults.forEach((result) => {
|
|
93
|
+
const relativePath = path.relative(rootDir, result.sourceFile.getFilePath());
|
|
94
|
+
const outPath = path.join(tsOutDir, relativePath);
|
|
95
|
+
const outDir = path.dirname(outPath);
|
|
96
|
+
if (!fs.existsSync(outDir)) {
|
|
97
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
98
|
+
}
|
|
99
|
+
fs.writeFileSync(outPath, result.text);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
if (options.mapsOut) {
|
|
103
|
+
const mapsOutDir = path.isAbsolute(options.mapsOut)
|
|
104
|
+
? options.mapsOut
|
|
105
|
+
: path.join(process.cwd(), options.mapsOut);
|
|
106
|
+
if (!fs.existsSync(mapsOutDir)) {
|
|
107
|
+
fs.mkdirSync(mapsOutDir, { recursive: true });
|
|
108
|
+
}
|
|
109
|
+
const compilerOptions = project.getCompilerOptions();
|
|
110
|
+
const rootDir = compilerOptions.rootDir
|
|
111
|
+
? path.resolve(path.dirname(tsConfigFilePath), compilerOptions.rootDir)
|
|
112
|
+
: computeCommonSourceDirectory(projectFiles.map((f) => f.getFilePath()), path.dirname(tsConfigFilePath));
|
|
113
|
+
transformResults.forEach((result) => {
|
|
114
|
+
const relativePath = path.relative(rootDir, result.sourceFile.getFilePath());
|
|
115
|
+
const mapFileName = relativePath.replace(/\.ts$/, ".map.ts");
|
|
116
|
+
const outPath = path.join(mapsOutDir, mapFileName);
|
|
117
|
+
const outDir = path.dirname(outPath);
|
|
118
|
+
if (!fs.existsSync(outDir)) {
|
|
119
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
120
|
+
}
|
|
121
|
+
fs.writeFileSync(outPath, JSON.stringify(result.sourceMap.edits, null, 2));
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
// ---------- validate ----------
|
|
126
|
+
program
|
|
127
|
+
.command("validate")
|
|
128
|
+
.description("Validate that all classes with operator overloads are properly exported. " +
|
|
129
|
+
"Intended for library authors to run before publishing.")
|
|
130
|
+
.option("-p, --project <path>", "Path to tsconfig.json to use.", "tsconfig.json")
|
|
131
|
+
.option("--entry <path>", "Source entry point for deep reachability check (e.g. src/index.ts). " +
|
|
132
|
+
"When provided, validates that every overload class is transitively " +
|
|
133
|
+
"reachable via the entry file's export graph.")
|
|
134
|
+
.option("--warn", "Emit warnings instead of exiting with an error on violations.", false)
|
|
135
|
+
.action((options) => {
|
|
136
|
+
const tsConfigFilePath = resolveTsConfig(options.project);
|
|
137
|
+
const projectDir = path.dirname(tsConfigFilePath);
|
|
138
|
+
const config = loadConfig({ searchDir: projectDir });
|
|
139
|
+
const project = new TsMorphProject({ tsConfigFilePath });
|
|
140
|
+
const errorManager = new ErrorManager(config);
|
|
141
|
+
const overloadStore = new OverloadStore(project, errorManager, config.logger);
|
|
142
|
+
// Scan all files for overload definitions
|
|
143
|
+
project.getSourceFiles().forEach((file) => {
|
|
144
|
+
overloadStore.addOverloadsFromFile(file);
|
|
145
|
+
});
|
|
146
|
+
const entryPoint = options.entry
|
|
147
|
+
? path.isAbsolute(options.entry)
|
|
148
|
+
? options.entry
|
|
149
|
+
: path.join(process.cwd(), options.entry)
|
|
150
|
+
: undefined;
|
|
151
|
+
const result = validateExports({
|
|
152
|
+
project,
|
|
153
|
+
overloadStore,
|
|
154
|
+
projectDir,
|
|
155
|
+
entryPoint,
|
|
156
|
+
});
|
|
157
|
+
if (result.violations.length === 0) {
|
|
158
|
+
console.log("[boperators] All overload classes are properly exported.");
|
|
159
|
+
process.exit(0);
|
|
160
|
+
}
|
|
161
|
+
for (const violation of result.violations) {
|
|
162
|
+
const relPath = path.relative(projectDir, violation.classFilePath);
|
|
163
|
+
const message = violation.reason === "not-exported-from-file"
|
|
164
|
+
? `${violation.className} (${relPath}): not exported from its source file — add 'export' to the class declaration`
|
|
165
|
+
: `${violation.className} (${relPath}): not reachable from entry point — ensure it is re-exported from ${options.entry}`;
|
|
166
|
+
if (options.warn) {
|
|
167
|
+
console.warn(`[boperators] WARNING: ${message}`);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
console.error(`[boperators] ERROR: ${message}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (!options.warn) {
|
|
174
|
+
process.exit(1);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
program.parse(process.argv);
|
package/license.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2025 Dief Bell
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@boperators/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "CLI tool for boperators - transforms TypeScript files with operator overloads.",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/DiefBell/boperators",
|
|
9
|
+
"directory": "cli"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/DiefBell/boperators/tree/main/cli",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"bin": {
|
|
14
|
+
"bop": "dist/index.js",
|
|
15
|
+
"boperate": "dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"boperators",
|
|
19
|
+
"typescript",
|
|
20
|
+
"operator",
|
|
21
|
+
"overload",
|
|
22
|
+
"operator-overloading",
|
|
23
|
+
"cli",
|
|
24
|
+
"transform"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc",
|
|
28
|
+
"watch": "tsc -w",
|
|
29
|
+
"prepublish": "bun run build"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"package.json",
|
|
33
|
+
"README.md",
|
|
34
|
+
"license.txt",
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@commander-js/extra-typings": "^13.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"boperators": "0.1.0",
|
|
42
|
+
"typescript": ">=5.0.0 <5.10.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"boperators": "file:../package"
|
|
46
|
+
}
|
|
47
|
+
}
|