2020-05-06 18:21:13 -04:00
|
|
|
## Script installer
|
|
|
|
|
|
|
|
Deno provides `deno install` to easily install and distribute executable code.
|
|
|
|
|
|
|
|
`deno install [OPTIONS...] [URL] [SCRIPT_ARGS...]` will install the script
|
|
|
|
available at `URL` under the name `EXE_NAME`.
|
|
|
|
|
|
|
|
This command creates a thin, executable shell script which invokes `deno` using
|
2020-06-12 10:44:00 -04:00
|
|
|
the specified CLI flags and main module. It is placed in the installation root's
|
2020-05-06 18:21:13 -04:00
|
|
|
`bin` directory.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```shell
|
2020-07-31 05:12:20 -04:00
|
|
|
$ deno install --allow-net --allow-read https://deno.land/std@$STD_VERSION/http/file_server.ts
|
|
|
|
[1/1] Compiling https://deno.land/std@$STD_VERSION/http/file_server.ts
|
2020-05-06 18:21:13 -04:00
|
|
|
|
|
|
|
✅ Successfully installed file_server.
|
|
|
|
/Users/deno/.deno/bin/file_server
|
|
|
|
```
|
|
|
|
|
|
|
|
To change the executable name, use `-n`/`--name`:
|
|
|
|
|
|
|
|
```shell
|
2020-07-31 05:12:20 -04:00
|
|
|
deno install --allow-net --allow-read -n serve https://deno.land/std@$STD_VERSION/http/file_server.ts
|
2020-05-06 18:21:13 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
The executable name is inferred by default:
|
|
|
|
|
|
|
|
- Attempt to take the file stem of the URL path. The above example would become
|
|
|
|
'file_server'.
|
|
|
|
- If the file stem is something generic like 'main', 'mod', 'index' or 'cli',
|
|
|
|
and the path has no parent, take the file name of the parent path. Otherwise
|
|
|
|
settle with the generic name.
|
2020-08-27 16:55:58 -04:00
|
|
|
- If the resulting name has an '@...' suffix, strip it.
|
2020-05-06 18:21:13 -04:00
|
|
|
|
|
|
|
To change the installation root, use `--root`:
|
|
|
|
|
|
|
|
```shell
|
2020-07-31 05:12:20 -04:00
|
|
|
deno install --allow-net --allow-read --root /usr/local https://deno.land/std@$STD_VERSION/http/file_server.ts
|
2020-05-06 18:21:13 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
The installation root is determined, in order of precedence:
|
|
|
|
|
|
|
|
- `--root` option
|
|
|
|
- `DENO_INSTALL_ROOT` environment variable
|
|
|
|
- `$HOME/.deno`
|
|
|
|
|
|
|
|
These must be added to the path manually if required.
|
|
|
|
|
|
|
|
```shell
|
2020-06-10 20:24:41 -04:00
|
|
|
echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc
|
2020-05-06 18:21:13 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
You must specify permissions that will be used to run the script at installation
|
|
|
|
time.
|
|
|
|
|
|
|
|
```shell
|
2020-07-31 05:12:20 -04:00
|
|
|
deno install --allow-net --allow-read https://deno.land/std@$STD_VERSION/http/file_server.ts -p 8080
|
2020-05-06 18:21:13 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
The above command creates an executable called `file_server` that runs with
|
2020-06-12 10:44:00 -04:00
|
|
|
network and read permissions and binds to port 8080.
|
2020-05-06 18:21:13 -04:00
|
|
|
|
2020-12-10 13:49:50 -05:00
|
|
|
For good practice, use the [`import.meta.main`](../examples/module_metadata.md)
|
2020-05-09 21:09:42 -04:00
|
|
|
idiom to specify the entry point in an executable script.
|
2020-05-06 18:21:13 -04:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2021-03-10 08:17:24 -05:00
|
|
|
<!-- deno-fmt-ignore -->
|
2020-07-14 15:24:17 -04:00
|
|
|
|
2020-05-06 18:21:13 -04:00
|
|
|
```ts
|
|
|
|
// https://example.com/awesome/cli.ts
|
|
|
|
async function myAwesomeCli(): Promise<void> {
|
|
|
|
-- snip --
|
|
|
|
}
|
|
|
|
|
|
|
|
if (import.meta.main) {
|
|
|
|
myAwesomeCli();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
When you create an executable script make sure to let users know by adding an
|
|
|
|
example installation command to your repository:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
# Install using deno install
|
|
|
|
|
|
|
|
$ deno install -n awesome_cli https://example.com/awesome/cli.ts
|
|
|
|
```
|