@anvil-di/anvil-cli 0.0.2
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 +201 -0
- package/README.md +79 -0
- package/bin/anvil.js +26 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +123 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -0
- package/src/index.test.ts +114 -0
- package/src/index.ts +126 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 typescript-di contributors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# `@msulak/anvil-cli`
|
|
2
|
+
|
|
3
|
+
Native binary launcher for [`@msulak/anvil`](https://github.com/msulak13/tsdi) — installs the right prebuilt CLI for your platform, exposes it as an `anvil` command, and provides a programmatic API for tooling that needs to invoke codegen (like [`@msulak/anvil-unplugin`](https://www.npmjs.com/package/@msulak/anvil-unplugin)).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -D @msulak/anvil-cli # adds the launcher + npm picks one platform package
|
|
9
|
+
pnpm add -D @msulak/anvil-cli # pnpm honors optionalDependencies by default
|
|
10
|
+
yarn add -D @msulak/anvil-cli # yarn honors optionalDependencies by default
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The `@msulak/anvil-cli` package itself ships zero binaries. Its `optionalDependencies` field lists every supported platform; npm's `os`/`cpu` filtering installs **exactly one** of them (the one matching your machine). The CLI binary lives inside that platform package.
|
|
14
|
+
|
|
15
|
+
## Use as a command
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npx anvil build --entry src/app-component.ts
|
|
19
|
+
npx anvil check --entry src/app-component.ts
|
|
20
|
+
npx anvil watch --entry src/app-component.ts
|
|
21
|
+
npx anvil explain --entry src/app-component.ts
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The `anvil` shim in `node_modules/.bin` resolves the per-platform package, then `spawnSync`s the real binary with your argv pass-through.
|
|
25
|
+
|
|
26
|
+
## Use programmatically
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { resolveBinaryPath, unresolvableBinaryError } from "@msulak/anvil-cli";
|
|
30
|
+
|
|
31
|
+
const binary = resolveBinaryPath();
|
|
32
|
+
if (binary === null) {
|
|
33
|
+
throw new Error(unresolvableBinaryError());
|
|
34
|
+
}
|
|
35
|
+
// `binary` is an absolute path you can pass to execFile / spawn.
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
This is what `@msulak/anvil-unplugin` uses by default. Any tooling that needs the same "find my platform's binary" lookup can use the same API.
|
|
39
|
+
|
|
40
|
+
## Resolution order
|
|
41
|
+
|
|
42
|
+
`resolveBinaryPath()` checks, in order:
|
|
43
|
+
|
|
44
|
+
1. **`ANVIL_CLI_BIN` env var** — if it points at an existing file, that file wins. Useful for tests, monorepo dev (point at `target/release/anvil`), and CI that builds from source.
|
|
45
|
+
2. **`require.resolve("@msulak/anvil-cli-<platform>-<arch>")`** — the per-platform npm package corresponding to the current Node process.
|
|
46
|
+
|
|
47
|
+
If neither resolves, the function returns `null` and `unresolvableBinaryError()` produces a human-readable message diagnosing why (unsupported platform, optional deps skipped during install, etc.).
|
|
48
|
+
|
|
49
|
+
## Supported platforms
|
|
50
|
+
|
|
51
|
+
| Platform | Arch | Package |
|
|
52
|
+
|---|---|---|
|
|
53
|
+
| Linux | x64 | `@msulak/anvil-cli-linux-x64` |
|
|
54
|
+
| Linux | arm64 | `@msulak/anvil-cli-linux-arm64` |
|
|
55
|
+
| macOS | x64 (Intel) | `@msulak/anvil-cli-darwin-x64` |
|
|
56
|
+
| macOS | arm64 (Apple Silicon) | `@msulak/anvil-cli-darwin-arm64` |
|
|
57
|
+
| Windows | x64 | `@msulak/anvil-cli-win32-x64` |
|
|
58
|
+
|
|
59
|
+
Need another? Open an issue, or build from source:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
git clone https://github.com/msulak13/tsdi
|
|
63
|
+
cd tsdi
|
|
64
|
+
cargo build --release -p anvil-cli
|
|
65
|
+
export ANVIL_CLI_BIN=$PWD/target/release/anvil
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## How releases work
|
|
69
|
+
|
|
70
|
+
The repo's [`release-cli.yml` GitHub Actions workflow](https://github.com/msulak13/tsdi/blob/main/.github/workflows/release-cli.yml) builds the binary on each native runner (linux-x64, linux-arm64 via cross, macos-13, macos-14, windows-latest) and publishes the matching `@msulak/anvil-cli-<platform>-<arch>` package along with the launcher. The launcher's `optionalDependencies` are pinned to the same version, so a single `npm install @msulak/anvil-cli@<version>` always pulls a coherent set.
|
|
71
|
+
|
|
72
|
+
## Why this layout
|
|
73
|
+
|
|
74
|
+
esbuild popularized this pattern because it sidesteps every alternative's failure modes:
|
|
75
|
+
|
|
76
|
+
- **No `postinstall` build step** — no Rust toolchain on user machines, no opaque "compiling…" delay during `npm install`.
|
|
77
|
+
- **No giant universal binary in the launcher** — `npm install @msulak/anvil-cli` downloads ~4MB, not ~25MB times five.
|
|
78
|
+
- **No conditional logic in the launcher's `package.json`** — npm does the platform filtering for us via `os`/`cpu`.
|
|
79
|
+
- **Hash-pinned reproducibility** — every install gets the exact binary CI built; nothing is recompiled on the user's machine.
|
package/bin/anvil.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Thin launcher. Resolves the per-platform binary via the same
|
|
3
|
+
// logic the programmatic `resolveBinaryPath()` uses, then `spawnSync`s
|
|
4
|
+
// it with the user's argv pass-through.
|
|
5
|
+
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import { resolveBinaryPath, unresolvableBinaryError } from "../dist/index.js";
|
|
8
|
+
|
|
9
|
+
const binary = resolveBinaryPath();
|
|
10
|
+
if (binary === null) {
|
|
11
|
+
process.stderr.write(unresolvableBinaryError() + "\n");
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const result = spawnSync(binary, process.argv.slice(2), {
|
|
16
|
+
stdio: "inherit",
|
|
17
|
+
// Forward all env vars (the Rust CLI consults ANVIL_WATCH_ITERATIONS, etc.)
|
|
18
|
+
env: process.env,
|
|
19
|
+
windowsHide: true,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
if (result.error !== undefined) {
|
|
23
|
+
process.stderr.write(`@anvil-di/anvil-cli: failed to spawn ${binary}: ${result.error.message}\n`);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
process.exit(result.status ?? 1);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** The base name of the binary inside each platform package. */
|
|
2
|
+
declare const BIN_NAME: string;
|
|
3
|
+
/**
|
|
4
|
+
* Resolve the platform-package name for the current Node process.
|
|
5
|
+
* Returns `null` for unsupported (platform, arch) combinations.
|
|
6
|
+
*/
|
|
7
|
+
export declare function platformPackageName(): string | null;
|
|
8
|
+
/**
|
|
9
|
+
* Locate the native `anvil` binary on disk.
|
|
10
|
+
*
|
|
11
|
+
* Resolution order:
|
|
12
|
+
*
|
|
13
|
+
* 1. `ANVIL_CLI_BIN` env var — handy for tests, monorepo dev, and
|
|
14
|
+
* CI where the binary lives outside any npm package (e.g. a
|
|
15
|
+
* `cargo build` artifact).
|
|
16
|
+
* 2. The matching per-platform npm package, located via
|
|
17
|
+
* `require.resolve()` and the `binPath` constant each one
|
|
18
|
+
* exports.
|
|
19
|
+
*
|
|
20
|
+
* Returns `null` if no binary can be found — callers then surface a
|
|
21
|
+
* platform-specific error message to the user.
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveBinaryPath(): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Construct a human-readable error message for the case where no
|
|
26
|
+
* binary could be resolved. Surfaces the active platform/arch so the
|
|
27
|
+
* user can quickly tell whether they need a different prebuilt or
|
|
28
|
+
* whether an `npm install --include=optional` is missing.
|
|
29
|
+
*/
|
|
30
|
+
export declare function unresolvableBinaryError(): string;
|
|
31
|
+
/** Where this `@anvil-di/anvil-cli` package itself is installed (informational). */
|
|
32
|
+
export declare const PACKAGE_DIR: string;
|
|
33
|
+
export { BIN_NAME };
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA+BA,gEAAgE;AAChE,QAAA,MAAM,QAAQ,QAAuD,CAAC;AAEtE;;;GAGG;AACH,wBAAgB,mBAAmB,IAAI,MAAM,GAAG,IAAI,CAMnD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,GAAG,IAAI,CAuBjD;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,IAAI,MAAM,CA4BhD;AAED,oFAAoF;AACpF,eAAO,MAAM,WAAW,QAA+C,CAAC;AAExE,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@anvil-di/anvil-cli` — npm launcher for the native `anvil` Rust binary.
|
|
3
|
+
*
|
|
4
|
+
* The package itself ships **no binary**. At install time, npm
|
|
5
|
+
* resolves exactly one of the per-platform `optionalDependencies`
|
|
6
|
+
* (e.g. `@anvil-di/anvil-cli-linux-x64` or `@anvil-di/anvil-cli-win32-x64`)
|
|
7
|
+
* based on the `os`/`cpu` fields each declares; only that one is fetched.
|
|
8
|
+
* At runtime, `resolveBinaryPath()` dynamically requires the matching
|
|
9
|
+
* platform package and reads the prebuilt binary path from it.
|
|
10
|
+
*
|
|
11
|
+
* This mirrors esbuild's distribution model — the user installs one
|
|
12
|
+
* thing (`npm install @anvil-di/anvil-cli`) and gets the right native
|
|
13
|
+
* binary for their platform without a `postinstall` build step.
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync } from "node:fs";
|
|
16
|
+
import { createRequire } from "node:module";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
19
|
+
const require = createRequire(import.meta.url);
|
|
20
|
+
/**
|
|
21
|
+
* The platform-package suffix mapping. Matches the names declared
|
|
22
|
+
* in this package's `optionalDependencies`.
|
|
23
|
+
*/
|
|
24
|
+
const PLATFORM_SUFFIXES = {
|
|
25
|
+
darwin: { arm64: "darwin-arm64", x64: "darwin-x64" },
|
|
26
|
+
linux: { arm64: "linux-arm64", x64: "linux-x64" },
|
|
27
|
+
win32: { x64: "win32-x64" },
|
|
28
|
+
};
|
|
29
|
+
/** The base name of the binary inside each platform package. */
|
|
30
|
+
const BIN_NAME = process.platform === "win32" ? "anvil.exe" : "anvil";
|
|
31
|
+
/**
|
|
32
|
+
* Resolve the platform-package name for the current Node process.
|
|
33
|
+
* Returns `null` for unsupported (platform, arch) combinations.
|
|
34
|
+
*/
|
|
35
|
+
export function platformPackageName() {
|
|
36
|
+
const arches = PLATFORM_SUFFIXES[process.platform];
|
|
37
|
+
if (arches === undefined)
|
|
38
|
+
return null;
|
|
39
|
+
const suffix = arches[process.arch];
|
|
40
|
+
if (suffix === undefined)
|
|
41
|
+
return null;
|
|
42
|
+
return `@anvil-di/anvil-cli-${suffix}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Locate the native `anvil` binary on disk.
|
|
46
|
+
*
|
|
47
|
+
* Resolution order:
|
|
48
|
+
*
|
|
49
|
+
* 1. `ANVIL_CLI_BIN` env var — handy for tests, monorepo dev, and
|
|
50
|
+
* CI where the binary lives outside any npm package (e.g. a
|
|
51
|
+
* `cargo build` artifact).
|
|
52
|
+
* 2. The matching per-platform npm package, located via
|
|
53
|
+
* `require.resolve()` and the `binPath` constant each one
|
|
54
|
+
* exports.
|
|
55
|
+
*
|
|
56
|
+
* Returns `null` if no binary can be found — callers then surface a
|
|
57
|
+
* platform-specific error message to the user.
|
|
58
|
+
*/
|
|
59
|
+
export function resolveBinaryPath() {
|
|
60
|
+
// 1. Explicit override.
|
|
61
|
+
const override = process.env.ANVIL_CLI_BIN;
|
|
62
|
+
if (override !== undefined && existsSync(override)) {
|
|
63
|
+
return override;
|
|
64
|
+
}
|
|
65
|
+
// 2. Per-platform npm package.
|
|
66
|
+
const pkg = platformPackageName();
|
|
67
|
+
if (pkg === null) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
let installed;
|
|
71
|
+
try {
|
|
72
|
+
installed = require(pkg);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// npm skipped this optional dep (because the platform doesn't
|
|
76
|
+
// match) or the user hasn't installed deps yet. Fall through.
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
if (typeof installed.binPath !== "string")
|
|
80
|
+
return null;
|
|
81
|
+
if (!existsSync(installed.binPath))
|
|
82
|
+
return null;
|
|
83
|
+
return installed.binPath;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Construct a human-readable error message for the case where no
|
|
87
|
+
* binary could be resolved. Surfaces the active platform/arch so the
|
|
88
|
+
* user can quickly tell whether they need a different prebuilt or
|
|
89
|
+
* whether an `npm install --include=optional` is missing.
|
|
90
|
+
*/
|
|
91
|
+
export function unresolvableBinaryError() {
|
|
92
|
+
const pkg = platformPackageName();
|
|
93
|
+
const platformInfo = `${process.platform}/${process.arch}`;
|
|
94
|
+
if (pkg === null) {
|
|
95
|
+
return [
|
|
96
|
+
`@anvil-di/anvil-cli: no prebuilt binary is available for ${platformInfo}.`,
|
|
97
|
+
"",
|
|
98
|
+
"Supported platforms in v0.2:",
|
|
99
|
+
" - darwin / arm64, darwin / x64",
|
|
100
|
+
" - linux / arm64, linux / x64",
|
|
101
|
+
" - win32 / x64",
|
|
102
|
+
"",
|
|
103
|
+
"If you need another platform, build the Rust crate from source:",
|
|
104
|
+
" git clone https://github.com/msulak13/anvil && cargo build --release -p anvil-cli",
|
|
105
|
+
"and either set ANVIL_CLI_BIN=/path/to/anvil or place the binary on $PATH.",
|
|
106
|
+
].join("\n");
|
|
107
|
+
}
|
|
108
|
+
return [
|
|
109
|
+
`@anvil-di/anvil-cli: couldn't locate the ${BIN_NAME} binary for ${platformInfo}.`,
|
|
110
|
+
"",
|
|
111
|
+
`Expected the npm package "${pkg}" to be installed alongside`,
|
|
112
|
+
"@anvil-di/anvil-cli. If npm skipped optional dependencies, retry the install:",
|
|
113
|
+
" npm install --include=optional",
|
|
114
|
+
" pnpm install # honors optional deps by default",
|
|
115
|
+
" yarn install # honors optional deps by default",
|
|
116
|
+
"",
|
|
117
|
+
"Or set ANVIL_CLI_BIN to a binary you've built yourself.",
|
|
118
|
+
].join("\n");
|
|
119
|
+
}
|
|
120
|
+
/** Where this `@anvil-di/anvil-cli` package itself is installed (informational). */
|
|
121
|
+
export const PACKAGE_DIR = path.dirname(fileURLToPath(import.meta.url));
|
|
122
|
+
export { BIN_NAME };
|
|
123
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anvil-di/anvil-cli",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Native binary launcher for the anvil codegen CLI. Resolves the right per-platform package at runtime; mirrors esbuild's distribution model.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"anvil": "./bin/anvil.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"bin",
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"optionalDependencies": {
|
|
25
|
+
"@anvil-di/anvil-cli-linux-x64": "0.0.2",
|
|
26
|
+
"@anvil-di/anvil-cli-win32-x64": "0.0.2",
|
|
27
|
+
"@anvil-di/anvil-cli-linux-arm64": "0.0.2",
|
|
28
|
+
"@anvil-di/anvil-cli-darwin-x64": "0.0.2",
|
|
29
|
+
"@anvil-di/anvil-cli-darwin-arm64": "0.0.2"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^22.10.0",
|
|
33
|
+
"typescript": "^5.7.2",
|
|
34
|
+
"vitest": "^2.1.8"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.json",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
|
+
"test": "vitest run"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
BIN_NAME,
|
|
9
|
+
platformPackageName,
|
|
10
|
+
resolveBinaryPath,
|
|
11
|
+
unresolvableBinaryError,
|
|
12
|
+
} from "./index.js";
|
|
13
|
+
|
|
14
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
15
|
+
const pkgRoot = path.resolve(here, "..");
|
|
16
|
+
const launcherShim = path.join(pkgRoot, "bin", "anvil.js");
|
|
17
|
+
|
|
18
|
+
describe("platformPackageName", () => {
|
|
19
|
+
it("returns the right package for the current Node process", () => {
|
|
20
|
+
const name = platformPackageName();
|
|
21
|
+
if (process.platform === "win32" && process.arch === "x64") {
|
|
22
|
+
expect(name).toBe("@anvil-di/anvil-cli-win32-x64");
|
|
23
|
+
} else if (process.platform === "darwin" && process.arch === "arm64") {
|
|
24
|
+
expect(name).toBe("@anvil-di/anvil-cli-darwin-arm64");
|
|
25
|
+
} else if (process.platform === "linux" && process.arch === "x64") {
|
|
26
|
+
expect(name).toBe("@anvil-di/anvil-cli-linux-x64");
|
|
27
|
+
} else {
|
|
28
|
+
// For other combinations the test environment isn't in the v0.2
|
|
29
|
+
// matrix; just ensure the function doesn't crash.
|
|
30
|
+
expect(name === null || typeof name === "string").toBe(true);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("resolveBinaryPath", () => {
|
|
36
|
+
it("honors ANVIL_CLI_BIN when it points to an existing file", () => {
|
|
37
|
+
const anvilBinary = path.resolve(
|
|
38
|
+
pkgRoot,
|
|
39
|
+
"..",
|
|
40
|
+
"..",
|
|
41
|
+
"target",
|
|
42
|
+
"release",
|
|
43
|
+
process.platform === "win32" ? "anvil.exe" : "anvil",
|
|
44
|
+
);
|
|
45
|
+
if (!existsSync(anvilBinary)) {
|
|
46
|
+
// No release build available in this environment; skip without
|
|
47
|
+
// marking the suite as failed. (`pnpm -r build` doesn't include
|
|
48
|
+
// a Rust step.)
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const prev = process.env["ANVIL_CLI_BIN"];
|
|
52
|
+
try {
|
|
53
|
+
process.env["ANVIL_CLI_BIN"] = anvilBinary;
|
|
54
|
+
const resolved = resolveBinaryPath();
|
|
55
|
+
expect(resolved).toBe(anvilBinary);
|
|
56
|
+
} finally {
|
|
57
|
+
if (prev === undefined) delete process.env["ANVIL_CLI_BIN"];
|
|
58
|
+
else process.env["ANVIL_CLI_BIN"] = prev;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("returns null when ANVIL_CLI_BIN points to a non-existent path", () => {
|
|
63
|
+
const prev = process.env["ANVIL_CLI_BIN"];
|
|
64
|
+
try {
|
|
65
|
+
process.env["ANVIL_CLI_BIN"] = "/this/definitely/does/not/exist";
|
|
66
|
+
// Without the override, the resolver falls through to the
|
|
67
|
+
// platform package — which may or may not be installed in this
|
|
68
|
+
// environment. We just verify the override-not-found case
|
|
69
|
+
// doesn't throw.
|
|
70
|
+
const resolved = resolveBinaryPath();
|
|
71
|
+
expect(resolved === null || typeof resolved === "string").toBe(true);
|
|
72
|
+
} finally {
|
|
73
|
+
if (prev === undefined) delete process.env["ANVIL_CLI_BIN"];
|
|
74
|
+
else process.env["ANVIL_CLI_BIN"] = prev;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("unresolvableBinaryError", () => {
|
|
80
|
+
it("includes the active platform in the message", () => {
|
|
81
|
+
const msg = unresolvableBinaryError();
|
|
82
|
+
expect(msg).toContain(`${process.platform}/${process.arch}`);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("uses the right binary name on each OS", () => {
|
|
86
|
+
if (process.platform === "win32") {
|
|
87
|
+
expect(BIN_NAME).toBe("anvil.exe");
|
|
88
|
+
} else {
|
|
89
|
+
expect(BIN_NAME).toBe("anvil");
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("bin/anvil.js launcher shim", () => {
|
|
95
|
+
it("forwards args to the resolved binary and pipes its exit code", () => {
|
|
96
|
+
const anvilBinary = path.resolve(
|
|
97
|
+
pkgRoot,
|
|
98
|
+
"..",
|
|
99
|
+
"..",
|
|
100
|
+
"target",
|
|
101
|
+
"release",
|
|
102
|
+
process.platform === "win32" ? "anvil.exe" : "anvil",
|
|
103
|
+
);
|
|
104
|
+
if (!existsSync(anvilBinary)) return; // see resolver test above
|
|
105
|
+
const result = spawnSync(process.execPath, [launcherShim, "--help"], {
|
|
106
|
+
env: { ...process.env, ANVIL_CLI_BIN: anvilBinary },
|
|
107
|
+
encoding: "utf8",
|
|
108
|
+
windowsHide: true,
|
|
109
|
+
});
|
|
110
|
+
// The Rust CLI's --help exits 0 and prints usage info on stdout.
|
|
111
|
+
expect(result.status).toBe(0);
|
|
112
|
+
expect(result.stdout.toLowerCase()).toContain("usage");
|
|
113
|
+
});
|
|
114
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@anvil-di/anvil-cli` — npm launcher for the native `anvil` Rust binary.
|
|
3
|
+
*
|
|
4
|
+
* The package itself ships **no binary**. At install time, npm
|
|
5
|
+
* resolves exactly one of the per-platform `optionalDependencies`
|
|
6
|
+
* (e.g. `@anvil-di/anvil-cli-linux-x64` or `@anvil-di/anvil-cli-win32-x64`)
|
|
7
|
+
* based on the `os`/`cpu` fields each declares; only that one is fetched.
|
|
8
|
+
* At runtime, `resolveBinaryPath()` dynamically requires the matching
|
|
9
|
+
* platform package and reads the prebuilt binary path from it.
|
|
10
|
+
*
|
|
11
|
+
* This mirrors esbuild's distribution model — the user installs one
|
|
12
|
+
* thing (`npm install @anvil-di/anvil-cli`) and gets the right native
|
|
13
|
+
* binary for their platform without a `postinstall` build step.
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync } from "node:fs";
|
|
16
|
+
import { createRequire } from "node:module";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
19
|
+
|
|
20
|
+
const require = createRequire(import.meta.url);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The platform-package suffix mapping. Matches the names declared
|
|
24
|
+
* in this package's `optionalDependencies`.
|
|
25
|
+
*/
|
|
26
|
+
const PLATFORM_SUFFIXES: Record<string, Record<string, string>> = {
|
|
27
|
+
darwin: { arm64: "darwin-arm64", x64: "darwin-x64" },
|
|
28
|
+
linux: { arm64: "linux-arm64", x64: "linux-x64" },
|
|
29
|
+
win32: { x64: "win32-x64" },
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** The base name of the binary inside each platform package. */
|
|
33
|
+
const BIN_NAME = process.platform === "win32" ? "anvil.exe" : "anvil";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resolve the platform-package name for the current Node process.
|
|
37
|
+
* Returns `null` for unsupported (platform, arch) combinations.
|
|
38
|
+
*/
|
|
39
|
+
export function platformPackageName(): string | null {
|
|
40
|
+
const arches = PLATFORM_SUFFIXES[process.platform];
|
|
41
|
+
if (arches === undefined) return null;
|
|
42
|
+
const suffix = arches[process.arch];
|
|
43
|
+
if (suffix === undefined) return null;
|
|
44
|
+
return `@anvil-di/anvil-cli-${suffix}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Locate the native `anvil` binary on disk.
|
|
49
|
+
*
|
|
50
|
+
* Resolution order:
|
|
51
|
+
*
|
|
52
|
+
* 1. `ANVIL_CLI_BIN` env var — handy for tests, monorepo dev, and
|
|
53
|
+
* CI where the binary lives outside any npm package (e.g. a
|
|
54
|
+
* `cargo build` artifact).
|
|
55
|
+
* 2. The matching per-platform npm package, located via
|
|
56
|
+
* `require.resolve()` and the `binPath` constant each one
|
|
57
|
+
* exports.
|
|
58
|
+
*
|
|
59
|
+
* Returns `null` if no binary can be found — callers then surface a
|
|
60
|
+
* platform-specific error message to the user.
|
|
61
|
+
*/
|
|
62
|
+
export function resolveBinaryPath(): string | null {
|
|
63
|
+
// 1. Explicit override.
|
|
64
|
+
const override = process.env.ANVIL_CLI_BIN;
|
|
65
|
+
if (override !== undefined && existsSync(override)) {
|
|
66
|
+
return override;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 2. Per-platform npm package.
|
|
70
|
+
const pkg = platformPackageName();
|
|
71
|
+
if (pkg === null) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
let installed: { binPath?: string };
|
|
75
|
+
try {
|
|
76
|
+
installed = require(pkg) as { binPath?: string };
|
|
77
|
+
} catch {
|
|
78
|
+
// npm skipped this optional dep (because the platform doesn't
|
|
79
|
+
// match) or the user hasn't installed deps yet. Fall through.
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
if (typeof installed.binPath !== "string") return null;
|
|
83
|
+
if (!existsSync(installed.binPath)) return null;
|
|
84
|
+
return installed.binPath;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Construct a human-readable error message for the case where no
|
|
89
|
+
* binary could be resolved. Surfaces the active platform/arch so the
|
|
90
|
+
* user can quickly tell whether they need a different prebuilt or
|
|
91
|
+
* whether an `npm install --include=optional` is missing.
|
|
92
|
+
*/
|
|
93
|
+
export function unresolvableBinaryError(): string {
|
|
94
|
+
const pkg = platformPackageName();
|
|
95
|
+
const platformInfo = `${process.platform}/${process.arch}`;
|
|
96
|
+
if (pkg === null) {
|
|
97
|
+
return [
|
|
98
|
+
`@anvil-di/anvil-cli: no prebuilt binary is available for ${platformInfo}.`,
|
|
99
|
+
"",
|
|
100
|
+
"Supported platforms in v0.2:",
|
|
101
|
+
" - darwin / arm64, darwin / x64",
|
|
102
|
+
" - linux / arm64, linux / x64",
|
|
103
|
+
" - win32 / x64",
|
|
104
|
+
"",
|
|
105
|
+
"If you need another platform, build the Rust crate from source:",
|
|
106
|
+
" git clone https://github.com/msulak13/anvil && cargo build --release -p anvil-cli",
|
|
107
|
+
"and either set ANVIL_CLI_BIN=/path/to/anvil or place the binary on $PATH.",
|
|
108
|
+
].join("\n");
|
|
109
|
+
}
|
|
110
|
+
return [
|
|
111
|
+
`@anvil-di/anvil-cli: couldn't locate the ${BIN_NAME} binary for ${platformInfo}.`,
|
|
112
|
+
"",
|
|
113
|
+
`Expected the npm package "${pkg}" to be installed alongside`,
|
|
114
|
+
"@anvil-di/anvil-cli. If npm skipped optional dependencies, retry the install:",
|
|
115
|
+
" npm install --include=optional",
|
|
116
|
+
" pnpm install # honors optional deps by default",
|
|
117
|
+
" yarn install # honors optional deps by default",
|
|
118
|
+
"",
|
|
119
|
+
"Or set ANVIL_CLI_BIN to a binary you've built yourself.",
|
|
120
|
+
].join("\n");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Where this `@anvil-di/anvil-cli` package itself is installed (informational). */
|
|
124
|
+
export const PACKAGE_DIR = path.dirname(fileURLToPath(import.meta.url));
|
|
125
|
+
|
|
126
|
+
export { BIN_NAME };
|