Skip to content

Commit d0655a6

Browse files
authored
Merge pull request #14 from git-pkgs/add-init-operation
Add init operation for 22 package managers
2 parents 233538b + effbf8f commit d0655a6

28 files changed

Lines changed: 390 additions & 20 deletions

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A Go library that wraps package manager CLIs behind a common interface. Part of
44

55
## What it does
66

7-
Translates generic operations (install, add, remove, list, outdated, update, vendor, resolve) into the correct CLI commands for each package manager. Define what you want to do once, and the library figures out the right command for npm, bundler, cargo, go, or any other supported manager.
7+
Translates generic operations (init, install, add, remove, list, outdated, update, vendor, resolve) into the correct CLI commands for each package manager. Define what you want to do once, and the library figures out the right command for npm, bundler, cargo, go, or any other supported manager.
88

99
```go
1010
translator := managers.NewTranslator()
@@ -83,7 +83,7 @@ cmd, _ = translator.BuildCommand("bundler", "add", managers.CommandInput{
8383
| helm | helm | Chart.lock |
8484
| brew | homebrew | - |
8585

86-
Most managers support: install, add, remove, list, outdated, update, resolve. Some also support vendor and path. Some managers (maven, gradle, sbt, lein) have limited CLI support for add/remove operations.
86+
Most managers support: install, add, remove, list, outdated, update, resolve. Some also support init, vendor and path. Some managers (maven, gradle, sbt, lein) have limited CLI support for add/remove operations.
8787

8888
## Installation
8989

@@ -211,6 +211,7 @@ Built-in policies include AllowAllPolicy, DenyAllPolicy, and PackageBlocklistPol
211211

212212
| Operation | Description |
213213
|-----------|-------------|
214+
| `init` | Initialize a new project with manifest file |
214215
| `install` | Install dependencies from lockfile |
215216
| `add` | Add a new dependency |
216217
| `remove` | Remove a dependency |
@@ -229,6 +230,20 @@ Built-in policies include AllowAllPolicy, DenyAllPolicy, and PackageBlocklistPol
229230
| `frozen` | Fail if lockfile would change (CI mode) |
230231
| `json` | Output in JSON format (where supported) |
231232

233+
### Initializing projects
234+
235+
The `init` operation creates a new project manifest in the current directory. This is useful for creating temporary projects for dependency resolution or scaffolding new projects.
236+
237+
```go
238+
manager, _ := managers.Detect("/path/to/empty/dir")
239+
result, _ := manager.Init(ctx)
240+
// Creates package.json (npm), Gemfile (bundler), Cargo.toml (cargo), etc.
241+
```
242+
243+
Only managers that can scaffold in-place are supported. Managers whose `new` command always creates a child directory (mix, lein, helm, stack, pub, rebar3) or whose `init` command bootstraps global state rather than a project (opam) are excluded.
244+
245+
**Managers with init support:** npm, pnpm, yarn, bun, bundler, cargo, gomod, uv, poetry, deno, composer, swift, nuget, gradle, conan, conda, cabal, nimble, luarocks, shards, vcpkg, renv
246+
232247
### Getting package paths
233248

234249
The `path` operation returns the filesystem path to an installed package, useful for source exploration or editor integration:

definitions/bun.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ detection:
1818
priority: 25 # Higher than npm/yarn/pnpm when bun lockfile present
1919

2020
commands:
21+
init:
22+
base: [init, -y]
23+
exit_codes:
24+
0: success
25+
1: error
26+
2127
install:
2228
base: [install]
2329
flags:
@@ -92,6 +98,7 @@ commands:
9298
1: error
9399

94100
capabilities:
101+
- init
95102
- install
96103
- install_frozen
97104
- add

definitions/bundler.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ version_detection:
1818
pattern: '(\d+\.\d+\.\d+)'
1919

2020
commands:
21+
init:
22+
base: [init]
23+
exit_codes:
24+
0: success
25+
1: error
26+
2127
install:
2228
base: [install]
2329
flags:
@@ -125,6 +131,7 @@ commands:
125131
1: error
126132

127133
capabilities:
134+
- init
128135
- install
129136
- install_frozen
130137
- add

definitions/cabal.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ version_detection:
1616
pattern: 'cabal-install version (\d+\.\d+\.\d+)'
1717

1818
commands:
19+
init:
20+
base: [init, -n, -p, resolve-tmp]
21+
exit_codes:
22+
0: success
23+
1: error
24+
1925
install:
2026
base: [build, --only-dependencies]
2127
flags:
@@ -63,6 +69,7 @@ commands:
6369
1: error
6470

6571
capabilities:
72+
- init
6673
- install
6774
- list
6875
- outdated

definitions/cargo.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ version_detection:
1818
pattern: 'cargo (\d+\.\d+\.\d+)'
1919

2020
commands:
21+
init:
22+
base: [init, --lib]
23+
exit_codes:
24+
0: success
25+
1: error
26+
2127
# cargo fetch downloads dependencies without building
2228
# cargo build would also work but does more than just install
2329
install:
@@ -129,6 +135,7 @@ commands:
129135
1: error
130136

131137
capabilities:
138+
- init
132139
- install
133140
- install_frozen
134141
- add

definitions/composer.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ detection:
1717
priority: 10
1818

1919
commands:
20+
init:
21+
base: [init, -n]
22+
default_flags: [--name, resolve/tmp]
23+
exit_codes:
24+
0: success
25+
1: error
26+
2027
install:
2128
base: [install]
2229
flags:
@@ -91,6 +98,7 @@ commands:
9198
1: error
9299

93100
capabilities:
101+
- init
94102
- install
95103
- add
96104
- add_dev

definitions/conan.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ version_detection:
1616
pattern: 'Conan version (\d+\.\d+\.\d+)'
1717

1818
commands:
19+
init:
20+
base: [new, basic, -d, "name=resolve_tmp", -d, "version=0.1"]
21+
exit_codes:
22+
0: success
23+
1: error
24+
1925
install:
2026
base: [install, .]
2127
flags:
@@ -82,6 +88,7 @@ commands:
8288
1: error
8389

8490
capabilities:
91+
- init
8592
- install
8693
- add
8794
- remove

definitions/conda.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ version_detection:
1616
pattern: 'conda (\d+\.\d+\.\d+)'
1717

1818
commands:
19+
init:
20+
base: [create, -n, resolve-tmp, --yes]
21+
exit_codes:
22+
0: success
23+
1: error
24+
1925
install:
2026
base: [install, --yes]
2127
base_overrides:
@@ -96,6 +102,7 @@ commands:
96102
1: error
97103

98104
capabilities:
105+
- init
99106
- install
100107
- install_frozen
101108
- add

definitions/deno.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ version_detection:
1616
pattern: 'deno (\d+\.\d+\.\d+)'
1717

1818
commands:
19+
init:
20+
base: [init]
21+
exit_codes:
22+
0: success
23+
1: error
24+
1925
install:
2026
base: [install]
2127
flags:
@@ -86,6 +92,7 @@ commands:
8692
1: error
8793

8894
capabilities:
95+
- init
8996
- install
9097
- install_frozen
9198
- add

definitions/gomod.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ version_detection:
1818
pattern: 'go(\d+\.\d+(?:\.\d+)?)'
1919

2020
commands:
21+
init:
22+
base: [mod, init, resolve-tmp]
23+
exit_codes:
24+
0: success
25+
1: error
26+
2127
# go mod download fetches dependencies
2228
install:
2329
base: [mod, download]
@@ -124,6 +130,7 @@ commands:
124130
1: error
125131

126132
capabilities:
133+
- init
127134
- install
128135
- add
129136
- remove

0 commit comments

Comments
 (0)