# ant add

```sh
ant add <path>              # the highest release, under the last segment of the path
ant add <path>@<version>    # that version
ant add <name>:<path>       # under a name you choose
ant add --dev <path>        # under devDependencies, for test files
```

`ant add` adds a [package](https://ant-lang.org/docs/guide/packages.md) to the project file, `ant.json`, or changes the
entry the file has for it. It then selects the version of every package the project uses, fetches
the ones the store lacks, and writes `ant.lock`. It takes one package or several.

## Add a package

```sh
$ ant add example.com/slug
ant: fetching example.com/slug 2.0.0
+ example.com/slug 2.0.0
```

The project file is the `ant.json` in the current directory or the nearest directory above. With
none, `ant add` creates one in the current directory. An `ant.json` makes one project of every
script in its directory and below, so a script there that pins a version in an import stops loading
until it imports by name.

```json [ant.json]
{
  "dependencies": {
    "slug": "example.com/slug@2.0.0"
  }
}
```

A program of the project imports the package by the name of its entry:

```ant
import "slug" Slug

println(Slug.make("Hello, Ant"))
```

```text
hello-ant
```

Standard output lists what changed in the lock: `+ path version` for a line added and
`- path version` for a line removed, in the order of the lock. A version that changed is one of
each. A fetch says so on standard error.

## Which version

A version you write has to be a tag of the repository: `1.2.0` is the tag `v1.2.0`. Without a
version:

- A package the project file does not have gets the highest release among the repository's tags.
- A package it has already gets the highest release of the [line](https://ant-lang.org/docs/guide/packages.md#versions) its
  entry is in, as [`ant update`](https://ant-lang.org/docs/tools/update.md) gives it. Adding a package again never moves it to
  another line.
- Where there is no release, the highest prerelease stands in for it.

## Which entry

With `name:`, `ant add` replaces the entry of that name or adds a new one. Without a name it looks
for the path in the project file:

- When no entry has the path, it adds one, named by the last segment of the path.
- With a version, it changes the entry that has the path and the version's line. When there is none
  and one entry has the path, it changes that one, which moves to the other line:

  ```sh
  $ ant add example.com/slug@1.2.0
  ant: fetching example.com/slug 1.2.0
  + example.com/slug 1.2.0
  - example.com/slug 2.0.0
  ```

- With no version, it changes the one entry that has the path.
- When another entry has the name it would give, or several entries have the path, it stops and asks
  for `name:`:

  ```text
  ant add: example.com/slug is in the project file as slug and slug2: say which is meant, as slug:example.com/slug
  ```

To use two lines of a package side by side, give the second a name of its own:

```sh
ant add slug1:example.com/slug@1.2.0
```

A package has one name for each line, so `ant add` refuses a change that would give one path and
line two names. To rename an entry, [remove](https://ant-lang.org/docs/tools/remove.md) it and add it again.

## Dev dependencies

`--dev` puts the entry under `devDependencies`, which only test files import. Without it the entry
goes under `dependencies`. Either way it leaves the other section, so `ant add example.com/golden`
without `--dev` moves a dev dependency to `dependencies`.

```sh
ant add --dev example.com/golden
```

## What it writes

`ant add` changes all or nothing. It writes `ant.json` and `ant.lock` only after every package was
selected and fetched, so a failure leaves both as they were.

It writes `ant.json` whole, in one form: two spaces of indentation, `main`, `bin`, `dependencies`,
then `devDependencies`, a section with nothing in it left out, the names of each section sorted, and
a newline at the end.

## Exit status

| Exit status | When                                                                                                                            |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `0`         | the files were written                                                                                                          |
| `1`         | a package cannot be fetched or is refused, a version has no tag, or an entry is not clear                                       |
| `2`         | the usage is wrong: an unknown flag, no package, or an argument such as `yaml` or `host/x@v1.2` that is not a path or a version |

## See also

[Packages](https://ant-lang.org/docs/guide/packages.md), [`ant remove`](https://ant-lang.org/docs/tools/remove.md), [`ant update`](https://ant-lang.org/docs/tools/update.md),
[`ant lock`](https://ant-lang.org/docs/tools/lock.md)
